Advertisement
Guest User

Untitled

a guest
Jul 1st, 2015
236
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.25 KB | None | 0 0
  1. /*
  2. ** commandserver.c -- Server using select for ServerGod game
  3. */
  4.  
  5. // Header information(needs to be moved to header file)
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include <sys/socket.h>
  10. #include <netinet/in.h>
  11. #include <sys/types.h>
  12. #include <arpa/inet.h>
  13. #include <unistd.h>
  14. #include <errno.h>
  15.  
  16. #define PORT 50505
  17. #define DATAMAX 512
  18. #define MAXCONNECTIONS 10
  19. #define READBUFFER 128
  20.  
  21. int make_socket (uint16_t port);
  22. void *get_in_addr(struct sockaddr *sa);
  23.  
  24. // Obvious main is obvious
  25. int main(void)
  26. {
  27. // File descriptor sets. One master and one for basic handling.
  28. // fdmax holds the total amount of file descriptors to handle.
  29. fd_set master;
  30. fd_set readfds;
  31. int fdmax;
  32.  
  33. // Socket variables: Socket; Socket Structure; Socket size
  34. int listener;
  35. int newfd;
  36. struct sockaddr_storage remoteaddr;
  37. socklen_t addrlen;
  38. char remoteIP[INET_ADDRSTRLEN];
  39.  
  40. // File and pipe pointers
  41. FILE *pipe_fp;
  42. char readbuf[READBUFFER];
  43.  
  44. // Flags, buffers, and other basic variables
  45. int i;
  46. char data[DATAMAX];
  47. int nbytes;
  48.  
  49. // Initialize the sets
  50. FD_ZERO(&master);
  51. FD_ZERO(&readfds);
  52.  
  53. // Start the socket and bind it.
  54. listener = make_socket(PORT);
  55. if(listen(listener, MAXCONNECTIONS) == -1)
  56. {
  57. perror("listen");
  58. return 1;
  59. }
  60.  
  61. // Add the listener to the master set.
  62. FD_SET(listener, &master);
  63.  
  64. // Listener is the biggest file descriptor so far.
  65. fdmax = listener;
  66.  
  67. // Main loop
  68. for(;;)
  69. {
  70. readfds = master; // Copy the master set for use
  71. if(select(fdmax+1, &readfds, NULL, NULL, NULL) == -1)
  72. {
  73. perror("select");
  74. return 2;
  75. }
  76.  
  77. for(i = 0; i <= fdmax; i++)
  78. {
  79. if(FD_ISSET(i, &readfds)) // Have a connection
  80. {
  81. if(i == listener) // Handle a new client
  82. {
  83. addrlen = sizeof(remoteaddr);
  84. newfd = accept(listener, (struct sockaddr *)&remoteaddr, &addrlen);
  85. if(newfd == -1)
  86. {
  87. perror("accept");
  88. return 3;
  89. }
  90. else
  91. {
  92. // Add the new connection to the set, and set the new max
  93. FD_SET(newfd, &master);
  94. if(newfd > fdmax)
  95. {
  96. fdmax = newfd;
  97. }
  98. printf("New connection from %s on socket %d.\n",
  99. inet_ntop(remoteaddr.ss_family,
  100. get_in_addr((struct sockaddr *)&remoteaddr), remoteIP,
  101. INET_ADDRSTRLEN), newfd);
  102.  
  103. }
  104. }
  105. else // Handle data from known client
  106. {
  107. bzero(data, DATAMAX);
  108. if((nbytes = recv(i, data, sizeof(data), 0)) <= 0)
  109. {
  110. if(nbytes == 0) // Error or disconnect
  111. {
  112. printf("Socket %d hung up.\n", i);
  113. }
  114. else
  115. {
  116. perror("recv");
  117. }
  118. close(i); // See ya (cockbite).
  119. FD_CLR(i, &master);
  120. }
  121. else // We got data from the client, now do something with it
  122. {
  123. // Get rid of any possible newline characters that made it's way in.
  124. strtok(data, "\n"); strtok(data, "\r");
  125.  
  126. if((pipe_fp = popen(data, "r")) == NULL)
  127. {
  128. perror("popen");
  129. return 4;
  130. }
  131.  
  132. // Sending the output to the client
  133. while(fgets(readbuf, READBUFFER, pipe_fp))
  134. {
  135. if(send(i, readbuf, READBUFFER, 0) == -1)
  136. {
  137. perror("send");
  138. return 5;
  139. }
  140. bzero(readbuf, READBUFFER);
  141. }
  142.  
  143. pclose(pipe_fp);
  144. }
  145. }
  146. }
  147. }
  148. }
  149.  
  150. return 0;
  151. }
  152.  
  153. // Socket creation and binding function taken from GNU exapmles
  154. int make_socket (uint16_t port)
  155. {
  156. int sock;
  157. struct sockaddr_in name;
  158.  
  159. /* Create the socket. */
  160. sock = socket (PF_INET, SOCK_STREAM, 0);
  161. if (sock < 0)
  162. {
  163. perror ("socket");
  164. exit (EXIT_FAILURE);
  165. }
  166.  
  167. /* Give the socket a name. */
  168. name.sin_family = AF_INET;
  169. name.sin_port = htons (port);
  170. name.sin_addr.s_addr = htonl (INADDR_ANY);
  171. if (bind (sock, (struct sockaddr *) &name, sizeof (name)) < 0)
  172. {
  173. perror ("bind");
  174. exit (EXIT_FAILURE);
  175. }
  176.  
  177. return sock;
  178. }
  179.  
  180. // Get the address from a struct
  181. void *get_in_addr(struct sockaddr *sa)
  182. {
  183. return &(((struct sockaddr_in *)sa)->sin_addr);
  184. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement