rtpw.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686
  1. /*
  2. * rtpw.c
  3. *
  4. * rtp word sender/receiver
  5. *
  6. * David A. McGrew
  7. * Cisco Systems, Inc.
  8. *
  9. * This app is a simple RTP application intended only for testing
  10. * libsrtp. It reads one word at a time from /usr/dict/words (or
  11. * whatever file is specified as DICT_FILE), and sends one word out
  12. * each USEC_RATE microseconds. Secure RTP protections can be
  13. * applied. See the usage() function for more details.
  14. *
  15. */
  16. /*
  17. *
  18. * Copyright (c) 2001-2006, Cisco Systems, Inc.
  19. * All rights reserved.
  20. *
  21. * Redistribution and use in source and binary forms, with or without
  22. * modification, are permitted provided that the following conditions
  23. * are met:
  24. *
  25. * Redistributions of source code must retain the above copyright
  26. * notice, this list of conditions and the following disclaimer.
  27. *
  28. * Redistributions in binary form must reproduce the above
  29. * copyright notice, this list of conditions and the following
  30. * disclaimer in the documentation and/or other materials provided
  31. * with the distribution.
  32. *
  33. * Neither the name of the Cisco Systems, Inc. nor the names of its
  34. * contributors may be used to endorse or promote products derived
  35. * from this software without specific prior written permission.
  36. *
  37. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  38. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  39. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  40. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  41. * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  42. * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  43. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  44. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  45. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  46. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  47. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  48. * OF THE POSSIBILITY OF SUCH DAMAGE.
  49. *
  50. */
  51. #include "datatypes.h"
  52. #include "getopt_s.h" /* for local getopt() */
  53. #include <stdio.h> /* for printf, fprintf */
  54. #include <stdlib.h> /* for atoi() */
  55. #include <errno.h>
  56. #include <signal.h> /* for signal() */
  57. #include <string.h> /* for strncpy() */
  58. #include <time.h> /* for usleep() */
  59. #ifdef HAVE_UNISTD_H
  60. #include <unistd.h> /* for close() */
  61. #endif
  62. #ifdef HAVE_SYS_SOCKET_H
  63. # include <sys/socket.h>
  64. #endif
  65. #ifdef HAVE_NETINET_IN_H
  66. # include <netinet/in.h>
  67. #elif defined HAVE_WINSOCK2_H
  68. # include <winsock2.h>
  69. # include <ws2tcpip.h>
  70. # define RTPW_USE_WINSOCK2 1
  71. #endif
  72. #ifdef HAVE_ARPA_INET_H
  73. # include <arpa/inet.h>
  74. #endif
  75. #include "srtp.h"
  76. #include "rtp.h"
  77. #ifdef RTPW_USE_WINSOCK2
  78. # define DICT_FILE "words.txt"
  79. #else
  80. # define DICT_FILE "/usr/share/dict/words"
  81. #endif
  82. #define USEC_RATE (5e5)
  83. #define MAX_WORD_LEN 128
  84. #define ADDR_IS_MULTICAST(a) IN_MULTICAST(htonl(a))
  85. #define MAX_KEY_LEN 96
  86. #ifndef HAVE_USLEEP
  87. # ifdef HAVE_WINDOWS_H
  88. # define usleep(us) Sleep((us)/1000)
  89. # else
  90. # define usleep(us) sleep((us)/1000000)
  91. # endif
  92. #endif
  93. /*
  94. * the function usage() prints an error message describing how this
  95. * program should be called, then calls exit()
  96. */
  97. void
  98. usage(char *prog_name);
  99. /*
  100. * leave_group(...) de-registers from a multicast group
  101. */
  102. void
  103. leave_group(int sock, struct ip_mreq mreq, char *name);
  104. /*
  105. * setup_signal_handler() sets up a signal handler to trigger
  106. * cleanups after an interrupt
  107. */
  108. int setup_signal_handler(char* name);
  109. /*
  110. * handle_signal(...) handles interrupt signal to trigger cleanups
  111. */
  112. volatile int interrupted = 0;
  113. /*
  114. * program_type distinguishes the [s]rtp sender and receiver cases
  115. */
  116. typedef enum { sender, receiver, unknown } program_type;
  117. int
  118. main (int argc, char *argv[]) {
  119. char *dictfile = DICT_FILE;
  120. FILE *dict;
  121. char word[MAX_WORD_LEN];
  122. int sock, ret;
  123. struct in_addr rcvr_addr;
  124. struct sockaddr_in name;
  125. struct ip_mreq mreq;
  126. #if BEW
  127. struct sockaddr_in local;
  128. #endif
  129. program_type prog_type = unknown;
  130. sec_serv_t sec_servs = sec_serv_none;
  131. unsigned char ttl = 5;
  132. int c;
  133. int key_size = 128;
  134. int tag_size = 8;
  135. int gcm_on = 0;
  136. char *input_key = NULL;
  137. char *address = NULL;
  138. char key[MAX_KEY_LEN];
  139. unsigned short port = 0;
  140. rtp_sender_t snd;
  141. srtp_policy_t policy;
  142. err_status_t status;
  143. int len;
  144. int do_list_mods = 0;
  145. uint32_t ssrc = 0xdeadbeef; /* ssrc value hardcoded for now */
  146. #ifdef RTPW_USE_WINSOCK2
  147. WORD wVersionRequested = MAKEWORD(2, 0);
  148. WSADATA wsaData;
  149. ret = WSAStartup(wVersionRequested, &wsaData);
  150. if (ret != 0) {
  151. fprintf(stderr, "error: WSAStartup() failed: %d\n", ret);
  152. exit(1);
  153. }
  154. #endif
  155. if (setup_signal_handler(argv[0]) != 0) {
  156. exit(1);
  157. }
  158. /* initialize srtp library */
  159. status = srtp_init();
  160. if (status) {
  161. printf("error: srtp initialization failed with error code %d\n", status);
  162. exit(1);
  163. }
  164. /* check args */
  165. while (1) {
  166. c = getopt_s(argc, argv, "k:rsgt:ae:ld:");
  167. if (c == -1) {
  168. break;
  169. }
  170. switch (c) {
  171. case 'k':
  172. input_key = optarg_s;
  173. break;
  174. case 'e':
  175. key_size = atoi(optarg_s);
  176. if (key_size != 128 && key_size != 256) {
  177. printf("error: encryption key size must be 128 or 256 (%d)\n", key_size);
  178. exit(1);
  179. }
  180. sec_servs |= sec_serv_conf;
  181. break;
  182. case 't':
  183. tag_size = atoi(optarg_s);
  184. if (tag_size != 8 && tag_size != 16) {
  185. printf("error: GCM tag size must be 8 or 16 (%d)\n", tag_size);
  186. exit(1);
  187. }
  188. break;
  189. case 'a':
  190. sec_servs |= sec_serv_auth;
  191. break;
  192. case 'g':
  193. gcm_on = 1;
  194. sec_servs |= sec_serv_auth;
  195. break;
  196. case 'r':
  197. prog_type = receiver;
  198. break;
  199. case 's':
  200. prog_type = sender;
  201. break;
  202. case 'd':
  203. status = crypto_kernel_set_debug_module(optarg_s, 1);
  204. if (status) {
  205. printf("error: set debug module (%s) failed\n", optarg_s);
  206. exit(1);
  207. }
  208. break;
  209. case 'l':
  210. do_list_mods = 1;
  211. break;
  212. default:
  213. usage(argv[0]);
  214. }
  215. }
  216. if (prog_type == unknown) {
  217. if (do_list_mods) {
  218. status = crypto_kernel_list_debug_modules();
  219. if (status) {
  220. printf("error: list of debug modules failed\n");
  221. exit(1);
  222. }
  223. return 0;
  224. } else {
  225. printf("error: neither sender [-s] nor receiver [-r] specified\n");
  226. usage(argv[0]);
  227. }
  228. }
  229. if ((sec_servs && !input_key) || (!sec_servs && input_key)) {
  230. /*
  231. * a key must be provided if and only if security services have
  232. * been requested
  233. */
  234. usage(argv[0]);
  235. }
  236. if (argc != optind_s + 2) {
  237. /* wrong number of arguments */
  238. usage(argv[0]);
  239. }
  240. /* get address from arg */
  241. address = argv[optind_s++];
  242. /* get port from arg */
  243. port = atoi(argv[optind_s++]);
  244. /* set address */
  245. #ifdef HAVE_INET_ATON
  246. if (0 == inet_aton(address, &rcvr_addr)) {
  247. fprintf(stderr, "%s: cannot parse IP v4 address %s\n", argv[0], address);
  248. exit(1);
  249. }
  250. if (rcvr_addr.s_addr == INADDR_NONE) {
  251. fprintf(stderr, "%s: address error", argv[0]);
  252. exit(1);
  253. }
  254. #else
  255. rcvr_addr.s_addr = inet_addr(address);
  256. if (0xffffffff == rcvr_addr.s_addr) {
  257. fprintf(stderr, "%s: cannot parse IP v4 address %s\n", argv[0], address);
  258. exit(1);
  259. }
  260. #endif
  261. /* open socket */
  262. sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
  263. if (sock < 0) {
  264. int err;
  265. #ifdef RTPW_USE_WINSOCK2
  266. err = WSAGetLastError();
  267. #else
  268. err = errno;
  269. #endif
  270. fprintf(stderr, "%s: couldn't open socket: %d\n", argv[0], err);
  271. exit(1);
  272. }
  273. name.sin_addr = rcvr_addr;
  274. name.sin_family = PF_INET;
  275. name.sin_port = htons(port);
  276. if (ADDR_IS_MULTICAST(rcvr_addr.s_addr)) {
  277. if (prog_type == sender) {
  278. ret = setsockopt(sock, IPPROTO_IP, IP_MULTICAST_TTL, &ttl,
  279. sizeof(ttl));
  280. if (ret < 0) {
  281. fprintf(stderr, "%s: Failed to set TTL for multicast group", argv[0]);
  282. perror("");
  283. exit(1);
  284. }
  285. }
  286. mreq.imr_multiaddr.s_addr = rcvr_addr.s_addr;
  287. mreq.imr_interface.s_addr = htonl(INADDR_ANY);
  288. ret = setsockopt(sock, IPPROTO_IP, IP_ADD_MEMBERSHIP, (void*)&mreq,
  289. sizeof(mreq));
  290. if (ret < 0) {
  291. fprintf(stderr, "%s: Failed to join multicast group", argv[0]);
  292. perror("");
  293. exit(1);
  294. }
  295. }
  296. /* report security services selected on the command line */
  297. printf("security services: ");
  298. if (sec_servs & sec_serv_conf)
  299. printf("confidentiality ");
  300. if (sec_servs & sec_serv_auth)
  301. printf("message authentication");
  302. if (sec_servs == sec_serv_none)
  303. printf("none");
  304. printf("\n");
  305. /* set up the srtp policy and master key */
  306. if (sec_servs) {
  307. /*
  308. * create policy structure, using the default mechanisms but
  309. * with only the security services requested on the command line,
  310. * using the right SSRC value
  311. */
  312. switch (sec_servs) {
  313. case sec_serv_conf_and_auth:
  314. if (gcm_on) {
  315. #ifdef OPENSSL
  316. switch (key_size) {
  317. case 128:
  318. crypto_policy_set_aes_gcm_128_8_auth(&policy.rtp);
  319. crypto_policy_set_aes_gcm_128_8_auth(&policy.rtcp);
  320. break;
  321. case 256:
  322. crypto_policy_set_aes_gcm_256_8_auth(&policy.rtp);
  323. crypto_policy_set_aes_gcm_256_8_auth(&policy.rtcp);
  324. break;
  325. }
  326. #else
  327. printf("error: GCM mode only supported when using the OpenSSL crypto engine.\n");
  328. return 0;
  329. #endif
  330. } else {
  331. switch (key_size) {
  332. case 128:
  333. crypto_policy_set_rtp_default(&policy.rtp);
  334. crypto_policy_set_rtcp_default(&policy.rtcp);
  335. break;
  336. case 256:
  337. crypto_policy_set_aes_cm_256_hmac_sha1_80(&policy.rtp);
  338. crypto_policy_set_rtcp_default(&policy.rtcp);
  339. break;
  340. }
  341. }
  342. break;
  343. case sec_serv_conf:
  344. if (gcm_on) {
  345. printf("error: GCM mode must always be used with auth enabled\n");
  346. return -1;
  347. } else {
  348. switch (key_size) {
  349. case 128:
  350. crypto_policy_set_aes_cm_128_null_auth(&policy.rtp);
  351. crypto_policy_set_rtcp_default(&policy.rtcp);
  352. break;
  353. case 256:
  354. crypto_policy_set_aes_cm_256_null_auth(&policy.rtp);
  355. crypto_policy_set_rtcp_default(&policy.rtcp);
  356. break;
  357. }
  358. }
  359. break;
  360. case sec_serv_auth:
  361. if (gcm_on) {
  362. #ifdef OPENSSL
  363. switch (key_size) {
  364. case 128:
  365. crypto_policy_set_aes_gcm_128_8_only_auth(&policy.rtp);
  366. crypto_policy_set_aes_gcm_128_8_only_auth(&policy.rtcp);
  367. break;
  368. case 256:
  369. crypto_policy_set_aes_gcm_256_8_only_auth(&policy.rtp);
  370. crypto_policy_set_aes_gcm_256_8_only_auth(&policy.rtcp);
  371. break;
  372. }
  373. #else
  374. printf("error: GCM mode only supported when using the OpenSSL crypto engine.\n");
  375. return 0;
  376. #endif
  377. } else {
  378. crypto_policy_set_null_cipher_hmac_sha1_80(&policy.rtp);
  379. crypto_policy_set_rtcp_default(&policy.rtcp);
  380. }
  381. break;
  382. default:
  383. printf("error: unknown security service requested\n");
  384. return -1;
  385. }
  386. policy.ssrc.type = ssrc_specific;
  387. policy.ssrc.value = ssrc;
  388. policy.key = (uint8_t *) key;
  389. policy.ekt = NULL;
  390. policy.next = NULL;
  391. policy.window_size = 128;
  392. policy.allow_repeat_tx = 0;
  393. policy.rtp.sec_serv = sec_servs;
  394. policy.rtcp.sec_serv = sec_serv_none; /* we don't do RTCP anyway */
  395. if (gcm_on && tag_size != 8) {
  396. policy.rtp.auth_tag_len = tag_size;
  397. }
  398. /*
  399. * read key from hexadecimal on command line into an octet string
  400. */
  401. len = hex_string_to_octet_string(key, input_key, policy.rtp.cipher_key_len*2);
  402. /* check that hex string is the right length */
  403. if (len < policy.rtp.cipher_key_len*2) {
  404. fprintf(stderr,
  405. "error: too few digits in key/salt "
  406. "(should be %d hexadecimal digits, found %d)\n",
  407. policy.rtp.cipher_key_len*2, len);
  408. exit(1);
  409. }
  410. if (strlen(input_key) > policy.rtp.cipher_key_len*2) {
  411. fprintf(stderr,
  412. "error: too many digits in key/salt "
  413. "(should be %d hexadecimal digits, found %u)\n",
  414. policy.rtp.cipher_key_len*2, (unsigned)strlen(input_key));
  415. exit(1);
  416. }
  417. printf("set master key/salt to %s/", octet_string_hex_string(key, 16));
  418. printf("%s\n", octet_string_hex_string(key+16, 14));
  419. } else {
  420. /*
  421. * we're not providing security services, so set the policy to the
  422. * null policy
  423. *
  424. * Note that this policy does not conform to the SRTP
  425. * specification, since RTCP authentication is required. However,
  426. * the effect of this policy is to turn off SRTP, so that this
  427. * application is now a vanilla-flavored RTP application.
  428. */
  429. policy.key = (uint8_t *)key;
  430. policy.ssrc.type = ssrc_specific;
  431. policy.ssrc.value = ssrc;
  432. policy.rtp.cipher_type = NULL_CIPHER;
  433. policy.rtp.cipher_key_len = 0;
  434. policy.rtp.auth_type = NULL_AUTH;
  435. policy.rtp.auth_key_len = 0;
  436. policy.rtp.auth_tag_len = 0;
  437. policy.rtp.sec_serv = sec_serv_none;
  438. policy.rtcp.cipher_type = NULL_CIPHER;
  439. policy.rtcp.cipher_key_len = 0;
  440. policy.rtcp.auth_type = NULL_AUTH;
  441. policy.rtcp.auth_key_len = 0;
  442. policy.rtcp.auth_tag_len = 0;
  443. policy.rtcp.sec_serv = sec_serv_none;
  444. policy.window_size = 0;
  445. policy.allow_repeat_tx = 0;
  446. policy.ekt = NULL;
  447. policy.next = NULL;
  448. }
  449. if (prog_type == sender) {
  450. #if BEW
  451. /* bind to local socket (to match crypto policy, if need be) */
  452. memset(&local, 0, sizeof(struct sockaddr_in));
  453. local.sin_addr.s_addr = htonl(INADDR_ANY);
  454. local.sin_port = htons(port);
  455. ret = bind(sock, (struct sockaddr *) &local, sizeof(struct sockaddr_in));
  456. if (ret < 0) {
  457. fprintf(stderr, "%s: bind failed\n", argv[0]);
  458. perror("");
  459. exit(1);
  460. }
  461. #endif /* BEW */
  462. /* initialize sender's rtp and srtp contexts */
  463. snd = rtp_sender_alloc();
  464. if (snd == NULL) {
  465. fprintf(stderr, "error: malloc() failed\n");
  466. exit(1);
  467. }
  468. rtp_sender_init(snd, sock, name, ssrc);
  469. status = rtp_sender_init_srtp(snd, &policy);
  470. if (status) {
  471. fprintf(stderr,
  472. "error: srtp_create() failed with code %d\n",
  473. status);
  474. exit(1);
  475. }
  476. /* open dictionary */
  477. dict = fopen (dictfile, "r");
  478. if (dict == NULL) {
  479. fprintf(stderr, "%s: couldn't open file %s\n", argv[0], dictfile);
  480. if (ADDR_IS_MULTICAST(rcvr_addr.s_addr)) {
  481. leave_group(sock, mreq, argv[0]);
  482. }
  483. exit(1);
  484. }
  485. /* read words from dictionary, then send them off */
  486. while (!interrupted && fgets(word, MAX_WORD_LEN, dict) != NULL) {
  487. len = strlen(word) + 1; /* plus one for null */
  488. if (len > MAX_WORD_LEN)
  489. printf("error: word %s too large to send\n", word);
  490. else {
  491. rtp_sendto(snd, word, len);
  492. printf("sending word: %s", word);
  493. }
  494. usleep(USEC_RATE);
  495. }
  496. rtp_sender_deinit_srtp(snd);
  497. rtp_sender_dealloc(snd);
  498. fclose(dict);
  499. } else { /* prog_type == receiver */
  500. rtp_receiver_t rcvr;
  501. if (bind(sock, (struct sockaddr *)&name, sizeof(name)) < 0) {
  502. close(sock);
  503. fprintf(stderr, "%s: socket bind error\n", argv[0]);
  504. perror(NULL);
  505. if (ADDR_IS_MULTICAST(rcvr_addr.s_addr)) {
  506. leave_group(sock, mreq, argv[0]);
  507. }
  508. exit(1);
  509. }
  510. rcvr = rtp_receiver_alloc();
  511. if (rcvr == NULL) {
  512. fprintf(stderr, "error: malloc() failed\n");
  513. exit(1);
  514. }
  515. rtp_receiver_init(rcvr, sock, name, ssrc);
  516. status = rtp_receiver_init_srtp(rcvr, &policy);
  517. if (status) {
  518. fprintf(stderr,
  519. "error: srtp_create() failed with code %d\n",
  520. status);
  521. exit(1);
  522. }
  523. /* get next word and loop */
  524. while (!interrupted) {
  525. len = MAX_WORD_LEN;
  526. if (rtp_recvfrom(rcvr, word, &len) > -1)
  527. printf("\tword: %s\n", word);
  528. }
  529. rtp_receiver_deinit_srtp(rcvr);
  530. rtp_receiver_dealloc(rcvr);
  531. }
  532. if (ADDR_IS_MULTICAST(rcvr_addr.s_addr)) {
  533. leave_group(sock, mreq, argv[0]);
  534. }
  535. #ifdef RTPW_USE_WINSOCK2
  536. ret = closesocket(sock);
  537. #else
  538. ret = close(sock);
  539. #endif
  540. if (ret < 0) {
  541. fprintf(stderr, "%s: Failed to close socket", argv[0]);
  542. perror("");
  543. }
  544. status = srtp_shutdown();
  545. if (status) {
  546. printf("error: srtp shutdown failed with error code %d\n", status);
  547. exit(1);
  548. }
  549. #ifdef RTPW_USE_WINSOCK2
  550. WSACleanup();
  551. #endif
  552. return 0;
  553. }
  554. void
  555. usage(char *string) {
  556. printf("usage: %s [-d <debug>]* [-k <key> [-a][-e]] "
  557. "[-s | -r] dest_ip dest_port\n"
  558. "or %s -l\n"
  559. "where -a use message authentication\n"
  560. " -e <key size> use encryption (use 128 or 256 for key size)\n"
  561. " -g Use AES-GCM mode (must be used with -e)\n"
  562. " -t <tag size> Tag size to use in GCM mode (use 8 or 16)\n"
  563. " -k <key> sets the srtp master key\n"
  564. " -s act as rtp sender\n"
  565. " -r act as rtp receiver\n"
  566. " -l list debug modules\n"
  567. " -d <debug> turn on debugging for module <debug>\n",
  568. string, string);
  569. exit(1);
  570. }
  571. void
  572. leave_group(int sock, struct ip_mreq mreq, char *name) {
  573. int ret;
  574. ret = setsockopt(sock, IPPROTO_IP, IP_DROP_MEMBERSHIP, (void*)&mreq,
  575. sizeof(mreq));
  576. if (ret < 0) {
  577. fprintf(stderr, "%s: Failed to leave multicast group", name);
  578. perror("");
  579. }
  580. }
  581. void handle_signal(int signum)
  582. {
  583. interrupted = 1;
  584. /* Reset handler explicitly, in case we don't have sigaction() (and signal()
  585. has BSD semantics), or we don't have SA_RESETHAND */
  586. signal(signum, SIG_DFL);
  587. }
  588. int setup_signal_handler(char* name)
  589. {
  590. #if HAVE_SIGACTION
  591. struct sigaction act;
  592. memset(&act, 0, sizeof(act));
  593. act.sa_handler = handle_signal;
  594. sigemptyset(&act.sa_mask);
  595. #if defined(SA_RESETHAND)
  596. act.sa_flags = SA_RESETHAND;
  597. #else
  598. act.sa_flags = 0;
  599. #endif
  600. /* Note that we're not setting SA_RESTART; we want recvfrom to return
  601. * EINTR when we signal the receiver. */
  602. if (sigaction(SIGTERM, &act, NULL) != 0) {
  603. fprintf(stderr, "%s: error setting up signal handler", name);
  604. perror("");
  605. return -1;
  606. }
  607. #else
  608. if (signal(SIGTERM, handle_signal) == SIG_ERR) {
  609. fprintf(stderr, "%s: error setting up signal handler", name);
  610. perror("");
  611. return -1;
  612. }
  613. #endif
  614. return 0;
  615. }