/* telnetchat.c This is a telnet chat server. Copyright (C) 2008 Kenny1987. Contact me : kenny1987 at users dot sourceforge dot net Telnetchat is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version. Telnetchat is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with GNU Anubis. If not, see <http://www.gnu.org/licenses/>.*/#include <sys/types.h>#include <sys/socket.h>#include <netinet/in.h>#include <netdb.h>#include <pthread.h>#include <unistd.h>#include <stdio.h>#include <fcntl.h>#define PORT 23#define MAX_QUEUE 3#define BUF_SIZE 1024#define BOX_NAME "Insert Boxname here"int sock_listen, sock_comm;char outputbuf[BUF_SIZE];struct sockaddr_in server;unsigned int usernr;void* handle_communication(void * arg) { int priv_sock_comm = sock_comm; // copy current socket info char username[BUF_SIZE]; char outputbufcp[BUF_SIZE]; char linebuf[BUF_SIZE]; char buffer[BUF_SIZE]; unsigned int my_usernr = usernr++; int i=0; sprintf(buffer, "Welcome to %s . Please enter a username\r\n", BOX_NAME); if (send(priv_sock_comm, buffer, strlen(buffer), 0) < 0) perror("Error when sending."); for (i=0; i < BUF_SIZE; i++) buffer[i] = 0; for (i=0; i < BUF_SIZE; i++) linebuf[i] = '\0'; int bytes = 0; // "login" for (;;) { int bytes = recv(priv_sock_comm, buffer, sizeof(buffer) -1, 0); //buffer[bytes - 2] = '\0'; //remove \n and \r buffer[bytes] = '\0'; if (bytes > 0) { strcat(linebuf, buffer); if (strchr(linebuf, '\n')!=NULL) { linebuf[strlen(linebuf) - 2] = '\0'; strcpy(&username, &linebuf); for (i=0; i < BUF_SIZE; i++) linebuf[i] = '\0'; break; } } } sprintf(outputbuf, "%s joined us. His user-id is %d. Have fun %s! \r\n", &username, my_usernr, &username); // chat for (;;) { int bytes = recv(priv_sock_comm, buffer, sizeof(buffer) -1, 0); buffer[bytes] = '\0'; if (bytes > 0) { // Input from user – build chatstring strcat(linebuf, buffer); if (strchr(linebuf, '\n')!=NULL) { linebuf[strlen(linebuf) - 2] = '\0'; if (strcmp(linebuf, "!QUIT")==0) break; sprintf(outputbuf, "'%s' says %s \r\n", &linebuf, &username); for (i=0; i < BUF_SIZE; i++) linebuf[i] = '\0'; } } if (strcmp(outputbuf, outputbufcp)!=0) { // Send outputbuffer if (outputbuf[1]=='@') { // 0 is \t char temp[BUF_SIZE]; strcpy(&temp, &outputbuf); temp[0] = '0'; temp[1] = '0'; char *space; space = strchr(&temp, ' '); *space = '\0'; // now @15 is 0015 and we can use atoi unsigned int recipent = atoi(&temp); if (recipent == my_usernr) { if (send(priv_sock_comm, outputbuf, strlen(outputbuf), 0) < 0) perror("Error when sending. (3)"); } } else { if (send(priv_sock_comm, outputbuf, strlen(outputbuf), 0) < 0) perror("Error when sending. (3)"); } strcpy(&outputbufcp, &outputbuf); } } sprintf(outputbuf, "%s left us \r\n", &username); close(priv_sock_comm); pthread_exit(NULL); }int main() { usernr = 0; // Create Server sock_listen = socket(AF_INET, SOCK_STREAM, 0); if (sock_listen < 0) { perror("Error when trying to listen."); return(1); } server.sin_family = AF_INET; server.sin_addr.s_addr = INADDR_ANY; server.sin_port = htons(PORT); //Binding socket int yes = 1; setsockopt(sock_listen, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)); if (bind(sock_listen, &server, sizeof(server))) { perror("Error when binding socket."); return(1); } for (;;) { listen(sock_listen, MAX_QUEUE); sock_comm = accept(sock_listen, 0, 0); fcntl(sock_comm, F_SETFL, O_NONBLOCK); pthread_t t; if (pthread_create(&t,NULL,handle_communication,NULL) != 0) perror("Could not create thread"); if (sock_comm < 0) { perror("Error when trying to accept."); return(1); } } close(sock_listen);}