3 #ifdef HAVE_SYS_SOCKET_H
5 #include <osmocom/core/logging.h>
6 #include <osmocom/core/select.h>
7 #include <osmocom/core/socket.h>
9 #include <sys/socket.h>
10 #include <sys/types.h>
20 int osmo_sock_init(uint16_t family, uint16_t type, uint8_t proto,
21 const char *host, uint16_t port, int connect0_bind1)
23 struct addrinfo hints, *result, *rp;
27 sprintf(portbuf, "%u", port);
28 memset(&hints, 0, sizeof(struct addrinfo));
29 hints.ai_family = family;
30 hints.ai_socktype = type;
32 hints.ai_protocol = proto;
35 hints.ai_flags |= AI_PASSIVE;
37 rc = getaddrinfo(host, portbuf, &hints, &result);
39 perror("getaddrinfo returned NULL");
43 for (rp = result; rp != NULL; rp = rp->ai_next) {
44 sfd = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
47 if (connect0_bind1 == 0) {
48 if (connect(sfd, rp->ai_addr, rp->ai_addrlen) != -1)
51 if (bind(sfd, rp->ai_addr, rp->ai_addrlen) != -1)
59 perror("unable to connect/bind socket");
63 setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
65 /* Make sure to call 'listen' on a bound, connection-oriented sock */
66 if (connect0_bind1 == 1) {
77 int osmo_sock_init_ofd(struct osmo_fd *ofd, int family, int type, int proto,
78 const char *host, uint16_t port, int connect0_bind1)
82 sfd = osmo_sock_init(family, type, proto, host, port, connect0_bind1);
87 ofd->when = BSC_FD_READ;
89 rc = osmo_fd_register(ofd);
98 int osmo_sock_init_sa(struct sockaddr *ss, uint16_t type,
99 uint8_t proto, int connect0_bind1)
101 char host[NI_MAXHOST];
103 struct sockaddr_in *sin;
104 struct sockaddr_in6 *sin6;
107 /* determine port and host from ss */
108 switch (ss->sa_family) {
110 sin = (struct sockaddr_in *) ss;
111 sa_len = sizeof(struct sockaddr_in);
112 port = ntohs(sin->sin_port);
115 sin6 = (struct sockaddr_in6 *) ss;
116 sa_len = sizeof(struct sockaddr_in6);
117 port = ntohs(sin6->sin6_port);
123 s = getnameinfo(ss, sa_len, host, NI_MAXHOST,
124 NULL, 0, NI_NUMERICHOST);
126 perror("getnameinfo failed");
130 return osmo_sock_init(ss->sa_family, type, proto, host,
131 port, connect0_bind1);
134 static int sockaddr_equal(const struct sockaddr *a,
135 const struct sockaddr *b, unsigned int len)
137 struct sockaddr_in *sin_a, *sin_b;
138 struct sockaddr_in6 *sin6_a, *sin6_b;
140 if (a->sa_family != b->sa_family)
143 switch (a->sa_family) {
145 sin_a = (struct sockaddr_in *)a;
146 sin_b = (struct sockaddr_in *)b;
147 if (!memcmp(&sin_a->sin_addr, &sin_b->sin_addr,
148 sizeof(struct in_addr)))
152 sin6_a = (struct sockaddr_in6 *)a;
153 sin6_b = (struct sockaddr_in6 *)b;
154 if (!memcmp(&sin6_a->sin6_addr, &sin6_b->sin6_addr,
155 sizeof(struct in6_addr)))
162 /* determine if the given address is a local address */
163 int osmo_sockaddr_is_local(struct sockaddr *addr, socklen_t addrlen)
165 struct ifaddrs *ifaddr, *ifa;
167 if (getifaddrs(&ifaddr) == -1) {
168 perror("getifaddrs");
172 for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) {
175 if (sockaddr_equal(ifa->ifa_addr, addr, addrlen))
182 #endif /* HAVE_SYS_SOCKET_H */