about summary refs log tree commit diff
path: root/cptc.c
blob: 1dafddc0a0cc25210b763a52c2112b68486becec (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
#include "cptc.h"

#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>

#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

#include <pthread.h>
#include <unistd.h>

#include <curl/curl.h>
#include <curl/easy.h>


#define error(text, status) \
    do                      \
    {                       \
        perror(text);       \
        exit(status);       \
    } while (0)             \


struct RequestHandlerArgs
{
    int fd;
    int n;
    bool accept;
};

static void *requestHandlerThread(struct RequestHandlerArgs *arg)
{
    int fd = arg->fd;
    int n = arg->n;
    arg->accept = true;
    CPTC_requestHandler(fd, n);
    close(fd);
    pthread_exit(NULL);
}


void CPTC(const char *ip, in_port_t port)
{
    struct sockaddr_in addr, peer;
    socklen_t peer_size;
    int fd, peerfd;
    int n = 0;

    curl_easy_init();

    if ((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
        error("socket()", 1);
    if ((setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &(int){1}, sizeof(int))) < 0)
        error("setsockopt()", 1);

    addr = (struct sockaddr_in)
    {
        .sin_addr.s_addr = inet_addr(ip),
        .sin_port = htons(port),
        .sin_family = AF_INET,
    };

    if (bind(fd, (struct sockaddr *)&addr, sizeof(addr)) < 0)
        error("bind()", 1);
    if (listen(fd, 128) == -1)
        error("listen()", 3);

    printf("Started serving at %s:%d\n", ip, port);
    for (;;)
    {
        peer_size = sizeof(peer);
        if ((peerfd = accept(fd, (struct sockaddr *)&peer, &peer_size)) < 0)
        {
            perror("accept()");
            continue;
        }
        printf("Connection from %s:%u!\n", inet_ntoa(peer.sin_addr), peer.sin_port);

        pthread_t thread;
        struct RequestHandlerArgs arg = {peerfd, n++, false};
        pthread_create(&thread, NULL, (void *(*)(void *))requestHandlerThread, &arg);
        pthread_detach(thread);
        while (!arg.accept);
    }
}