Guest User

Untitled

a guest
Feb 21st, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.54 KB | None | 0 0
  1. #define _GNU_SOURCE
  2.  
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <unistd.h>
  6. #include <errno.h>
  7. #include <string.h>
  8. #include <sys/types.h>
  9. #include <sys/socket.h>
  10. #include <netinet/in.h>
  11. #include <netdb.h>
  12. #include <arpa/inet.h>
  13. #include <sys/wait.h>
  14. #include <signal.h>
  15. #include <curl/curl.h>
  16. #include <fcntl.h>
  17.  
  18. #define PORT 31337
  19.  
  20. void sigchild(int s);
  21.  
  22. int main() {
  23.  
  24. // Interacted Variables
  25. int listensock;
  26. int newfd;
  27. struct sockaddr_in myaddr;
  28. struct sockaddr_in theiraddy;
  29.  
  30. fd_set readfds;
  31. fd_set masterfds;
  32. int maxfd;
  33.  
  34. int childpipe[2];
  35.  
  36. // Uninteracted values
  37. unsigned int sin_size;
  38. struct sigaction sa;
  39. int ret;
  40. int yes = 1;
  41.  
  42. // Temporary variables
  43. char *tmpbuf = malloc(20);
  44. int x;
  45.  
  46. listensock = socket(AF_INET, SOCK_STREAM, 0);
  47. if (listensock == -1) {
  48. perror("socket");
  49. exit(1);
  50. }
  51.  
  52. ret = setsockopt(listensock, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int));
  53. if (ret == -1) {
  54. perror("setsockopt");
  55. exit(1);
  56. }
  57.  
  58. memset(&myaddr, '\0', sizeof(struct sockaddr_in));
  59. myaddr.sin_family = AF_INET;
  60. myaddr.sin_port = htons(PORT);
  61. myaddr.sin_addr.s_addr = INADDR_ANY;
  62.  
  63. ret = bind(listensock, (struct sockaddr *)&myaddr, sizeof(struct sockaddr));
  64. if (ret == -1) {
  65. perror("bind");
  66. exit(1);
  67. }
  68.  
  69. ret = listen(listensock, 50);
  70. if (ret == -1) {
  71. perror("listen");
  72. exit(1);
  73. }
  74.  
  75. sa.sa_handler = sigchild;
  76. sigemptyset(&sa.sa_mask);
  77. sa.sa_flags = SA_RESTART;
  78.  
  79. ret = sigaction(SIGCHLD, &sa, NULL);
  80. if (ret == -1) {
  81. perror("sigaction");
  82. exit(1);
  83. }
  84.  
  85. FD_ZERO(&readfds);
  86. FD_ZERO(&masterfds);
  87. FD_SET(listensock, &masterfds);
  88. maxfd = listensock;
  89.  
  90. while(1) {
  91.  
  92. readfds = masterfds;
  93. sin_size = sizeof(struct sockaddr_in);
  94.  
  95. ret = select(maxfd+1, &readfds, NULL, NULL, NULL);
  96. if (ret == -1) {
  97. perror("select");
  98. printf("Error: %s\n", strerror(errno));
  99. exit(1);
  100. }
  101.  
  102. if (FD_ISSET(listensock, &readfds)) {
  103.  
  104. newfd = accept(listensock, (struct sockaddr *)&theiraddy, &sin_size);
  105.  
  106. if (newfd == -1) { // Weird error, just continue
  107. perror("accept");
  108. close(newfd);
  109. fprintf(stderr, "Server capped out, bad accept\n");
  110. continue;
  111. }
  112. else {
  113. printf("SERVER: New connection from %s\n", inet_ntoa(theiraddy.sin_addr));
  114. }
  115.  
  116. ret = pipe(childpipe);
  117. if (ret == -1) {
  118. perror("The pipe did not work, not sure what to do\n");
  119. exit(4);
  120. }
  121.  
  122. //FD_SET(childpipe[0], &masterfds);
  123. //if (childpipe[0] > maxfd)
  124. // maxfd = childpipe[0];
  125.  
  126. if(!fork()) {
  127. // This is the child process
  128. close(childpipe[1]); // The child will write to childpipe[0]
  129.  
  130. close(listensock);
  131. close(newfd);
  132. exit(0);
  133. }
  134. else {
  135. // This is the parent process
  136. close(childpipe[0]); // The parent will write to childpipe[1]
  137. close(newfd);
  138. }
  139. } // The IF of whether its the listensock
  140. else {
  141. for(x=0;x<maxfd;x++) {
  142. if (FD_ISSET(x, &readfds)) {
  143. read(x, tmpbuf, 10);
  144. printf("%s\n", tmpbuf);
  145. }
  146. }
  147. } // This means a child sent a message back to the client
  148.  
  149. }
  150.  
  151. }
  152.  
  153. void sigchild(int s) {
  154. while(wait(NULL) > 0);
  155. }
Add Comment
Please, Sign In to add comment