You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

97 lines
2.1 KiB

8 years ago
​
​
​
​
​
​
​
​
​
8 years ago
​
​
8 years ago
8 years ago
8 years ago
​
​
​
​
8 years ago
​
​
8 years ago
​
8 years ago
​
​
​
​
8 years ago
​
​
​
8 years ago
8 years ago
​
​
​
​
​
8 years ago
​
​
​
8 years ago
​
​
8 years ago
​
​
8 years ago
​
​
​
​
​
8 years ago
​
8 years ago
​
​
​
​
​
8 years ago
​
​
​
8 years ago
​
​
​
8 years ago
​
​
​
​
  1. /* $Id: showminissdpdnotif.c,v 1.2 2016/01/19 10:25:24 nanard Exp $ */
  2. /* vim: shiftwidth=4 tabstop=4 noexpandtab
  3. * MiniUPnP project
  4. * (c) 2016 Thomas Bernard
  5. * website : http://miniupnp.free.fr/ or http://miniupnp.tuxfamily.org/
  6. * This software is subject to the conditions detailed
  7. * in the LICENCE file provided within the distribution */
  8. #include "codelength.h"
  9. #include "printresponse.h"
  10. #include <errno.h>
  11. #include <signal.h>
  12. #include <stdio.h>
  13. #include <string.h>
  14. #include <sys/socket.h>
  15. #include <sys/types.h>
  16. #include <sys/un.h>
  17. #include <unistd.h>
  18. static volatile sig_atomic_t quitting = 0;
  19. static void sighandler(int sig)
  20. {
  21. (void) sig;
  22. quitting = 1;
  23. }
  24. int main(int argc, char ** argv)
  25. {
  26. int i;
  27. int s;
  28. struct sockaddr_un addr;
  29. const char * sockpath = "/var/run/minissdpd.sock";
  30. unsigned char buffer[4096];
  31. ssize_t n;
  32. const char command5[] = {0x05, 0x00};
  33. struct sigaction sa;
  34. for (i = 0; i < argc - 1; i++)
  35. {
  36. if (0 == strcmp(argv[i], "-s"))
  37. {
  38. sockpath = argv[++i];
  39. }
  40. }
  41. /* set signal handlers */
  42. memset(&sa, 0, sizeof(struct sigaction));
  43. sa.sa_handler = sighandler;
  44. if (sigaction(SIGINT, &sa, NULL))
  45. {
  46. fprintf(stderr, "Failed to set SIGINT handler.\n");
  47. }
  48. sa.sa_handler = sighandler;
  49. if (sigaction(SIGTERM, &sa, NULL))
  50. {
  51. fprintf(stderr, "Failed to set SIGTERM handler.\n");
  52. }
  53. s = socket(AF_UNIX, SOCK_STREAM, 0);
  54. addr.sun_family = AF_UNIX;
  55. strncpy(addr.sun_path, sockpath, sizeof(addr.sun_path));
  56. if (connect(s, (struct sockaddr *) &addr, sizeof(struct sockaddr_un)) < 0)
  57. {
  58. fprintf(stderr, "connecting to %s : ", addr.sun_path);
  59. perror("connect");
  60. return 1;
  61. }
  62. printf("connected to %s\n", addr.sun_path);
  63. n = write(s, command5, sizeof(command5)); /* NOTIF command */
  64. printf("%d bytes written\n", (int) n);
  65. while (! quitting)
  66. {
  67. n = read(s, buffer, sizeof(buffer));
  68. if (n < 0)
  69. {
  70. if (errno == EINTR)
  71. {
  72. continue;
  73. }
  74. perror("read");
  75. break;
  76. }
  77. else if (n == 0)
  78. {
  79. printf("Socket closed\n");
  80. break;
  81. }
  82. printf("%d bytes read\n", (int) n);
  83. printresponse(buffer, (int) n);
  84. }
  85. printf("Quit...\n");
  86. close(s);
  87. return 0;
  88. }