about summary refs log tree commit diff stats
path: root/src/main.c
blob: c8e0886141ead080ba7825a449481aaef870f121 (plain) (blame)
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
#include <unistd.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdio.h>
#include <syslog.h>
#include <string.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <netdb.h>
#include <pthread.h>
#include <pwd.h>
#include <errno.h>

#include "packet.h"
#include "netascii.h"

void usage(char* name) {
	printf("USAGE: %s -d\n", name);
	printf("\td: daemonize\n");
}

int main(int argc, char** argv) {
	int daemonize = 0;
	int ch = 0;
	char addr[16], user[32], group[32];
	strcpy(addr, "172.0.0.1");
	strcpy(user, "nobody");
	strcpy(group, "nobody");
	int port = 69;
	while ((ch = getopt(argc, argv, "da:p:u:g:h")) != -1) {
		switch (ch) {
			case 'a':
				strcpy(addr, optarg);
				break;
			case 'p':
				port = atoi(optarg);
				if ((port <= 0) || (port >= 65536)) {
					fprintf(stderr, "invalid port specified.\n");
					return -1;
				}
				break;
			case 'd':
				daemonize = 1;
				break;
			case 'g':
				if (daemonize) {
					strcpy(group, optarg);
				} else {
					fprintf(stderr, "-g requires -d\n");
					return -1;
				}
				break
			case 'u':
				if (daemonize) {
					strcpy(user, optarg);
				} else {
					fprintf(stderr, "-u requires -d\n");
					return -1;
				}
				break
			case '?': case 'h':
				usage(argv[0]);
				return -1;
		}
	}

	setlogmask(LOG_UPTO(LOG_INFO));
	openlog(argv[0], LOG_PID | LOG_PERROR | LOG_NDELAY, LOG_FTP);
	syslog(LOG_INFO, "starting up...");

	if (daemonize) {
		if (daemon(1, 0)) {
			syslog(LOG_ERR, "failed to daemonize as requested!");
			return -1;
		} else {
			syslog(LOG_INFO, "daemonized");
		}
	}

	int s = socket(AF_INET, SOCK_DGRAM, 0);
	if (s <= 0) {
		syslog(LOG_ERR, "unable to bind socket!");
		return -1;
	}
	struct sockaddr_in server_addr;
	server_addr.sin_family = AF_INET;
	server_addr.sin_port = htons(port);
	server_addr.sin_addr.s_addr = inet_addr(addr);

	//TODO: drop privs!
	//TODO: threading!
	syslog(LOG_INFO, "program completed successfully!");
	close(s);
	return 0;
}