Xenithz

Reverse_server

Oct 23rd, 2011
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.87 KB | None | 0 0
  1. /*
  2.    To Compile under Windows:
  3.      cl server.c /link ws2_32.lib
  4.  
  5.    To Compile under Linux or Cygwin:
  6.      make server
  7.  
  8.    To Compile under Sun:
  9.      gcc -lsocket -lnsl server
  10. */
  11.  
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14. #include <string.h>
  15. #include <stdarg.h>
  16.  
  17. #ifdef _WIN32
  18. #  include <winsock2.h>
  19. #else
  20. #  include <sys/types.h>
  21. #  include <sys/socket.h>
  22. #  include <netinet/in.h>
  23. #  include <unistd.h>
  24. #  define SOCKET int
  25. #endif
  26.  
  27. #define END_LINE '\n'
  28.  
  29. void logit(FILE *fp, char *client, char *message);
  30. int read_line(int fd, char *buf, int buf_size);
  31. void reverse(char *dest, const char *source);
  32. int write_client(int fd, const char *fmt, ...);
  33. void winsock_init();
  34.  
  35. int main(int argc, char *argv[]) {
  36.     SOCKET listenfd, sockfd;
  37.     int clientLen, serverPort;
  38.     char reversed_line[100];
  39.     struct sockaddr_in clientAddress, serverAddress;
  40.     char line[500];
  41.     FILE *fpLog = NULL;
  42.     int secret = 0xDEADC0DE;
  43.     int clientQuit = 0;
  44.  
  45.     if (argc != 2)
  46.     {
  47.         printf("Usage: %s <port>\n", argv[0]);
  48.         return 1;
  49.     }
  50. #ifdef _WIN32
  51.     winsock_init();
  52. #endif
  53.  
  54.     serverPort = atoi(argv[1]);
  55.     /* create socket */
  56.     listenfd = socket(PF_INET, SOCK_STREAM, 0);
  57.     if ( listenfd < 0 )
  58.     {
  59.         perror("socket error ");
  60.         return 1;
  61.     }
  62.  
  63.     /* bind server port */
  64.     memset(&serverAddress, 0, sizeof(serverAddress));
  65.     serverAddress.sin_family = AF_INET;
  66.     // serverAddress.sin_addr.s_addr = htonl(INADDR_ANY);
  67.     serverAddress.sin_addr.s_addr = inet_addr("127.0.0.1");
  68.     serverAddress.sin_port = htons(serverPort);
  69.  
  70.     if ( -1 == bind(listenfd, (struct sockaddr *) &serverAddress, sizeof(serverAddress)) )
  71.     {
  72.         perror("bind error");
  73.         return 1;
  74.     }
  75.  
  76.     if (-1 == listen(listenfd, 5))
  77.     {
  78.         perror("listen error");
  79.         return 1;
  80.     }
  81.  
  82.     fpLog = fopen("server.log", "w");
  83.     if (fpLog == NULL)
  84.     {
  85.         perror("couldn't open server.log for writing");
  86.         return 1;
  87.     }
  88.  
  89.     while ( ! clientQuit )
  90.     {
  91.         printf("%s: waiting for connection on TCP port %u\n\n", argv[0], serverPort);
  92.  
  93.         clientLen = sizeof(clientAddress);
  94.         sockfd = accept(listenfd, (struct sockaddr *) &clientAddress, &clientLen);
  95.         if ( -1 == sockfd )
  96.         {
  97.             perror("error accepting connection ");
  98.             return 1;
  99.         }
  100.  
  101.         write_client(sockfd, "Type QUIT on a line by itself to quit\n");
  102.  
  103.         /* init line */
  104.         memset(line, 0, sizeof(line));
  105.  
  106.         clientQuit = 0;
  107.         while ( !clientQuit &&  read_line(sockfd, line, sizeof(line)) != 0)
  108.         {
  109.             printf( "%s:%d %s\n",
  110.                     inet_ntoa(clientAddress.sin_addr),
  111.                     ntohs(clientAddress.sin_port),
  112.                     line);
  113.             if (0 == strncmp(line, "QUIT", 4))
  114.             {
  115.                 clientQuit = 1;
  116.                 write_client(sockfd, "Goodbye\n");
  117.                 close(sockfd);
  118.             }
  119.             else
  120.             {
  121.                 reverse(reversed_line, line);
  122.                 write_client(sockfd, reversed_line);
  123.                 write_client(sockfd, "\n");
  124.                 // logit(fpLog, inet_ntoa(clientAddress.sin_addr), line);
  125.             }
  126.             memset(line, 0, sizeof(line));
  127.         }
  128.     }
  129. #ifdef _WIN32
  130.     WSACleanup();
  131. #endif
  132. }
  133.  
  134. int write_client(int fd, const char *fmt, ...)
  135. {
  136.     va_list args;
  137.     char message[100];
  138.  
  139.     if (fmt == NULL)
  140.         return 0;
  141.     va_start(args, fmt);
  142. #ifdef _WIN32
  143.     vsprintf(message, fmt, args);
  144. #else
  145.     vsnprintf(message, sizeof(message), fmt, args);
  146. #endif
  147.     va_end(args);
  148.     return send(fd, message, strlen(message), 0);
  149. }
  150.  
  151. int read_line(int fd, char *buf, int buf_size)
  152. {
  153.     int nleft, nread, buf_len;
  154.  
  155.     nleft = buf_size;
  156.     while (nleft > 0)
  157.     {
  158.         if ((nread = recv(fd, buf, nleft, 0)) < 0)
  159.         {
  160.             return nread;
  161.         }
  162.         else if (nread == 0)
  163.         {
  164.             break;
  165.         }
  166.         nleft -= nread;
  167.         buf += nread;
  168.         if (*(buf - 1) == END_LINE)
  169.         {
  170.             break;
  171.         }
  172.     }
  173.     buf_len = buf_size - nleft;
  174.     if (buf_len > 0)
  175.     {
  176.         *(buf - 1) = '\0';
  177.     }
  178.  
  179.     return buf_len;
  180. }
  181.  
  182. void logit(FILE *fp, char *client, char *message)
  183. {
  184.     fprintf(fp, "%s", client);
  185.     fprintf(fp, message);
  186.     fprintf(fp, "\n");
  187.     fflush(fp);
  188. }
  189.  
  190. void reverse(char *dest, const char *source)
  191. {
  192.     int len, i;
  193.     len = strlen(source);
  194.     i = 0;
  195.     while (len > 0)
  196.     {
  197.         dest[i++] = source[--len];
  198.     }
  199.     dest[i] = '\0';
  200. }
  201.  
  202. #ifdef _WIN32
  203. void winsock_init()
  204. {
  205.     static WSADATA wsaData;    
  206.     int wsaret;
  207.  
  208.     if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)
  209.     {
  210.         fprintf(stderr, "Couldn't start winsock\n");
  211.         WSACleanup();
  212.         exit(1);
  213.     }
  214. }
  215. #endif
  216.  
  217.  
Advertisement
Add Comment
Please, Sign In to add comment