#include "../config.h"
+#ifdef HAVE_SYS_SOCKET_H
+
#include <osmocom/core/logging.h>
#include <osmocom/core/select.h>
#include <osmocom/core/socket.h>
-#include <arpa/inet.h>
#include <sys/socket.h>
#include <sys/types.h>
-#include <netinet/in.h>
#include <stdio.h>
#include <unistd.h>
const char *host, uint16_t port, int connect0_bind1)
{
struct addrinfo hints, *result, *rp;
- int sfd, rc;
+ int sfd, rc, on = 1;
char portbuf[16];
sprintf(portbuf, "%u", port);
hints.ai_flags = 0;
hints.ai_protocol = proto;
+ if (connect0_bind1)
+ hints.ai_flags |= AI_PASSIVE;
+
rc = getaddrinfo(host, portbuf, &hints, &result);
if (rc != 0) {
perror("getaddrinfo returned NULL");
perror("unable to connect/bind socket");
return -ENODEV;
}
+
+ setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
+
+ /* Make sure to call 'listen' on a bound, connection-oriented sock */
+ if (connect0_bind1 == 1) {
+ switch (type) {
+ case SOCK_STREAM:
+ case SOCK_SEQPACKET:
+ listen(sfd, 10);
+ break;
+ }
+ }
+ return sfd;
+}
+
+int osmo_sock_init_ofd(struct osmo_fd *ofd, int family, int type, int proto,
+ const char *host, uint16_t port, int connect0_bind1)
+{
+ int sfd, rc;
+
+ sfd = osmo_sock_init(family, type, proto, host, port, connect0_bind1);
+ if (sfd < 0)
+ return sfd;
+
+ ofd->fd = sfd;
+ ofd->when = BSC_FD_READ;
+
+ rc = osmo_fd_register(ofd);
+ if (rc < 0) {
+ close(sfd);
+ return rc;
+ }
+
return sfd;
}
default:
return -EINVAL;
}
- fprintf(stderr, "==> PORT = %u\n", port);
s = getnameinfo(ss, sa_len, host, NI_MAXHOST,
NULL, 0, NI_NUMERICHOST);
}
static int sockaddr_equal(const struct sockaddr *a,
- const struct sockaddr *b, socklen_t len)
+ const struct sockaddr *b, unsigned int len)
{
struct sockaddr_in *sin_a, *sin_b;
struct sockaddr_in6 *sin6_a, *sin6_b;
}
for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) {
+ if (!ifa->ifa_addr)
+ continue;
if (sockaddr_equal(ifa->ifa_addr, addr, addrlen))
return 1;
}
return 0;
}
+
+#endif /* HAVE_SYS_SOCKET_H */