/work/open62541_15/src_generated/mdnsd/inet.c
Line | Count | Source |
1 | | #include "open62541/config.h" |
2 | | /* IPv4/IPv6 address helpers, cf. the inet.[ch] wrapper in SMCRoute */ |
3 | | #include <arpa/inet.h> |
4 | | #include <string.h> |
5 | | |
6 | | #include "inet.h" |
7 | | |
8 | | sa_family_t inet_family(const inet_addr_t *ss) |
9 | 207 | { |
10 | 207 | return ss->ss_family; |
11 | 207 | } |
12 | | |
13 | | socklen_t inet_len(const inet_addr_t *ss) |
14 | 0 | { |
15 | 0 | if (ss->ss_family == AF_INET6) |
16 | 0 | return sizeof(struct sockaddr_in6); |
17 | 0 | return sizeof(struct sockaddr_in); |
18 | 0 | } |
19 | | |
20 | | in_port_t inet_port(const inet_addr_t *ss) |
21 | 0 | { |
22 | | #ifdef ENABLE_IPV6 |
23 | | if (ss->ss_family == AF_INET6) |
24 | | return ntohs(((const struct sockaddr_in6 *)ss)->sin6_port); |
25 | | #endif |
26 | 0 | return ntohs(((const struct sockaddr_in *)ss)->sin_port); |
27 | 0 | } |
28 | | |
29 | | void inet_set_port(inet_addr_t *ss, in_port_t port) |
30 | 0 | { |
31 | | #ifdef ENABLE_IPV6 |
32 | | if (ss->ss_family == AF_INET6) { |
33 | | ((struct sockaddr_in6 *)ss)->sin6_port = htons(port); |
34 | | return; |
35 | | } |
36 | | #endif |
37 | 0 | ((struct sockaddr_in *)ss)->sin_port = htons(port); |
38 | 0 | } |
39 | | |
40 | | void inet_anyaddr(sa_family_t family, in_port_t port, inet_addr_t *ss) |
41 | 389 | { |
42 | 389 | memset(ss, 0, sizeof(*ss)); |
43 | 389 | ss->ss_family = family; |
44 | | |
45 | | #ifdef ENABLE_IPV6 |
46 | | if (family == AF_INET6) { |
47 | | struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)ss; |
48 | | |
49 | | sin6->sin6_addr = in6addr_any; |
50 | | sin6->sin6_port = htons(port); |
51 | | return; |
52 | | } |
53 | | #endif |
54 | 389 | ((struct sockaddr_in *)ss)->sin_addr.s_addr = htonl(INADDR_ANY); |
55 | 389 | ((struct sockaddr_in *)ss)->sin_port = htons(port); |
56 | 389 | } |
57 | | |
58 | | const char *inet_ntop2(const inet_addr_t *ss, char *buf, size_t len) |
59 | 0 | { |
60 | 0 | buf[0] = 0; |
61 | |
|
62 | | #ifdef ENABLE_IPV6 |
63 | | if (ss->ss_family == AF_INET6) |
64 | | return inet_ntop(AF_INET6, &((const struct sockaddr_in6 *)ss)->sin6_addr, buf, len); |
65 | | #endif |
66 | | return inet_ntop(AF_INET, &((const struct sockaddr_in *)ss)->sin_addr, buf, len); |
67 | 0 | } |