3 #ifdef HAVE_SYS_SOCKET_H
5 #include <osmocom/core/logging.h>
6 #include <osmocom/core/select.h>
7 #include <osmocom/core/socket.h>
10 #include <sys/socket.h>
11 #include <sys/types.h>
12 #include <netinet/in.h>
22 int osmo_sock_init(uint16_t family, uint16_t type, uint8_t proto,
23 const char *host, uint16_t port, int connect0_bind1)
25 struct addrinfo hints, *result, *rp;
29 sprintf(portbuf, "%u", port);
30 memset(&hints, 0, sizeof(struct addrinfo));
31 hints.ai_family = family;
32 hints.ai_socktype = type;
34 hints.ai_protocol = proto;
36 rc = getaddrinfo(host, portbuf, &hints, &result);
38 perror("getaddrinfo returned NULL");
42 for (rp = result; rp != NULL; rp = rp->ai_next) {
43 sfd = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
46 if (connect0_bind1 == 0) {
47 if (connect(sfd, rp->ai_addr, rp->ai_addrlen) != -1)
50 if (bind(sfd, rp->ai_addr, rp->ai_addrlen) != -1)
58 perror("unable to connect/bind socket");
64 int osmo_sock_init_sa(struct sockaddr *ss, uint16_t type,
65 uint8_t proto, int connect0_bind1)
67 char host[NI_MAXHOST];
69 struct sockaddr_in *sin;
70 struct sockaddr_in6 *sin6;
73 /* determine port and host from ss */
74 switch (ss->sa_family) {
76 sin = (struct sockaddr_in *) ss;
77 sa_len = sizeof(struct sockaddr_in);
78 port = ntohs(sin->sin_port);
81 sin6 = (struct sockaddr_in6 *) ss;
82 sa_len = sizeof(struct sockaddr_in6);
83 port = ntohs(sin6->sin6_port);
88 fprintf(stderr, "==> PORT = %u\n", port);
90 s = getnameinfo(ss, sa_len, host, NI_MAXHOST,
91 NULL, 0, NI_NUMERICHOST);
93 perror("getnameinfo failed");
97 return osmo_sock_init(ss->sa_family, type, proto, host,
98 port, connect0_bind1);
101 static int sockaddr_equal(const struct sockaddr *a,
102 const struct sockaddr *b, unsigned int len)
104 struct sockaddr_in *sin_a, *sin_b;
105 struct sockaddr_in6 *sin6_a, *sin6_b;
107 if (a->sa_family != b->sa_family)
110 switch (a->sa_family) {
112 sin_a = (struct sockaddr_in *)a;
113 sin_b = (struct sockaddr_in *)b;
114 if (!memcmp(&sin_a->sin_addr, &sin_b->sin_addr,
115 sizeof(struct in_addr)))
119 sin6_a = (struct sockaddr_in6 *)a;
120 sin6_b = (struct sockaddr_in6 *)b;
121 if (!memcmp(&sin6_a->sin6_addr, &sin6_b->sin6_addr,
122 sizeof(struct in6_addr)))
129 /* determine if the given address is a local address */
130 int osmo_sockaddr_is_local(struct sockaddr *addr, socklen_t addrlen)
132 struct ifaddrs *ifaddr, *ifa;
134 if (getifaddrs(&ifaddr) == -1) {
135 perror("getifaddrs");
139 for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) {
140 if (sockaddr_equal(ifa->ifa_addr, addr, addrlen))
147 #endif /* HAVE_SYS_SOCKET_H */