1 | /*
|
---|
2 | * Copyright (c) 2015 Jiri Svoboda
|
---|
3 | * All rights reserved.
|
---|
4 | *
|
---|
5 | * Redistribution and use in source and binary forms, with or without
|
---|
6 | * modification, are permitted provided that the following conditions
|
---|
7 | * are met:
|
---|
8 | *
|
---|
9 | * - Redistributions of source code must retain the above copyright
|
---|
10 | * notice, this list of conditions and the following disclaimer.
|
---|
11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
12 | * notice, this list of conditions and the following disclaimer in the
|
---|
13 | * documentation and/or other materials provided with the distribution.
|
---|
14 | * - The name of the author may not be used to endorse or promote products
|
---|
15 | * derived from this software without specific prior written permission.
|
---|
16 | *
|
---|
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
27 | */
|
---|
28 |
|
---|
29 | /** @addtogroup udp
|
---|
30 | * @{
|
---|
31 | */
|
---|
32 |
|
---|
33 | /**
|
---|
34 | * @file UDP client associations
|
---|
35 | *
|
---|
36 | * Ties UDP associations into the namespace of a client
|
---|
37 | */
|
---|
38 |
|
---|
39 | #include <errno.h>
|
---|
40 | #include <inet/endpoint.h>
|
---|
41 | #include <io/log.h>
|
---|
42 | #include <stdlib.h>
|
---|
43 |
|
---|
44 | #include "cassoc.h"
|
---|
45 | #include "udp_type.h"
|
---|
46 |
|
---|
47 | /** Add message to client receive queue.
|
---|
48 | *
|
---|
49 | * @param cassoc Client association
|
---|
50 | * @param epp Endpoint pair on which message was received
|
---|
51 | * @param msg Message
|
---|
52 | *
|
---|
53 | * @return EOK on success, ENOMEM if out of memory
|
---|
54 | */
|
---|
55 | errno_t udp_cassoc_queue_msg(udp_cassoc_t *cassoc, inet_ep2_t *epp,
|
---|
56 | udp_msg_t *msg)
|
---|
57 | {
|
---|
58 | udp_crcv_queue_entry_t *rqe;
|
---|
59 |
|
---|
60 | log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_cassoc_queue_msg(%p, %p, %p)",
|
---|
61 | cassoc, epp, msg);
|
---|
62 |
|
---|
63 | rqe = calloc(1, sizeof(udp_crcv_queue_entry_t));
|
---|
64 | if (rqe == NULL)
|
---|
65 | return ENOMEM;
|
---|
66 |
|
---|
67 | link_initialize(&rqe->link);
|
---|
68 | rqe->epp = *epp;
|
---|
69 | rqe->msg = msg;
|
---|
70 | rqe->cassoc = cassoc;
|
---|
71 |
|
---|
72 | list_append(&rqe->link, &cassoc->client->crcv_queue);
|
---|
73 | return EOK;
|
---|
74 | }
|
---|
75 |
|
---|
76 | /** Create client association.
|
---|
77 | *
|
---|
78 | * This effectively adds an association into a client's namespace.
|
---|
79 | *
|
---|
80 | * @param client Client
|
---|
81 | * @param assoc Association
|
---|
82 | * @param rcassoc Place to store pointer to new client association
|
---|
83 | *
|
---|
84 | * @return EOK on soccess, ENOMEM if out of memory
|
---|
85 | */
|
---|
86 | errno_t udp_cassoc_create(udp_client_t *client, udp_assoc_t *assoc,
|
---|
87 | udp_cassoc_t **rcassoc)
|
---|
88 | {
|
---|
89 | udp_cassoc_t *cassoc;
|
---|
90 | sysarg_t id;
|
---|
91 |
|
---|
92 | cassoc = calloc(1, sizeof(udp_cassoc_t));
|
---|
93 | if (cassoc == NULL)
|
---|
94 | return ENOMEM;
|
---|
95 |
|
---|
96 | /* Allocate new ID */
|
---|
97 | id = 0;
|
---|
98 | list_foreach (client->cassoc, lclient, udp_cassoc_t, cassoc) {
|
---|
99 | if (cassoc->id >= id)
|
---|
100 | id = cassoc->id + 1;
|
---|
101 | }
|
---|
102 |
|
---|
103 | cassoc->id = id;
|
---|
104 | cassoc->client = client;
|
---|
105 | cassoc->assoc = assoc;
|
---|
106 |
|
---|
107 | list_append(&cassoc->lclient, &client->cassoc);
|
---|
108 | *rcassoc = cassoc;
|
---|
109 | return EOK;
|
---|
110 | }
|
---|
111 |
|
---|
112 | /** Destroy client association.
|
---|
113 | *
|
---|
114 | * @param cassoc Client association
|
---|
115 | */
|
---|
116 | void udp_cassoc_destroy(udp_cassoc_t *cassoc)
|
---|
117 | {
|
---|
118 | list_remove(&cassoc->lclient);
|
---|
119 | free(cassoc);
|
---|
120 | }
|
---|
121 |
|
---|
122 | /** Get client association by ID.
|
---|
123 | *
|
---|
124 | * @param client Client
|
---|
125 | * @param id Client association ID
|
---|
126 | * @param rcassoc Place to store pointer to client association
|
---|
127 | *
|
---|
128 | * @return EOK on success, ENOENT if no client association with the given ID
|
---|
129 | * is found.
|
---|
130 | */
|
---|
131 | errno_t udp_cassoc_get(udp_client_t *client, sysarg_t id,
|
---|
132 | udp_cassoc_t **rcassoc)
|
---|
133 | {
|
---|
134 | list_foreach (client->cassoc, lclient, udp_cassoc_t, cassoc) {
|
---|
135 | if (cassoc->id == id) {
|
---|
136 | *rcassoc = cassoc;
|
---|
137 | return EOK;
|
---|
138 | }
|
---|
139 | }
|
---|
140 |
|
---|
141 | return ENOENT;
|
---|
142 | }
|
---|
143 |
|
---|
144 | /**
|
---|
145 | * @}
|
---|
146 | */
|
---|