diff --git a/Makefile b/Makefile index 9cb8537..8b94b88 100644 --- a/Makefile +++ b/Makefile @@ -9,13 +9,13 @@ LD = ld #Binary File Name -BINARY = podzd +BINARY = tuved #Delete Program REMOVE = rm -f #Objects -OBJS = main.o socket.o podz.o +OBJS = main.o socket.o tuve.o LIBRARIES = CFLAGS = -Wall -O diff --git a/main.c b/main.c index 06f3718..0ba3285 100644 --- a/main.c +++ b/main.c @@ -11,7 +11,7 @@ #include #include -#include "podzd.h" +#include "tuved.h" FILE *logFile = 0x0; diff --git a/podz.c b/podz.c deleted file mode 100644 index 587bf8d..0000000 --- a/podz.c +++ /dev/null @@ -1,179 +0,0 @@ -/* - (c) 2007 Christopher Olsen - $Id$ -*/ -#include -#include -#include -#include -#include -#include - -#include "podzd.h" - -int podzGetData(myConnections_t *userConnection) { - - writeLog("Getting Data\n"); - - memset(&userConnection->data,0x0,1024); - userConnection->recLen = sReadSocket(userConnection->fd,&userConnection->data,1024); - if (userConnection->recLen == 0) - return(-1); - - userConnection->userInfo.pong = time(NULL); - - #ifdef DEBUG - writeLog("Got: [%i][%s]\n",userConnection->recLen,userConnection->data); - #endif - - if (userConnection->recLen == 1) { - writeLog("[%i][%c]\n",userConnection->data[0],userConnection->data[0]); - while(1); - } - - podzProcessData(userConnection); - - return(0x0); - } - - -int podzSendAll(char *output,int fd) { - myConnections_t *tmpConnection = 0x0; - - for (tmpConnection = connections;tmpConnection != 0x0;tmpConnection = tmpConnection->next) { - if (tmpConnection->fd != fd) - send(tmpConnection->fd,output,strlen(output),MSG_NOSIGNAL); - } - - return(0x0); - } - -int podzProcessData(myConnections_t *userConnection) { - myConnections_t *tmpConnection = 0x0; - char *cmd = 0x0; - char *msg = 0x0; - char *data = 0x0; - char *tok_last = 0x0; - char output[1024]; - char buffer[1024]; - - cmd = userConnection->data; - if (cmd[userConnection->recLen - 1] == '\n') - cmd[userConnection->recLen - 1] = '\0'; - - if (cmd[userConnection->recLen - 2] == 13) - cmd[userConnection->recLen - 2] = '\0'; - - if (strstr(userConnection->data," ")) { - cmd = strtok_r(userConnection->data," ",&tok_last); - data = strtok_r(NULL,"\n",&tok_last); - writeLog("DATA[%s:%s]\n",cmd,data); - tok_last = 0x0; - } - else { - writeLog("DATA[%s]\n",cmd); - } - - if (!strcasecmp(cmd,"ident")) { - writeLog("(ident)\n"); - if (strstr(data,":")) { - sprintf(userConnection->userInfo.username,strtok_r(data,":",&tok_last)); - userConnection->userInfo.uid = atoi(strtok_r(NULL,"\n",&tok_last)); - sprintf(output,"ison:%i:1",userConnection->userInfo.uid); - podzSendAll(output,userConnection->fd); - userConnection->userInfo.ident = 0x1; - tok_last = 0x0; - } - else { - send(userConnection->fd,"invalid ident",sizeof("invalid ident"),0); - } - } - else if (strcasecmp(cmd,"") == 0x0) { - sprintf(output,"\n\n\n\n"); - send(userConnection->fd,output,strlen(output) + 1,MSG_NOSIGNAL); - writeLog("Send CROSSDOMAIN\n"); - } - else if (userConnection->userInfo.ident != 0x1) { - send(userConnection->fd,"Please Auth",sizeof("Please Auth"),0); - } - else if (strcasecmp(cmd,"whoson") == 0x0) { - writeLog("(whoson)\n"); - for (tmpConnection = connections->next;tmpConnection != 0x0;tmpConnection = tmpConnection->next) { - sprintf(output,"User: [%s:%i]\n",tmpConnection->userInfo.username,tmpConnection->userInfo.uid); - send(userConnection->fd,output,strlen(output),MSG_NOSIGNAL); - } - } - else if (strcasecmp(cmd,"mail") == 0x0) { - for (tmpConnection = connections->next;tmpConnection != 0x0;tmpConnection = tmpConnection->next) { - if (tmpConnection->userInfo.uid == atoi(data)) { - writeLog("Found User\n"); - break; - } - } - - if (tmpConnection == 0x0) - send(userConnection->fd,"User Not On",strlen("User Not On"),MSG_NOSIGNAL); - else - send(tmpConnection->fd,"gotmail",strlen("gotmail"),MSG_NOSIGNAL); - } - else if (strcasecmp(cmd,"logoff") == 0x0) { - for (tmpConnection = connections->next;tmpConnection != 0x0;tmpConnection = tmpConnection->next) { - if (tmpConnection->userInfo.uid == atoi(data)) - break; - } - if (tmpConnection == 0x0) - send(userConnection->fd,"User Not On",strlen("User Not On"),MSG_NOSIGNAL); - else - send(tmpConnection->fd,"signoff",strlen("signoff"),MSG_NOSIGNAL); - } - else if (strcasecmp(cmd,"ison") == 0x0) { - sprintf(buffer,data); - sprintf(output,"ison"); - for (msg = strtok_r(buffer,",",&tok_last);msg;msg = strtok_r(NULL, ",", &tok_last)) { - for (tmpConnection = connections->next;tmpConnection != 0x0;tmpConnection = tmpConnection->next) { - if (tmpConnection->userInfo.uid == atoi(msg)) - break; - } - if (tmpConnection == 0x0) - sprintf(output,"%s:%i:0",output,atoi(msg)); - else - sprintf(output,"%s:%i:1",output,atoi(msg)); - } - send(userConnection->fd,output,strlen(output),MSG_NOSIGNAL); - } - else if (strcasecmp(cmd,"privmsg") == 0x0) { - for (tmpConnection = connections->next;tmpConnection != 0x0;tmpConnection = tmpConnection->next) { - if (tmpConnection->userInfo.uid == atoi(strtok(data," "))) - break; - } - if (tmpConnection == 0x0) { - sprintf(output,"privmsg:System Notification:%i:User is Offline",atoi(data)); - send(userConnection->fd,output,strlen(output),MSG_NOSIGNAL); - } - else { - msg = data + strlen(data) + 1;//strtok(NULL,"\n"); - sprintf(output,"privmsg:%s:%i:%s",userConnection->userInfo.username,userConnection->userInfo.uid,msg); - send(tmpConnection->fd,output,strlen(output),MSG_NOSIGNAL); - } - } - else if (strcasecmp(cmd,"!die") == 0x0) { - writeLog("Podz Daemon Shutting Down\n"); - fflush(logFile); - exit(0); - } - else if (strcasecmp(cmd,"PONG!") == 0x0) { - writeLog("(PONG! :NULL)\n"); - userConnection->userInfo.pfailed = 0x0; - } - else if (strcasecmp(cmd,"chanmsg") == 0x0) { - sprintf(output,"chanmsg:%s\n",data); - podzSendAll(output,userConnection->fd); - } - else { - send(userConnection->fd,"invalid command",sizeof("invalid command"),MSG_NOSIGNAL); - writeLog("Invalid CMD: [%s]\n",cmd); - } - - return(0x0); - } - diff --git a/podzd.h b/podzd.h deleted file mode 100644 index f9e24c6..0000000 --- a/podzd.h +++ /dev/null @@ -1,54 +0,0 @@ -/* - (c) 2007 Christopher Olsen - - $Id$ -*/ -#include - -#define MYPORT 9999 // the port users will be connecting to -#define BACKLOG 10 // how many pending connections queue will hold -#define LOG_FILE "./podzd.log" - -typedef struct podz { - short ident; - short pfailed; - short status; - int uid; - time_t pong; - char username[32]; - } podz_t; - -typedef struct myConnections { - struct myConnections *prev; - struct myConnections *next; - int fd; - int recLen; - char data[1024]; - podz_t userInfo; - } myConnections_t; - -/* - Global variables very not safe - */ -extern int listenerFD; -extern int highSock; -extern myConnections_t *connections; -extern FILE *logFile; - -/* Socket Functions */ -ssize_t sReadSocket(int socketFD,void *buffer,size_t length); -myConnections_t *sFindConnection(int fd); -int sStartListener(); -int sAddConnection(int); -int sGetConnections(fd_set *); -int sProcessConnections(fd_set *); -int sSendPing(time_t); -int sCleanConnections(); - -/* Podz Fucntions */ -int podzGetData(myConnections_t *); -int podzProcessData(myConnections_t *); -int podzSendAll(char *,int); - -/* Log Functions */ -int writeLog(char const * __restrict, ...); diff --git a/socket.c b/socket.c index 0231950..8687d63 100644 --- a/socket.c +++ b/socket.c @@ -16,7 +16,7 @@ #include #include -#include "podzd.h" +#include "tuved.h" myConnections_t *connections = 0x0; diff --git a/tuve.c b/tuve.c new file mode 100644 index 0000000..76043a9 --- /dev/null +++ b/tuve.c @@ -0,0 +1,179 @@ +/* + (c) 2007 Christopher Olsen + $Id$ +*/ +#include +#include +#include +#include +#include +#include + +#include "tuved.h" + +int podzGetData(myConnections_t *userConnection) { + + writeLog("Getting Data\n"); + + memset(&userConnection->data,0x0,1024); + userConnection->recLen = sReadSocket(userConnection->fd,&userConnection->data,1024); + if (userConnection->recLen == 0) + return(-1); + + userConnection->userInfo.pong = time(NULL); + + #ifdef DEBUG + writeLog("Got: [%i][%s]\n",userConnection->recLen,userConnection->data); + #endif + + if (userConnection->recLen == 1) { + writeLog("[%i][%c]\n",userConnection->data[0],userConnection->data[0]); + while(1); + } + + podzProcessData(userConnection); + + return(0x0); + } + + +int podzSendAll(char *output,int fd) { + myConnections_t *tmpConnection = 0x0; + + for (tmpConnection = connections;tmpConnection != 0x0;tmpConnection = tmpConnection->next) { + if (tmpConnection->fd != fd) + send(tmpConnection->fd,output,strlen(output),MSG_NOSIGNAL); + } + + return(0x0); + } + +int podzProcessData(myConnections_t *userConnection) { + myConnections_t *tmpConnection = 0x0; + char *cmd = 0x0; + char *msg = 0x0; + char *data = 0x0; + char *tok_last = 0x0; + char output[1024]; + char buffer[1024]; + + cmd = userConnection->data; + if (cmd[userConnection->recLen - 1] == '\n') + cmd[userConnection->recLen - 1] = '\0'; + + if (cmd[userConnection->recLen - 2] == 13) + cmd[userConnection->recLen - 2] = '\0'; + + if (strstr(userConnection->data," ")) { + cmd = strtok_r(userConnection->data," ",&tok_last); + data = strtok_r(NULL,"\n",&tok_last); + writeLog("DATA[%s:%s]\n",cmd,data); + tok_last = 0x0; + } + else { + writeLog("DATA[%s]\n",cmd); + } + + if (!strcasecmp(cmd,"ident")) { + writeLog("(ident)\n"); + if (strstr(data,":")) { + sprintf(userConnection->userInfo.username,strtok_r(data,":",&tok_last)); + userConnection->userInfo.uid = atoi(strtok_r(NULL,"\n",&tok_last)); + sprintf(output,"ison:%i:1",userConnection->userInfo.uid); + podzSendAll(output,userConnection->fd); + userConnection->userInfo.ident = 0x1; + tok_last = 0x0; + } + else { + send(userConnection->fd,"invalid ident",sizeof("invalid ident"),0); + } + } + else if (strcasecmp(cmd,"") == 0x0) { + sprintf(output,"\n\n\n\n"); + send(userConnection->fd,output,strlen(output) + 1,MSG_NOSIGNAL); + writeLog("Send CROSSDOMAIN\n"); + } + else if (userConnection->userInfo.ident != 0x1) { + send(userConnection->fd,"Please Auth",sizeof("Please Auth"),0); + } + else if (strcasecmp(cmd,"whoson") == 0x0) { + writeLog("(whoson)\n"); + for (tmpConnection = connections->next;tmpConnection != 0x0;tmpConnection = tmpConnection->next) { + sprintf(output,"User: [%s:%i]\n",tmpConnection->userInfo.username,tmpConnection->userInfo.uid); + send(userConnection->fd,output,strlen(output),MSG_NOSIGNAL); + } + } + else if (strcasecmp(cmd,"mail") == 0x0) { + for (tmpConnection = connections->next;tmpConnection != 0x0;tmpConnection = tmpConnection->next) { + if (tmpConnection->userInfo.uid == atoi(data)) { + writeLog("Found User\n"); + break; + } + } + + if (tmpConnection == 0x0) + send(userConnection->fd,"User Not On",strlen("User Not On"),MSG_NOSIGNAL); + else + send(tmpConnection->fd,"gotmail",strlen("gotmail"),MSG_NOSIGNAL); + } + else if (strcasecmp(cmd,"logoff") == 0x0) { + for (tmpConnection = connections->next;tmpConnection != 0x0;tmpConnection = tmpConnection->next) { + if (tmpConnection->userInfo.uid == atoi(data)) + break; + } + if (tmpConnection == 0x0) + send(userConnection->fd,"User Not On",strlen("User Not On"),MSG_NOSIGNAL); + else + send(tmpConnection->fd,"signoff",strlen("signoff"),MSG_NOSIGNAL); + } + else if (strcasecmp(cmd,"ison") == 0x0) { + sprintf(buffer,data); + sprintf(output,"ison"); + for (msg = strtok_r(buffer,",",&tok_last);msg;msg = strtok_r(NULL, ",", &tok_last)) { + for (tmpConnection = connections->next;tmpConnection != 0x0;tmpConnection = tmpConnection->next) { + if (tmpConnection->userInfo.uid == atoi(msg)) + break; + } + if (tmpConnection == 0x0) + sprintf(output,"%s:%i:0",output,atoi(msg)); + else + sprintf(output,"%s:%i:1",output,atoi(msg)); + } + send(userConnection->fd,output,strlen(output),MSG_NOSIGNAL); + } + else if (strcasecmp(cmd,"privmsg") == 0x0) { + for (tmpConnection = connections->next;tmpConnection != 0x0;tmpConnection = tmpConnection->next) { + if (tmpConnection->userInfo.uid == atoi(strtok(data," "))) + break; + } + if (tmpConnection == 0x0) { + sprintf(output,"privmsg:System Notification:%i:User is Offline",atoi(data)); + send(userConnection->fd,output,strlen(output),MSG_NOSIGNAL); + } + else { + msg = data + strlen(data) + 1;//strtok(NULL,"\n"); + sprintf(output,"privmsg:%s:%i:%s",userConnection->userInfo.username,userConnection->userInfo.uid,msg); + send(tmpConnection->fd,output,strlen(output),MSG_NOSIGNAL); + } + } + else if (strcasecmp(cmd,"!die") == 0x0) { + writeLog("Podz Daemon Shutting Down\n"); + fflush(logFile); + exit(0); + } + else if (strcasecmp(cmd,"PONG!") == 0x0) { + writeLog("(PONG! :NULL)\n"); + userConnection->userInfo.pfailed = 0x0; + } + else if (strcasecmp(cmd,"chanmsg") == 0x0) { + sprintf(output,"chanmsg:%s:%s\n",userConnection->userInfo.username,data); + podzSendAll(output,userConnection->fd); + } + else { + send(userConnection->fd,"invalid command",sizeof("invalid command"),MSG_NOSIGNAL); + writeLog("Invalid CMD: [%s]\n",cmd); + } + + return(0x0); + } + diff --git a/tuved.h b/tuved.h new file mode 100644 index 0000000..f9e24c6 --- /dev/null +++ b/tuved.h @@ -0,0 +1,54 @@ +/* + (c) 2007 Christopher Olsen + + $Id$ +*/ +#include + +#define MYPORT 9999 // the port users will be connecting to +#define BACKLOG 10 // how many pending connections queue will hold +#define LOG_FILE "./podzd.log" + +typedef struct podz { + short ident; + short pfailed; + short status; + int uid; + time_t pong; + char username[32]; + } podz_t; + +typedef struct myConnections { + struct myConnections *prev; + struct myConnections *next; + int fd; + int recLen; + char data[1024]; + podz_t userInfo; + } myConnections_t; + +/* + Global variables very not safe + */ +extern int listenerFD; +extern int highSock; +extern myConnections_t *connections; +extern FILE *logFile; + +/* Socket Functions */ +ssize_t sReadSocket(int socketFD,void *buffer,size_t length); +myConnections_t *sFindConnection(int fd); +int sStartListener(); +int sAddConnection(int); +int sGetConnections(fd_set *); +int sProcessConnections(fd_set *); +int sSendPing(time_t); +int sCleanConnections(); + +/* Podz Fucntions */ +int podzGetData(myConnections_t *); +int podzProcessData(myConnections_t *); +int podzSendAll(char *,int); + +/* Log Functions */ +int writeLog(char const * __restrict, ...);