capt_get_packet(): check for key press only every 20ms
[iptraf-ng.git] / src / sockaddr.c
blobf5de68cb4e61fb4f32d6e5c828ef84365f490288
1 /* For terms of usage/redistribution/modification see the LICENSE file */
2 /* For authors and contributors see the AUTHORS file */
4 #include "iptraf-ng-compat.h"
6 void sockaddr_make_ipv4(struct sockaddr_storage *sockaddr,
7 u_int32_t addr)
9 if (!sockaddr)
10 die("%s(): sockaddr == NULL", __FUNCTION__);
12 memset(sockaddr, 0, sizeof(*sockaddr));
13 struct sockaddr_in *sockaddr_in = (struct sockaddr_in *)sockaddr;
14 sockaddr_in->sin_family = AF_INET;
15 sockaddr_in->sin_port = 0;
16 sockaddr_in->sin_addr.s_addr = addr;
19 void sockaddr_make_ipv6(struct sockaddr_storage *sockaddr,
20 struct in6_addr *addr)
22 if (!sockaddr)
23 die("%s(): sockaddr == NULL", __FUNCTION__);
24 if (!addr)
25 die("%s(): addr == NULL", __FUNCTION__);
27 memset(sockaddr, 0, sizeof(*sockaddr));
28 struct sockaddr_in6 *sockaddr_in6 = (struct sockaddr_in6 *)sockaddr;
29 sockaddr_in6->sin6_family = AF_INET6;
30 sockaddr_in6->sin6_port = 0;
31 sockaddr_in6->sin6_addr = *addr;
32 sockaddr_in6->sin6_flowinfo = 0;
33 sockaddr_in6->sin6_scope_id = 0;
36 in_port_t sockaddr_get_port(struct sockaddr_storage *sockaddr)
38 if (!sockaddr)
39 die("%s(): sockaddr == NULL", __FUNCTION__);
41 switch (sockaddr->ss_family) {
42 case AF_INET:
43 return ((struct sockaddr_in *)sockaddr)->sin_port;
44 case AF_INET6:
45 return ((struct sockaddr_in6 *)sockaddr)->sin6_port;
46 default:
47 die("%s(): Unknown address family", __FUNCTION__);
51 void sockaddr_set_port(struct sockaddr_storage *sockaddr, in_port_t port)
53 if (!sockaddr)
54 die("%s(): sockaddr == NULL", __FUNCTION__);
56 switch (sockaddr->ss_family) {
57 case AF_INET:
58 ((struct sockaddr_in *)sockaddr)->sin_port = port;
59 break;
60 case AF_INET6:
61 ((struct sockaddr_in6 *)sockaddr)->sin6_port = port;
62 break;
63 default:
64 die("%s(): Unknown address family", __FUNCTION__);
68 int sockaddr_is_equal(struct sockaddr_storage *addr1,
69 struct sockaddr_storage *addr2)
71 if (!addr1)
72 die("%s(): addr1 == NULL", __FUNCTION__);
73 if (!addr2)
74 die("%s(): addr2 == NULL", __FUNCTION__);
76 if (addr1->ss_family != addr2->ss_family)
77 return 0;
79 switch (addr1->ss_family) {
80 case AF_INET: {
81 struct sockaddr_in *sa1 = (struct sockaddr_in *)addr1;
82 struct sockaddr_in *sa2 = (struct sockaddr_in *)addr2;
84 if ((sa1->sin_addr.s_addr == sa2->sin_addr.s_addr)
85 && (sa1->sin_port == sa2->sin_port))
86 return 1;
87 else
88 return 0;
90 case AF_INET6: {
91 struct sockaddr_in6 *sa1 = (struct sockaddr_in6 *)addr1;
92 struct sockaddr_in6 *sa2 = (struct sockaddr_in6 *)addr2;
94 if ((sa1->sin6_port == sa2->sin6_port)
95 && (sa1->sin6_flowinfo == sa2->sin6_flowinfo)
96 && (sa1->sin6_scope_id == sa2->sin6_scope_id)
97 && (memcmp(&sa1->sin6_addr, &sa2->sin6_addr, sizeof(sa1->sin6_addr)) == 0))
98 return 1;
99 else
100 return 0;
102 default:
103 die("%s(): Unknown address family", __FUNCTION__);
107 void sockaddr_ntop(struct sockaddr_storage *addr, char *buf, size_t buflen)
109 if(!addr)
110 die("%s(): addr == NULL", __FUNCTION__);
112 const char *ret;
113 size_t minlen;
115 memset(buf, 0, buflen);
116 switch (addr->ss_family) {
117 case AF_INET:
118 minlen = INET_ADDRSTRLEN;
119 ret = inet_ntop(AF_INET, &((struct sockaddr_in *)addr)->sin_addr, buf, buflen - 1);
120 break;
121 case AF_INET6:
122 minlen = INET6_ADDRSTRLEN;
123 ret = inet_ntop(AF_INET6, &((struct sockaddr_in6 *)addr)->sin6_addr, buf, buflen - 1);
124 break;
125 default:
126 die("%s(): Unknown address family", __FUNCTION__);
128 if (ret == NULL) {
129 switch (errno) {
130 case ENOSPC:
131 die("%s(): buffer too small (must be at least %zu bytes)", __FUNCTION__, minlen);
132 case EAFNOSUPPORT:
133 die("%s(): Unknown address family", __FUNCTION__);
138 struct hostent *sockaddr_gethostbyaddr(struct sockaddr_storage *addr)
140 if(!addr)
141 die("%s(): addr == NULL", __FUNCTION__);
143 switch (addr->ss_family) {
144 case AF_INET:
145 return gethostbyaddr(&((struct sockaddr_in *)addr)->sin_addr, sizeof(struct in_addr), AF_INET);
146 case AF_INET6:
147 return gethostbyaddr(&((struct sockaddr_in6 *)addr)->sin6_addr, sizeof(struct in6_addr), AF_INET6);
148 default:
149 die("%s(): Unknown address family", __FUNCTION__);
153 void sockaddr_copy(struct sockaddr_storage *dest, struct sockaddr_storage *src)
155 if (!src)
156 die("%s(): src == NULL", __FUNCTION__);
157 if (!dest)
158 die("%s(): dest == NULL", __FUNCTION__);
160 memcpy(dest, src, sizeof(struct sockaddr_storage));