summaryrefslogtreecommitdiff
path: root/other/bootstrap_serverdaemon/DHT_bootstrap_daemon.c
diff options
context:
space:
mode:
authorirungentoo <irungentoo@gmail.com>2013-07-18 18:46:14 -0400
committerirungentoo <irungentoo@gmail.com>2013-07-18 18:46:14 -0400
commit60957885fd0f458b8f5a929860f3bc026824dc4c (patch)
treeb9c47da647b40bcdadc9f4f6aa169149eb69ce13 /other/bootstrap_serverdaemon/DHT_bootstrap_daemon.c
parentf06cd56ecec14928c8249d0133a248c5c9905734 (diff)
Added a daemon version of DHT_bootstrap made by tawm_.
Diffstat (limited to 'other/bootstrap_serverdaemon/DHT_bootstrap_daemon.c')
-rw-r--r--other/bootstrap_serverdaemon/DHT_bootstrap_daemon.c133
1 files changed, 133 insertions, 0 deletions
diff --git a/other/bootstrap_serverdaemon/DHT_bootstrap_daemon.c b/other/bootstrap_serverdaemon/DHT_bootstrap_daemon.c
new file mode 100644
index 00000000..276821e6
--- /dev/null
+++ b/other/bootstrap_serverdaemon/DHT_bootstrap_daemon.c
@@ -0,0 +1,133 @@
1/* DHT boostrap
2*
3* A simple DHT boostrap server for tox (daemon edition)
4*/
5
6#include <sys/types.h> /* pid_t */
7#include <sys/stat.h> /* umask */
8#include <stdio.h>
9#include <stdlib.h>
10#include <unistd.h> /* POSIX things */
11#include <errno.h>
12
13#include "../core/DHT.h"
14
15/* Sleep function (x = milliseconds) */
16#ifdef WIN32
17#define c_sleep(x) Sleep(1*x)
18#else
19#include <unistd.h>
20#define c_sleep(x) usleep(1000*x)
21#endif
22
23#define PORT 33445
24#define USERNAME getenv("USER")
25#define PIDFILE "/home/%s/.bootstrap_server.pid" /* %s represents the unser's name */
26
27int main(int argc, char *argv[]) {
28
29 char pidfloc[512]; /* Location of the soon-to-be PID file */
30 pid_t pid, sid; /* Process- and Session-ID */
31
32 FILE *pidf; /* The PID file */
33
34 /* Assemble PID file location an try to open the file */
35 sprintf(pidfloc, PIDFILE, USERNAME);
36 pidf = fopen(pidfloc, "w");
37
38 /* Generate new keypair */
39 new_keys();
40
41 /* Public key */
42 uint32_t i;
43
44 printf("\nPublic Key: ");
45 for(i = 0; i < 32; i++)
46 {
47 uint8_t ln, hn;
48 ln = 0x0F & self_public_key[i];
49 hn = 0xF0 & self_public_key[i];
50 hn = hn >> 4;
51 printf("%X%X", hn, ln);
52 }
53 printf("\n");
54
55 /* initialize networking
56 bind to ip 0.0.0.0:PORT */
57 IP ip;
58 ip.i = 0;
59 init_networking(ip, PORT);
60
61 /* If there's been an error, exit before forking off */
62 if (errno != 0) {
63 perror("Error");
64 printf("Error(s) occured during start-up. Exiting.\n");
65 exit(EXIT_FAILURE);
66 }
67
68// /* Assemble the location of the PID file */
69// sprintf(pidfloc, PIDFILE, USERNAME);
70// pidf = fopen(pidfloc, "w");
71// /* Check if we can actually open the file */
72// if(pidf == NULL) {
73// printf("Couldn't open PID-File %s for writing.\n", pidfloc);
74// exit(EXIT_FAILURE);
75// }
76
77 /* Fork off the parent process */
78 pid = fork();
79 if (pid < 0) {
80 printf("Forking failed.\n");
81 exit(EXIT_FAILURE);
82 }
83
84 /* If we got a good PID, then
85 we can exit the parent process. */
86 if (pid > 0) {
87 printf("Forked successfully: %d\n", pid);
88
89 /* Write the PID file */
90 fprintf(pidf, "%d\n", pid);
91 fclose(pidf);
92
93 /* Exit parent */
94 exit(EXIT_SUCCESS);
95 }
96
97 /* Change the file mode mask */
98 umask(0);
99
100 /* Create a new SID for the child process */
101 sid = setsid();
102 if (sid < 0) {
103 printf("SID creation failure.\n");
104 exit(EXIT_FAILURE);
105 }
106
107 /* Change the current working directory */
108 if ((chdir("/")) < 0) {
109 exit(EXIT_FAILURE);
110 }
111
112 /* Go quiet */
113 close(STDIN_FILENO);
114 close(STDOUT_FILENO);
115 close(STDERR_FILENO);
116
117 IP_Port ip_port;
118 uint8_t data[MAX_UDP_PACKET_SIZE];
119 uint32_t length;
120
121 /* Main loop */
122 while(1) {
123 doDHT();
124 while(receivepacket(&ip_port, data, &length) != -1) {
125 DHT_handlepacket(data, length, ip_port);
126 }
127 c_sleep(1);
128 }
129
130 shutdown_networking();
131 exit(EXIT_SUCCESS);
132}
133