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.

85 lines
2.0 KiB

7 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 <stdio.h>
  9. #include <string.h>
  10. #include <unistd.h>
  11. #include <errno.h>
  12. #include <signal.h>
  13. #include <sys/types.h>
  14. #include <sys/socket.h>
  15. #include <sys/un.h>
  16. #include "codelength.h"
  17. #include "printresponse.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. if(0==strcmp(argv[i], "-s"))
  36. sockpath = argv[++i];
  37. }
  38. /* set signal handlers */
  39. memset(&sa, 0, sizeof(struct sigaction));
  40. sa.sa_handler = sighandler;
  41. if(sigaction(SIGINT, &sa, NULL)) {
  42. fprintf(stderr, "Failed to set SIGINT handler.\n");
  43. }
  44. sa.sa_handler = sighandler;
  45. if(sigaction(SIGTERM, &sa, NULL)) {
  46. fprintf(stderr, "Failed to set SIGTERM handler.\n");
  47. }
  48. s = socket(AF_UNIX, SOCK_STREAM, 0);
  49. addr.sun_family = AF_UNIX;
  50. strncpy(addr.sun_path, sockpath, sizeof(addr.sun_path));
  51. if(connect(s, (struct sockaddr *)&addr, sizeof(struct sockaddr_un)) < 0) {
  52. fprintf(stderr, "connecting to %s : ", addr.sun_path);
  53. perror("connect");
  54. return 1;
  55. }
  56. printf("connected to %s\n", addr.sun_path);
  57. n = write(s, command5, sizeof(command5)); /* NOTIF command */
  58. printf("%d bytes written\n", (int)n);
  59. while(!quitting) {
  60. n = read(s, buffer, sizeof(buffer));
  61. if(n < 0) {
  62. if(errno == EINTR) continue;
  63. perror("read");
  64. break;
  65. } else if(n == 0) {
  66. printf("Socket closed\n");
  67. break;
  68. }
  69. printf("%d bytes read\n", (int)n);
  70. printresponse(buffer, (int)n);
  71. }
  72. printf("Quit...\n");
  73. close(s);
  74. return 0;
  75. }