123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187 |
- #include <stdio.h>
- #include <stdlib.h>
- #include <netdb.h>
- #include <netinet/in.h>
- #include <termios.h>
- #include <unistd.h>
- #include <string.h>
- void doprocessing (int sock);
- char * cmds[10];
- void set_raw(int fd) {
- struct termios tio_raw;
- tcgetattr(fd, &tio_raw);
- cfmakeraw(&tio_raw);
- tcsetattr(fd, TCSANOW, &tio_raw);
- }
- int main( int argc, char *argv[] ) {
- int sockfd, newsockfd, portno, clilen;
- char buffer[256];
- struct sockaddr_in serv_addr, cli_addr;
- int n, pid;
-
- /* First call to socket() function */
- sockfd = socket(AF_INET, SOCK_STREAM, 0);
-
- if (sockfd < 0) {
- perror("ERROR opening socket");
- exit(1);
- }
- if (argc != 3) {
- printf("I need a listening port number, and command/door to call.\n");
- exit(2);
- }
-
- int listen_port = atoi(argv[1]);
- char * command = strdup(argv[2]);
- char * cp;
- int cmd_count;
- cp = command;
- cmds[cmd_count] = cp;
- cmd_count++;
- while (*cp != 0) {
- if (*cp == ' ') {
- *cp = 0;
- cp++;
- cmds[cmd_count] = cp;
- cmd_count++;
- continue;
- }
- cp++;
- }
- cmds[cmd_count] = (char *)NULL;
- if (listen_port ==0) {
- printf("I need a listening port number as a parameter.\n");
- exit(2);
- }
- /* Initialize socket structure */
- bzero((char *) &serv_addr, sizeof(serv_addr));
- portno = listen_port;
-
- serv_addr.sin_family = AF_INET;
- serv_addr.sin_addr.s_addr = INADDR_ANY;
- serv_addr.sin_port = htons(portno);
- int flag = 1;
- if (-1 == setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &flag, sizeof(flag))) {
- perror("setsockopt fail");
- }
- /* Now bind the host address using bind() call.*/
- if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) {
- perror("ERROR on binding");
- exit(1);
- }
-
- /* Now start listening for the clients, here
- * process will go in sleep mode and will wait
- * for the incoming connection
- */
- printf("Listening for connections on %d\n", listen_port);
- listen(sockfd,5);
- clilen = sizeof(cli_addr);
-
- while (1) {
- newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen);
-
- if (newsockfd < 0) {
- perror("ERROR on accept");
- exit(1);
- }
-
- set_raw(newsockfd);
- /* Create child process */
- pid = fork();
-
- if (pid < 0) {
- perror("ERROR on fork");
- exit(1);
- }
-
- if (pid == 0) {
- /* This is the client process */
- close(sockfd);
- doprocessing(newsockfd);
- exit(0);
- }
- else {
- close(newsockfd);
- }
-
- } /* end of while */
- free((void*)command);
- }
- void doprocessing (int sock) {
- int n;
- char buffer[256];
- // raw mode
- set_raw(sock);
- /*
- struct termios tio_raw;
- tcgetattr(sock, &tio_raw);
- cfmakeraw(&tio_raw);
- tcsetattr(sock, TCSANOW, &tio_raw);
- */
- // telnet client into character mode
- // sprintf(buffer, "\377\375\042\377\373\001Welcome socket %d\n\r", sock);
- sprintf(buffer, "Welcome socket %d\n\r", sock);
- write(sock, buffer, strlen(buffer) );
- // can I read the buffer until empty, maybe?
- FILE * fp;
- fp = fopen("door32.sys", "w");
- if ( fp == NULL) {
- return;
- }
- fprintf(fp, "%d\n", 2);
- fprintf(fp, "%d\n", sock);
- fprintf(fp, "38400\n");
- fprintf(fp, "Fake Door32 BBS\n");
- fprintf(fp, "1\n");
- fprintf(fp, "%s\n%s\n", "Bugz Laundry", "Bugz");
- fprintf(fp, "%d\n", 100);
- fprintf(fp, "%d\n", 120);
- fprintf(fp, "1\n1\n");
- fclose(fp);
- // This resets the termios ??
- // execl("./testdoor", "testdoor", "-d", "door32.sys", (const char *)NULL);
- execv(cmds[0], cmds);
- // step1: write out door32.sys file
- // exec / replace this with our door
- /*
- bzero(buffer,256);
-
- n = read(sock,buffer,255);
-
- if (n < 0) {
- perror("ERROR reading from socket");
- exit(1);
- }
-
- printf("Here is the message: %s\n",buffer);
- n = write(sock,"I got your message",18);
-
- if (n < 0) {
- perror("ERROR writing to socket");
- exit(1);
- }
- */
- }
|