fork download
  1. #include <arpa/inet.h>
  2. #include <netinet/in.h>
  3. #include <stdio.h>
  4. #include <string.h>
  5. #include <sys/types.h>
  6. #include <sys/socket.h>
  7. #include <unistd.h>
  8. #include <stdlib.h> // call to exit()
  9.  
  10. #define CONNECTED_PORT 29000
  11. #define EXAMPLE_REMOTE_IP "8.8.8.8"
  12. #define EXAMPLE_REMOTE_PORT 33222
  13.  
  14. void die(const char * msg) {
  15. perror(msg);
  16. exit(1);
  17. }
  18.  
  19. void grep(int port) {
  20. char command[64];
  21. char buffer[256];
  22.  
  23. sprintf(command, "lsof | grep -o 'UDP.*%d'", port);
  24. FILE * output = popen(command, "r");
  25. printf(" --> Output from lsof | grep -o 'UDP.*%d'\n", port);
  26. while (fgets(buffer, 256, output) != NULL)
  27. puts(buffer);
  28. pclose(output);
  29.  
  30. sprintf(command, "netstat -anu | grep %d", port);
  31. output = popen(command, "r");
  32. printf(" --> Output from netstat -anu | grep %d\n", port);
  33. while (fgets(buffer, 256, output) != NULL)
  34. puts(buffer);
  35. pclose(output);
  36. }
  37.  
  38. int main(void) {
  39. struct sockaddr_in co_sockaddr, remote_sockaddr;
  40. int co_s;
  41.  
  42. memset((char *) &co_sockaddr, 0, sizeof(co_sockaddr));
  43. memset((char *) &remote_sockaddr, 0, sizeof(remote_sockaddr));
  44.  
  45. puts("--> Creating socket");
  46. if ((co_s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1)
  47. die("connected socket()\n");
  48.  
  49. co_sockaddr.sin_family = AF_INET;
  50. co_sockaddr.sin_port = htons(CONNECTED_PORT);
  51. co_sockaddr.sin_addr.s_addr = htonl(INADDR_ANY);
  52.  
  53. printf("--> Binding socket to INADDR_ANY:%d (aka *:%d or 0.0.0.0:%d)\n", CONNECTED_PORT, CONNECTED_PORT, CONNECTED_PORT);
  54. if (bind(co_s, (struct sockaddr *) &co_sockaddr, sizeof(co_sockaddr)) == -1)
  55. die("connected bind()\n");
  56.  
  57. /* At this point, the socket is correctly shown as accepting on all interfaces,
  58. as shown by the following output of lsof and netstat */
  59.  
  60. grep(CONNECTED_PORT);
  61.  
  62. remote_sockaddr.sin_family = AF_INET;
  63. remote_sockaddr.sin_port = htons(EXAMPLE_REMOTE_PORT);
  64. if (inet_aton(EXAMPLE_REMOTE_IP, &remote_sockaddr.sin_addr) == 0)
  65. die("remote inet_aton()\n");
  66.  
  67. printf("--> Connecting socket to %s:%d (no data will be sent)\n", EXAMPLE_REMOTE_IP, EXAMPLE_REMOTE_PORT);
  68. if (connect(co_s, (struct sockaddr *) &remote_sockaddr, sizeof(remote_sockaddr)) == -1)
  69. die("connect()\n");
  70.  
  71. /* Here, the socket is not bound to * or 0.0.0.0 anymore, but to one of the interfaces only. Why?
  72. The manual pages for bind() or connect() do not provide any helpful information */
  73.  
  74. grep(CONNECTED_PORT);
  75.  
  76. close(co_s);
  77.  
  78. }
  79.  
Runtime error #stdin #stdout 0.01s 5312KB
stdin
Standard input is empty
stdout
--> Creating socket
--> Binding socket to INADDR_ANY:29000 (aka *:29000 or 0.0.0.0:29000)
  --> Output from lsof | grep -o 'UDP.*29000'
  --> Output from netstat -anu | grep 29000
--> Connecting socket to 8.8.8.8:33222 (no data will be sent)