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
,
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
)
23 die("%s(): sockaddr == NULL", __FUNCTION__
);
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
)
39 die("%s(): sockaddr == NULL", __FUNCTION__
);
41 switch (sockaddr
->ss_family
) {
43 return ((struct sockaddr_in
*)sockaddr
)->sin_port
;
45 return ((struct sockaddr_in6
*)sockaddr
)->sin6_port
;
47 die("%s(): Unknown address family", __FUNCTION__
);
51 void sockaddr_set_port(struct sockaddr_storage
*sockaddr
, in_port_t port
)
54 die("%s(): sockaddr == NULL", __FUNCTION__
);
56 switch (sockaddr
->ss_family
) {
58 ((struct sockaddr_in
*)sockaddr
)->sin_port
= port
;
61 ((struct sockaddr_in6
*)sockaddr
)->sin6_port
= port
;
64 die("%s(): Unknown address family", __FUNCTION__
);
68 int sockaddr_is_equal(struct sockaddr_storage
*addr1
,
69 struct sockaddr_storage
*addr2
)
72 die("%s(): addr1 == NULL", __FUNCTION__
);
74 die("%s(): addr2 == NULL", __FUNCTION__
);
76 if (addr1
->ss_family
!= addr2
->ss_family
)
79 switch (addr1
->ss_family
) {
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
))
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))
103 die("%s(): Unknown address family", __FUNCTION__
);
107 void sockaddr_ntop(struct sockaddr_storage
*addr
, char *buf
, size_t buflen
)
110 die("%s(): addr == NULL", __FUNCTION__
);
115 memset(buf
, 0, buflen
);
116 switch (addr
->ss_family
) {
118 minlen
= INET_ADDRSTRLEN
;
119 ret
= inet_ntop(AF_INET
, &((struct sockaddr_in
*)addr
)->sin_addr
, buf
, buflen
- 1);
122 minlen
= INET6_ADDRSTRLEN
;
123 ret
= inet_ntop(AF_INET6
, &((struct sockaddr_in6
*)addr
)->sin6_addr
, buf
, buflen
- 1);
126 die("%s(): Unknown address family", __FUNCTION__
);
131 die("%s(): buffer too small (must be at least %zu bytes)", __FUNCTION__
, minlen
);
133 die("%s(): Unknown address family", __FUNCTION__
);
138 struct hostent
*sockaddr_gethostbyaddr(struct sockaddr_storage
*addr
)
141 die("%s(): addr == NULL", __FUNCTION__
);
143 switch (addr
->ss_family
) {
145 return gethostbyaddr(&((struct sockaddr_in
*)addr
)->sin_addr
, sizeof(struct in_addr
), AF_INET
);
147 return gethostbyaddr(&((struct sockaddr_in6
*)addr
)->sin6_addr
, sizeof(struct in6_addr
), AF_INET6
);
149 die("%s(): Unknown address family", __FUNCTION__
);
153 void sockaddr_copy(struct sockaddr_storage
*dest
, struct sockaddr_storage
*src
)
156 die("%s(): src == NULL", __FUNCTION__
);
158 die("%s(): dest == NULL", __FUNCTION__
);
160 memcpy(dest
, src
, sizeof(struct sockaddr_storage
));