Changeset 2f19103 in mainline for uspace/srv/net/udp/assoc.c


Ignore:
Timestamp:
2015-05-22T07:21:37Z (9 years ago)
Author:
Jiri Svoboda <jiri@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
58e9dec
Parents:
bf7587b0
Message:

TCP and UDP servers can make use of inet/endpoint.h types internally.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/srv/net/udp/assoc.c

    rbf7587b0 r2f19103  
    11/*
    2  * Copyright (c) 2012 Jiri Svoboda
     2 * Copyright (c) 2015 Jiri Svoboda
    33 * All rights reserved.
    44 *
     
    5252FIBRIL_MUTEX_INITIALIZE(assoc_list_lock);
    5353
    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 *);
     54static udp_assoc_t *udp_assoc_find_ref(inet_ep2_t *);
     55static int udp_assoc_queue_msg(udp_assoc_t *, inet_ep2_t *, udp_msg_t *);
     56static bool udp_ep_match(inet_ep_t *, inet_ep_t *);
     57static bool udp_ep2_match(inet_ep2_t *, inet_ep2_t *);
    5858
    5959/** Create new association structure.
    6060 *
    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
    6364 * @return              New association or NULL
    6465 */
    65 udp_assoc_t *udp_assoc_new(udp_sock_t *lsock, udp_sock_t *fsock,
    66     udp_assoc_cb_t *cb, void *cb_arg)
     66udp_assoc_t *udp_assoc_new(inet_ep2_t *epp, udp_assoc_cb_t *cb, void *cb_arg)
    6767{
    6868        udp_assoc_t *assoc = NULL;
     
    8282        fibril_condvar_initialize(&assoc->rcv_queue_cv);
    8383
    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;
    8986
    9087        assoc->cb = cb;
     
    200197            assoc, iplink);
    201198        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 */
     208void 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.
    220217 *
    221218 * @param assoc Association
    222  * @param lsock 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, lsock);
    228         fibril_mutex_lock(&assoc->lock);
    229         assoc->ident.local = *lsock;
     219 * @param local Local endpoint (deeply copied)
     220 *
     221 */
     222void 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;
    230227        fibril_mutex_unlock(&assoc->lock);
    231228}
     
    248245 *
    249246 * @param assoc         Association
    250  * @param fsock         Foreign socket or NULL not to override @a assoc
     247 * @param remote        Remote endpoint or NULL not to override @a assoc
    251248 * @param msg           Message
    252249 *
    253250 * @return              EOK on success
    254  *                      EINVAL if foreign socket is not set
     251 *                      EINVAL if remote endpoint is not set
    255252 *                      ENOMEM if out of resources
    256253 *                      EIO if no route to destination exists
    257254 */
    258 int udp_assoc_send(udp_assoc_t *assoc, udp_sock_t *fsock, udp_msg_t *msg)
     255int udp_assoc_send(udp_assoc_t *assoc, inet_ep_t *remote, udp_msg_t *msg)
    259256{
    260257        udp_pdu_t *pdu;
    261         udp_sockpair_t sp;
     258        inet_ep2_t epp;
    262259        int rc;
    263260
    264261        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;
    271268
    272269        log_msg(LOG_DEFAULT, LVL_NOTE, "udp_assoc_send - check addr any");
    273270
    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))
    276273                return EINVAL;
    277274
    278275        log_msg(LOG_DEFAULT, LVL_NOTE, "udp_assoc_send - check version");
    279276
    280         if (sp.foreign.addr.version != sp.local.addr.version)
     277        if (epp.remote.addr.version != epp.local.addr.version)
    281278                return EINVAL;
    282279
    283280        log_msg(LOG_DEFAULT, LVL_NOTE, "udp_assoc_send - encode pdu");
    284281
    285         rc = udp_pdu_encode(&sp, msg, &pdu);
     282        rc = udp_pdu_encode(&epp, msg, &pdu);
    286283        if (rc != EOK)
    287284                return ENOMEM;
     
    303300 * Pull one message from the association's receive queue.
    304301 */
    305 int udp_assoc_recv(udp_assoc_t *assoc, udp_msg_t **msg, udp_sock_t *fsock)
     302int udp_assoc_recv(udp_assoc_t *assoc, udp_msg_t **msg, inet_ep_t *remote)
    306303{
    307304        link_t *link;
     
    329326
    330327        *msg = rqe->msg;
    331         *fsock = rqe->sp.foreign;
     328        *remote = rqe->epp.remote;
    332329        free(rqe);
    333330
     
    339336 * Find the association to which the message belongs and queue it.
    340337 */
    341 void udp_assoc_received(udp_sockpair_t *rsp, udp_msg_t *msg)
     338void udp_assoc_received(inet_ep2_t *repp, udp_msg_t *msg)
    342339{
    343340        udp_assoc_t *assoc;
    344341        int rc;
    345342
    346         log_msg(LOG_DEFAULT, LVL_NOTE, "udp_assoc_received(%p, %p)", rsp, msg);
    347 
    348         assoc = udp_assoc_find_ref(rsp);
     343        log_msg(LOG_DEFAULT, LVL_NOTE, "udp_assoc_received(%p, %p)", repp, msg);
     344
     345        assoc = udp_assoc_find_ref(repp);
    349346        if (assoc == NULL) {
    350347                log_msg(LOG_DEFAULT, LVL_NOTE, "No association found. Message dropped.");
     
    356353
    357354        if (0) {
    358                 rc = udp_assoc_queue_msg(assoc, rsp, msg);
     355                rc = udp_assoc_queue_msg(assoc, repp, msg);
    359356                if (rc != EOK) {
    360357                        log_msg(LOG_DEFAULT, LVL_DEBUG, "Out of memory. Message dropped.");
     
    364361
    365362        log_msg(LOG_DEFAULT, LVL_NOTE, "call assoc->cb->recv_msg");
    366         assoc->cb->recv_msg(assoc->cb_arg, rsp, msg);
     363        assoc->cb->recv_msg(assoc->cb_arg, repp, msg);
    367364        udp_assoc_delref(assoc);
    368365}
     
    381378}
    382379
    383 static int udp_assoc_queue_msg(udp_assoc_t *assoc, udp_sockpair_t *sp,
     380static int udp_assoc_queue_msg(udp_assoc_t *assoc, inet_ep2_t *epp,
    384381    udp_msg_t *msg)
    385382{
     
    387384
    388385        log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_assoc_queue_msg(%p, %p, %p)",
    389             assoc, sp, msg);
     386            assoc, epp, msg);
    390387
    391388        rqe = calloc(1, sizeof(udp_rcv_queue_entry_t));
     
    394391
    395392        link_initialize(&rqe->link);
    396         rqe->sp = *sp;
     393        rqe->epp = *epp;
    397394        rqe->msg = msg;
    398395
     
    406403}
    407404
    408 /** Match socket with pattern. */
    409 static bool udp_socket_match(udp_sock_t *sock, udp_sock_t *patt)
     405/** Match endpoint with pattern. */
     406static bool udp_ep_match(inet_ep_t *ep, inet_ep_t *patt)
    410407{
    411408        char *sa, *pa;
    412409
    413410        sa = pa = (char *)"?";
    414         (void) inet_addr_format(&sock->addr, &sa);
     411        (void) inet_addr_format(&ep->addr, &sa);
    415412        (void) inet_addr_format(&patt->addr, &pa);
    416413
    417414        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
    421418        if ((!inet_addr_is_any(&patt->addr)) &&
    422             (!inet_addr_compare(&patt->addr, &sock->addr)))
     419            (!inet_addr_compare(&patt->addr, &ep->addr)))
    423420                return false;
    424        
     421
    425422        log_msg(LOG_DEFAULT, LVL_NOTE, "addr OK");
    426        
     423
    427424        if ((patt->port != UDP_PORT_ANY) &&
    428             (patt->port != sock->port))
     425            (patt->port != ep->port))
    429426                return false;
    430        
     427
    431428        log_msg(LOG_DEFAULT, LVL_NOTE, " -> match");
    432        
     429
    433430        return true;
    434431}
    435432
    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. */
     434static 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))
    442439                return false;
    443440
    444         if (!udp_socket_match(&sp->foreign, &pattern->foreign))
     441        if (!udp_ep_match(&epp->remote, &pattern->remote))
    445442                return false;
    446443
    447         log_msg(LOG_DEFAULT, LVL_DEBUG, "Socket pair matched.");
     444        log_msg(LOG_DEFAULT, LVL_DEBUG, "Endpoint pair matched.");
    448445        return true;
    449446}
    450447
    451448
    452 /** Find association structure for specified socket pair.
    453  *
    454  * An association is uniquely identified by a socket pair. Look up our
    455  * 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.
    456453 * The association reference count is bumped by one.
    457454 *
    458  * @param sp    Socket pair
     455 * @param epp   Endpoint pair
    459456 * @return      Association structure or NULL if not found.
    460457 */
    461 static udp_assoc_t *udp_assoc_find_ref(udp_sockpair_t *sp)
     458static udp_assoc_t *udp_assoc_find_ref(inet_ep2_t *epp)
    462459{
    463460        char *la, *ra;
    464461
    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
    467464        fibril_mutex_lock(&assoc_list_lock);
    468        
     465
    469466        log_msg(LOG_DEFAULT, LVL_NOTE, "associations:");
    470467        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
    473470                la = ra = NULL;
    474471
    475                 (void) inet_addr_format(&asp->local.addr, &la);
    476                 (void) inet_addr_format(&asp->foreign.addr, &ra);
    477 
    478                 log_msg(LOG_DEFAULT, LVL_NOTE, "find_ref:asp=%p la=%s ra=%s",
    479                     asp, 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);
    480477                /* Skip unbound associations */
    481                 if (asp->local.port == UDP_PORT_ANY) {
     478                if (aepp->local.port == UDP_PORT_ANY) {
    482479                        log_msg(LOG_DEFAULT, LVL_NOTE, "skip unbound");
    483480                        continue;
    484481                }
    485                
    486                 if (udp_sockpair_match(sp, asp)) {
     482
     483                if (udp_ep2_match(epp, aepp)) {
    487484                        log_msg(LOG_DEFAULT, LVL_DEBUG, "Returning assoc %p", assoc);
    488485                        udp_assoc_addref(assoc);
     
    493490                }
    494491        }
    495        
     492
    496493        log_msg(LOG_DEFAULT, LVL_NOTE, "associations END");
    497494        fibril_mutex_unlock(&assoc_list_lock);
Note: See TracChangeset for help on using the changeset viewer.