aboutsummaryrefslogtreecommitdiff
path: root/src/server.c
blob: 73a8d595021d3f6bf16daa374a7295798fa58d2b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <sys/socket.h>
#include <netdb.h>
#include <arpa/inet.h>

#include "log.h"
#include "server.h"

struct sock_t {
    int sockfd;
    char ip[INET6_ADDRSTRLEN];
};

static void *get_in_addr(struct sockaddr *sa)
{
    if (sa->sa_family == AF_INET) {
        return &(((struct sockaddr_in*)sa)->sin_addr);
    }

    return &(((struct sockaddr_in6*)sa)->sin6_addr);
}

int server_start(char *port, sock_t *sock)
{
    struct addrinfo hints, *servinfo;
    int rv;

    memset(&hints, 0, sizeof hints);
    hints.ai_family = AF_UNSPEC;
    hints.ai_socktype = SOCK_STREAM;
    hints.ai_flags = AI_PASSIVE; // use my IP

    if ((rv = getaddrinfo(NULL, port, &hints, &servinfo)) != 0) {
        err("getaddrinfo: %s", gai_strerror(rv));
        return 1;
    }

    // loop through all the results and bind to the first we can
    struct addrinfo *p = NULL;
    for(p = servinfo; p != NULL; p = p->ai_next) {
        if ((sock->sockfd = socket(p->ai_family, p->ai_socktype,
                p->ai_protocol)) == -1) {
            err("socker: %s", strerror(errno));
            continue;
        }

        int yes=1;
        if (setsockopt(sock->sockfd, SOL_SOCKET, SO_REUSEADDR, &yes,
                       sizeof(int)) == -1) {
            err("setsockopt: %s", strerror(errno));
            return 1;
        }

        if (bind(sock->sockfd, p->ai_addr, p->ai_addrlen) == -1) {
            close(sock->sockfd);
            err("bind: %s", strerror(errno));
            continue;
        }

        break;
    }

    if (p == NULL)  {
        err("server: failed to bind");
        return 1;
    }

    freeaddrinfo(servinfo);

    if(listen(sock->sockfd, 10) == -1) {
        err("listen: %s", strerror(errno));
        return 1;
    }
    return 0;
}

int server_accept(sock_t *sock, sock_t *conn)
{
    struct sockaddr_storage conn_addr;
    socklen_t sz = sizeof(conn_addr);
    conn->sockfd = accept(sock->sockfd, (struct sockaddr *)&conn_addr, &sz);
    if(conn->sockfd == -1) {
        err("accept: %s", strerror(errno));
        return 1;
    }

    inet_ntop(conn_addr.ss_family,
              get_in_addr((struct sockaddr *)&conn_addr),
              conn->ip, sizeof(conn->ip));

    return 0;
}

ssize_t server_send(sock_t *sock, char *buf, size_t sz)
{
    return send(sock->sockfd, buf, sz, 0);
}

ssize_t server_recv(sock_t *sock, char *buf, size_t sz)
{
    return recv(sock->sockfd, buf, sz, 0);
}

char *server_connection_ip(sock_t *conn)
{
    return conn->ip;
}

sock_t *server_sock_create()
{
    return (sock_t *)malloc(sizeof(sock_t));
}

void server_sock_close(sock_t *s)
{
    close(s->sockfd);
    free(s);
}