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");
62 setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
64 /* Make sure to call 'listen' on a bound, connection-oriented sock */
65 if (connect0_bind1 == 1) {
76 int osmo_sock_init_ofd(struct osmo_fd *ofd, int family, int type, int proto,
77 const char *host, uint16_t port, int connect0_bind1)
81 sfd = osmo_sock_init(family, type, proto, host, port, connect0_bind1);
86 ofd->when = BSC_FD_READ;
88 rc = osmo_fd_register(ofd);
97 int osmo_sock_init_sa(struct sockaddr *ss, uint16_t type,
98 uint8_t proto, int connect0_bind1)
100 char host[NI_MAXHOST];
102 struct sockaddr_in *sin;
103 struct sockaddr_in6 *sin6;
106 /* determine port and host from ss */
107 switch (ss->sa_family) {
109 sin = (struct sockaddr_in *) ss;
110 sa_len = sizeof(struct sockaddr_in);
111 port = ntohs(sin->sin_port);
114 sin6 = (struct sockaddr_in6 *) ss;
115 sa_len = sizeof(struct sockaddr_in6);
116 port = ntohs(sin6->sin6_port);
122 s = getnameinfo(ss, sa_len, host, NI_MAXHOST,
123 NULL, 0, NI_NUMERICHOST);
125 perror("getnameinfo failed");
129 return osmo_sock_init(ss->sa_family, type, proto, host,
130 port, connect0_bind1);
133 static int sockaddr_equal(const struct sockaddr *a,
134 const struct sockaddr *b, unsigned int len)
136 struct sockaddr_in *sin_a, *sin_b;
137 struct sockaddr_in6 *sin6_a, *sin6_b;
139 if (a->sa_family != b->sa_family)
142 switch (a->sa_family) {
144 sin_a = (struct sockaddr_in *)a;
145 sin_b = (struct sockaddr_in *)b;
146 if (!memcmp(&sin_a->sin_addr, &sin_b->sin_addr,
147 sizeof(struct in_addr)))
151 sin6_a = (struct sockaddr_in6 *)a;
152 sin6_b = (struct sockaddr_in6 *)b;
153 if (!memcmp(&sin6_a->sin6_addr, &sin6_b->sin6_addr,
154 sizeof(struct in6_addr)))
161 /* determine if the given address is a local address */
162 int osmo_sockaddr_is_local(struct sockaddr *addr, socklen_t addrlen)
164 struct ifaddrs *ifaddr, *ifa;
166 if (getifaddrs(&ifaddr) == -1) {
167 perror("getifaddrs");
171 for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) {
174 if (sockaddr_equal(ifa->ifa_addr, addr, addrlen))
181 #endif /* HAVE_SYS_SOCKET_H */