Debian Codegraph @

src/common/addr.cpp

raw symbols SCIP
1 /*
2 * Copyright (C) 1999,2000,2001 Ross Combs ([email protected])
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 */
18 #define ADDR_INTERNAL_ACCESScxx . . $ `src/common/addr.cpp:18:9`!
19 #include "common/setup_before.h"cxx . . $ `<file>/src/common/setup_before.h`/
20 #include "common/addr.h"cxx . . $ `<file>/src/common/addr.h`/
21
22 #include <cstdio>
23 #include <cstring>
24 #include <cerrno>
25 #include <cassert>
26
27 #include "compat/psock.h"cxx . . $ `<file>/src/compat/psock.h`/
28 #include "common/eventlog.h"cxx . . $ `<file>/src/common/eventlog.h`/
29 #include "common/list.h"cxx . . $ `<file>/src/common/list.h`/
30 #include "common/util.h"cxx . . $ `<file>/src/common/util.h`/
31 #include "common/xalloc.h"cxx . . $ `<file>/src/common/xalloc.h`/
32
33 #ifdef HAVE_ARPA_INET_Hcxx . . $ `build/config.h:13:9`!
34 # include <arpa/inet.h>
35 #endif
36 #ifdef HAVE_WS2TCPIP_H
37 # include <Ws2tcpip.h>
38 #endif
39
40 #include "common/setup_after.h"cxx . . $ `<file>/src/common/setup_after.h`/
41
42 namespace pvpgncxx . . $ pvpgn/
43 {
44
45 static char const * netaddr_num_to_addr_strcxx . . $ pvpgn/netaddr_num_to_addr_str(f1aafdd40709cfa1).(unsigned int netipaddr, unsigned int netmask);
46
47
48 #define HACK_SIZEcxx . . $ `src/common/addr.cpp:48:9`! 4
49
50 /* both arguments are in host byte order */
51 extern char const * addr_num_to_addr_strcxx . . $ pvpgn/addr_num_to_addr_str(9313bf3c546cb16).(unsigned int ipaddr, unsigned short port)
52 {
53 static unsigned int curr = 0;
54 static char temp[HACK_SIZEcxx . . $ `src/common/addr.cpp:48:9`!][64];
55 struct sockaddr_in tsa = { 0 };
56
57 curr = (curr + 1) % HACK_SIZEcxx . . $ `src/common/addr.cpp:48:9`!;
58
59 tsa.sin_family = PSOCK_AF_INETcxx . . $ `src/compat/psock.h:189:9`!;
60 tsa.sin_port = htons((unsigned short)0);
61 tsa.sin_addr.s_addr = htonl(ipaddr);
62
63 char addrstr[INET_ADDRSTRLENcxx . . $ `<external>/9581057695539925927/in.h:244:9`!] = { 0 };
64 inet_ntop(AF_INETcxx . . $ `<external>/8962547315054503424/socket.h:97:9`!, &(tsa.sin_addr), addrstr, sizeof(addrstr));
65 stdcxx . . $ std/::sprintfcxx . . $ std/sprintf(bb743b22d71e45d5).(temp[curr], "%.32s:%hu", addrstr, port);
66
67 return temp[curr];
68 }
69
70
71 /* ipaddr is in host byte order */
72 extern char const * addr_num_to_ip_strcxx . . $ pvpgn/addr_num_to_ip_str(81a3ae90da26a711).(unsigned int ipaddr)
73 {
74 static unsigned int curr = 0;
75 static char temp[HACK_SIZEcxx . . $ `src/common/addr.cpp:48:9`!][64];
76 struct sockaddr_in tsa;
77
78 curr = (curr + 1) % HACK_SIZEcxx . . $ `src/common/addr.cpp:48:9`!;
79
80 stdcxx . . $ std/::memset(&tsa, 0, sizeof(tsa));
81 tsa.sin_family = PSOCK_AF_INETcxx . . $ `src/compat/psock.h:189:9`!;
82 tsa.sin_port = htons((unsigned short)0);
83 tsa.sin_addr.s_addr = htonl(ipaddr);
84
85 char addrstr[INET_ADDRSTRLENcxx . . $ `<external>/9581057695539925927/in.h:244:9`!] = { 0 };
86 inet_ntop(AF_INETcxx . . $ `<external>/8962547315054503424/socket.h:97:9`!, &(tsa.sin_addr), addrstr, sizeof(addrstr));
87 stdcxx . . $ std/::sprintfcxx . . $ std/sprintf(bb743b22d71e45d5).(temp[curr], "%.32s", addrstr);
88
89 return temp[curr];
90 }
91
92
93 static char const * netaddr_num_to_addr_strcxx . . $ pvpgn/netaddr_num_to_addr_str(f1aafdd40709cfa1).(unsigned int netipaddr, unsigned int netmask)
94 {
95 static unsigned int curr = 0;
96 static char temp[HACK_SIZEcxx . . $ `src/common/addr.cpp:48:9`!][64];
97 struct sockaddr_in tsa;
98
99 curr = (curr + 1) % HACK_SIZEcxx . . $ `src/common/addr.cpp:48:9`!;
100
101 stdcxx . . $ std/::memset(&tsa, 0, sizeof(tsa));
102 tsa.sin_family = PSOCK_AF_INETcxx . . $ `src/compat/psock.h:189:9`!;
103 tsa.sin_port = htons((unsigned short)0);
104 tsa.sin_addr.s_addr = htonl(netipaddr);
105
106 char addrstr[INET_ADDRSTRLENcxx . . $ `<external>/9581057695539925927/in.h:244:9`!] = { 0 };
107 inet_ntop(AF_INETcxx . . $ `<external>/8962547315054503424/socket.h:97:9`!, &(tsa.sin_addr), addrstr, sizeof(addrstr));
108 stdcxx . . $ std/::sprintfcxx . . $ std/sprintf(bb743b22d71e45d5).(temp[curr], "%.32s/0x%08x", addrstr, netmask);
109
110 return temp[curr];
111 }
112
113
114
115 extern char const * host_lookupcxx . . $ pvpgn/host_lookup(fa7b797f304c585f).(char const * hoststr, unsigned int * ipaddr)
116 {
117 struct sockaddr_in tsa;
118 #ifdef HAVE_GETHOSTBYNAMEcxx . . $ `build/config.h:45:9`!
119 struct hostent * hp;
120 #endif
121
122 if (!hoststr)
123 {
124 eventlogcxx . . $ pvpgn/eventlog(f1d8dd72586f1e25).(eventlog_level_errorcxx . . $ pvpgn/eventlog_level_error., __FUNCTION__, "got NULL hoststr");
125 return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!;
126 }
127 if (!ipaddr)
128 {
129 eventlogcxx . . $ pvpgn/eventlog(f1d8dd72586f1e25).(eventlog_level_errorcxx . . $ pvpgn/eventlog_level_error., __FUNCTION__, "got NULL ipaddr");
130 return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!;
131 }
132
133 stdcxx . . $ std/::memset(&tsa, 0, sizeof(tsa));
134 tsa.sin_family = PSOCK_AF_INETcxx . . $ `src/compat/psock.h:189:9`!;
135 tsa.sin_port = htons(0);
136
137 #ifdef HAVE_GETHOSTBYNAMEcxx . . $ `build/config.h:45:9`!
138 #ifdef WIN32
139 psock_init();
140 #endif
141 hp = gethostbyname(hoststr);
142 if (!hp || !hp->h_addr_list)
143 #endif
144 {
145 if (inet_pton(AF_INETcxx . . $ `<external>/8962547315054503424/socket.h:97:9`!, hoststr, &tsa.sin_addr) == 1)
146 {
147 *ipaddr = ntohl(tsa.sin_addr.s_addr);
148 return hoststr; /* We could call gethostbyaddr() on tsa to try and get the
149 official hostname but most systems would have already found
150 it when sending a dotted-quad to gethostbyname(). This is
151 good enough when that fails. */
152 }
153 eventlogcxx . . $ pvpgn/eventlog(f1d8dd72586f1e25).(eventlog_level_errorcxx . . $ pvpgn/eventlog_level_error., __FUNCTION__, "could not lookup host \"{}\"", hoststr);
154 return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!;
155 }
156
157 #ifdef HAVE_GETHOSTBYNAMEcxx . . $ `build/config.h:45:9`!
158 stdcxx . . $ std/::memcpy(&tsa.sin_addr, (void *)hp->h_addr_list[0], sizeof(struct in_addr)); /* avoid warning */
159 *ipaddr = ntohl(tsa.sin_addr.s_addr);
160 if (hp->h_name)
161 return hp->h_name;
162 return hoststr;
163 #endif
164 }
165
166
167 extern t_addrcxx . . $ pvpgn/t_addr# * addr_create_numcxx . . $ pvpgn/addr_create_num(97d318a7d2cf6420).(unsigned int ipaddr, unsigned short port)
168 {
169 t_addrcxx . . $ pvpgn/t_addr# * temp;
170
171 temp = (t_addrcxx . . $ pvpgn/t_addr#*)xmalloccxx . . $ `src/common/xalloc.h:43:9`!(sizeof(t_addr));
172 temp->strcxx . . $ pvpgn/addr_struct#str. = xstrdupcxx . . $ `src/common/xalloc.h:49:9`!(addr_num_to_addr_str(ipaddr, port));
173 temp->ipcxx . . $ pvpgn/addr_struct#ip. = ipaddr;
174 temp->portcxx . . $ pvpgn/addr_struct#port. = port;
175 temp->datacxx . . $ pvpgn/addr_struct#data..pcxx . . $ pvpgn/$anonymous_type_583377e381542df8_0#p. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!;
176
177 return temp;
178 }
179
180
181 extern t_addrcxx . . $ pvpgn/t_addr# * addr_create_strcxx . . $ pvpgn/addr_create_str(1da662362765fa7c).(char const * str, unsigned int defipaddr, unsigned short defport)
182 {
183 char * tstr;
184 t_addrcxx . . $ pvpgn/t_addr# * temp;
185 unsigned int ipaddr;
186 unsigned short port;
187 char const * hoststr;
188 char * portstr;
189 char const * hostname;
190
191 if (!str)
192 {
193 eventlogcxx . . $ pvpgn/eventlog(f1d8dd72586f1e25).(eventlog_level_errorcxx . . $ pvpgn/eventlog_level_error., __FUNCTION__, "got NULL str");
194 return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!;
195 }
196
197 tstr = xstrdupcxx . . $ `src/common/xalloc.h:49:9`!(str);
198
199 if ((portstr = stdcxx . . $ std/::strrchr(tstr, ':')))
200 {
201 char * protstr;
202
203 *portstr = '\0';
204 portstr++;
205
206 if ((protstr = stdcxx . . $ std/::strrchr(portstr, '/')))
207 {
208 *protstr = '\0';
209 protstr++;
210 }
211
212 if (portstr[0] != '\0')
213 {
214 if (str_to_ushortcxx . . $ pvpgn/str_to_ushort(c9bce2f4bcd31d4).(portstr, &port) < 0)
215 {
216 #ifdef HAVE_GETSERVBYNAMEcxx . . $ `build/config.h:52:9`!
217 struct servent * sp;
218
219 if (!(sp = getservbyname(portstr, protstr ? protstr : "tcp")))
220 #endif
221 {
222 eventlogcxx . . $ pvpgn/eventlog(f1d8dd72586f1e25).(eventlog_level_errorcxx . . $ pvpgn/eventlog_level_error., __FUNCTION__, "could not convert \"{}\" to a port number", portstr);
223 xfreecxx . . $ `src/common/xalloc.h:51:9`!(tstr);
224 return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!;
225 }
226 #ifdef HAVE_GETSERVBYNAMEcxx . . $ `build/config.h:52:9`!
227 port = ntohs(sp->s_port);
228 #endif
229 }
230 }
231 else
232 port = defport;
233 }
234 else
235 port = defport;
236
237 char addrstr[INET_ADDRSTRLENcxx . . $ `<external>/9581057695539925927/in.h:244:9`!] = {};
238
239 if (tstr[0] != '\0')
240 {
241 hoststr = tstr;
242 }
243 else
244 {
245 struct sockaddr_in tsa {};
246 tsa.sin_addr.s_addr = htonl(defipaddr);
247
248 hoststr = inet_ntop(AF_INETcxx . . $ `<external>/8962547315054503424/socket.h:97:9`!, &(tsa.sin_addr), addrstr, sizeof(addrstr));
249 }
250
251 if (!(hostname = host_lookupcxx . . $ pvpgn/host_lookup(fa7b797f304c585f).(hoststr, &ipaddr)))
252 {
253 eventlogcxx . . $ pvpgn/eventlog(f1d8dd72586f1e25).(eventlog_level_errorcxx . . $ pvpgn/eventlog_level_error., __FUNCTION__, "could not lookup host \"{}\"", hoststr);
254 xfreecxx . . $ `src/common/xalloc.h:51:9`!(tstr);
255 return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!;
256 }
257
258 temp = (t_addrcxx . . $ pvpgn/t_addr#*)xmalloccxx . . $ `src/common/xalloc.h:43:9`!(sizeof(t_addr));
259 temp->strcxx . . $ pvpgn/addr_struct#str. = xstrdupcxx . . $ `src/common/xalloc.h:49:9`!(hostname);
260 xfreecxx . . $ `src/common/xalloc.h:51:9`!(tstr);
261
262 temp->ipcxx . . $ pvpgn/addr_struct#ip. = ipaddr;
263 temp->portcxx . . $ pvpgn/addr_struct#port. = port;
264 temp->datacxx . . $ pvpgn/addr_struct#data..pcxx . . $ pvpgn/$anonymous_type_583377e381542df8_0#p. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!;
265
266 return temp;
267 }
268
269
270 extern int addr_destroycxx . . $ pvpgn/addr_destroy(b8ff9276d6945afa).(t_addrcxx . . $ pvpgn/t_addr# const * addr)
271 {
272 if (!addr)
273 {
274 eventlogcxx . . $ pvpgn/eventlog(f1d8dd72586f1e25).(eventlog_level_errorcxx . . $ pvpgn/eventlog_level_error., __FUNCTION__, "got NULL addr");
275 return -1;
276 }
277
278 if (addr->strcxx . . $ pvpgn/addr_struct#str.)
279 xfreecxx . . $ `src/common/xalloc.h:51:9`!((void *)addr->str); /* avoid warning */
280 xfreecxx . . $ `src/common/xalloc.h:51:9`!((void *)addr); /* avoid warning */
281
282 return 0;
283 }
284
285
286 /* hostname or IP */
287 extern char * addr_get_host_strcxx . . $ pvpgn/addr_get_host_str(b93b79389147bbb8).(t_addrcxx . . $ pvpgn/t_addr# const * addr, char * str, unsigned int len)
288 {
289 if (!addr)
290 {
291 eventlogcxx . . $ pvpgn/eventlog(f1d8dd72586f1e25).(eventlog_level_errorcxx . . $ pvpgn/eventlog_level_error., __FUNCTION__, "got NULL addr");
292 return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!;
293 }
294 if (!str)
295 {
296 eventlogcxx . . $ pvpgn/eventlog(f1d8dd72586f1e25).(eventlog_level_errorcxx . . $ pvpgn/eventlog_level_error., __FUNCTION__, "got NULL str");
297 return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!;
298 }
299 if (len < 2)
300 {
301 eventlogcxx . . $ pvpgn/eventlog(f1d8dd72586f1e25).(eventlog_level_errorcxx . . $ pvpgn/eventlog_level_error., __FUNCTION__, "str too short");
302 return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!;
303 }
304
305 if (!addr->strcxx . . $ pvpgn/addr_struct#str.)
306 {
307 eventlogcxx . . $ pvpgn/eventlog(f1d8dd72586f1e25).(eventlog_level_errorcxx . . $ pvpgn/eventlog_level_error., __FUNCTION__, "addr has NULL str");
308 return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!;
309 }
310
311 stdcxx . . $ std/::strncpy(str, addr->strcxx . . $ pvpgn/addr_struct#str., len - 1);
312 str[len - 1] = '\0';
313
314 return str;
315 }
316
317
318 /* IP:port */
319 extern char * addr_get_addr_strcxx . . $ pvpgn/addr_get_addr_str(b93b79389147bbb8).(t_addrcxx . . $ pvpgn/t_addr# const * addr, char * str, unsigned int len)
320 {
321 if (!addr)
322 {
323 eventlogcxx . . $ pvpgn/eventlog(f1d8dd72586f1e25).(eventlog_level_errorcxx . . $ pvpgn/eventlog_level_error., __FUNCTION__, "got NULL addr");
324 return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!;
325 }
326 if (!str)
327 {
328 eventlogcxx . . $ pvpgn/eventlog(f1d8dd72586f1e25).(eventlog_level_errorcxx . . $ pvpgn/eventlog_level_error., __FUNCTION__, "got NULL str");
329 return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!;
330 }
331 if (len < 2)
332 {
333 eventlogcxx . . $ pvpgn/eventlog(f1d8dd72586f1e25).(eventlog_level_errorcxx . . $ pvpgn/eventlog_level_error., __FUNCTION__, "str too short");
334 return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!;
335 }
336
337 stdcxx . . $ std/::strncpy(str, addr_num_to_addr_strcxx . . $ pvpgn/addr_num_to_addr_str(9313bf3c546cb16).(addr->ipcxx . . $ pvpgn/addr_struct#ip., addr->portcxx . . $ pvpgn/addr_struct#port.), len - 1);
338 str[len - 1] = '\0';
339
340 return str;
341 }
342
343
344 extern unsigned int addr_get_ipcxx . . $ pvpgn/addr_get_ip(9026149328323fab).(t_addrcxx . . $ pvpgn/t_addr# const * addr)
345 {
346 if (!addr)
347 {
348 eventlogcxx . . $ pvpgn/eventlog(f1d8dd72586f1e25).(eventlog_level_errorcxx . . $ pvpgn/eventlog_level_error., __FUNCTION__, "got NULL addr");
349 return 0;
350 }
351
352 return addr->ipcxx . . $ pvpgn/addr_struct#ip.;
353 }
354
355
356 extern unsigned short addr_get_portcxx . . $ pvpgn/addr_get_port(51ac8fdf58f03e6a).(t_addrcxx . . $ pvpgn/t_addr# const * addr)
357 {
358 if (!addr)
359 {
360 eventlogcxx . . $ pvpgn/eventlog(f1d8dd72586f1e25).(eventlog_level_errorcxx . . $ pvpgn/eventlog_level_error., __FUNCTION__, "got NULL addr");
361 return 0;
362 }
363
364 return addr->portcxx . . $ pvpgn/addr_struct#port.;
365 }
366
367
368 extern int addr_set_datacxx . . $ pvpgn/addr_set_data(923901faeaeb5a29).(t_addrcxx . . $ pvpgn/t_addr# * addr, t_addr_datacxx . . $ pvpgn/t_addr_data# data)
369 {
370 if (!addr)
371 {
372 eventlogcxx . . $ pvpgn/eventlog(f1d8dd72586f1e25).(eventlog_level_errorcxx . . $ pvpgn/eventlog_level_error., __FUNCTION__, "got NULL addr");
373 return -1;
374 }
375
376 addr->datacxx . . $ pvpgn/addr_struct#data. =cxx . . $ pvpgn/$anonymous_type_583377e381542df8_0#`operator=`(593a66dbf5f09737). data;
377 return 0;
378 }
379
380
381 extern t_addr_datacxx . . $ pvpgn/t_addr_data# addr_get_datacxx . . $ pvpgn/addr_get_data(f1ed834059e839e0).(t_addrcxx . . $ pvpgn/t_addr# const * addr)
382 {
383 t_addr_datacxx . . $ pvpgn/t_addr_data# tdata;
384
385 if (!addr)
386 {
387 eventlogcxx . . $ pvpgn/eventlog(f1d8dd72586f1e25).(eventlog_level_errorcxx . . $ pvpgn/eventlog_level_error., __FUNCTION__, "got NULL addr");
388 tdata.pcxx . . $ pvpgn/$anonymous_type_583377e381542df8_0#p. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!;
389 return tdata;
390 }
391
392 return addr->datacxx . . $ pvpgn/addr_struct#data.;
393 }
394
395
396 extern t_netaddrcxx . . $ pvpgn/t_netaddr# * netaddr_create_strcxx . . $ pvpgn/netaddr_create_str(9ebd152e18e009fb).(char const * netstr)
397 {
398 t_netaddrcxx . . $ pvpgn/t_netaddr# * netaddr;
399 char * temp;
400 char const * netipstr;
401 char const * netmaskstr;
402 unsigned int netip;
403 unsigned int netmask;
404
405 if (!netstr)
406 {
407 eventlogcxx . . $ pvpgn/eventlog(f1d8dd72586f1e25).(eventlog_level_errorcxx . . $ pvpgn/eventlog_level_error., __FUNCTION__, "unable to allocate memory for netaddr");
408 return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!;
409 }
410
411 temp = xstrdupcxx . . $ `src/common/xalloc.h:49:9`!(netstr);
412 if (!(netipstr = stdcxx . . $ std/::strtok(temp, "/")))
413 {
414 xfreecxx . . $ `src/common/xalloc.h:51:9`!(temp);
415 return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!;
416 }
417 if (!(netmaskstr = stdcxx . . $ std/::strtok(NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!, "/")))
418 {
419 xfreecxx . . $ `src/common/xalloc.h:51:9`!(temp);
420 return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!;
421 }
422
423 netaddr = (t_netaddrcxx . . $ pvpgn/t_netaddr#*)xmalloccxx . . $ `src/common/xalloc.h:43:9`!(sizeof(t_netaddr));
424
425 /* FIXME: call getnetbyname() first, then host_lookup() */
426 if (!host_lookupcxx . . $ pvpgn/host_lookup(fa7b797f304c585f).(netipstr, &netip))
427 {
428 eventlogcxx . . $ pvpgn/eventlog(f1d8dd72586f1e25).(eventlog_level_errorcxx . . $ pvpgn/eventlog_level_error., __FUNCTION__, "could not lookup net");
429 xfreecxx . . $ `src/common/xalloc.h:51:9`!(netaddr);
430 xfreecxx . . $ `src/common/xalloc.h:51:9`!(temp);
431 return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!;
432 }
433 netaddr->ipcxx . . $ pvpgn/netaddr_struct#ip. = netip;
434
435 if (str_to_uintcxx . . $ pvpgn/str_to_uint(6974107818eeb505).(netmaskstr, &netmask) < 0)
436 {
437 struct sockaddr_in tsa;
438
439 if (inet_pton(AF_INETcxx . . $ `<external>/8962547315054503424/socket.h:97:9`!, netmaskstr, &tsa.sin_addr) == 1)
440 netmask = ntohl(tsa.sin_addr.s_addr);
441 else
442 {
443 eventlogcxx . . $ pvpgn/eventlog(f1d8dd72586f1e25).(eventlog_level_errorcxx . . $ pvpgn/eventlog_level_error., __FUNCTION__, "could not convert mask");
444 xfreecxx . . $ `src/common/xalloc.h:51:9`!(netaddr);
445 xfreecxx . . $ `src/common/xalloc.h:51:9`!(temp);
446 return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!;
447 }
448 }
449 else
450 {
451 if (netmask > 32)
452 {
453 eventlogcxx . . $ pvpgn/eventlog(f1d8dd72586f1e25).(eventlog_level_errorcxx . . $ pvpgn/eventlog_level_error., __FUNCTION__, "network bits must be less than or equal to 32 ({})", netmask);
454 xfreecxx . . $ `src/common/xalloc.h:51:9`!(netaddr);
455 xfreecxx . . $ `src/common/xalloc.h:51:9`!(temp);
456 return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!;
457 }
458 /* for example, 8 -> 11111111000000000000000000000000 */
459 if (netmask != 0)
460 netmask = ~((1 << (32 - netmask)) - 1);
461 }
462 netaddr->maskcxx . . $ pvpgn/netaddr_struct#mask. = netmask;
463
464 xfreecxx . . $ `src/common/xalloc.h:51:9`!(temp);
465
466 return netaddr;
467 }
468
469
470 extern int netaddr_destroycxx . . $ pvpgn/netaddr_destroy(889b3810d111ab5f).(t_netaddrcxx . . $ pvpgn/t_netaddr# const * netaddr)
471 {
472 if (!netaddr)
473 {
474 eventlogcxx . . $ pvpgn/eventlog(f1d8dd72586f1e25).(eventlog_level_errorcxx . . $ pvpgn/eventlog_level_error., __FUNCTION__, "got NULL netaddr");
475 return -1;
476 }
477
478 xfreecxx . . $ `src/common/xalloc.h:51:9`!((void *)netaddr); /* avoid warning */
479
480 return 0;
481 }
482
483
484 extern char * netaddr_get_addr_strcxx . . $ pvpgn/netaddr_get_addr_str(129d4d743b5f8f1).(t_netaddrcxx . . $ pvpgn/t_netaddr# const * netaddr, char * str, unsigned int len)
485 {
486 if (!netaddr)
487 {
488 eventlogcxx . . $ pvpgn/eventlog(f1d8dd72586f1e25).(eventlog_level_errorcxx . . $ pvpgn/eventlog_level_error., __FUNCTION__, "got NULL netaddr");
489 return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!;
490 }
491 if (!str)
492 {
493 eventlogcxx . . $ pvpgn/eventlog(f1d8dd72586f1e25).(eventlog_level_errorcxx . . $ pvpgn/eventlog_level_error., __FUNCTION__, "got NULL str");
494 return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!;
495 }
496 if (len < 2)
497 {
498 eventlogcxx . . $ pvpgn/eventlog(f1d8dd72586f1e25).(eventlog_level_errorcxx . . $ pvpgn/eventlog_level_error., __FUNCTION__, "str too short");
499 return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!;
500 }
501
502 stdcxx . . $ std/::strncpy(str, netaddr_num_to_addr_strcxx . . $ pvpgn/netaddr_num_to_addr_str(f1aafdd40709cfa1).(netaddr->ipcxx . . $ pvpgn/netaddr_struct#ip., netaddr->maskcxx . . $ pvpgn/netaddr_struct#mask.), len - 1); /* FIXME: format nicely with x.x.x.x/bitcount */
503 str[len - 1] = '\0';
504
505 return str;
506 }
507
508
509 extern int netaddr_contains_addr_numcxx . . $ pvpgn/netaddr_contains_addr_num(526ee617a5ce9c6d).(t_netaddrcxx . . $ pvpgn/t_netaddr# const * netaddr, unsigned int ipaddr)
510 {
511 if (!netaddr)
512 {
513 eventlogcxx . . $ pvpgn/eventlog(f1d8dd72586f1e25).(eventlog_level_errorcxx . . $ pvpgn/eventlog_level_error., __FUNCTION__, "got NULL netaddr");
514 return -1;
515 }
516
517 return (ipaddr&netaddr->maskcxx . . $ pvpgn/netaddr_struct#mask.) == netaddr->ipcxx . . $ pvpgn/netaddr_struct#ip.;
518 }
519
520
521 extern int addrlist_appendcxx . . $ pvpgn/addrlist_append(a00d8714a8e44592).(t_addrlistcxx . . $ pvpgn/t_addrlist# * addrlist, char const * str, unsigned int defipaddr, unsigned short defport)
522 {
523 t_addrcxx . . $ pvpgn/t_addr# * addr;
524 char * tstr;
525 char * tok;
526
527 assertcxx . . $ `<external>/4398592474888995393/assert.h:102:11`!(addrlist != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!);
528
529 if (!str)
530 {
531 eventlogcxx . . $ pvpgn/eventlog(f1d8dd72586f1e25).(eventlog_level_errorcxx . . $ pvpgn/eventlog_level_error., __FUNCTION__, "got NULL str");
532 return -1;
533 }
534
535 tstr = xstrdupcxx . . $ `src/common/xalloc.h:49:9`!(str);
536 for (tok = stdcxx . . $ std/::strtok(tstr, ","); tok; tok = stdcxx . . $ std/::strtok(NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!, ",")) /* std::strtok modifies the string it is passed */
537 {
538 if (!(addr = addr_create_strcxx . . $ pvpgn/addr_create_str(1da662362765fa7c).(tok, defipaddr, defport)))
539 {
540 eventlogcxx . . $ pvpgn/eventlog(f1d8dd72586f1e25).(eventlog_level_errorcxx . . $ pvpgn/eventlog_level_error., __FUNCTION__, "could not create addr");
541 xfreecxx . . $ `src/common/xalloc.h:51:9`!(tstr);
542 return -1;
543 }
544 list_append_datacxx . . $ pvpgn/list_append_data(4e1348bd294d996f).(addrlist, addr);
545 }
546
547 xfreecxx . . $ `src/common/xalloc.h:51:9`!(tstr);
548
549 return 0;
550 }
551
552 extern t_addrlistcxx . . $ pvpgn/t_addrlist# * addrlist_createcxx . . $ pvpgn/addrlist_create(4be7ff7e5cf77a03).(char const * str, unsigned int defipaddr, unsigned short defport)
553 {
554 t_addrlistcxx . . $ pvpgn/t_addrlist# * addrlist;
555
556 if (!str)
557 {
558 eventlogcxx . . $ pvpgn/eventlog(f1d8dd72586f1e25).(eventlog_level_errorcxx . . $ pvpgn/eventlog_level_error., __FUNCTION__, "got NULL str");
559 return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!;
560 }
561
562 addrlist = list_createcxx . . $ pvpgn/list_create(7a6cb11de248e2c9).();
563
564 if (addrlist_appendcxx . . $ pvpgn/addrlist_append(a00d8714a8e44592).(addrlist, str, defipaddr, defport) < 0) {
565 eventlogcxx . . $ pvpgn/eventlog(f1d8dd72586f1e25).(eventlog_level_errorcxx . . $ pvpgn/eventlog_level_error., __FUNCTION__, "could not append to newly created addrlist");
566 list_destroycxx . . $ pvpgn/list_destroy(20db0232078327de).(addrlist);
567 return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!;
568 }
569
570 return addrlist;
571 }
572
573 extern int addrlist_destroycxx . . $ pvpgn/addrlist_destroy(20db0232078327de).(t_addrlistcxx . . $ pvpgn/t_addrlist# * addrlist)
574 {
575 t_elem * curr;
576 t_addrcxx . . $ pvpgn/t_addr# * addr;
577
578 if (!addrlist)
579 {
580 eventlogcxx . . $ pvpgn/eventlog(f1d8dd72586f1e25).(eventlog_level_errorcxx . . $ pvpgn/eventlog_level_error., __FUNCTION__, "got NULL addrlist");
581 return -1;
582 }
583
584 LIST_TRAVERSEcxx . . $ `src/common/list.h:94:9`!(addrlist, curr)
585 {
586 if (!(addr = (t_addrcxx . . $ pvpgn/t_addr#*)elem_get_datacxx . . $ pvpgn/elem_get_data(317ff0e7f02bf2b).(curr)))
587 eventlogcxx . . $ pvpgn/eventlog(f1d8dd72586f1e25).(eventlog_level_errorcxx . . $ pvpgn/eventlog_level_error., __FUNCTION__, "found NULL addr in list");
588 else
589 addr_destroycxx . . $ pvpgn/addr_destroy(b8ff9276d6945afa).(addr);
590 list_remove_elemcxx . . $ pvpgn/list_remove_elem(17f7246ce0d3563b).(addrlist, &curr);
591 }
592
593 return list_destroycxx . . $ pvpgn/list_destroy(20db0232078327de).(addrlist);
594 }
595
596
597 extern int addrlist_get_lengthcxx . . $ pvpgn/addrlist_get_length(a0f84c70630f53e0).(t_addrlistcxx . . $ pvpgn/t_addrlist# const * addrlist)
598 {
599 return list_get_lengthcxx . . $ pvpgn/list_get_length(ff288adc7992d0f0).(addrlist);
600 }
601
602 }