summaryrefslogtreecommitdiffstats
path: root/src/shared/libosmocore/src/socket.c
diff options
context:
space:
mode:
authorHarald Welte <laforge@gnumonks.org>2011-05-23 22:17:26 +0200
committerHarald Welte <laforge@gnumonks.org>2011-05-23 22:17:26 +0200
commitd76345aefa34c576dd142eb64cb0f765cda22933 (patch)
tree6a08f29e160aea04859d9953dd4db2fc7696271f /src/shared/libosmocore/src/socket.c
parent308f9e506e6ae2f30a4b49895f80aa26605e50c0 (diff)
parent3036612d59a5c8d97b2086a5e7817613f45948ef (diff)
Merge commit '3036612d59a5c8d97b2086a5e7817613f45948ef'
Diffstat (limited to 'src/shared/libosmocore/src/socket.c')
-rw-r--r--src/shared/libosmocore/src/socket.c36
1 files changed, 34 insertions, 2 deletions
diff --git a/src/shared/libosmocore/src/socket.c b/src/shared/libosmocore/src/socket.c
index e053c24d..66907c8c 100644
--- a/src/shared/libosmocore/src/socket.c
+++ b/src/shared/libosmocore/src/socket.c
@@ -23,7 +23,7 @@ int osmo_sock_init(uint16_t family, uint16_t type, uint8_t proto,
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);
@@ -58,6 +58,39 @@ int osmo_sock_init(uint16_t family, uint16_t type, uint8_t proto,
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;
}
@@ -85,7 +118,6 @@ int osmo_sock_init_sa(struct sockaddr *ss, uint16_t type,
default:
return -EINVAL;
}
- fprintf(stderr, "==> PORT = %u\n", port);
s = getnameinfo(ss, sa_len, host, NI_MAXHOST,
NULL, 0, NI_NUMERICHOST);