#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "packet.h" #include "netascii.h" static int stop = 0; void stop_handler() { stop = 1; } void usage(char* name) { printf("USAGE: %s -d -u nobody -g nobody -a 127.0.0.1 -p 69 -t 128\n", name); printf("\td: daemonize\n"); printf("\tu: username to run as (default: nobody)\n"); printf("\tg: group to run as (default: nobody)\n"); printf("\ta: address to bind to (default: 127.0.0.1)\n"); printf("\tp: port to bind to (default: 69)\n"); printf("\tt: number of worker threads to spawn (default 128)\n"); } int main(int argc, char** argv) { int daemonize = 0; char addr[16], user[32], group[32]; memset(addr, '\0', sizeof(addr)); memset(user, '\0', sizeof(user)); memset(group, '\0', sizeof(group)); strcpy(addr, "127.0.0.1"); strcpy(user, "nobody"); strcpy(group, "nobody"); int port = 69; int threadcount = 128; int ch = 0; while ((ch = getopt(argc, argv, "da:p:u:g:t:h")) != -1) { switch (ch) { case 'a': //TODO: this is unsafe strcpy(addr, optarg); break; case 'p': port = atoi(optarg); if ((port <= 0) || (port >= 65536)) { fprintf(stderr, "invalid port specified.\n"); return -1; } break; case 't': threadcount = atoi(optarg); if ((threadcount <= 0) || (threadcount > 51200)) { fprintf(stderr, "threadcount must be between 1 and 51200\n"); return -1; } break; case 'd': daemonize = 1; break; case 'g': if (daemonize) { //TODO: this is unsafe strcpy(group, optarg); } else { fprintf(stderr, "-g requires -d\n"); return -1; } break; case 'u': if (daemonize) { //TODO: this is unsafe 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 saddr; saddr.sin_family = AF_INET; saddr.sin_port = htons(port); saddr.sin_addr.s_addr = inet_addr(addr); if (bind(s, (struct sockaddr*)&saddr, sizeof(saddr)) < 0) { switch (errno) { case EACCES: syslog(LOG_ERR, "socket bind failed: permission denied"); break; case EADDRINUSE: syslog(LOG_ERR, "socket bind failed: address already in use"); break; case EADDRNOTAVAIL: syslog(LOG_ERR, "socket bind failed: address not available"); break; default: syslog(LOG_ERR, "socket bind failed"); } return -1; } syslog(LOG_INFO, "socket bind success"); //TODO: drop privs! pthread_t threads[128]; memset(threads, '\0', sizeof(threads)); //TODO: spawn threadpool while (!stop) { struct sockaddr_in caddr; unsigned int clen = sizeof(caddr); char buf[1024]; if (recvfrom(s, buf, sizeof(buf), 0, (struct sockaddr*)&caddr, &clen) < 0) { syslog(LOG_ERR, "got a client connection, but unable to receive data!"); } //TODO: handle threadpool } return 0; }