Changeset 2f19103 in mainline for uspace/srv/net/udp/assoc.c
- Timestamp:
- 2015-05-22T07:21:37Z (10 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- 58e9dec
- Parents:
- bf7587b0
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/srv/net/udp/assoc.c
rbf7587b0 r2f19103 1 1 /* 2 * Copyright (c) 201 2Jiri Svoboda2 * Copyright (c) 2015 Jiri Svoboda 3 3 * All rights reserved. 4 4 * … … 52 52 FIBRIL_MUTEX_INITIALIZE(assoc_list_lock); 53 53 54 static udp_assoc_t *udp_assoc_find_ref( udp_sockpair_t *);55 static int udp_assoc_queue_msg(udp_assoc_t *, udp_sockpair_t *, udp_msg_t *);56 static bool udp_ socket_match(udp_sock_t *, udp_sock_t *);57 static bool udp_ sockpair_match(udp_sockpair_t *, udp_sockpair_t *);54 static udp_assoc_t *udp_assoc_find_ref(inet_ep2_t *); 55 static int udp_assoc_queue_msg(udp_assoc_t *, inet_ep2_t *, udp_msg_t *); 56 static bool udp_ep_match(inet_ep_t *, inet_ep_t *); 57 static bool udp_ep2_match(inet_ep2_t *, inet_ep2_t *); 58 58 59 59 /** Create new association structure. 60 60 * 61 * @param lsock Local socket (will be deeply copied) 62 * @param fsock Foreign socket (will be deeply copied) 61 * @param epp Endpoint pair (will be copied) 62 * @param cb Callbacks 63 * @param cb_arg Callback argument 63 64 * @return New association or NULL 64 65 */ 65 udp_assoc_t *udp_assoc_new(udp_sock_t *lsock, udp_sock_t *fsock, 66 udp_assoc_cb_t *cb, void *cb_arg) 66 udp_assoc_t *udp_assoc_new(inet_ep2_t *epp, udp_assoc_cb_t *cb, void *cb_arg) 67 67 { 68 68 udp_assoc_t *assoc = NULL; … … 82 82 fibril_condvar_initialize(&assoc->rcv_queue_cv); 83 83 84 if (lsock != NULL) 85 assoc->ident.local = *lsock; 86 87 if (fsock != NULL) 88 assoc->ident.foreign = *fsock; 84 if (epp != NULL) 85 assoc->ident = *epp; 89 86 90 87 assoc->cb = cb; … … 200 197 assoc, iplink); 201 198 fibril_mutex_lock(&assoc->lock); 202 assoc->ident. iplink = iplink;203 fibril_mutex_unlock(&assoc->lock); 204 } 205 206 /** Set foreign socket in association.207 * 208 * @param assoc Association 209 * @param fsock Foreign socket (deeply copied)210 */ 211 void udp_assoc_set_ foreign(udp_assoc_t *assoc, udp_sock_t *fsock)212 { 213 log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_assoc_set_ foreign(%p, %p)", assoc, fsock);214 fibril_mutex_lock(&assoc->lock); 215 assoc->ident. foreign = *fsock;216 fibril_mutex_unlock(&assoc->lock); 217 } 218 219 /** Set local socket in association.199 assoc->ident.local_link = iplink; 200 fibril_mutex_unlock(&assoc->lock); 201 } 202 203 /** Set remote endpoint in association. 204 * 205 * @param assoc Association 206 * @param remote Remote endpoint (deeply copied) 207 */ 208 void udp_assoc_set_remote(udp_assoc_t *assoc, inet_ep_t *remote) 209 { 210 log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_assoc_set_remote(%p, %p)", assoc, remote); 211 fibril_mutex_lock(&assoc->lock); 212 assoc->ident.remote = *remote; 213 fibril_mutex_unlock(&assoc->lock); 214 } 215 216 /** Set local endpoint in association. 220 217 * 221 218 * @param assoc Association 222 * @param l sock Local socket (deeply copied)223 * 224 */ 225 void udp_assoc_set_local(udp_assoc_t *assoc, udp_sock_t *lsock)226 { 227 log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_assoc_set_local(%p, %p)", assoc, l sock);228 fibril_mutex_lock(&assoc->lock); 229 assoc->ident.local = *l sock;219 * @param local Local endpoint (deeply copied) 220 * 221 */ 222 void udp_assoc_set_local(udp_assoc_t *assoc, inet_ep_t *local) 223 { 224 log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_assoc_set_local(%p, %p)", assoc, local); 225 fibril_mutex_lock(&assoc->lock); 226 assoc->ident.local = *local; 230 227 fibril_mutex_unlock(&assoc->lock); 231 228 } … … 248 245 * 249 246 * @param assoc Association 250 * @param fsock Foreign socket or NULL not to override @a assoc247 * @param remote Remote endpoint or NULL not to override @a assoc 251 248 * @param msg Message 252 249 * 253 250 * @return EOK on success 254 * EINVAL if foreign socket is not set251 * EINVAL if remote endpoint is not set 255 252 * ENOMEM if out of resources 256 253 * EIO if no route to destination exists 257 254 */ 258 int udp_assoc_send(udp_assoc_t *assoc, udp_sock_t *fsock, udp_msg_t *msg)255 int udp_assoc_send(udp_assoc_t *assoc, inet_ep_t *remote, udp_msg_t *msg) 259 256 { 260 257 udp_pdu_t *pdu; 261 udp_sockpair_t sp;258 inet_ep2_t epp; 262 259 int rc; 263 260 264 261 log_msg(LOG_DEFAULT, LVL_NOTE, "udp_assoc_send(%p, %p, %p)", 265 assoc, fsock, msg);266 267 /* @a fsock can be used to override the foreign socket */268 sp = assoc->ident;269 if ( fsock!= NULL)270 sp.foreign = *fsock;262 assoc, remote, msg); 263 264 /* @a remote can be used to override the remote endpoint */ 265 epp = assoc->ident; 266 if (remote != NULL) 267 epp.remote = *remote; 271 268 272 269 log_msg(LOG_DEFAULT, LVL_NOTE, "udp_assoc_send - check addr any"); 273 270 274 if ((inet_addr_is_any(& sp.foreign.addr)) ||275 ( sp.foreign.port == UDP_PORT_ANY))271 if ((inet_addr_is_any(&epp.remote.addr)) || 272 (epp.remote.port == UDP_PORT_ANY)) 276 273 return EINVAL; 277 274 278 275 log_msg(LOG_DEFAULT, LVL_NOTE, "udp_assoc_send - check version"); 279 276 280 if ( sp.foreign.addr.version != sp.local.addr.version)277 if (epp.remote.addr.version != epp.local.addr.version) 281 278 return EINVAL; 282 279 283 280 log_msg(LOG_DEFAULT, LVL_NOTE, "udp_assoc_send - encode pdu"); 284 281 285 rc = udp_pdu_encode(& sp, msg, &pdu);282 rc = udp_pdu_encode(&epp, msg, &pdu); 286 283 if (rc != EOK) 287 284 return ENOMEM; … … 303 300 * Pull one message from the association's receive queue. 304 301 */ 305 int udp_assoc_recv(udp_assoc_t *assoc, udp_msg_t **msg, udp_sock_t *fsock)302 int udp_assoc_recv(udp_assoc_t *assoc, udp_msg_t **msg, inet_ep_t *remote) 306 303 { 307 304 link_t *link; … … 329 326 330 327 *msg = rqe->msg; 331 * fsock = rqe->sp.foreign;328 *remote = rqe->epp.remote; 332 329 free(rqe); 333 330 … … 339 336 * Find the association to which the message belongs and queue it. 340 337 */ 341 void udp_assoc_received( udp_sockpair_t *rsp, udp_msg_t *msg)338 void udp_assoc_received(inet_ep2_t *repp, udp_msg_t *msg) 342 339 { 343 340 udp_assoc_t *assoc; 344 341 int rc; 345 342 346 log_msg(LOG_DEFAULT, LVL_NOTE, "udp_assoc_received(%p, %p)", r sp, msg);347 348 assoc = udp_assoc_find_ref(r sp);343 log_msg(LOG_DEFAULT, LVL_NOTE, "udp_assoc_received(%p, %p)", repp, msg); 344 345 assoc = udp_assoc_find_ref(repp); 349 346 if (assoc == NULL) { 350 347 log_msg(LOG_DEFAULT, LVL_NOTE, "No association found. Message dropped."); … … 356 353 357 354 if (0) { 358 rc = udp_assoc_queue_msg(assoc, r sp, msg);355 rc = udp_assoc_queue_msg(assoc, repp, msg); 359 356 if (rc != EOK) { 360 357 log_msg(LOG_DEFAULT, LVL_DEBUG, "Out of memory. Message dropped."); … … 364 361 365 362 log_msg(LOG_DEFAULT, LVL_NOTE, "call assoc->cb->recv_msg"); 366 assoc->cb->recv_msg(assoc->cb_arg, r sp, msg);363 assoc->cb->recv_msg(assoc->cb_arg, repp, msg); 367 364 udp_assoc_delref(assoc); 368 365 } … … 381 378 } 382 379 383 static int udp_assoc_queue_msg(udp_assoc_t *assoc, udp_sockpair_t *sp,380 static int udp_assoc_queue_msg(udp_assoc_t *assoc, inet_ep2_t *epp, 384 381 udp_msg_t *msg) 385 382 { … … 387 384 388 385 log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_assoc_queue_msg(%p, %p, %p)", 389 assoc, sp, msg);386 assoc, epp, msg); 390 387 391 388 rqe = calloc(1, sizeof(udp_rcv_queue_entry_t)); … … 394 391 395 392 link_initialize(&rqe->link); 396 rqe-> sp = *sp;393 rqe->epp = *epp; 397 394 rqe->msg = msg; 398 395 … … 406 403 } 407 404 408 /** Match socket with pattern. */409 static bool udp_ socket_match(udp_sock_t *sock, udp_sock_t *patt)405 /** Match endpoint with pattern. */ 406 static bool udp_ep_match(inet_ep_t *ep, inet_ep_t *patt) 410 407 { 411 408 char *sa, *pa; 412 409 413 410 sa = pa = (char *)"?"; 414 (void) inet_addr_format(& sock->addr, &sa);411 (void) inet_addr_format(&ep->addr, &sa); 415 412 (void) inet_addr_format(&patt->addr, &pa); 416 413 417 414 log_msg(LOG_DEFAULT, LVL_NOTE, 418 "udp_ socket_match(sock=(%s,%u), pat=(%s,%u))",419 sa, sock->port, pa, patt->port);420 415 "udp_ep_match(ep=(%s,%u), pat=(%s,%u))", 416 sa, ep->port, pa, patt->port); 417 421 418 if ((!inet_addr_is_any(&patt->addr)) && 422 (!inet_addr_compare(&patt->addr, & sock->addr)))419 (!inet_addr_compare(&patt->addr, &ep->addr))) 423 420 return false; 424 421 425 422 log_msg(LOG_DEFAULT, LVL_NOTE, "addr OK"); 426 423 427 424 if ((patt->port != UDP_PORT_ANY) && 428 (patt->port != sock->port))425 (patt->port != ep->port)) 429 426 return false; 430 427 431 428 log_msg(LOG_DEFAULT, LVL_NOTE, " -> match"); 432 429 433 430 return true; 434 431 } 435 432 436 /** Match socket pair with pattern. */437 static bool udp_ sockpair_match(udp_sockpair_t *sp, udp_sockpair_t *pattern)438 { 439 log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_ sockpair_match(%p, %p)", sp, pattern);440 441 if (!udp_ socket_match(&sp->local, &pattern->local))433 /** Match endpoint pair with pattern. */ 434 static bool udp_ep2_match(inet_ep2_t *epp, inet_ep2_t *pattern) 435 { 436 log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_ep2_match(%p, %p)", epp, pattern); 437 438 if (!udp_ep_match(&epp->local, &pattern->local)) 442 439 return false; 443 440 444 if (!udp_ socket_match(&sp->foreign, &pattern->foreign))441 if (!udp_ep_match(&epp->remote, &pattern->remote)) 445 442 return false; 446 443 447 log_msg(LOG_DEFAULT, LVL_DEBUG, " Socket pair matched.");444 log_msg(LOG_DEFAULT, LVL_DEBUG, "Endpoint pair matched."); 448 445 return true; 449 446 } 450 447 451 448 452 /** Find association structure for specified socket pair.453 * 454 * An association is uniquely identified by a socket pair. Look up our455 * association map and return association structure based on socket pair.449 /** Find association structure for specified endpoint pair. 450 * 451 * An association is uniquely identified by an endpoint pair. Look up our 452 * association map and return association structure based on endpoint pair. 456 453 * The association reference count is bumped by one. 457 454 * 458 * @param sp Socket pair455 * @param epp Endpoint pair 459 456 * @return Association structure or NULL if not found. 460 457 */ 461 static udp_assoc_t *udp_assoc_find_ref( udp_sockpair_t *sp)458 static udp_assoc_t *udp_assoc_find_ref(inet_ep2_t *epp) 462 459 { 463 460 char *la, *ra; 464 461 465 log_msg(LOG_DEFAULT, LVL_NOTE, "udp_assoc_find_ref(%p)", sp);466 462 log_msg(LOG_DEFAULT, LVL_NOTE, "udp_assoc_find_ref(%p)", epp); 463 467 464 fibril_mutex_lock(&assoc_list_lock); 468 465 469 466 log_msg(LOG_DEFAULT, LVL_NOTE, "associations:"); 470 467 list_foreach(assoc_list, link, udp_assoc_t, assoc) { 471 udp_sockpair_t *asp = &assoc->ident;472 468 inet_ep2_t *aepp = &assoc->ident; 469 473 470 la = ra = NULL; 474 471 475 (void) inet_addr_format(&a sp->local.addr, &la);476 (void) inet_addr_format(&a sp->foreign.addr, &ra);477 478 log_msg(LOG_DEFAULT, LVL_NOTE, "find_ref:a sp=%p la=%s ra=%s",479 a sp, la, ra);472 (void) inet_addr_format(&aepp->local.addr, &la); 473 (void) inet_addr_format(&aepp->remote.addr, &ra); 474 475 log_msg(LOG_DEFAULT, LVL_NOTE, "find_ref:aepp=%p la=%s ra=%s", 476 aepp, la, ra); 480 477 /* Skip unbound associations */ 481 if (a sp->local.port == UDP_PORT_ANY) {478 if (aepp->local.port == UDP_PORT_ANY) { 482 479 log_msg(LOG_DEFAULT, LVL_NOTE, "skip unbound"); 483 480 continue; 484 481 } 485 486 if (udp_ sockpair_match(sp, asp)) {482 483 if (udp_ep2_match(epp, aepp)) { 487 484 log_msg(LOG_DEFAULT, LVL_DEBUG, "Returning assoc %p", assoc); 488 485 udp_assoc_addref(assoc); … … 493 490 } 494 491 } 495 492 496 493 log_msg(LOG_DEFAULT, LVL_NOTE, "associations END"); 497 494 fibril_mutex_unlock(&assoc_list_lock);
Note:
See TracChangeset
for help on using the changeset viewer.