nsctl.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. /* l2tpns plugin control */
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <unistd.h>
  5. #include <errno.h>
  6. #include <string.h>
  7. #include <netdb.h>
  8. #include <signal.h>
  9. #include "dhcp6.h"
  10. #include "l2tpns.h"
  11. #include "control.h"
  12. struct {
  13. char *command;
  14. char *usage;
  15. int action;
  16. } builtins[] = {
  17. { "load_plugin", " PLUGIN Load named plugin", NSCTL_REQ_LOAD },
  18. { "unload_plugin", " PLUGIN Unload named plugin", NSCTL_REQ_UNLOAD },
  19. { "help", " List available commands", NSCTL_REQ_HELP },
  20. { 0 }
  21. };
  22. static int debug = 0;
  23. static int timeout = 2; // 2 seconds
  24. static char *me;
  25. #define USAGE() fprintf(stderr, "Usage: %s [-d] [-h HOST[:PORT]] [-t TIMEOUT] COMMAND [ARG ...]\n", me)
  26. static struct nsctl *request(char *host, int port, int type, int argc, char *argv[]);
  27. int main(int argc, char *argv[])
  28. {
  29. int req_type = 0;
  30. char *host = 0;
  31. int port;
  32. int i;
  33. char *p;
  34. struct nsctl *res;
  35. if ((p = strrchr((me = argv[0]), '/')))
  36. me = p + 1;
  37. opterr = 0;
  38. while ((i = getopt(argc, argv, "dh:t:")) != -1)
  39. switch (i)
  40. {
  41. case 'd':
  42. debug++;
  43. break;
  44. case 'h':
  45. host = optarg;
  46. break;
  47. case 't':
  48. timeout = atoi(optarg);
  49. break;
  50. default:
  51. USAGE();
  52. return EXIT_FAILURE;
  53. }
  54. argc -= optind;
  55. argv += optind;
  56. if (argc < 1 || !argv[0][0])
  57. {
  58. USAGE();
  59. return EXIT_FAILURE;
  60. }
  61. if (!host)
  62. host = "127.0.0.1";
  63. if ((p = strchr(host, ':')))
  64. {
  65. port = atoi(p + 1);
  66. if (!port)
  67. {
  68. fprintf(stderr, "%s: invalid port `%s'\n", me, p + 1);
  69. return EXIT_FAILURE;
  70. }
  71. *p = 0;
  72. }
  73. else
  74. {
  75. port = NSCTL_PORT;
  76. }
  77. for (i = 0; !req_type && builtins[i].command; i++)
  78. if (!strcmp(argv[0], builtins[i].command))
  79. req_type = builtins[i].action;
  80. if (req_type == NSCTL_REQ_HELP)
  81. {
  82. printf("Available commands:\n");
  83. for (i = 0; builtins[i].command; i++)
  84. printf(" %s%s\n", builtins[i].command, builtins[i].usage);
  85. }
  86. if (req_type)
  87. {
  88. argc--;
  89. argv++;
  90. }
  91. else
  92. {
  93. req_type = NSCTL_REQ_CONTROL;
  94. }
  95. if ((res = request(host, port, req_type, argc, argv)))
  96. {
  97. FILE *stream = stderr;
  98. int status = EXIT_FAILURE;
  99. if (res->type == NSCTL_RES_OK)
  100. {
  101. stream = stdout;
  102. status = EXIT_SUCCESS;
  103. }
  104. for (i = 0; i < res->argc; i++)
  105. fprintf(stream, "%s\n", res->argv[i]);
  106. return status;
  107. }
  108. return EXIT_FAILURE;
  109. }
  110. static void sigalrm_handler(int sig) { }
  111. static struct nsctl *request(char *host, int port, int type, int argc, char *argv[])
  112. {
  113. static struct nsctl res;
  114. struct sockaddr_in peer;
  115. socklen_t len = sizeof(peer);
  116. struct hostent *h = gethostbyname(host);
  117. int fd;
  118. uint8_t buf[NSCTL_MAX_PKT_SZ];
  119. int sz;
  120. char *err;
  121. if (!h || h->h_addrtype != AF_INET)
  122. {
  123. fprintf(stderr, "%s: invalid host `%s'\n", me, host);
  124. return 0;
  125. }
  126. if ((fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0)
  127. {
  128. fprintf(stderr, "%s: can't create udp socket (%s)\n", me, strerror(errno));
  129. return 0;
  130. }
  131. memset(&peer, 0, len);
  132. peer.sin_family = AF_INET;
  133. peer.sin_port = htons(port);
  134. memcpy(&peer.sin_addr.s_addr, h->h_addr, sizeof(peer.sin_addr.s_addr));
  135. if (connect(fd, (struct sockaddr *) &peer, sizeof(peer)) < 0)
  136. {
  137. fprintf(stderr, "%s: udp connect failed (%s)\n", me, strerror(errno));
  138. return 0;
  139. }
  140. if ((sz = pack_control(buf, sizeof(buf), type, argc, argv)) < 0)
  141. {
  142. fprintf(stderr, "%s: error packing request\n", me);
  143. return 0;
  144. }
  145. if (debug)
  146. {
  147. struct nsctl req;
  148. if (unpack_control(&req, buf, sz) == type)
  149. {
  150. fprintf(stderr, "Sending ");
  151. dump_control(&req, stderr);
  152. }
  153. }
  154. if (send(fd, buf, sz, 0) < 0)
  155. {
  156. fprintf(stderr, "%s: error sending request (%s)\n", me, strerror(errno));
  157. return 0;
  158. }
  159. /* set timer */
  160. if (timeout)
  161. {
  162. struct sigaction alrm;
  163. alrm.sa_handler = sigalrm_handler;
  164. sigemptyset(&alrm.sa_mask);
  165. alrm.sa_flags = 0;
  166. sigaction(SIGALRM, &alrm, 0);
  167. alarm(timeout);
  168. }
  169. sz = recv(fd, buf, sizeof(buf), 0);
  170. alarm(0);
  171. if (sz < 0)
  172. {
  173. fprintf(stderr, "%s: error receiving response (%s)\n", me,
  174. errno == EINTR ? "timed out" : strerror(errno));
  175. return 0;
  176. }
  177. if ((type = unpack_control(&res, buf, sz)) > 0 && type & NSCTL_RESPONSE)
  178. {
  179. if (debug)
  180. {
  181. fprintf(stderr, "Received ");
  182. dump_control(&res, stderr);
  183. }
  184. return &res;
  185. }
  186. err = "unknown error";
  187. switch (type)
  188. {
  189. case NSCTL_ERR_SHORT: err = "short packet"; break;
  190. case NSCTL_ERR_LONG: err = "extra data"; break;
  191. case NSCTL_ERR_MAGIC: err = "bad magic"; break;
  192. case NSCTL_ERR_TYPE: err = "invalid type"; break;
  193. }
  194. fprintf(stderr, "%s: %s\n", me, err);
  195. return 0;
  196. }