source: mainline/uspace/lib/nettl/src/portrng.c@ c3f7d37

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since c3f7d37 was c3f7d37, checked in by Jiri Svoboda <jiri@…>, 10 years ago

Remove excessive debugging output.

  • Property mode set to 100644
File size: 4.2 KB
RevLine 
[4794417]1/*
[2f19103]2 * Copyright (c) 2015 Jiri Svoboda
[4794417]3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
[2989c7e]29/** @addtogroup libnettl
[4794417]30 * @{
31 */
[2989c7e]32
33/**
34 * @file Port range allocator
35 *
36 * Allocates port numbers from IETF port number ranges.
[4794417]37 */
38
[ab6326bc]39#include <adt/list.h>
[2989c7e]40#include <errno.h>
[ab6326bc]41#include <inet/endpoint.h>
[2989c7e]42#include <nettl/portrng.h>
43#include <stdint.h>
[ab6326bc]44#include <stdlib.h>
45
46#include <io/log.h>
[2989c7e]47
48int portrng_create(portrng_t **rpr)
49{
[ab6326bc]50 portrng_t *pr;
51
[c3f7d37]52 log_msg(LOG_DEFAULT, LVL_DEBUG2, "portrng_create() - begin");
[ab6326bc]53
54 pr = calloc(1, sizeof(portrng_t));
55 if (pr == NULL)
56 return ENOMEM;
57
58 list_initialize(&pr->used);
59 *rpr = pr;
[c3f7d37]60 log_msg(LOG_DEFAULT, LVL_DEBUG2, "portrng_create() - end");
[2989c7e]61 return EOK;
62}
63
64void portrng_destroy(portrng_t *pr)
65{
[c3f7d37]66 log_msg(LOG_DEFAULT, LVL_DEBUG2, "portrng_destroy()");
[ab6326bc]67 assert(list_empty(&pr->used));
68 free(pr);
[2989c7e]69}
70
[ab6326bc]71int portrng_alloc(portrng_t *pr, uint16_t pnum, void *arg,
72 portrng_flags_t flags, uint16_t *apnum)
[2989c7e]73{
[ab6326bc]74 portrng_port_t *p;
75 uint32_t i;
76 bool found;
77
[c3f7d37]78 log_msg(LOG_DEFAULT, LVL_DEBUG2, "portrng_alloc() - begin");
[ab6326bc]79
80 if (pnum == inet_port_any) {
81
82 for (i = inet_port_dyn_lo; i <= inet_port_dyn_hi; i++) {
[c3f7d37]83 log_msg(LOG_DEFAULT, LVL_DEBUG2, "trying %" PRIu32, i);
[ab6326bc]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 }
[c3f7d37]102 log_msg(LOG_DEFAULT, LVL_DEBUG2, "selected %" PRIu16, pnum);
[ab6326bc]103 } else {
[c3f7d37]104 log_msg(LOG_DEFAULT, LVL_DEBUG2, "user asked for %" PRIu16, pnum);
[1ede059]105
[ab6326bc]106 if ((flags & pf_allow_system) == 0 &&
107 pnum < inet_port_user_lo) {
[c3f7d37]108 log_msg(LOG_DEFAULT, LVL_DEBUG2, "system port not allowed");
[ab6326bc]109 return EINVAL;
110 }
111
112 list_foreach(pr->used, lprng, portrng_port_t, port) {
[1ede059]113 if (port->pn == pnum) {
[c3f7d37]114 log_msg(LOG_DEFAULT, LVL_DEBUG2, "port already used");
[ab6326bc]115 return EEXISTS;
[1ede059]116 }
[ab6326bc]117 }
118 }
119
120 p = calloc(1, sizeof(portrng_port_t));
121 if (p == NULL)
122 return ENOMEM;
123
124 p->pn = pnum;
[8d48c7e]125 p->arg = arg;
[ab6326bc]126 list_append(&p->lprng, &pr->used);
127 *apnum = pnum;
[c3f7d37]128 log_msg(LOG_DEFAULT, LVL_DEBUG2, "portrng_alloc() - end OK pn=%" PRIu16,
[ab6326bc]129 pnum);
[2989c7e]130 return EOK;
131}
132
[8d48c7e]133int portrng_find_port(portrng_t *pr, uint16_t pnum, void **rarg)
134{
135 list_foreach(pr->used, lprng, portrng_port_t, port) {
136 if (port->pn == pnum) {
137 *rarg = port->arg;
138 return EOK;
139 }
140 }
141
142 return ENOENT;
143}
144
[ab6326bc]145void portrng_free_port(portrng_t *pr, uint16_t pnum)
[2989c7e]146{
[c3f7d37]147 log_msg(LOG_DEFAULT, LVL_DEBUG2, "portrng_free_port() - begin");
[ab6326bc]148 list_foreach(pr->used, lprng, portrng_port_t, port) {
149 if (port->pn == pnum) {
150 list_remove(&port->lprng);
151 free(port);
152 return;
153 }
154 }
155
156 assert(false);
[c3f7d37]157 log_msg(LOG_DEFAULT, LVL_DEBUG2, "portrng_free_port() - end");
[2989c7e]158}
159
[ab6326bc]160bool portrng_empty(portrng_t *pr)
[2989c7e]161{
[c3f7d37]162 log_msg(LOG_DEFAULT, LVL_DEBUG2, "portrng_empty()");
[ab6326bc]163 return list_empty(&pr->used);
[2989c7e]164}
165
166/**
167 * @}
[4794417]168 */
[2989c7e]169
Note: See TracBrowser for help on using the repository browser.