Added a daemon version of DHT_bootstrap made by tawm_.

This commit is contained in:
irungentoo 2013-07-18 18:46:14 -04:00
parent f06cd56ece
commit 60957885fd
2 changed files with 242 additions and 0 deletions

View File

@ -0,0 +1,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);
}

View File

@ -0,0 +1,109 @@
#! /bin/sh
### BEGIN INIT INFO
# Provides: bootstrap_server
# Required-Start: $remote_fs $syslog
# Required-Stop: $remote_fs $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Start the Tox bootstrapping server
# Description: Use this piece of junk to start the Tox
# bootstrap server.
### END INIT INFO
# PATH should only include /usr/* if it runs after the mountnfs.sh script
PATH=/sbin:/usr/sbin:/bin:/usr/bin
DESC="ProjectTox bootstrap server daemon"
NAME=bootstrap_server
DAEMON=/home/$USER/$NAME
DAEMON_ARGS=""
PIDFILE=/home/$USER/.$NAME.pid
SCRIPTNAME=/etc/init.d/$NAME
# Exit if the package is not installed
[ -x "$DAEMON" ] || exit 0
# Read configuration variable file if it is present
[ -r /etc/default/$NAME ] && . /etc/default/$NAME
# Load the VERBOSE setting and other rcS variables
. /lib/init/vars.sh
# Define LSB log_* functions.
# Depend on lsb-base (>= 3.2-14) to ensure that this file is present
# and status_of_proc is working.
. /lib/lsb/init-functions
#
# Function that starts the daemon/service
#
do_start()
{
start-stop-daemon --start --quiet --pidfile $PIDFILE --exec $DAEMON --test > /dev/null \
|| return 1
start-stop-daemon --start --quiet --pidfile $PIDFILE --exec $DAEMON -- \
$DAEMON_ARGS \
|| return 2
sleep 1
}
#
# Function that stops the daemon/service
#
do_stop()
{
start-stop-daemon --stop --quiet --retry=TERM/30/KILL/5 --pidfile $PIDFILE --exec $DAEMON
RETVAL="$?"
[ "$RETVAL" = 2 ] && return 2
start-stop-daemon --stop --quiet --oknodo --retry=0/30/KILL/5 --exec $DAEMON
[ "$?" = 2 ] && return 2
# Many daemons don't delete their pidfiles when they exit.
rm -f $PIDFILE
return "$RETVAL"
}
case "$1" in
start)
[ "$VERBOSE" != no ] && log_daemon_msg "Starting $DESC" "$NAME"
do_start
case "$?" in
0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
esac
;;
stop)
[ "$VERBOSE" != no ] && log_daemon_msg "Stopping $DESC" "$NAME"
do_stop
case "$?" in
0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
esac
;;
status)
status_of_proc "$DAEMON" "$NAME" && exit 0 || exit $?
;;
restart) #|force-reload)
log_daemon_msg "Restarting $DESC" "$NAME"
do_stop
case "$?" in
0|1)
do_start
case "$?" in
0) log_end_msg 0 ;;
1) log_end_msg 1 ;; # Old process is still running
*) log_end_msg 1 ;; # Failed to start
esac
;;
*)
# Failed to stop
log_end_msg 1
;;
esac
;;
*)
echo "Usage: $SCRIPTNAME {start|stop|status|restart}" >&2
exit 3
;;
esac