Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/lib/nettl/src/amap.c

    r8a637a4 rc3f7d37  
    3636 * Manages allocations of endpoints / endpoint pairs (corresponding to
    3737 * UDP associations, TCP listeners and TCP connections)
    38  *
    39  * An association map contains different types of entries, based on which
    40  * set of attributes (key) they specify. In order from most specific to the
    41  * least specific one:
    42  *
    43  *  - repla (remote endpoint, local address)
    44  *  - laddr (local address)
    45  *  - llink (local link)
    46  *  - unspec (unspecified)
    47  *
    48  * In the unspecified case only the local port is known and the entry matches
    49  * all remote and local addresses.
    5038 */
    5139
     
    5947#include <stdlib.h>
    6048
    61 /** Create association map.
    62  *
    63  * @param rmap Place to store pointer to new association map
    64  * @return EOk on success, ENOMEM if out of memory
    65  */
    6649int amap_create(amap_t **rmap)
    6750{
     
    9073}
    9174
    92 /** Destroy association map.
    93  *
    94  * @param map Association map
    95  */
    9675void amap_destroy(amap_t *map)
    9776{
     
    10079}
    10180
    102 /** Find exact repla.
    103  *
    104  * Find repla (remote endpoint, local address) entry by exact match.
    105  *
    106  * @param map Association map
    107  * @param rep Remote endpoint
    108  * @param la  Local address
    109  * @param rrepla Place to store pointer to repla
    110  *
    111  * @return EOK on success, ENOENT if not found
    112  */
     81/** Find exact repla */
    11382static int amap_repla_find(amap_t *map, inet_ep_t *rep, inet_addr_t *la,
    11483    amap_repla_t **rrepla)
     
    144113}
    145114
    146 /** Insert repla.
    147  *
    148  * Insert new repla (remote endpoint, local address) entry to association map.
    149  *
    150  * @param amap   Association map
    151  * @param rep    Remote endpoint
    152  * @param la     Local address
    153  * @param rrepla Place to store pointer to new repla
    154  *
    155  * @return EOK on success, ENOMEM if out of memory
    156  */
    157115static int amap_repla_insert(amap_t *map, inet_ep_t *rep, inet_addr_t *la,
    158116    amap_repla_t **rrepla)
     
    181139}
    182140
    183 /** Remove repla from association map.
    184  *
    185  * Remove repla (remote endpoint, local address) from association map.
    186  *
    187  * @param map   Association map
    188  * @param repla Repla
    189  */
    190141static void amap_repla_remove(amap_t *map, amap_repla_t *repla)
    191142{
     
    195146}
    196147
    197 /** Find exact laddr.
    198  *
    199  * Find laddr (local address) entry by exact match.
    200  *
    201  * @param map    Association map
    202  * @param addr   Address
    203  * @param rladdr Place to store pointer to laddr entry
    204  *
    205  * @return EOK on success, ENOENT if not found.
    206  */
     148/** Find exact laddr */
    207149static int amap_laddr_find(amap_t *map, inet_addr_t *addr,
    208150    amap_laddr_t **rladdr)
     
    219161}
    220162
    221 /** Insert laddr.
    222  *
    223  * Insert new laddr (local address) entry to association map.
    224  *
    225  * @param addr   Local address
    226  * @param rladdr Place to store pointer to new laddr
    227  *
    228  * @return EOK on success, ENOMEM if out of memory
    229  */
    230163static int amap_laddr_insert(amap_t *map, inet_addr_t *addr,
    231164    amap_laddr_t **rladdr)
     
    253186}
    254187
    255 /** Remove laddr from association map.
    256  *
    257  * Remove laddr (local address) entry from association map.
    258  *
    259  * @param map   Association map
    260  * @param laddr Laddr entry
    261  */
    262188static void amap_laddr_remove(amap_t *map, amap_laddr_t *laddr)
    263189{
     
    267193}
    268194
    269 /** Find exact llink.
    270  *
    271  * Find llink (local link) entry by exact match.
    272  *
    273  * @param map     Association map
    274  * @param link_id Local link ID
    275  * @param rllink  Place to store pointer to llink entry
    276  *
    277  * @return EOK on success, ENOENT if not found.
    278  */
     195/** Find exact llink */
    279196static int amap_llink_find(amap_t *map, sysarg_t link_id,
    280197    amap_llink_t **rllink)
     
    291208}
    292209
    293 /** Insert llink.
    294  *
    295  * Insert new llink (local link) entry to association map.
    296  *
    297  * @param link_id Local link
    298  * @param rllink  Place to store pointer to new llink
    299  *
    300  * @return EOK on success, ENOMEM if out of memory
    301  */
    302210static int amap_llink_insert(amap_t *map, sysarg_t link_id,
    303211    amap_llink_t **rllink)
     
    325233}
    326234
    327 /** Remove llink from association map.
    328  *
    329  * Remove llink (local link) entry from association map.
    330  *
    331  * @param map   Association map
    332  * @param llink Llink entry
    333  */
    334235static void amap_llink_remove(amap_t *map, amap_llink_t *llink)
    335236{
     
    339240}
    340241
    341 /** Insert endpoint pair into map with repla as key.
    342  *
    343  * If local port number is not specified, it is allocated.
    344  *
    345  * @param map Association map
    346  * @param epp Endpoint pair, possibly with local port inet_port_any
    347  * @param arg arg User value
    348  * @param flags Flags
    349  * @param aepp Place to store actual endpoint pair, possibly with allocated port
    350  *
    351  * @return EOK on success, EEXIST if conflicting epp exists,
    352  *         ENOMEM if out of memory
    353  */
    354242static int amap_insert_repla(amap_t *map, inet_ep2_t *epp, void *arg,
    355243    amap_flags_t flags, inet_ep2_t *aepp)
     
    384272}
    385273
    386 /** Insert endpoint pair into map with laddr as key.
    387  *
    388  * If local port number is not specified, it is allocated.
     274static int amap_insert_laddr(amap_t *map, inet_ep2_t *epp, void *arg,
     275    amap_flags_t flags, inet_ep2_t *aepp)
     276{
     277        amap_laddr_t *laddr;
     278        inet_ep2_t mepp;
     279        int rc;
     280
     281        log_msg(LOG_DEFAULT, LVL_DEBUG2, "amap_insert_laddr()");
     282
     283        rc = amap_laddr_find(map, &epp->local.addr, &laddr);
     284        if (rc != EOK) {
     285                /* New laddr */
     286                rc = amap_laddr_insert(map, &epp->local.addr, &laddr);
     287                if (rc != EOK) {
     288                        assert(rc == ENOMEM);
     289                        return rc;
     290                }
     291        }
     292
     293        mepp = *epp;
     294
     295        rc = portrng_alloc(laddr->portrng, epp->local.port, arg, flags,
     296            &mepp.local.port);
     297        if (rc != EOK) {
     298                return rc;
     299        }
     300
     301        *aepp = mepp;
     302        return EOK;
     303}
     304
     305static int amap_insert_llink(amap_t *map, inet_ep2_t *epp, void *arg,
     306    amap_flags_t flags, inet_ep2_t *aepp)
     307{
     308        amap_llink_t *llink;
     309        inet_ep2_t mepp;
     310        int rc;
     311
     312        log_msg(LOG_DEFAULT, LVL_DEBUG2, "amap_insert_llink()");
     313
     314        rc = amap_llink_find(map, epp->local_link, &llink);
     315        if (rc != EOK) {
     316                /* New llink */
     317                rc = amap_llink_insert(map, epp->local_link, &llink);
     318                if (rc != EOK) {
     319                        assert(rc == ENOMEM);
     320                        return rc;
     321                }
     322        }
     323
     324        mepp = *epp;
     325
     326        rc = portrng_alloc(llink->portrng, epp->local.port, arg, flags,
     327            &mepp.local.port);
     328        if (rc != EOK) {
     329                return rc;
     330        }
     331
     332        *aepp = mepp;
     333        return EOK;
     334}
     335
     336static int amap_insert_unspec(amap_t *map, inet_ep2_t *epp, void *arg,
     337    amap_flags_t flags, inet_ep2_t *aepp)
     338{
     339        inet_ep2_t mepp;
     340        int rc;
     341
     342        log_msg(LOG_DEFAULT, LVL_DEBUG2, "amap_insert_unspec()");
     343        mepp = *epp;
     344
     345        rc = portrng_alloc(map->unspec, epp->local.port, arg, flags,
     346            &mepp.local.port);
     347        if (rc != EOK) {
     348                return rc;
     349        }
     350
     351        *aepp = mepp;
     352        return EOK;
     353}
     354
     355/** Insert endpoint pair into map.
     356 *
     357 * If local endpoint is not fully specified, it is filled in (determine
     358 * source address, allocate port number). Checks for conflicting endpoint pair.
    389359 *
    390360 * @param map Association map
     
    394364 * @param aepp Place to store actual endpoint pair, possibly with allocated port
    395365 *
    396  * @return EOK on success, EEXIST if conflicting epp exists,
    397  *         ENOMEM if out of memory
    398  */
    399 static int amap_insert_laddr(amap_t *map, inet_ep2_t *epp, void *arg,
    400     amap_flags_t flags, inet_ep2_t *aepp)
    401 {
    402         amap_laddr_t *laddr;
    403         inet_ep2_t mepp;
    404         int rc;
    405 
    406         log_msg(LOG_DEFAULT, LVL_DEBUG2, "amap_insert_laddr()");
    407 
    408         rc = amap_laddr_find(map, &epp->local.addr, &laddr);
    409         if (rc != EOK) {
    410                 /* New laddr */
    411                 rc = amap_laddr_insert(map, &epp->local.addr, &laddr);
    412                 if (rc != EOK) {
    413                         assert(rc == ENOMEM);
    414                         return rc;
    415                 }
    416         }
    417 
    418         mepp = *epp;
    419 
    420         rc = portrng_alloc(laddr->portrng, epp->local.port, arg, flags,
    421             &mepp.local.port);
    422         if (rc != EOK) {
    423                 return rc;
    424         }
    425 
    426         *aepp = mepp;
    427         return EOK;
    428 }
    429 
    430 /** Insert endpoint pair into map with llink as key.
    431  *
    432  * If local port number is not specified, it is allocated.
    433  *
    434  * @param map Association map
    435  * @param epp Endpoint pair, possibly with local port inet_port_any
    436  * @param arg arg User value
    437  * @param flags Flags
    438  * @param aepp Place to store actual endpoint pair, possibly with allocated port
    439  *
    440  * @return EOK on success, EEXIST if conflicting epp exists,
    441  *         ENOMEM if out of memory
    442  */
    443 static int amap_insert_llink(amap_t *map, inet_ep2_t *epp, void *arg,
    444     amap_flags_t flags, inet_ep2_t *aepp)
    445 {
    446         amap_llink_t *llink;
    447         inet_ep2_t mepp;
    448         int rc;
    449 
    450         log_msg(LOG_DEFAULT, LVL_DEBUG2, "amap_insert_llink()");
    451 
    452         rc = amap_llink_find(map, epp->local_link, &llink);
    453         if (rc != EOK) {
    454                 /* New llink */
    455                 rc = amap_llink_insert(map, epp->local_link, &llink);
    456                 if (rc != EOK) {
    457                         assert(rc == ENOMEM);
    458                         return rc;
    459                 }
    460         }
    461 
    462         mepp = *epp;
    463 
    464         rc = portrng_alloc(llink->portrng, epp->local.port, arg, flags,
    465             &mepp.local.port);
    466         if (rc != EOK) {
    467                 return rc;
    468         }
    469 
    470         *aepp = mepp;
    471         return EOK;
    472 }
    473 
    474 /** Insert endpoint pair into map with unspec as key.
    475  *
    476  * If local port number is not specified, it is allocated.
    477  *
    478  * @param map Association map
    479  * @param epp Endpoint pair, possibly with local port inet_port_any
    480  * @param arg arg User value
    481  * @param flags Flags
    482  * @param aepp Place to store actual endpoint pair, possibly with allocated port
    483  *
    484  * @return EOK on success, EEXIST if conflicting epp exists,
    485  *         ENOMEM if out of memory
    486  */
    487 static int amap_insert_unspec(amap_t *map, inet_ep2_t *epp, void *arg,
    488     amap_flags_t flags, inet_ep2_t *aepp)
    489 {
    490         inet_ep2_t mepp;
    491         int rc;
    492 
    493         log_msg(LOG_DEFAULT, LVL_DEBUG2, "amap_insert_unspec()");
    494         mepp = *epp;
    495 
    496         rc = portrng_alloc(map->unspec, epp->local.port, arg, flags,
    497             &mepp.local.port);
    498         if (rc != EOK) {
    499                 return rc;
    500         }
    501 
    502         *aepp = mepp;
    503         return EOK;
    504 }
    505 
    506 /** Insert endpoint pair into map.
    507  *
    508  * If local endpoint is not fully specified, it is filled in (determine
    509  * source address, allocate port number). Checks for conflicting endpoint pair.
    510  *
    511  * @param map Association map
    512  * @param epp Endpoint pair, possibly with local port inet_port_any
    513  * @param arg arg User value
    514  * @param flags Flags
    515  * @param aepp Place to store actual endpoint pair, possibly with allocated port
    516  *
    517  * @return EOK on success, EEXIST if conflicting epp exists,
     366 * @return EOK on success, EEXISTS if conflicting epp exists,
    518367 *         ENOMEM if out of memory
    519368 */
     
    568417}
    569418
    570 /** Remove endpoint pair using repla as key from map.
    571  *
    572  * The endpoint pair must be present in the map, otherwise behavior
    573  * is unspecified.
    574  *
    575  * @param map Association map
    576  * @param epp Endpoint pair
    577  */
    578419static void amap_remove_repla(amap_t *map, inet_ep2_t *epp)
    579420{
     
    593434}
    594435
    595 /** Remove endpoint pair using laddr as key from map.
    596  *
    597  * The endpoint pair must be present in the map, otherwise behavior
    598  * is unspecified.
    599  *
    600  * @param map Association map
    601  * @param epp Endpoint pair
    602  */
    603436static void amap_remove_laddr(amap_t *map, inet_ep2_t *epp)
    604437{
     
    618451}
    619452
    620 /** Remove endpoint pair using llink as key from map.
    621  *
    622  * The endpoint pair must be present in the map, otherwise behavior
    623  * is unspecified.
    624  *
    625  * @param map Association map
    626  * @param epp Endpoint pair
    627  */
    628453static void amap_remove_llink(amap_t *map, inet_ep2_t *epp)
    629454{
     
    643468}
    644469
    645 /** Remove endpoint pair using unspec as key from map.
    646  *
    647  * The endpoint pair must be present in the map, otherwise behavior
    648  * is unspecified.
    649  *
    650  * @param map Association map
    651  * @param epp Endpoint pair
    652  */
    653470static void amap_remove_unspec(amap_t *map, inet_ep2_t *epp)
    654471{
     
    656473}
    657474
    658 /** Remove endpoint pair from map.
    659  *
    660  * The endpoint pair must be present in the map, otherwise behavior
    661  * is unspecified.
    662  *
    663  * @param map Association map
    664  * @param epp Endpoint pair
    665  */
    666475void amap_remove(amap_t *map, inet_ep2_t *epp)
    667476{
Note: See TracChangeset for help on using the changeset viewer.