blob: 66907c8cbcb9030fce589cc88a3bbbac66659fd4 [file] [log] [blame]
Harald Welte33cb71a2011-05-21 18:54:32 +02001#include "../config.h"
2
Harald Weltee4764422011-05-22 12:25:57 +02003#ifdef HAVE_SYS_SOCKET_H
4
Harald Welte33cb71a2011-05-21 18:54:32 +02005#include <osmocom/core/logging.h>
6#include <osmocom/core/select.h>
7#include <osmocom/core/socket.h>
8
9#include <arpa/inet.h>
10#include <sys/socket.h>
11#include <sys/types.h>
12#include <netinet/in.h>
13
14#include <stdio.h>
15#include <unistd.h>
16#include <stdint.h>
17#include <string.h>
18#include <errno.h>
19#include <netdb.h>
20#include <ifaddrs.h>
21
22int osmo_sock_init(uint16_t family, uint16_t type, uint8_t proto,
23 const char *host, uint16_t port, int connect0_bind1)
24{
25 struct addrinfo hints, *result, *rp;
Harald Welte68b15742011-05-22 21:47:29 +020026 int sfd, rc, on = 1;
Harald Welte33cb71a2011-05-21 18:54:32 +020027 char portbuf[16];
28
29 sprintf(portbuf, "%u", port);
30 memset(&hints, 0, sizeof(struct addrinfo));
31 hints.ai_family = family;
32 hints.ai_socktype = type;
33 hints.ai_flags = 0;
34 hints.ai_protocol = proto;
35
36 rc = getaddrinfo(host, portbuf, &hints, &result);
37 if (rc != 0) {
38 perror("getaddrinfo returned NULL");
39 return -EINVAL;
40 }
41
42 for (rp = result; rp != NULL; rp = rp->ai_next) {
43 sfd = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
44 if (sfd == -1)
45 continue;
46 if (connect0_bind1 == 0) {
47 if (connect(sfd, rp->ai_addr, rp->ai_addrlen) != -1)
48 break;
49 } else {
50 if (bind(sfd, rp->ai_addr, rp->ai_addrlen) != -1)
51 break;
52 }
53 close(sfd);
54 }
55 freeaddrinfo(result);
56
57 if (rp == NULL) {
58 perror("unable to connect/bind socket");
59 return -ENODEV;
60 }
Harald Welte68b15742011-05-22 21:47:29 +020061
62 setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
63
64 /* Make sure to call 'listen' on a bound, connection-oriented sock */
65 if (connect0_bind1 == 1) {
66 switch (type) {
67 case SOCK_STREAM:
68 case SOCK_SEQPACKET:
69 listen(sfd, 10);
70 break;
71 }
72 }
73 return sfd;
74}
75
76int osmo_sock_init_ofd(struct osmo_fd *ofd, int family, int type, int proto,
77 const char *host, uint16_t port, int connect0_bind1)
78{
79 int sfd, rc;
80
81 sfd = osmo_sock_init(family, type, proto, host, port, connect0_bind1);
82 if (sfd < 0)
83 return sfd;
84
85 ofd->fd = sfd;
86 ofd->when = BSC_FD_READ;
87
88 rc = osmo_fd_register(ofd);
89 if (rc < 0) {
90 close(sfd);
91 return rc;
92 }
93
Harald Welte33cb71a2011-05-21 18:54:32 +020094 return sfd;
95}
96
97int osmo_sock_init_sa(struct sockaddr *ss, uint16_t type,
98 uint8_t proto, int connect0_bind1)
99{
100 char host[NI_MAXHOST];
101 uint16_t port;
102 struct sockaddr_in *sin;
103 struct sockaddr_in6 *sin6;
104 int s, sa_len;
105
106 /* determine port and host from ss */
107 switch (ss->sa_family) {
108 case AF_INET:
109 sin = (struct sockaddr_in *) ss;
110 sa_len = sizeof(struct sockaddr_in);
111 port = ntohs(sin->sin_port);
112 break;
113 case AF_INET6:
114 sin6 = (struct sockaddr_in6 *) ss;
115 sa_len = sizeof(struct sockaddr_in6);
116 port = ntohs(sin6->sin6_port);
117 break;
118 default:
119 return -EINVAL;
120 }
Harald Welte33cb71a2011-05-21 18:54:32 +0200121
122 s = getnameinfo(ss, sa_len, host, NI_MAXHOST,
123 NULL, 0, NI_NUMERICHOST);
124 if (s != 0) {
125 perror("getnameinfo failed");
126 return s;
127 }
128
129 return osmo_sock_init(ss->sa_family, type, proto, host,
130 port, connect0_bind1);
131}
132
133static int sockaddr_equal(const struct sockaddr *a,
Harald Weltee4764422011-05-22 12:25:57 +0200134 const struct sockaddr *b, unsigned int len)
Harald Welte33cb71a2011-05-21 18:54:32 +0200135{
136 struct sockaddr_in *sin_a, *sin_b;
137 struct sockaddr_in6 *sin6_a, *sin6_b;
138
139 if (a->sa_family != b->sa_family)
140 return 0;
141
142 switch (a->sa_family) {
143 case AF_INET:
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)))
148 return 1;
149 break;
150 case AF_INET6:
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)))
155 return 1;
156 break;
157 }
158 return 0;
159}
160
161/* determine if the given address is a local address */
162int osmo_sockaddr_is_local(struct sockaddr *addr, socklen_t addrlen)
163{
164 struct ifaddrs *ifaddr, *ifa;
165
166 if (getifaddrs(&ifaddr) == -1) {
167 perror("getifaddrs");
168 return -EIO;
169 }
170
171 for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) {
172 if (sockaddr_equal(ifa->ifa_addr, addr, addrlen))
173 return 1;
174 }
175
176 return 0;
177}
Harald Weltee4764422011-05-22 12:25:57 +0200178
179#endif /* HAVE_SYS_SOCKET_H */