summaryrefslogtreecommitdiff
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
parentf06cd56ecec14928c8249d0133a248c5c9905734 (diff)
Added a daemon version of DHT_bootstrap made by tawm_.
-rw-r--r--other/bootstrap_serverdaemon/DHT_bootstrap_daemon.c133
-rw-r--r--other/bootstrap_serverdaemon/initscript.sh109
2 files changed, 242 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
diff --git a/other/bootstrap_serverdaemon/initscript.sh b/other/bootstrap_serverdaemon/initscript.sh
new file mode 100644
index 00000000..aa4b3e77
--- /dev/null
+++ b/other/bootstrap_serverdaemon/initscript.sh
@@ -0,0 +1,109 @@
1#! /bin/sh
2### BEGIN INIT INFO
3# Provides: bootstrap_server
4# Required-Start: $remote_fs $syslog
5# Required-Stop: $remote_fs $syslog
6# Default-Start: 2 3 4 5
7# Default-Stop: 0 1 6
8# Short-Description: Start the Tox bootstrapping server
9# Description: Use this piece of junk to start the Tox
10# bootstrap server.
11### END INIT INFO
12
13# PATH should only include /usr/* if it runs after the mountnfs.sh script
14PATH=/sbin:/usr/sbin:/bin:/usr/bin
15DESC="ProjectTox bootstrap server daemon"
16NAME=bootstrap_server
17DAEMON=/home/$USER/$NAME
18DAEMON_ARGS=""
19PIDFILE=/home/$USER/.$NAME.pid
20SCRIPTNAME=/etc/init.d/$NAME
21
22# Exit if the package is not installed
23[ -x "$DAEMON" ] || exit 0
24
25# Read configuration variable file if it is present
26[ -r /etc/default/$NAME ] && . /etc/default/$NAME
27
28# Load the VERBOSE setting and other rcS variables
29. /lib/init/vars.sh
30
31# Define LSB log_* functions.
32# Depend on lsb-base (>= 3.2-14) to ensure that this file is present
33# and status_of_proc is working.
34. /lib/lsb/init-functions
35
36#
37# Function that starts the daemon/service
38#
39do_start()
40{
41 start-stop-daemon --start --quiet --pidfile $PIDFILE --exec $DAEMON --test > /dev/null \
42 || return 1
43 start-stop-daemon --start --quiet --pidfile $PIDFILE --exec $DAEMON -- \
44 $DAEMON_ARGS \
45 || return 2
46 sleep 1
47}
48
49#
50# Function that stops the daemon/service
51#
52do_stop()
53{
54 start-stop-daemon --stop --quiet --retry=TERM/30/KILL/5 --pidfile $PIDFILE --exec $DAEMON
55 RETVAL="$?"
56 [ "$RETVAL" = 2 ] && return 2
57
58 start-stop-daemon --stop --quiet --oknodo --retry=0/30/KILL/5 --exec $DAEMON
59 [ "$?" = 2 ] && return 2
60 # Many daemons don't delete their pidfiles when they exit.
61 rm -f $PIDFILE
62 return "$RETVAL"
63}
64
65case "$1" in
66 start)
67 [ "$VERBOSE" != no ] && log_daemon_msg "Starting $DESC" "$NAME"
68 do_start
69 case "$?" in
70 0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
71 2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
72 esac
73 ;;
74 stop)
75 [ "$VERBOSE" != no ] && log_daemon_msg "Stopping $DESC" "$NAME"
76 do_stop
77 case "$?" in
78 0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
79 2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
80 esac
81 ;;
82 status)
83 status_of_proc "$DAEMON" "$NAME" && exit 0 || exit $?
84 ;;
85
86 restart) #|force-reload)
87 log_daemon_msg "Restarting $DESC" "$NAME"
88 do_stop
89 case "$?" in
90 0|1)
91 do_start
92 case "$?" in
93 0) log_end_msg 0 ;;
94 1) log_end_msg 1 ;; # Old process is still running
95 *) log_end_msg 1 ;; # Failed to start
96 esac
97 ;;
98 *)
99 # Failed to stop
100 log_end_msg 1
101 ;;
102 esac
103 ;;
104 *)
105 echo "Usage: $SCRIPTNAME {start|stop|status|restart}" >&2
106 exit 3
107 ;;
108esac
109