]> git.proxmox.com Git - mirror_frr.git/blob - lib/sockunion.c
Merge pull request #128 from donaldsharp/readme
[mirror_frr.git] / lib / sockunion.c
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"
28 #include "log.h"
29 #include "jhash.h"
30
31 DEFINE_MTYPE_STATIC(LIB, SOCKUNION, "Socket union")
32
33 const char *
34 inet_sutop (const union sockunion *su, char *str)
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;
41 case AF_INET6:
42 inet_ntop (AF_INET6, &su->sin6.sin6_addr, str, INET6_ADDRSTRLEN);
43 break;
44 }
45 return str;
46 }
47
48 int
49 str2sockunion (const char *str, union sockunion *su)
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;
59 #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
60 su->sin.sin_len = sizeof(struct sockaddr_in);
61 #endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
62 return 0;
63 }
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 }
73 return -1;
74 }
75
76 const char *
77 sockunion2str (const union sockunion *su, char *buf, size_t len)
78 {
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);
86 case AF_INET6:
87 return inet_ntop (AF_INET6, &su->sin6.sin6_addr, buf, len);
88 }
89 snprintf (buf, len, "(af %d)", sockunion_family(su));
90 return buf;
91 }
92
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
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
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;
116 sin.sin_port = su->sin6.sin6_port;
117 memcpy (&sin.sin_addr, ((char *)&su->sin6.sin6_addr) + 12, 4);
118 memcpy (su, &sin, sizeof (struct sockaddr_in));
119 }
120 }
121
122 /* return sockunion structure : this function should be revised. */
123 static const char *
124 sockunion_log (const union sockunion *su, char *buf, size_t len)
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
141 /* Return socket of sockunion. */
142 int
143 sockunion_socket (const union sockunion *su)
144 {
145 int sock;
146
147 sock = socket (su->sa.sa_family, SOCK_STREAM, 0);
148 if (sock < 0)
149 {
150 char buf[SU_ADDRSTRLEN];
151 zlog (NULL, LOG_WARNING, "Can't make socket for %s : %s",
152 sockunion_log (su, buf, SU_ADDRSTRLEN), safe_strerror (errno));
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
169 sockunion_normalise_mapped (su);
170 return client_sock;
171 }
172
173 /* Return sizeof union sockunion. */
174 static int
175 sockunion_sizeof (const union sockunion *su)
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;
185 case AF_INET6:
186 ret = sizeof (struct sockaddr_in6);
187 break;
188 }
189 return ret;
190 }
191
192 /* sockunion_connect returns
193 -1 : error occured
194 0 : connect success
195 1 : connect is in progress */
196 enum connect_result
197 sockunion_connect (int fd, const union sockunion *peersu, unsigned short port,
198 ifindex_t ifindex)
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;
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 {
216 su.sin6.sin6_scope_id = ifindex;
217 SET_IN6_LINKLOCAL_IFINDEX (su.sin6.sin6_addr, ifindex);
218 }
219 #endif /* KAME */
220 break;
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 {
242 char str[SU_ADDRSTRLEN];
243 zlog_info ("can't connect to %s fd %d : %s",
244 sockunion_log (&su, str, sizeof str),
245 fd, safe_strerror (errno));
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)
267 zlog (NULL, LOG_WARNING, "can't make socket sockunion_stream_socket");
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);
284 #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
285 su->sin.sin_len = size;
286 #endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
287 if (su_addr == NULL)
288 sockunion2ip (su) = htonl (INADDR_ANY);
289 }
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 {
299 #ifdef LINUX_IPV6
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 }
306
307 ret = bind (sock, (struct sockaddr *)su, size);
308 if (ret < 0)
309 {
310 char buf[SU_ADDRSTRLEN];
311 zlog (NULL, LOG_WARNING, "can't bind socket for %s : %s",
312 sockunion_log (su, buf, SU_ADDRSTRLEN), safe_strerror (errno));
313 }
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 {
328 zlog (NULL, LOG_WARNING, "can't set sockopt SO_REUSEADDR to socket %d", sock);
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 {
345 zlog (NULL, LOG_WARNING, "can't set sockopt SO_REUSEPORT to socket %d", sock);
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 {
370 zlog (NULL, LOG_WARNING, "can't set sockopt IP_TTL %d to socket %d", ttl, sock);
371 return -1;
372 }
373 return 0;
374 }
375 #endif /* IP_TTL */
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 {
382 zlog (NULL, LOG_WARNING, "can't set sockopt IPV6_UNICAST_HOPS %d to socket %d",
383 ttl, sock);
384 return -1;
385 }
386 return 0;
387 }
388 return 0;
389 }
390
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;
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;
417 #endif
418 }
419
420 int
421 sockopt_minttl (int family, int sock, int minttl)
422 {
423 #ifdef IP_MINTTL
424 if (family == AF_INET)
425 {
426 int ret = setsockopt (sock, IPPROTO_IP, IP_MINTTL, &minttl, sizeof(minttl));
427 if (ret < 0)
428 zlog (NULL, LOG_WARNING,
429 "can't set sockopt IP_MINTTL to %d on socket %d: %s",
430 minttl, sock, safe_strerror (errno));
431 return ret;
432 }
433 #endif /* IP_MINTTL */
434 #ifdef IPV6_MINHOPCOUNT
435 if (family == AF_INET6)
436 {
437 int ret = setsockopt (sock, IPPROTO_IPV6, IPV6_MINHOPCOUNT, &minttl, sizeof(minttl));
438 if (ret < 0)
439 zlog (NULL, LOG_WARNING,
440 "can't set sockopt IPV6_MINHOPCOUNT to %d on socket %d: %s",
441 minttl, sock, safe_strerror (errno));
442 return ret;
443 }
444 #endif
445
446 errno = EOPNOTSUPP;
447 return -1;
448 }
449
450 int
451 sockopt_v6only (int family, int sock)
452 {
453 int ret, on = 1;
454
455 #ifdef IPV6_V6ONLY
456 if (family == AF_INET6)
457 {
458 ret = setsockopt (sock, IPPROTO_IPV6, IPV6_V6ONLY,
459 (void *) &on, sizeof (int));
460 if (ret < 0)
461 {
462 zlog (NULL, LOG_WARNING, "can't set sockopt IPV6_V6ONLY "
463 "to socket %d", sock);
464 return -1;
465 }
466 return 0;
467 }
468 #endif /* IPV6_V6ONLY */
469 return 0;
470 }
471
472 /* If same family and same prefix return 1. */
473 int
474 sockunion_same (const union sockunion *su1, const union sockunion *su2)
475 {
476 int ret = 0;
477
478 if (su1->sa.sa_family != su2->sa.sa_family)
479 return 0;
480
481 switch (su1->sa.sa_family)
482 {
483 case AF_INET:
484 ret = memcmp (&su1->sin.sin_addr, &su2->sin.sin_addr,
485 sizeof (struct in_addr));
486 break;
487 case AF_INET6:
488 ret = memcmp (&su1->sin6.sin6_addr, &su2->sin6.sin6_addr,
489 sizeof (struct in6_addr));
490 if ((ret == 0) && IN6_IS_ADDR_LINKLOCAL(&su1->sin6.sin6_addr))
491 {
492 /* compare interface indices */
493 if (su1->sin6.sin6_scope_id && su2->sin6.sin6_scope_id)
494 ret = (su1->sin6.sin6_scope_id == su2->sin6.sin6_scope_id) ? 0 : 1;
495 }
496 break;
497 }
498 if (ret == 0)
499 return 1;
500 else
501 return 0;
502 }
503
504 unsigned int
505 sockunion_hash (const union sockunion *su)
506 {
507 switch (sockunion_family(su))
508 {
509 case AF_INET:
510 return jhash_1word(su->sin.sin_addr.s_addr, 0);
511 case AF_INET6:
512 return jhash2(su->sin6.sin6_addr.s6_addr32, ZEBRA_NUM_OF(su->sin6.sin6_addr.s6_addr32), 0);
513 }
514 return 0;
515 }
516
517 size_t
518 family2addrsize(int family)
519 {
520 switch (family)
521 {
522 case AF_INET:
523 return sizeof(struct in_addr);
524 case AF_INET6:
525 return sizeof(struct in6_addr);
526 }
527 return 0;
528 }
529
530 size_t
531 sockunion_get_addrlen(const union sockunion *su)
532 {
533 return family2addrsize(sockunion_family(su));
534 }
535
536 const u_char *
537 sockunion_get_addr(const union sockunion *su)
538 {
539 switch (sockunion_family(su))
540 {
541 case AF_INET:
542 return (const u_char *) &su->sin.sin_addr.s_addr;
543 case AF_INET6:
544 return (const u_char *) &su->sin6.sin6_addr;
545 }
546 return NULL;
547 }
548
549 void
550 sockunion_set(union sockunion *su, int family, const u_char *addr, size_t bytes)
551 {
552 if (family2addrsize(family) != bytes)
553 return;
554
555 sockunion_family(su) = family;
556 switch (family)
557 {
558 case AF_INET:
559 memcpy(&su->sin.sin_addr.s_addr, addr, bytes);
560 break;
561 case AF_INET6:
562 memcpy(&su->sin6.sin6_addr, addr, bytes);
563 break;
564 }
565 }
566
567 /* After TCP connection is established. Get local address and port. */
568 union sockunion *
569 sockunion_getsockname (int fd)
570 {
571 int ret;
572 socklen_t len;
573 union
574 {
575 struct sockaddr sa;
576 struct sockaddr_in sin;
577 struct sockaddr_in6 sin6;
578 char tmp_buffer[128];
579 } name;
580 union sockunion *su;
581
582 memset (&name, 0, sizeof name);
583 len = sizeof name;
584
585 ret = getsockname (fd, (struct sockaddr *)&name, &len);
586 if (ret < 0)
587 {
588 zlog_warn ("Can't get local address and port by getsockname: %s",
589 safe_strerror (errno));
590 return NULL;
591 }
592
593 if (name.sa.sa_family == AF_INET)
594 {
595 su = XCALLOC (MTYPE_SOCKUNION, sizeof (union sockunion));
596 memcpy (su, &name, sizeof (struct sockaddr_in));
597 return su;
598 }
599 if (name.sa.sa_family == AF_INET6)
600 {
601 su = XCALLOC (MTYPE_SOCKUNION, sizeof (union sockunion));
602 memcpy (su, &name, sizeof (struct sockaddr_in6));
603 sockunion_normalise_mapped (su);
604 return su;
605 }
606 return NULL;
607 }
608
609 /* After TCP connection is established. Get remote address and port. */
610 union sockunion *
611 sockunion_getpeername (int fd)
612 {
613 int ret;
614 socklen_t len;
615 union
616 {
617 struct sockaddr sa;
618 struct sockaddr_in sin;
619 struct sockaddr_in6 sin6;
620 char tmp_buffer[128];
621 } name;
622 union sockunion *su;
623
624 memset (&name, 0, sizeof name);
625 len = sizeof name;
626 ret = getpeername (fd, (struct sockaddr *)&name, &len);
627 if (ret < 0)
628 {
629 zlog (NULL, LOG_WARNING, "Can't get remote address and port: %s",
630 safe_strerror (errno));
631 return NULL;
632 }
633
634 if (name.sa.sa_family == AF_INET)
635 {
636 su = XCALLOC (MTYPE_SOCKUNION, sizeof (union sockunion));
637 memcpy (su, &name, sizeof (struct sockaddr_in));
638 return su;
639 }
640 if (name.sa.sa_family == AF_INET6)
641 {
642 su = XCALLOC (MTYPE_SOCKUNION, sizeof (union sockunion));
643 memcpy (su, &name, sizeof (struct sockaddr_in6));
644 sockunion_normalise_mapped (su);
645 return su;
646 }
647 return NULL;
648 }
649
650 /* Print sockunion structure */
651 static void __attribute__ ((unused))
652 sockunion_print (const union sockunion *su)
653 {
654 if (su == NULL)
655 return;
656
657 switch (su->sa.sa_family)
658 {
659 case AF_INET:
660 printf ("%s\n", inet_ntoa (su->sin.sin_addr));
661 break;
662 case AF_INET6:
663 {
664 char buf [SU_ADDRSTRLEN];
665
666 printf ("%s\n", inet_ntop (AF_INET6, &(su->sin6.sin6_addr),
667 buf, sizeof (buf)));
668 }
669 break;
670
671 #ifdef AF_LINK
672 case AF_LINK:
673 {
674 struct sockaddr_dl *sdl;
675
676 sdl = (struct sockaddr_dl *)&(su->sa);
677 printf ("link#%d\n", sdl->sdl_index);
678 }
679 break;
680 #endif /* AF_LINK */
681 default:
682 printf ("af_unknown %d\n", su->sa.sa_family);
683 break;
684 }
685 }
686
687 static int
688 in6addr_cmp (const struct in6_addr *addr1, const struct in6_addr *addr2)
689 {
690 unsigned int i;
691 const u_char *p1, *p2;
692
693 p1 = (const u_char *)addr1;
694 p2 = (const u_char *)addr2;
695
696 for (i = 0; i < sizeof (struct in6_addr); i++)
697 {
698 if (p1[i] > p2[i])
699 return 1;
700 else if (p1[i] < p2[i])
701 return -1;
702 }
703 return 0;
704 }
705
706 int
707 sockunion_cmp (const union sockunion *su1, const union sockunion *su2)
708 {
709 if (su1->sa.sa_family > su2->sa.sa_family)
710 return 1;
711 if (su1->sa.sa_family < su2->sa.sa_family)
712 return -1;
713
714 if (su1->sa.sa_family == AF_INET)
715 {
716 if (ntohl (sockunion2ip (su1)) == ntohl (sockunion2ip (su2)))
717 return 0;
718 if (ntohl (sockunion2ip (su1)) > ntohl (sockunion2ip (su2)))
719 return 1;
720 else
721 return -1;
722 }
723 if (su1->sa.sa_family == AF_INET6)
724 return in6addr_cmp (&su1->sin6.sin6_addr, &su2->sin6.sin6_addr);
725 return 0;
726 }
727
728 /* Duplicate sockunion. */
729 union sockunion *
730 sockunion_dup (const union sockunion *su)
731 {
732 union sockunion *dup = XCALLOC (MTYPE_SOCKUNION, sizeof (union sockunion));
733 memcpy (dup, su, sizeof (union sockunion));
734 return dup;
735 }
736
737 void
738 sockunion_free (union sockunion *su)
739 {
740 XFREE (MTYPE_SOCKUNION, su);
741 }
742
743 void
744 sockunion_init (union sockunion *su)
745 {
746 memset(su, 0, sizeof(union sockunion));
747 }