Changeset 89ba88c in mainline
- Timestamp:
- 2019-10-14T11:16:24Z (5 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- f1cd4b0
- Parents:
- 25525133
- git-author:
- Jiri Svoboda <jiri@…> (2019-10-13 18:08:59)
- git-committer:
- Jiri Svoboda <jiri@…> (2019-10-14 11:16:24)
- Location:
- uspace/srv/net/udp
- Files:
-
- 8 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/srv/net/udp/assoc.c
r25525133 r89ba88c 40 40 #include <fibril_synch.h> 41 41 #include <inet/endpoint.h> 42 #include <inet/inet.h>43 42 #include <io/log.h> 44 43 #include <nettl/amap.h> … … 48 47 #include "msg.h" 49 48 #include "pdu.h" 50 #include "udp_inet.h"51 49 #include "udp_type.h" 52 50 … … 57 55 static udp_assoc_t *udp_assoc_find_ref(inet_ep2_t *); 58 56 static errno_t udp_assoc_queue_msg(udp_assoc_t *, inet_ep2_t *, udp_msg_t *); 57 static udp_assocs_dep_t *assocs_dep; 59 58 60 59 /** Initialize associations. */ 61 errno_t udp_assocs_init( void)60 errno_t udp_assocs_init(udp_assocs_dep_t *dep) 62 61 { 63 62 errno_t rc; … … 69 68 } 70 69 70 assocs_dep = dep; 71 71 return EOK; 72 72 } … … 253 253 errno_t udp_assoc_send(udp_assoc_t *assoc, inet_ep_t *remote, udp_msg_t *msg) 254 254 { 255 udp_pdu_t *pdu;256 255 inet_ep2_t epp; 257 256 errno_t rc; … … 275 274 if (inet_addr_is_any(&epp.local.addr) && !assoc->nolocal) { 276 275 log_msg(LOG_DEFAULT, LVL_DEBUG, "Determine local address."); 277 rc = inet_get_srcaddr(&epp.remote.addr, 0, &epp.local.addr); 276 rc = (*assocs_dep->get_srcaddr)(&epp.remote.addr, 0, 277 &epp.local.addr); 278 278 if (rc != EOK) { 279 279 log_msg(LOG_DEFAULT, LVL_DEBUG, "Cannot determine " … … 289 289 return EINVAL; 290 290 291 log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_assoc_send - encode pdu");292 293 rc = udp_pdu_encode(&epp, msg, &pdu);294 if (rc != EOK)295 return ENOMEM;296 297 291 log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_assoc_send - transmit"); 298 299 rc = udp_transmit_pdu(pdu); 300 udp_pdu_delete(pdu); 292 rc = (*assocs_dep->transmit_msg)(&epp, msg); 301 293 302 294 if (rc != EOK) -
uspace/srv/net/udp/assoc.h
r25525133 r89ba88c 40 40 #include "udp_type.h" 41 41 42 extern errno_t udp_assocs_init( void);42 extern errno_t udp_assocs_init(udp_assocs_dep_t *); 43 43 extern void udp_assocs_fini(void); 44 44 extern udp_assoc_t *udp_assoc_new(inet_ep2_t *, udp_assoc_cb_t *, void *); -
uspace/srv/net/udp/meson.build
r25525133 r89ba88c 32 32 'assoc.c', 33 33 'msg.c', 34 'pdu.c', 34 35 ) 35 36 36 37 src = files( 37 'pdu.c',38 38 'service.c', 39 39 'udp.c', -
uspace/srv/net/udp/test/assoc.c
r25525133 r89ba88c 46 46 }; 47 47 48 static errno_t test_get_srcaddr(inet_addr_t *, uint8_t, inet_addr_t *); 49 static errno_t test_transmit_msg(inet_ep2_t *, udp_msg_t *); 50 51 static udp_assocs_dep_t test_assocs_dep = { 52 .get_srcaddr = test_get_srcaddr, 53 .transmit_msg = test_transmit_msg 54 }; 55 56 static inet_ep2_t *sent_epp; 57 static udp_msg_t *sent_msg; 58 48 59 PCUT_TEST_BEFORE 49 60 { … … 54 65 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 55 66 56 rc = udp_assocs_init( );67 rc = udp_assocs_init(&test_assocs_dep); 57 68 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 58 69 } … … 129 140 } 130 141 131 PCUT_TEST(send_recv) 132 { 142 /** Sending message with destination not set in association and NULL 143 * destination argument should fail with EINVAL. 144 */ 145 PCUT_TEST(send_null_dest) 146 { 147 udp_assoc_t *assoc; 148 inet_ep2_t epp; 149 inet_ep_t dest; 150 errno_t rc; 151 udp_msg_t *msg; 152 const char *msgstr = "Hello"; 153 154 msg = udp_msg_new(); 155 PCUT_ASSERT_NOT_NULL(msg); 156 msg->data_size = str_size(msgstr) + 1; 157 msg->data = str_dup(msgstr); 158 159 inet_ep2_init(&epp); 160 inet_ep_init(&dest); 161 162 assoc = udp_assoc_new(&epp, &test_assoc_cb, NULL); 163 PCUT_ASSERT_NOT_NULL(assoc); 164 165 rc = udp_assoc_add(assoc); 166 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 167 168 rc = udp_assoc_send(assoc, &dest, msg); 169 PCUT_ASSERT_ERRNO_VAL(EINVAL, rc); 170 171 udp_msg_delete(msg); 172 173 udp_assoc_remove(assoc); 174 udp_assoc_delete(assoc); 175 } 176 177 /** Sending message with destination not set in association and unset 178 * destination argument should fail with EINVAL. 179 */ 180 PCUT_TEST(send_unset_dest) 181 { 182 udp_assoc_t *assoc; 183 inet_ep2_t epp; 184 inet_ep_t dest; 185 errno_t rc; 186 udp_msg_t *msg; 187 const char *msgstr = "Hello"; 188 189 msg = udp_msg_new(); 190 PCUT_ASSERT_NOT_NULL(msg); 191 msg->data_size = str_size(msgstr) + 1; 192 msg->data = str_dup(msgstr); 193 194 inet_ep2_init(&epp); 195 inet_ep_init(&dest); 196 197 assoc = udp_assoc_new(&epp, &test_assoc_cb, NULL); 198 PCUT_ASSERT_NOT_NULL(assoc); 199 200 rc = udp_assoc_add(assoc); 201 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 202 203 rc = udp_assoc_send(assoc, &dest, msg); 204 PCUT_ASSERT_ERRNO_VAL(EINVAL, rc); 205 206 udp_msg_delete(msg); 207 208 udp_assoc_remove(assoc); 209 udp_assoc_delete(assoc); 210 } 211 212 /** Sending message with explicit destination */ 213 PCUT_TEST(send_explicit_dest) 214 { 215 udp_assoc_t *assoc; 216 inet_ep2_t epp; 217 inet_ep_t dest; 218 errno_t rc; 219 udp_msg_t *msg; 220 const char *msgstr = "Hello"; 221 222 msg = udp_msg_new(); 223 PCUT_ASSERT_NOT_NULL(msg); 224 msg->data_size = str_size(msgstr) + 1; 225 msg->data = str_dup(msgstr); 226 227 inet_ep2_init(&epp); 228 inet_addr(&dest.addr, 127, 0, 0, 1); 229 dest.port = 42; 230 231 assoc = udp_assoc_new(&epp, &test_assoc_cb, NULL); 232 PCUT_ASSERT_NOT_NULL(assoc); 233 234 rc = udp_assoc_add(assoc); 235 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 236 237 sent_epp = NULL; 238 sent_msg = NULL; 239 240 rc = udp_assoc_send(assoc, &dest, msg); 241 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 242 PCUT_ASSERT_EQUALS(msg, sent_msg); 243 PCUT_ASSERT_TRUE(inet_addr_compare(&dest.addr, &sent_epp->remote.addr)); 244 PCUT_ASSERT_EQUALS(dest.port, sent_epp->remote.port); 245 246 udp_msg_delete(msg); 247 248 udp_assoc_remove(assoc); 249 udp_assoc_delete(assoc); 250 } 251 252 /** Sending message with destination set in association and NULL destination 253 * argument 254 */ 255 PCUT_TEST(send_assoc_null_dest) 256 { 257 udp_assoc_t *assoc; 258 inet_ep2_t epp; 259 errno_t rc; 260 udp_msg_t *msg; 261 const char *msgstr = "Hello"; 262 263 msg = udp_msg_new(); 264 PCUT_ASSERT_NOT_NULL(msg); 265 msg->data_size = str_size(msgstr) + 1; 266 msg->data = str_dup(msgstr); 267 268 inet_ep2_init(&epp); 269 inet_addr(&epp.remote.addr, 127, 0, 0, 1); 270 epp.remote.port = 42; 271 inet_addr(&epp.local.addr, 127, 0, 0, 1); 272 epp.local.port = 1; 273 274 assoc = udp_assoc_new(&epp, &test_assoc_cb, NULL); 275 PCUT_ASSERT_NOT_NULL(assoc); 276 277 rc = udp_assoc_add(assoc); 278 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 279 280 sent_epp = NULL; 281 sent_msg = NULL; 282 283 rc = udp_assoc_send(assoc, NULL, msg); 284 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 285 PCUT_ASSERT_EQUALS(msg, sent_msg); 286 PCUT_ASSERT_TRUE(inet_addr_compare(&epp.remote.addr, &sent_epp->remote.addr)); 287 PCUT_ASSERT_EQUALS(epp.remote.port, sent_epp->remote.port); 288 289 udp_msg_delete(msg); 290 291 udp_assoc_remove(assoc); 292 udp_assoc_delete(assoc); 293 } 294 295 /** Sending message with destination set in association and unset destination 296 * argument should return EINVAL 297 */ 298 PCUT_TEST(send_assoc_unset_dest) 299 { 300 udp_assoc_t *assoc; 301 inet_ep2_t epp; 302 inet_ep_t dest; 303 errno_t rc; 304 udp_msg_t *msg; 305 const char *msgstr = "Hello"; 306 307 msg = udp_msg_new(); 308 PCUT_ASSERT_NOT_NULL(msg); 309 msg->data_size = str_size(msgstr) + 1; 310 msg->data = str_dup(msgstr); 311 312 inet_ep2_init(&epp); 313 inet_addr(&epp.remote.addr, 127, 0, 0, 1); 314 epp.remote.port = 42; 315 inet_addr(&epp.local.addr, 127, 0, 0, 1); 316 epp.local.port = 1; 317 inet_ep_init(&dest); 318 319 assoc = udp_assoc_new(&epp, &test_assoc_cb, NULL); 320 PCUT_ASSERT_NOT_NULL(assoc); 321 322 rc = udp_assoc_add(assoc); 323 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 324 325 rc = udp_assoc_send(assoc, &dest, msg); 326 PCUT_ASSERT_ERRNO_VAL(EINVAL, rc); 327 328 udp_msg_delete(msg); 329 330 udp_assoc_remove(assoc); 331 udp_assoc_delete(assoc); 332 } 333 334 PCUT_TEST(recv) 335 { 336 // XXX Looks like currently udp_assoc_recv() is not used at all 133 337 } 134 338 … … 189 393 } 190 394 395 static errno_t test_get_srcaddr(inet_addr_t *remote, uint8_t tos, 396 inet_addr_t *local) 397 { 398 inet_addr(local, 127, 0, 0, 1); 399 return EOK; 400 } 401 402 static errno_t test_transmit_msg(inet_ep2_t *epp, udp_msg_t *msg) 403 { 404 sent_epp = epp; 405 sent_msg = msg; 406 return EOK; 407 } 408 191 409 PCUT_EXPORT(assoc); -
uspace/srv/net/udp/udp.c
r25525133 r89ba88c 47 47 #define NAME "udp" 48 48 49 static udp_assocs_dep_t udp_assocs_dep = { 50 .get_srcaddr = udp_get_srcaddr, 51 .transmit_msg = udp_transmit_msg 52 }; 53 49 54 static errno_t udp_init(void) 50 55 { … … 53 58 log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_init()"); 54 59 55 rc = udp_assocs_init( );60 rc = udp_assocs_init(&udp_assocs_dep); 56 61 if (rc != EOK) { 57 62 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed initializing associations."); -
uspace/srv/net/udp/udp_inet.c
r25525133 r89ba88c 134 134 } 135 135 136 /** Get source address. 137 * 138 * @param remote Remote address 139 * @param tos Type of service 140 * @param local Place to store local address 141 * @return EOK on success or an error code 142 */ 143 errno_t udp_get_srcaddr(inet_addr_t *remote, uint8_t tos, inet_addr_t *local) 144 { 145 return inet_get_srcaddr(remote, tos, local); 146 } 147 148 /** Transmit message over network layer. */ 149 errno_t udp_transmit_msg(inet_ep2_t *epp, udp_msg_t *msg) 150 { 151 udp_pdu_t *pdu; 152 errno_t rc; 153 154 log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_transmit_msg()"); 155 156 rc = udp_pdu_encode(epp, msg, &pdu); 157 if (rc != EOK) { 158 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed encoding PDU"); 159 return rc; 160 } 161 162 rc = udp_transmit_pdu(pdu); 163 udp_pdu_delete(pdu); 164 165 return rc; 166 } 167 136 168 /** 137 169 * @} -
uspace/srv/net/udp/udp_inet.h
r25525133 r89ba88c 36 36 #define UDP_INET_H 37 37 38 #include <inet/addr.h> 39 #include <stdint.h> 38 40 #include "udp_type.h" 39 41 40 42 extern errno_t udp_inet_init(void); 41 43 extern errno_t udp_transmit_pdu(udp_pdu_t *); 44 extern errno_t udp_get_srcaddr(inet_addr_t *, uint8_t, inet_addr_t *); 45 extern errno_t udp_transmit_msg(inet_ep2_t *, udp_msg_t *); 42 46 43 47 #endif -
uspace/srv/net/udp/udp_type.h
r25525133 r89ba88c 37 37 38 38 #include <async.h> 39 #include <errno.h> 39 40 #include <fibril.h> 40 41 #include <fibril_synch.h> … … 44 45 #include <stdbool.h> 45 46 #include <stddef.h> 47 #include <stdint.h> 46 48 #include <inet/addr.h> 47 49 … … 87 89 } udp_pdu_t; 88 90 89 /** Association callbacks */ 91 /** Functions needed by associations module. 92 * 93 * Functions that need to be provided by the caller so that the associations 94 * module can function. 95 */ 96 typedef struct { 97 errno_t (*get_srcaddr)(inet_addr_t *, uint8_t, inet_addr_t *); 98 errno_t (*transmit_msg)(inet_ep2_t *, udp_msg_t *); 99 } udp_assocs_dep_t; 100 101 /** Association callbacks. 102 * 103 * Callbacks for a particular association, to notify caller of events 104 * on the association. 105 */ 90 106 typedef struct { 91 107 /** Message received */
Note:
See TracChangeset
for help on using the changeset viewer.