Changeset ab6326bc in mainline for uspace/lib/nettl/src/portrng.c
- Timestamp:
- 2015-05-28T15:31:10Z (9 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- 8d48c7e
- Parents:
- 2989c7e
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/lib/nettl/src/portrng.c
r2989c7e rab6326bc 37 37 */ 38 38 39 #include <adt/list.h> 39 40 #include <errno.h> 41 #include <inet/endpoint.h> 40 42 #include <nettl/portrng.h> 41 43 #include <stdint.h> 44 #include <stdlib.h> 45 46 #include <io/log.h> 42 47 43 48 int portrng_create(portrng_t **rpr) 44 49 { 50 portrng_t *pr; 51 52 log_msg(LOG_DEFAULT, LVL_NOTE, "portrng_create() - begin"); 53 54 pr = calloc(1, sizeof(portrng_t)); 55 if (pr == NULL) 56 return ENOMEM; 57 58 list_initialize(&pr->used); 59 *rpr = pr; 60 log_msg(LOG_DEFAULT, LVL_NOTE, "portrng_create() - end"); 45 61 return EOK; 46 62 } … … 48 64 void portrng_destroy(portrng_t *pr) 49 65 { 66 log_msg(LOG_DEFAULT, LVL_NOTE, "portrng_destroy()"); 67 assert(list_empty(&pr->used)); 68 free(pr); 50 69 } 51 70 52 int portrng_alloc _specific(portrng_t *pr, uint16_t pnum, void *arg,53 portrng_flags_t flags )71 int portrng_alloc(portrng_t *pr, uint16_t pnum, void *arg, 72 portrng_flags_t flags, uint16_t *apnum) 54 73 { 55 return EOK; 56 } 74 portrng_port_t *p; 75 uint32_t i; 76 bool found; 57 77 58 int portrng_alloc_dynamic(portrng_t *pr, void *arg, uint16_t *rpnum) 59 { 78 log_msg(LOG_DEFAULT, LVL_NOTE, "portrng_alloc() - begin"); 79 80 if (pnum == inet_port_any) { 81 82 for (i = inet_port_dyn_lo; i <= inet_port_dyn_hi; i++) { 83 log_msg(LOG_DEFAULT, LVL_NOTE, "trying %" PRIu32, i); 84 found = false; 85 list_foreach(pr->used, lprng, portrng_port_t, port) { 86 if (port->pn == pnum) { 87 found = true; 88 break; 89 } 90 } 91 92 if (!found) { 93 pnum = i; 94 break; 95 } 96 } 97 98 if (pnum == inet_port_any) { 99 /* No free port found */ 100 return ENOENT; 101 } 102 log_msg(LOG_DEFAULT, LVL_NOTE, "selected %" PRIu16, pnum); 103 } else { 104 if ((flags & pf_allow_system) == 0 && 105 pnum < inet_port_user_lo) { 106 return EINVAL; 107 } 108 109 list_foreach(pr->used, lprng, portrng_port_t, port) { 110 if (port->pn == pnum) 111 return EEXISTS; 112 } 113 } 114 115 p = calloc(1, sizeof(portrng_port_t)); 116 if (p == NULL) 117 return ENOMEM; 118 119 p->pn = pnum; 120 list_append(&p->lprng, &pr->used); 121 *apnum = pnum; 122 log_msg(LOG_DEFAULT, LVL_NOTE, "portrng_alloc() - end OK pn=%" PRIu16, 123 pnum); 60 124 return EOK; 61 125 } … … 63 127 void portrng_free_port(portrng_t *pr, uint16_t pnum) 64 128 { 129 log_msg(LOG_DEFAULT, LVL_NOTE, "portrng_free_port() - begin"); 130 list_foreach(pr->used, lprng, portrng_port_t, port) { 131 if (port->pn == pnum) { 132 list_remove(&port->lprng); 133 free(port); 134 return; 135 } 136 } 137 138 assert(false); 139 log_msg(LOG_DEFAULT, LVL_NOTE, "portrng_free_port() - end"); 140 } 141 142 bool portrng_empty(portrng_t *pr) 143 { 144 log_msg(LOG_DEFAULT, LVL_NOTE, "portrng_empty()"); 145 return list_empty(&pr->used); 65 146 } 66 147
Note:
See TracChangeset
for help on using the changeset viewer.