summaryrefslogtreecommitdiff
path: root/other/bootstrap_serverdaemon/DHT_bootstrap_daemon.c
blob: 276821e678fed2bc6ef9f691356346101c45854c (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
123
124
125
126
127
128
129
130
131
132
133
/* DHT boostrap
*
* A simple DHT boostrap server for tox (daemon edition)
*/
    
#include <sys/types.h> /* pid_t */
#include <sys/stat.h> /* umask */
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h> /* POSIX things */
#include <errno.h>
    
#include "../core/DHT.h"
    
/* Sleep function (x = milliseconds) */
#ifdef WIN32
#define c_sleep(x) Sleep(1*x)
#else
#include <unistd.h>
#define c_sleep(x) usleep(1000*x)
#endif
    
#define PORT 33445
#define USERNAME getenv("USER")
#define PIDFILE "/home/%s/.bootstrap_server.pid" /* %s represents the unser's name */
    
int main(int argc, char *argv[]) {
    
    char pidfloc[512]; /* Location of the soon-to-be PID file */
    pid_t pid, sid; /* Process- and Session-ID */
    
    FILE *pidf; /* The PID file */
    
    /* Assemble PID file location an try to open the file */
    sprintf(pidfloc, PIDFILE, USERNAME);
    pidf = fopen(pidfloc, "w");
    
    /* Generate new keypair */
    new_keys();
    
    /* Public key */
    uint32_t i;
    
    printf("\nPublic Key: ");
    for(i = 0; i < 32; i++)
    {
        uint8_t ln, hn;
        ln = 0x0F & self_public_key[i];
        hn = 0xF0 & self_public_key[i];
        hn = hn >> 4;
        printf("%X%X", hn, ln);
    }
    printf("\n");
    
    /* initialize networking
    bind to ip 0.0.0.0:PORT */
    IP ip;
    ip.i = 0;
    init_networking(ip, PORT);
    
    /* If there's been an error, exit before forking off */
    if (errno != 0) {
        perror("Error");
        printf("Error(s) occured during start-up. Exiting.\n");
        exit(EXIT_FAILURE);
    }
    
//    /* Assemble the location of the PID file */
//    sprintf(pidfloc, PIDFILE, USERNAME);
//    pidf = fopen(pidfloc, "w");
//    /* Check if we can actually open the file */
//    if(pidf == NULL) {
//        printf("Couldn't open PID-File %s for writing.\n", pidfloc);
//        exit(EXIT_FAILURE);
//    }
    
    /* Fork off the parent process */
    pid = fork();
    if (pid < 0) {
        printf("Forking failed.\n");
        exit(EXIT_FAILURE);
    }
    
    /* If we got a good PID, then
    we can exit the parent process. */
    if (pid > 0) {
        printf("Forked successfully: %d\n", pid);
    
        /* Write the PID file */
        fprintf(pidf, "%d\n", pid);
        fclose(pidf);
    
        /* Exit parent */
        exit(EXIT_SUCCESS);
    }
    
    /* Change the file mode mask */
    umask(0);
    
    /* Create a new SID for the child process */
    sid = setsid();
    if (sid < 0) {
        printf("SID creation failure.\n");
        exit(EXIT_FAILURE);
    }
    
    /* Change the current working directory */
    if ((chdir("/")) < 0) {
        exit(EXIT_FAILURE);
    }
    
    /* Go quiet */
    close(STDIN_FILENO);
    close(STDOUT_FILENO);
    close(STDERR_FILENO);
    
    IP_Port ip_port;
    uint8_t data[MAX_UDP_PACKET_SIZE];
    uint32_t length;
    
    /* Main loop */
    while(1) {
        doDHT();
        while(receivepacket(&ip_port, data, &length) != -1) {
            DHT_handlepacket(data, length, ip_port);
        }
        c_sleep(1);
    }
    
    shutdown_networking();
    exit(EXIT_SUCCESS);
}