]>
Commit | Line | Data |
---|---|---|
718e3744 | 1 | /* Socket union related function. |
2 | * Copyright (c) 1997, 98 Kunihiro Ishiguro | |
3 | * | |
4 | * This file is part of GNU Zebra. | |
5 | * | |
6 | * GNU Zebra is free software; you can redistribute it and/or modify it | |
7 | * under the terms of the GNU General Public License as published by the | |
8 | * Free Software Foundation; either version 2, or (at your option) any | |
9 | * later version. | |
10 | * | |
11 | * GNU Zebra is distributed in the hope that it will be useful, but | |
12 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 | * General Public License for more details. | |
15 | * | |
896014f4 DL |
16 | * You should have received a copy of the GNU General Public License along |
17 | * with this program; see the file COPYING; if not, write to the Free Software | |
18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
718e3744 | 19 | */ |
20 | ||
21 | #include <zebra.h> | |
22 | ||
23 | #include "prefix.h" | |
24 | #include "vty.h" | |
25 | #include "sockunion.h" | |
26 | #include "memory.h" | |
718e3744 | 27 | #include "log.h" |
7cb5cdbb | 28 | #include "jhash.h" |
718e3744 | 29 | |
4a1ab8e4 DL |
30 | DEFINE_MTYPE_STATIC(LIB, SOCKUNION, "Socket union") |
31 | ||
718e3744 | 32 | const char * |
c0d8db4d | 33 | inet_sutop (const union sockunion *su, char *str) |
718e3744 | 34 | { |
35 | switch (su->sa.sa_family) | |
36 | { | |
37 | case AF_INET: | |
38 | inet_ntop (AF_INET, &su->sin.sin_addr, str, INET_ADDRSTRLEN); | |
39 | break; | |
718e3744 | 40 | case AF_INET6: |
41 | inet_ntop (AF_INET6, &su->sin6.sin6_addr, str, INET6_ADDRSTRLEN); | |
42 | break; | |
718e3744 | 43 | } |
44 | return str; | |
45 | } | |
46 | ||
47 | int | |
a149411b | 48 | str2sockunion (const char *str, union sockunion *su) |
718e3744 | 49 | { |
50 | int ret; | |
51 | ||
52 | memset (su, 0, sizeof (union sockunion)); | |
53 | ||
54 | ret = inet_pton (AF_INET, str, &su->sin.sin_addr); | |
55 | if (ret > 0) /* Valid IPv4 address format. */ | |
56 | { | |
57 | su->sin.sin_family = AF_INET; | |
6f0e3f6e | 58 | #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN |
718e3744 | 59 | su->sin.sin_len = sizeof(struct sockaddr_in); |
6f0e3f6e | 60 | #endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */ |
718e3744 | 61 | return 0; |
62 | } | |
718e3744 | 63 | ret = inet_pton (AF_INET6, str, &su->sin6.sin6_addr); |
64 | if (ret > 0) /* Valid IPv6 address format. */ | |
65 | { | |
66 | su->sin6.sin6_family = AF_INET6; | |
67 | #ifdef SIN6_LEN | |
68 | su->sin6.sin6_len = sizeof(struct sockaddr_in6); | |
69 | #endif /* SIN6_LEN */ | |
70 | return 0; | |
71 | } | |
718e3744 | 72 | return -1; |
73 | } | |
74 | ||
75 | const char * | |
c0d8db4d | 76 | sockunion2str (const union sockunion *su, char *buf, size_t len) |
718e3744 | 77 | { |
67e2b6f0 TT |
78 | switch (sockunion_family(su)) |
79 | { | |
80 | case AF_UNSPEC: | |
81 | snprintf (buf, len, "(unspec)"); | |
82 | return buf; | |
83 | case AF_INET: | |
84 | return inet_ntop (AF_INET, &su->sin.sin_addr, buf, len); | |
67e2b6f0 TT |
85 | case AF_INET6: |
86 | return inet_ntop (AF_INET6, &su->sin6.sin6_addr, buf, len); | |
67e2b6f0 TT |
87 | } |
88 | snprintf (buf, len, "(af %d)", sockunion_family(su)); | |
89 | return buf; | |
718e3744 | 90 | } |
91 | ||
2fb2a455 PJ |
92 | union sockunion * |
93 | sockunion_str2su (const char *str) | |
94 | { | |
95 | union sockunion *su = XCALLOC (MTYPE_SOCKUNION, sizeof (union sockunion)); | |
96 | ||
97 | if (!str2sockunion (str, su)) | |
98 | return su; | |
99 | ||
100 | XFREE (MTYPE_SOCKUNION, su); | |
101 | return NULL; | |
102 | } | |
103 | ||
1a7dcf42 PJ |
104 | /* Convert IPv4 compatible IPv6 address to IPv4 address. */ |
105 | static void | |
106 | sockunion_normalise_mapped (union sockunion *su) | |
107 | { | |
108 | struct sockaddr_in sin; | |
109 | ||
1a7dcf42 PJ |
110 | if (su->sa.sa_family == AF_INET6 |
111 | && IN6_IS_ADDR_V4MAPPED (&su->sin6.sin6_addr)) | |
112 | { | |
113 | memset (&sin, 0, sizeof (struct sockaddr_in)); | |
114 | sin.sin_family = AF_INET; | |
3fa3f957 | 115 | sin.sin_port = su->sin6.sin6_port; |
1a7dcf42 PJ |
116 | memcpy (&sin.sin_addr, ((char *)&su->sin6.sin6_addr) + 12, 4); |
117 | memcpy (su, &sin, sizeof (struct sockaddr_in)); | |
118 | } | |
1a7dcf42 PJ |
119 | } |
120 | ||
fc9707c3 DW |
121 | /* return sockunion structure : this function should be revised. */ |
122 | static const char * | |
c0d8db4d | 123 | sockunion_log (const union sockunion *su, char *buf, size_t len) |
fc9707c3 DW |
124 | { |
125 | switch (su->sa.sa_family) | |
126 | { | |
127 | case AF_INET: | |
128 | return inet_ntop(AF_INET, &su->sin.sin_addr, buf, len); | |
129 | ||
130 | case AF_INET6: | |
131 | return inet_ntop(AF_INET6, &(su->sin6.sin6_addr), buf, len); | |
132 | break; | |
133 | ||
134 | default: | |
135 | snprintf (buf, len, "af_unknown %d ", su->sa.sa_family); | |
136 | return buf; | |
137 | } | |
138 | } | |
139 | ||
718e3744 | 140 | /* Return socket of sockunion. */ |
141 | int | |
c0d8db4d | 142 | sockunion_socket (const union sockunion *su) |
718e3744 | 143 | { |
144 | int sock; | |
145 | ||
146 | sock = socket (su->sa.sa_family, SOCK_STREAM, 0); | |
147 | if (sock < 0) | |
148 | { | |
fc9707c3 | 149 | char buf[SU_ADDRSTRLEN]; |
4525281a DL |
150 | zlog_warn("Can't make socket for %s : %s", |
151 | sockunion_log(su, buf, SU_ADDRSTRLEN), safe_strerror(errno)); | |
718e3744 | 152 | return -1; |
153 | } | |
154 | ||
155 | return sock; | |
156 | } | |
157 | ||
158 | /* Return accepted new socket file descriptor. */ | |
159 | int | |
160 | sockunion_accept (int sock, union sockunion *su) | |
161 | { | |
162 | socklen_t len; | |
163 | int client_sock; | |
164 | ||
165 | len = sizeof (union sockunion); | |
166 | client_sock = accept (sock, (struct sockaddr *) su, &len); | |
167 | ||
84152ee6 | 168 | sockunion_normalise_mapped (su); |
718e3744 | 169 | return client_sock; |
170 | } | |
171 | ||
172 | /* Return sizeof union sockunion. */ | |
8cc4198f | 173 | static int |
c0d8db4d | 174 | sockunion_sizeof (const union sockunion *su) |
718e3744 | 175 | { |
176 | int ret; | |
177 | ||
178 | ret = 0; | |
179 | switch (su->sa.sa_family) | |
180 | { | |
181 | case AF_INET: | |
182 | ret = sizeof (struct sockaddr_in); | |
183 | break; | |
718e3744 | 184 | case AF_INET6: |
185 | ret = sizeof (struct sockaddr_in6); | |
186 | break; | |
718e3744 | 187 | } |
188 | return ret; | |
189 | } | |
190 | ||
718e3744 | 191 | /* sockunion_connect returns |
192 | -1 : error occured | |
193 | 0 : connect success | |
194 | 1 : connect is in progress */ | |
195 | enum connect_result | |
c0d8db4d | 196 | sockunion_connect (int fd, const union sockunion *peersu, unsigned short port, |
b892f1dd | 197 | ifindex_t ifindex) |
718e3744 | 198 | { |
199 | int ret; | |
200 | int val; | |
201 | union sockunion su; | |
202 | ||
203 | memcpy (&su, peersu, sizeof (union sockunion)); | |
204 | ||
205 | switch (su.sa.sa_family) | |
206 | { | |
207 | case AF_INET: | |
208 | su.sin.sin_port = port; | |
209 | break; | |
718e3744 | 210 | case AF_INET6: |
211 | su.sin6.sin6_port = port; | |
212 | #ifdef KAME | |
213 | if (IN6_IS_ADDR_LINKLOCAL(&su.sin6.sin6_addr) && ifindex) | |
214 | { | |
f2345335 | 215 | su.sin6.sin6_scope_id = ifindex; |
718e3744 | 216 | SET_IN6_LINKLOCAL_IFINDEX (su.sin6.sin6_addr, ifindex); |
217 | } | |
218 | #endif /* KAME */ | |
219 | break; | |
718e3744 | 220 | } |
221 | ||
222 | /* Make socket non-block. */ | |
223 | val = fcntl (fd, F_GETFL, 0); | |
224 | fcntl (fd, F_SETFL, val|O_NONBLOCK); | |
225 | ||
226 | /* Call connect function. */ | |
227 | ret = connect (fd, (struct sockaddr *) &su, sockunion_sizeof (&su)); | |
228 | ||
229 | /* Immediate success */ | |
230 | if (ret == 0) | |
231 | { | |
232 | fcntl (fd, F_SETFL, val); | |
233 | return connect_success; | |
234 | } | |
235 | ||
236 | /* If connect is in progress then return 1 else it's real error. */ | |
237 | if (ret < 0) | |
238 | { | |
239 | if (errno != EINPROGRESS) | |
240 | { | |
b24b19f7 | 241 | char str[SU_ADDRSTRLEN]; |
718e3744 | 242 | zlog_info ("can't connect to %s fd %d : %s", |
b24b19f7 SH |
243 | sockunion_log (&su, str, sizeof str), |
244 | fd, safe_strerror (errno)); | |
718e3744 | 245 | return connect_error; |
246 | } | |
247 | } | |
248 | ||
249 | fcntl (fd, F_SETFL, val); | |
250 | ||
251 | return connect_in_progress; | |
252 | } | |
253 | ||
254 | /* Make socket from sockunion union. */ | |
255 | int | |
256 | sockunion_stream_socket (union sockunion *su) | |
257 | { | |
258 | int sock; | |
259 | ||
260 | if (su->sa.sa_family == 0) | |
261 | su->sa.sa_family = AF_INET_UNION; | |
262 | ||
263 | sock = socket (su->sa.sa_family, SOCK_STREAM, 0); | |
264 | ||
265 | if (sock < 0) | |
4525281a | 266 | zlog_warn("can't make socket sockunion_stream_socket"); |
718e3744 | 267 | |
268 | return sock; | |
269 | } | |
270 | ||
271 | /* Bind socket to specified address. */ | |
272 | int | |
273 | sockunion_bind (int sock, union sockunion *su, unsigned short port, | |
274 | union sockunion *su_addr) | |
275 | { | |
276 | int size = 0; | |
277 | int ret; | |
278 | ||
279 | if (su->sa.sa_family == AF_INET) | |
280 | { | |
281 | size = sizeof (struct sockaddr_in); | |
282 | su->sin.sin_port = htons (port); | |
6f0e3f6e | 283 | #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN |
718e3744 | 284 | su->sin.sin_len = size; |
6f0e3f6e | 285 | #endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */ |
718e3744 | 286 | if (su_addr == NULL) |
12829328 | 287 | sockunion2ip (su) = htonl (INADDR_ANY); |
718e3744 | 288 | } |
718e3744 | 289 | else if (su->sa.sa_family == AF_INET6) |
290 | { | |
291 | size = sizeof (struct sockaddr_in6); | |
292 | su->sin6.sin6_port = htons (port); | |
293 | #ifdef SIN6_LEN | |
294 | su->sin6.sin6_len = size; | |
295 | #endif /* SIN6_LEN */ | |
296 | if (su_addr == NULL) | |
297 | { | |
1cbb5dfc | 298 | #ifdef LINUX_IPV6 |
718e3744 | 299 | memset (&su->sin6.sin6_addr, 0, sizeof (struct in6_addr)); |
300 | #else | |
301 | su->sin6.sin6_addr = in6addr_any; | |
302 | #endif /* LINUX_IPV6 */ | |
303 | } | |
304 | } | |
718e3744 | 305 | |
306 | ret = bind (sock, (struct sockaddr *)su, size); | |
307 | if (ret < 0) | |
fc9707c3 DW |
308 | { |
309 | char buf[SU_ADDRSTRLEN]; | |
4525281a DL |
310 | zlog_warn("can't bind socket for %s : %s", |
311 | sockunion_log(su, buf, SU_ADDRSTRLEN), safe_strerror(errno)); | |
fc9707c3 | 312 | } |
718e3744 | 313 | |
314 | return ret; | |
315 | } | |
316 | ||
317 | int | |
318 | sockopt_reuseaddr (int sock) | |
319 | { | |
320 | int ret; | |
321 | int on = 1; | |
322 | ||
323 | ret = setsockopt (sock, SOL_SOCKET, SO_REUSEADDR, | |
324 | (void *) &on, sizeof (on)); | |
325 | if (ret < 0) | |
326 | { | |
4525281a | 327 | zlog_warn("can't set sockopt SO_REUSEADDR to socket %d", sock); |
718e3744 | 328 | return -1; |
329 | } | |
330 | return 0; | |
331 | } | |
332 | ||
333 | #ifdef SO_REUSEPORT | |
334 | int | |
335 | sockopt_reuseport (int sock) | |
336 | { | |
337 | int ret; | |
338 | int on = 1; | |
339 | ||
340 | ret = setsockopt (sock, SOL_SOCKET, SO_REUSEPORT, | |
341 | (void *) &on, sizeof (on)); | |
342 | if (ret < 0) | |
343 | { | |
4525281a | 344 | zlog_warn("can't set sockopt SO_REUSEPORT to socket %d", sock); |
718e3744 | 345 | return -1; |
346 | } | |
347 | return 0; | |
348 | } | |
349 | #else | |
350 | int | |
351 | sockopt_reuseport (int sock) | |
352 | { | |
353 | return 0; | |
354 | } | |
355 | #endif /* 0 */ | |
356 | ||
357 | int | |
358 | sockopt_ttl (int family, int sock, int ttl) | |
359 | { | |
360 | int ret; | |
361 | ||
362 | #ifdef IP_TTL | |
363 | if (family == AF_INET) | |
364 | { | |
365 | ret = setsockopt (sock, IPPROTO_IP, IP_TTL, | |
366 | (void *) &ttl, sizeof (int)); | |
367 | if (ret < 0) | |
368 | { | |
4525281a | 369 | zlog_warn("can't set sockopt IP_TTL %d to socket %d", ttl, sock); |
718e3744 | 370 | return -1; |
371 | } | |
372 | return 0; | |
373 | } | |
374 | #endif /* IP_TTL */ | |
718e3744 | 375 | if (family == AF_INET6) |
376 | { | |
377 | ret = setsockopt (sock, IPPROTO_IPV6, IPV6_UNICAST_HOPS, | |
378 | (void *) &ttl, sizeof (int)); | |
379 | if (ret < 0) | |
380 | { | |
4525281a DL |
381 | zlog_warn("can't set sockopt IPV6_UNICAST_HOPS %d to socket %d", |
382 | ttl, sock); | |
718e3744 | 383 | return -1; |
384 | } | |
385 | return 0; | |
386 | } | |
718e3744 | 387 | return 0; |
388 | } | |
389 | ||
58192df7 SH |
390 | int |
391 | sockopt_cork (int sock, int onoff) | |
392 | { | |
393 | #ifdef TCP_CORK | |
394 | return setsockopt (sock, IPPROTO_TCP, TCP_CORK, &onoff, sizeof(onoff)); | |
395 | #else | |
396 | return 0; | |
ed40466a DS |
397 | #endif |
398 | } | |
399 | ||
400 | int sockopt_mark_default(int sock, int mark, struct zebra_privs_t *cap) | |
401 | { | |
402 | #ifdef SO_MARK | |
403 | int ret; | |
404 | ||
405 | if ( cap->change (ZPRIVS_RAISE) ) | |
406 | zlog_err ("routing_socket: Can't raise privileges"); | |
407 | ||
408 | ret = setsockopt(sock, SOL_SOCKET, SO_MARK, &mark, sizeof(mark)); | |
409 | ||
410 | if ( cap->change (ZPRIVS_LOWER) ) | |
411 | zlog_err ("routing_socket: Can't lower privileges"); | |
412 | ||
413 | return ret; | |
414 | #else | |
415 | return 0; | |
58192df7 SH |
416 | #endif |
417 | } | |
418 | ||
fa411a21 NH |
419 | int |
420 | sockopt_minttl (int family, int sock, int minttl) | |
421 | { | |
89b6d1f8 | 422 | #ifdef IP_MINTTL |
d876bdf4 | 423 | if (family == AF_INET) |
fa411a21 | 424 | { |
d876bdf4 SH |
425 | int ret = setsockopt (sock, IPPROTO_IP, IP_MINTTL, &minttl, sizeof(minttl)); |
426 | if (ret < 0) | |
4525281a DL |
427 | zlog_warn("can't set sockopt IP_MINTTL to %d on socket %d: %s", |
428 | minttl, sock, safe_strerror(errno)); | |
d876bdf4 | 429 | return ret; |
fa411a21 | 430 | } |
d876bdf4 | 431 | #endif /* IP_MINTTL */ |
d8dc5257 | 432 | #ifdef IPV6_MINHOPCOUNT |
d876bdf4 SH |
433 | if (family == AF_INET6) |
434 | { | |
d8dc5257 | 435 | int ret = setsockopt (sock, IPPROTO_IPV6, IPV6_MINHOPCOUNT, &minttl, sizeof(minttl)); |
d876bdf4 | 436 | if (ret < 0) |
4525281a DL |
437 | zlog_warn("can't set sockopt IPV6_MINHOPCOUNT to %d on socket %d: %s", |
438 | minttl, sock, safe_strerror(errno)); | |
d876bdf4 SH |
439 | return ret; |
440 | } | |
441 | #endif | |
fa411a21 | 442 | |
89b6d1f8 SH |
443 | errno = EOPNOTSUPP; |
444 | return -1; | |
fa411a21 NH |
445 | } |
446 | ||
ca051269 DL |
447 | int |
448 | sockopt_v6only (int family, int sock) | |
449 | { | |
450 | int ret, on = 1; | |
451 | ||
ca051269 DL |
452 | #ifdef IPV6_V6ONLY |
453 | if (family == AF_INET6) | |
454 | { | |
455 | ret = setsockopt (sock, IPPROTO_IPV6, IPV6_V6ONLY, | |
456 | (void *) &on, sizeof (int)); | |
457 | if (ret < 0) | |
458 | { | |
4525281a DL |
459 | zlog_warn("can't set sockopt IPV6_V6ONLY " "to socket %d", |
460 | sock); | |
ca051269 DL |
461 | return -1; |
462 | } | |
463 | return 0; | |
464 | } | |
465 | #endif /* IPV6_V6ONLY */ | |
ca051269 DL |
466 | return 0; |
467 | } | |
468 | ||
718e3744 | 469 | /* If same family and same prefix return 1. */ |
470 | int | |
3f9c7369 | 471 | sockunion_same (const union sockunion *su1, const union sockunion *su2) |
718e3744 | 472 | { |
473 | int ret = 0; | |
474 | ||
475 | if (su1->sa.sa_family != su2->sa.sa_family) | |
476 | return 0; | |
477 | ||
478 | switch (su1->sa.sa_family) | |
479 | { | |
480 | case AF_INET: | |
481 | ret = memcmp (&su1->sin.sin_addr, &su2->sin.sin_addr, | |
482 | sizeof (struct in_addr)); | |
483 | break; | |
718e3744 | 484 | case AF_INET6: |
485 | ret = memcmp (&su1->sin6.sin6_addr, &su2->sin6.sin6_addr, | |
486 | sizeof (struct in6_addr)); | |
f2345335 DS |
487 | if ((ret == 0) && IN6_IS_ADDR_LINKLOCAL(&su1->sin6.sin6_addr)) |
488 | { | |
489 | /* compare interface indices */ | |
490 | if (su1->sin6.sin6_scope_id && su2->sin6.sin6_scope_id) | |
491 | ret = (su1->sin6.sin6_scope_id == su2->sin6.sin6_scope_id) ? 0 : 1; | |
492 | } | |
718e3744 | 493 | break; |
718e3744 | 494 | } |
495 | if (ret == 0) | |
496 | return 1; | |
497 | else | |
498 | return 0; | |
499 | } | |
500 | ||
7cb5cdbb TT |
501 | unsigned int |
502 | sockunion_hash (const union sockunion *su) | |
503 | { | |
504 | switch (sockunion_family(su)) | |
505 | { | |
506 | case AF_INET: | |
507 | return jhash_1word(su->sin.sin_addr.s_addr, 0); | |
7cb5cdbb TT |
508 | case AF_INET6: |
509 | return jhash2(su->sin6.sin6_addr.s6_addr32, ZEBRA_NUM_OF(su->sin6.sin6_addr.s6_addr32), 0); | |
7cb5cdbb TT |
510 | } |
511 | return 0; | |
512 | } | |
513 | ||
95e0999c TT |
514 | size_t |
515 | family2addrsize(int family) | |
516 | { | |
517 | switch (family) | |
518 | { | |
519 | case AF_INET: | |
520 | return sizeof(struct in_addr); | |
95e0999c TT |
521 | case AF_INET6: |
522 | return sizeof(struct in6_addr); | |
95e0999c TT |
523 | } |
524 | return 0; | |
525 | } | |
526 | ||
527 | size_t | |
528 | sockunion_get_addrlen(const union sockunion *su) | |
529 | { | |
530 | return family2addrsize(sockunion_family(su)); | |
531 | } | |
532 | ||
533 | const u_char * | |
534 | sockunion_get_addr(const union sockunion *su) | |
535 | { | |
536 | switch (sockunion_family(su)) | |
537 | { | |
538 | case AF_INET: | |
539 | return (const u_char *) &su->sin.sin_addr.s_addr; | |
95e0999c TT |
540 | case AF_INET6: |
541 | return (const u_char *) &su->sin6.sin6_addr; | |
95e0999c TT |
542 | } |
543 | return NULL; | |
544 | } | |
545 | ||
546 | void | |
547 | sockunion_set(union sockunion *su, int family, const u_char *addr, size_t bytes) | |
548 | { | |
549 | if (family2addrsize(family) != bytes) | |
550 | return; | |
551 | ||
552 | sockunion_family(su) = family; | |
553 | switch (family) | |
554 | { | |
555 | case AF_INET: | |
556 | memcpy(&su->sin.sin_addr.s_addr, addr, bytes); | |
557 | break; | |
95e0999c TT |
558 | case AF_INET6: |
559 | memcpy(&su->sin6.sin6_addr, addr, bytes); | |
560 | break; | |
95e0999c TT |
561 | } |
562 | } | |
563 | ||
718e3744 | 564 | /* After TCP connection is established. Get local address and port. */ |
565 | union sockunion * | |
566 | sockunion_getsockname (int fd) | |
567 | { | |
568 | int ret; | |
22528299 | 569 | socklen_t len; |
718e3744 | 570 | union |
571 | { | |
572 | struct sockaddr sa; | |
573 | struct sockaddr_in sin; | |
718e3744 | 574 | struct sockaddr_in6 sin6; |
718e3744 | 575 | char tmp_buffer[128]; |
576 | } name; | |
577 | union sockunion *su; | |
578 | ||
579 | memset (&name, 0, sizeof name); | |
580 | len = sizeof name; | |
581 | ||
582 | ret = getsockname (fd, (struct sockaddr *)&name, &len); | |
583 | if (ret < 0) | |
584 | { | |
585 | zlog_warn ("Can't get local address and port by getsockname: %s", | |
6099b3b5 | 586 | safe_strerror (errno)); |
718e3744 | 587 | return NULL; |
588 | } | |
589 | ||
590 | if (name.sa.sa_family == AF_INET) | |
591 | { | |
2ba9a37a | 592 | su = XCALLOC (MTYPE_SOCKUNION, sizeof (union sockunion)); |
718e3744 | 593 | memcpy (su, &name, sizeof (struct sockaddr_in)); |
594 | return su; | |
595 | } | |
718e3744 | 596 | if (name.sa.sa_family == AF_INET6) |
597 | { | |
2ba9a37a | 598 | su = XCALLOC (MTYPE_SOCKUNION, sizeof (union sockunion)); |
718e3744 | 599 | memcpy (su, &name, sizeof (struct sockaddr_in6)); |
1a7dcf42 | 600 | sockunion_normalise_mapped (su); |
718e3744 | 601 | return su; |
602 | } | |
718e3744 | 603 | return NULL; |
604 | } | |
605 | ||
606 | /* After TCP connection is established. Get remote address and port. */ | |
607 | union sockunion * | |
608 | sockunion_getpeername (int fd) | |
609 | { | |
610 | int ret; | |
22528299 | 611 | socklen_t len; |
718e3744 | 612 | union |
613 | { | |
614 | struct sockaddr sa; | |
615 | struct sockaddr_in sin; | |
718e3744 | 616 | struct sockaddr_in6 sin6; |
718e3744 | 617 | char tmp_buffer[128]; |
618 | } name; | |
619 | union sockunion *su; | |
620 | ||
621 | memset (&name, 0, sizeof name); | |
622 | len = sizeof name; | |
623 | ret = getpeername (fd, (struct sockaddr *)&name, &len); | |
624 | if (ret < 0) | |
625 | { | |
4525281a | 626 | zlog_warn("Can't get remote address and port: %s", safe_strerror(errno)); |
718e3744 | 627 | return NULL; |
628 | } | |
629 | ||
630 | if (name.sa.sa_family == AF_INET) | |
631 | { | |
2ba9a37a | 632 | su = XCALLOC (MTYPE_SOCKUNION, sizeof (union sockunion)); |
718e3744 | 633 | memcpy (su, &name, sizeof (struct sockaddr_in)); |
634 | return su; | |
635 | } | |
718e3744 | 636 | if (name.sa.sa_family == AF_INET6) |
637 | { | |
2ba9a37a | 638 | su = XCALLOC (MTYPE_SOCKUNION, sizeof (union sockunion)); |
718e3744 | 639 | memcpy (su, &name, sizeof (struct sockaddr_in6)); |
1a7dcf42 | 640 | sockunion_normalise_mapped (su); |
718e3744 | 641 | return su; |
642 | } | |
718e3744 | 643 | return NULL; |
644 | } | |
645 | ||
646 | /* Print sockunion structure */ | |
8cc4198f | 647 | static void __attribute__ ((unused)) |
c0d8db4d | 648 | sockunion_print (const union sockunion *su) |
718e3744 | 649 | { |
650 | if (su == NULL) | |
651 | return; | |
652 | ||
653 | switch (su->sa.sa_family) | |
654 | { | |
655 | case AF_INET: | |
656 | printf ("%s\n", inet_ntoa (su->sin.sin_addr)); | |
657 | break; | |
718e3744 | 658 | case AF_INET6: |
659 | { | |
42d49865 | 660 | char buf [SU_ADDRSTRLEN]; |
718e3744 | 661 | |
662 | printf ("%s\n", inet_ntop (AF_INET6, &(su->sin6.sin6_addr), | |
663 | buf, sizeof (buf))); | |
664 | } | |
665 | break; | |
718e3744 | 666 | |
667 | #ifdef AF_LINK | |
668 | case AF_LINK: | |
669 | { | |
670 | struct sockaddr_dl *sdl; | |
671 | ||
672 | sdl = (struct sockaddr_dl *)&(su->sa); | |
673 | printf ("link#%d\n", sdl->sdl_index); | |
674 | } | |
675 | break; | |
676 | #endif /* AF_LINK */ | |
677 | default: | |
678 | printf ("af_unknown %d\n", su->sa.sa_family); | |
679 | break; | |
680 | } | |
681 | } | |
682 | ||
8cc4198f | 683 | static int |
c0d8db4d | 684 | in6addr_cmp (const struct in6_addr *addr1, const struct in6_addr *addr2) |
718e3744 | 685 | { |
8c328f11 | 686 | unsigned int i; |
35dece84 | 687 | const u_char *p1, *p2; |
718e3744 | 688 | |
35dece84 DS |
689 | p1 = (const u_char *)addr1; |
690 | p2 = (const u_char *)addr2; | |
718e3744 | 691 | |
692 | for (i = 0; i < sizeof (struct in6_addr); i++) | |
693 | { | |
694 | if (p1[i] > p2[i]) | |
695 | return 1; | |
696 | else if (p1[i] < p2[i]) | |
697 | return -1; | |
698 | } | |
699 | return 0; | |
700 | } | |
718e3744 | 701 | |
702 | int | |
c0d8db4d | 703 | sockunion_cmp (const union sockunion *su1, const union sockunion *su2) |
718e3744 | 704 | { |
705 | if (su1->sa.sa_family > su2->sa.sa_family) | |
706 | return 1; | |
707 | if (su1->sa.sa_family < su2->sa.sa_family) | |
708 | return -1; | |
709 | ||
710 | if (su1->sa.sa_family == AF_INET) | |
711 | { | |
12829328 | 712 | if (ntohl (sockunion2ip (su1)) == ntohl (sockunion2ip (su2))) |
718e3744 | 713 | return 0; |
12829328 | 714 | if (ntohl (sockunion2ip (su1)) > ntohl (sockunion2ip (su2))) |
718e3744 | 715 | return 1; |
716 | else | |
717 | return -1; | |
718 | } | |
718e3744 | 719 | if (su1->sa.sa_family == AF_INET6) |
720 | return in6addr_cmp (&su1->sin6.sin6_addr, &su2->sin6.sin6_addr); | |
718e3744 | 721 | return 0; |
722 | } | |
723 | ||
724 | /* Duplicate sockunion. */ | |
725 | union sockunion * | |
c0d8db4d | 726 | sockunion_dup (const union sockunion *su) |
718e3744 | 727 | { |
728 | union sockunion *dup = XCALLOC (MTYPE_SOCKUNION, sizeof (union sockunion)); | |
729 | memcpy (dup, su, sizeof (union sockunion)); | |
730 | return dup; | |
731 | } | |
732 | ||
733 | void | |
734 | sockunion_free (union sockunion *su) | |
735 | { | |
736 | XFREE (MTYPE_SOCKUNION, su); | |
737 | } | |
dd793e4a DW |
738 | |
739 | void | |
740 | sockunion_init (union sockunion *su) | |
741 | { | |
742 | memset(su, 0, sizeof(union sockunion)); | |
743 | } |