aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorlns <matzeton@googlemail.com>2018-04-11 14:28:18 +0200
committerlns <matzeton@googlemail.com>2018-04-11 14:28:18 +0200
commitb1fd0299f0dfb69ab11e50a2d1b3c7714c2bb1fa (patch)
treee91aeaeac6ce1d9b485decf8b83766ecd222dc81 /src
parentebabaa69c0a3ba992895c7a66729e81e0923d5f1 (diff)
POTD skeleton.
Diffstat (limited to 'src')
-rw-r--r--src/log.h3
-rw-r--r--src/main.c12
-rw-r--r--src/server_ssh.c2
-rw-r--r--src/socket.c66
-rw-r--r--src/socket.h10
5 files changed, 72 insertions, 21 deletions
diff --git a/src/log.h b/src/log.h
index 3363395..02f56df 100644
--- a/src/log.h
+++ b/src/log.h
@@ -22,8 +22,11 @@
#define E2(fmt, ...) log_fmtex(ERROR, __FILE__, __LINE__, fmt, __VA_ARGS__)
#define W_STRERR(msg) { if (errno) W2("%s failed: %s", msg, strerror(errno)); }
#define E_STRERR(msg) { if (errno) E2("%s failed: %s", msg, strerror(errno)); }
+#define E_GAIERR(ret, msg) { if (ret) { E2("%s failed: %s", msg, gai_strerror(ret)); } }
#define ABORT_ON_FATAL(expr, msg) \
{ errno = 0; if (expr) { E_STRERR(msg); abort(); } }
+#define GAI_ABORT_ON_FATAL(expr, msg) \
+ { int rv = expr; if (rv) { E_GAIERR(rv, msg); abort(); } }
typedef enum log_priority {
DEBUG = 0, NOTICE, WARNING, ERROR
diff --git a/src/main.c b/src/main.c
index 7ccc404..7ae3cd7 100644
--- a/src/main.c
+++ b/src/main.c
@@ -10,6 +10,7 @@
int main(int argc, char *argv[])
{
static server_ctx srv = {0};
+ struct addrinfo *netifs = NULL;
(void)argc;
(void)argv;
@@ -21,10 +22,15 @@ int main(int argc, char *argv[])
"Server initialisation" );
server_validate_ctx(&srv);
- ABORT_ON_FATAL( socket_init_in(&srv.sock, "127.0.0.1", 2222),
+ GAI_ABORT_ON_FATAL( socket_init_in(&srv.sock, NULL, "2222", &netifs),
"Socket initialisation" );
- ABORT_ON_FATAL( socket_bind_listen(&srv.sock),
- "Socket bind and listen" );
+
+ ABORT_ON_FATAL( socket_bind_in(&srv.sock, netifs),
+ "Socket bind" );
+
+ ABORT_ON_FATAL( socket_listen_in(&srv.sock),
+ "Socket listen" );
+
ABORT_ON_FATAL( srv.server_cbs.on_listen(&srv.server_dat),
"Socket on listen callback" );
diff --git a/src/server_ssh.c b/src/server_ssh.c
index 92d9454..24fdd3e 100644
--- a/src/server_ssh.c
+++ b/src/server_ssh.c
@@ -6,7 +6,7 @@
#include "server.h"
static void set_default_keys(ssh_bind sshbind, int rsa_already_set,
- int dsa_already_set, int ecdsa_already_set);
+ int dsa_already_set, int ecdsa_already_set);
int ssh_on_connect(struct server_data *data, struct server_session *ses)
diff --git a/src/socket.c b/src/socket.c
index 99e4477..7edbda2 100644
--- a/src/socket.c
+++ b/src/socket.c
@@ -1,3 +1,6 @@
+#include <stdio.h>
+#include <unistd.h>
+#include <fcntl.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
@@ -6,25 +9,62 @@
#include "socket.h"
-int socket_init_in(psocket *psocket, const char *listen_addr, unsigned int listen_port)
+static int socket_nonblock(psocket *psocket)
{
- struct in_addr addr = {0};
+ int flags;
- assert(psocket);
- if (!inet_aton(listen_addr, &addr))
+ flags = fcntl(psocket->fd, F_GETFL, 0);
+ if (flags < 0)
+ return 1;
+ flags |= O_NONBLOCK;
+ if (fcntl(psocket->fd, F_SETFL, flags) == -1)
return 1;
+ return 0;
+}
+
+int socket_init_in(psocket *psocket, const char *listen_addr,
+ const char *listen_port, struct addrinfo **results)
+{
+ struct addrinfo hints = {0};
+
+ assert(psocket);
+ assert(listen_addr || listen_port); /* getaddrinfo wants either node or service */
+ hints.ai_family = AF_UNSPEC; /* IPV4 && IPV6 */
+ hints.ai_socktype = SOCK_STREAM; /* TCP */
+ hints.ai_flags = AI_PASSIVE; /* all interfaces */
+
+ return getaddrinfo(listen_addr, listen_port, &hints, results);
+}
+
+int socket_bind_in(psocket *psocket, struct addrinfo *results)
+{
+ int fd = -1, rv;
+ struct addrinfo *rp = NULL;
+
+ assert(psocket && results);
- psocket->sock.sin_family = AF_INET;
- psocket->sock.sin_addr = addr;
- psocket->sock.sin_port = htons(listen_port);
- psocket->fd = socket(AF_INET, SOCK_STREAM | SOCK_NONBLOCK, 0);
- return psocket->fd < 0;
+ for (rp = results; rp != NULL; rp = rp->ai_next) {
+ fd = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
+ if (fd < 0)
+ continue;
+ rv = bind(fd, rp->ai_addr, rp->ai_addrlen);
+ if (!rv)
+ break;
+ close(fd);
+ }
+
+ if (!rp)
+ return -1;
+
+ psocket->fd = fd;
+ psocket->addr = *rp;
+ freeaddrinfo(results);
+ return socket_nonblock(psocket);
}
-int socket_bind_listen(psocket *psocket)
+int socket_listen_in(psocket *psocket)
{
assert(psocket);
- if (bind(psocket->fd, &psocket->sock, sizeof(psocket->sock)) < 0)
- return 1;
- return listen(psocket->fd, POTD_BACKLOG) < 0;
+
+ return listen(psocket->fd, POTD_BACKLOG);
}
diff --git a/src/socket.h b/src/socket.h
index 1b1a762..289c4f1 100644
--- a/src/socket.h
+++ b/src/socket.h
@@ -1,19 +1,21 @@
#ifndef POTD_SOCKET_H
#define POTD_SOCKET_H 1
-#include <netinet/in.h>
+#include <netdb.h>
#define POTD_BACKLOG 3
typedef struct psocket {
int fd;
- struct sockaddr_in sock;
+ struct addrinfo addr;
} psocket;
int socket_init_in(psocket *psocket, const char *listen_addr,
- unsigned int listen_port);
+ const char *listen_port, struct addrinfo **results);
-int socket_bind_listen(psocket *psocket);
+int socket_bind_in(psocket *psocket, struct addrinfo *results);
+
+int socket_listen_in(psocket *psocket);
#endif