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