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
Line 
1/*
2 * Copyright (c) 2015 Jiri Svoboda
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
29/** @addtogroup libnettl
30 * @{
31 */
32
33/**
34 * @file Port range allocator
35 *
36 * Allocates port numbers from IETF port number ranges.
37 */
38
39#include <adt/list.h>
40#include <errno.h>
41#include <inet/endpoint.h>
42#include <nettl/portrng.h>
43#include <stdint.h>
44#include <stdlib.h>
45
46#include <io/log.h>
47
48int portrng_create(portrng_t **rpr)
49{
50 portrng_t *pr;
51
52 log_msg(LOG_DEFAULT, LVL_DEBUG2, "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_DEBUG2, "portrng_create() - end");
61 return EOK;
62}
63
64void portrng_destroy(portrng_t *pr)
65{
66 log_msg(LOG_DEFAULT, LVL_DEBUG2, "portrng_destroy()");
67 assert(list_empty(&pr->used));
68 free(pr);
69}
70
71int portrng_alloc(portrng_t *pr, uint16_t pnum, void *arg,
72 portrng_flags_t flags, uint16_t *apnum)
73{
74 portrng_port_t *p;
75 uint32_t i;
76 bool found;
77
78 log_msg(LOG_DEFAULT, LVL_DEBUG2, "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_DEBUG2, "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_DEBUG2, "selected %" PRIu16, pnum);
103 } else {
104 log_msg(LOG_DEFAULT, LVL_DEBUG2, "user asked for %" PRIu16, pnum);
105
106 if ((flags & pf_allow_system) == 0 &&
107 pnum < inet_port_user_lo) {
108 log_msg(LOG_DEFAULT, LVL_DEBUG2, "system port not allowed");
109 return EINVAL;
110 }
111
112 list_foreach(pr->used, lprng, portrng_port_t, port) {
113 if (port->pn == pnum) {
114 log_msg(LOG_DEFAULT, LVL_DEBUG2, "port already used");
115 return EEXISTS;
116 }
117 }
118 }
119
120 p = calloc(1, sizeof(portrng_port_t));
121 if (p == NULL)
122 return ENOMEM;
123
124 p->pn = pnum;
125 p->arg = arg;
126 list_append(&p->lprng, &pr->used);
127 *apnum = pnum;
128 log_msg(LOG_DEFAULT, LVL_DEBUG2, "portrng_alloc() - end OK pn=%" PRIu16,
129 pnum);
130 return EOK;
131}
132
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
145void portrng_free_port(portrng_t *pr, uint16_t pnum)
146{
147 log_msg(LOG_DEFAULT, LVL_DEBUG2, "portrng_free_port() - begin");
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);
157 log_msg(LOG_DEFAULT, LVL_DEBUG2, "portrng_free_port() - end");
158}
159
160bool portrng_empty(portrng_t *pr)
161{
162 log_msg(LOG_DEFAULT, LVL_DEBUG2, "portrng_empty()");
163 return list_empty(&pr->used);
164}
165
166/**
167 * @}
168 */
169
Note: See TracBrowser for help on using the repository browser.