2
0

rtpw.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699
  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 words.txt (or
  11. * whatever file is specified as DICT_FILE or with -w), 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-2017, 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. #ifdef HAVE_CONFIG_H
  52. #include <config.h>
  53. #endif
  54. #include "getopt_s.h" /* for local getopt() */
  55. #include <stdio.h> /* for printf, fprintf */
  56. #include <stdlib.h> /* for atoi() */
  57. #include <errno.h>
  58. #include <signal.h> /* for signal() */
  59. #include <string.h> /* for strncpy() */
  60. #include <time.h> /* for usleep() */
  61. #ifdef HAVE_UNISTD_H
  62. #include <unistd.h> /* for close() */
  63. #elif defined(_MSC_VER)
  64. #include <io.h> /* for _close() */
  65. #define close _close
  66. #endif
  67. #ifdef HAVE_SYS_SOCKET_H
  68. #include <sys/socket.h>
  69. #endif
  70. #ifdef HAVE_NETINET_IN_H
  71. #include <netinet/in.h>
  72. #elif defined HAVE_WINSOCK2_H
  73. #include <winsock2.h>
  74. #include <ws2tcpip.h>
  75. #define RTPW_USE_WINSOCK2 1
  76. #endif
  77. #ifdef HAVE_ARPA_INET_H
  78. #include <arpa/inet.h>
  79. #endif
  80. #include "srtp.h"
  81. #include "rtp.h"
  82. #include "util.h"
  83. #define DICT_FILE "words.txt"
  84. #define USEC_RATE (5e5)
  85. #define MAX_WORD_LEN 128
  86. #define ADDR_IS_MULTICAST(a) IN_MULTICAST(htonl(a))
  87. #define MAX_KEY_LEN 96
  88. #ifndef HAVE_USLEEP
  89. #ifdef HAVE_WINDOWS_H
  90. #define usleep(us) Sleep(((DWORD)us) / 1000)
  91. #else
  92. #define usleep(us) sleep((us) / 1000000)
  93. #endif
  94. #endif
  95. /*
  96. * the function usage() prints an error message describing how this
  97. * program should be called, then calls exit()
  98. */
  99. void usage(char *prog_name);
  100. /*
  101. * leave_group(...) de-registers from a multicast group
  102. */
  103. void 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 main(int argc, char *argv[])
  118. {
  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. srtp_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. int b64_input = 0;
  138. char *address = NULL;
  139. char key[MAX_KEY_LEN];
  140. unsigned short port = 0;
  141. rtp_sender_t snd;
  142. srtp_policy_t policy;
  143. srtp_err_status_t status;
  144. int len;
  145. int expected_len;
  146. int do_list_mods = 0;
  147. uint32_t ssrc = 0xdeadbeef; /* ssrc value hardcoded for now */
  148. #ifdef RTPW_USE_WINSOCK2
  149. WORD wVersionRequested = MAKEWORD(2, 0);
  150. WSADATA wsaData;
  151. ret = WSAStartup(wVersionRequested, &wsaData);
  152. if (ret != 0) {
  153. fprintf(stderr, "error: WSAStartup() failed: %d\n", ret);
  154. exit(1);
  155. }
  156. #endif
  157. memset(&policy, 0x0, sizeof(srtp_policy_t));
  158. printf("Using %s [0x%x]\n", srtp_get_version_string(), srtp_get_version());
  159. if (setup_signal_handler(argv[0]) != 0) {
  160. exit(1);
  161. }
  162. /* initialize srtp library */
  163. status = srtp_init();
  164. if (status) {
  165. printf("error: srtp initialization failed with error code %d\n",
  166. status);
  167. exit(1);
  168. }
  169. /* check args */
  170. while (1) {
  171. c = getopt_s(argc, argv, "b:k:rsgt:ae:ld:w:");
  172. if (c == -1) {
  173. break;
  174. }
  175. switch (c) {
  176. case 'b':
  177. b64_input = 1;
  178. /* fall thru */
  179. case 'k':
  180. input_key = optarg_s;
  181. break;
  182. case 'e':
  183. key_size = atoi(optarg_s);
  184. if (key_size != 128 && key_size != 256) {
  185. printf("error: encryption key size must be 128 or 256 (%d)\n",
  186. key_size);
  187. exit(1);
  188. }
  189. sec_servs |= sec_serv_conf;
  190. break;
  191. case 't':
  192. tag_size = atoi(optarg_s);
  193. if (tag_size != 8 && tag_size != 16) {
  194. printf("error: GCM tag size must be 8 or 16 (%d)\n", tag_size);
  195. exit(1);
  196. }
  197. break;
  198. case 'a':
  199. sec_servs |= sec_serv_auth;
  200. break;
  201. case 'g':
  202. gcm_on = 1;
  203. sec_servs |= sec_serv_auth;
  204. break;
  205. case 'r':
  206. prog_type = receiver;
  207. break;
  208. case 's':
  209. prog_type = sender;
  210. break;
  211. case 'd':
  212. status = srtp_set_debug_module(optarg_s, 1);
  213. if (status) {
  214. printf("error: set debug module (%s) failed\n", optarg_s);
  215. exit(1);
  216. }
  217. break;
  218. case 'l':
  219. do_list_mods = 1;
  220. break;
  221. case 'w':
  222. dictfile = optarg_s;
  223. break;
  224. default:
  225. usage(argv[0]);
  226. }
  227. }
  228. if (prog_type == unknown) {
  229. if (do_list_mods) {
  230. status = srtp_list_debug_modules();
  231. if (status) {
  232. printf("error: list of debug modules failed\n");
  233. exit(1);
  234. }
  235. return 0;
  236. } else {
  237. printf("error: neither sender [-s] nor receiver [-r] specified\n");
  238. usage(argv[0]);
  239. }
  240. }
  241. if ((sec_servs && !input_key) || (!sec_servs && input_key)) {
  242. /*
  243. * a key must be provided if and only if security services have
  244. * been requested
  245. */
  246. usage(argv[0]);
  247. }
  248. if (argc != optind_s + 2) {
  249. /* wrong number of arguments */
  250. usage(argv[0]);
  251. }
  252. /* get address from arg */
  253. address = argv[optind_s++];
  254. /* get port from arg */
  255. port = atoi(argv[optind_s++]);
  256. /* set address */
  257. #ifdef HAVE_INET_ATON
  258. if (0 == inet_aton(address, &rcvr_addr)) {
  259. fprintf(stderr, "%s: cannot parse IP v4 address %s\n", argv[0],
  260. address);
  261. exit(1);
  262. }
  263. if (rcvr_addr.s_addr == INADDR_NONE) {
  264. fprintf(stderr, "%s: address error", argv[0]);
  265. exit(1);
  266. }
  267. #else
  268. rcvr_addr.s_addr = inet_addr(address);
  269. if (0xffffffff == rcvr_addr.s_addr) {
  270. fprintf(stderr, "%s: cannot parse IP v4 address %s\n", argv[0],
  271. address);
  272. exit(1);
  273. }
  274. #endif
  275. /* open socket */
  276. sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
  277. if (sock < 0) {
  278. int err;
  279. #ifdef RTPW_USE_WINSOCK2
  280. err = WSAGetLastError();
  281. #else
  282. err = errno;
  283. #endif
  284. fprintf(stderr, "%s: couldn't open socket: %d\n", argv[0], err);
  285. exit(1);
  286. }
  287. memset(&name, 0, sizeof(struct sockaddr_in));
  288. name.sin_addr = rcvr_addr;
  289. name.sin_family = PF_INET;
  290. name.sin_port = htons(port);
  291. if (ADDR_IS_MULTICAST(rcvr_addr.s_addr)) {
  292. if (prog_type == sender) {
  293. ret = setsockopt(sock, IPPROTO_IP, IP_MULTICAST_TTL, &ttl,
  294. sizeof(ttl));
  295. if (ret < 0) {
  296. fprintf(stderr, "%s: Failed to set TTL for multicast group",
  297. argv[0]);
  298. perror("");
  299. exit(1);
  300. }
  301. }
  302. mreq.imr_multiaddr.s_addr = rcvr_addr.s_addr;
  303. mreq.imr_interface.s_addr = htonl(INADDR_ANY);
  304. ret = setsockopt(sock, IPPROTO_IP, IP_ADD_MEMBERSHIP, (void *)&mreq,
  305. sizeof(mreq));
  306. if (ret < 0) {
  307. fprintf(stderr, "%s: Failed to join multicast group", argv[0]);
  308. perror("");
  309. exit(1);
  310. }
  311. }
  312. /* report security services selected on the command line */
  313. printf("security services: ");
  314. if (sec_servs & sec_serv_conf)
  315. printf("confidentiality ");
  316. if (sec_servs & sec_serv_auth)
  317. printf("message authentication");
  318. if (sec_servs == sec_serv_none)
  319. printf("none");
  320. printf("\n");
  321. /* set up the srtp policy and master key */
  322. if (sec_servs) {
  323. /*
  324. * create policy structure, using the default mechanisms but
  325. * with only the security services requested on the command line,
  326. * using the right SSRC value
  327. */
  328. switch (sec_servs) {
  329. case sec_serv_conf_and_auth:
  330. if (gcm_on) {
  331. #ifdef GCM
  332. switch (key_size) {
  333. case 128:
  334. srtp_crypto_policy_set_aes_gcm_128_8_auth(&policy.rtp);
  335. srtp_crypto_policy_set_aes_gcm_128_8_auth(&policy.rtcp);
  336. break;
  337. case 256:
  338. srtp_crypto_policy_set_aes_gcm_256_8_auth(&policy.rtp);
  339. srtp_crypto_policy_set_aes_gcm_256_8_auth(&policy.rtcp);
  340. break;
  341. }
  342. #else
  343. printf("error: GCM mode only supported when using the OpenSSL "
  344. "or NSS crypto engine.\n");
  345. return 0;
  346. #endif
  347. } else {
  348. switch (key_size) {
  349. case 128:
  350. srtp_crypto_policy_set_rtp_default(&policy.rtp);
  351. srtp_crypto_policy_set_rtcp_default(&policy.rtcp);
  352. break;
  353. case 256:
  354. srtp_crypto_policy_set_aes_cm_256_hmac_sha1_80(&policy.rtp);
  355. srtp_crypto_policy_set_rtcp_default(&policy.rtcp);
  356. break;
  357. }
  358. }
  359. break;
  360. case sec_serv_conf:
  361. if (gcm_on) {
  362. printf(
  363. "error: GCM mode must always be used with auth enabled\n");
  364. return -1;
  365. } else {
  366. switch (key_size) {
  367. case 128:
  368. srtp_crypto_policy_set_aes_cm_128_null_auth(&policy.rtp);
  369. srtp_crypto_policy_set_rtcp_default(&policy.rtcp);
  370. break;
  371. case 256:
  372. srtp_crypto_policy_set_aes_cm_256_null_auth(&policy.rtp);
  373. srtp_crypto_policy_set_rtcp_default(&policy.rtcp);
  374. break;
  375. }
  376. }
  377. break;
  378. case sec_serv_auth:
  379. if (gcm_on) {
  380. #ifdef GCM
  381. switch (key_size) {
  382. case 128:
  383. srtp_crypto_policy_set_aes_gcm_128_8_only_auth(&policy.rtp);
  384. srtp_crypto_policy_set_aes_gcm_128_8_only_auth(
  385. &policy.rtcp);
  386. break;
  387. case 256:
  388. srtp_crypto_policy_set_aes_gcm_256_8_only_auth(&policy.rtp);
  389. srtp_crypto_policy_set_aes_gcm_256_8_only_auth(
  390. &policy.rtcp);
  391. break;
  392. }
  393. #else
  394. printf("error: GCM mode only supported when using the OpenSSL "
  395. "crypto engine.\n");
  396. return 0;
  397. #endif
  398. } else {
  399. srtp_crypto_policy_set_null_cipher_hmac_sha1_80(&policy.rtp);
  400. srtp_crypto_policy_set_rtcp_default(&policy.rtcp);
  401. }
  402. break;
  403. default:
  404. printf("error: unknown security service requested\n");
  405. return -1;
  406. }
  407. policy.ssrc.type = ssrc_specific;
  408. policy.ssrc.value = ssrc;
  409. policy.key = (uint8_t *)key;
  410. policy.next = NULL;
  411. policy.window_size = 128;
  412. policy.allow_repeat_tx = 0;
  413. policy.rtp.sec_serv = sec_servs;
  414. policy.rtcp.sec_serv = sec_serv_none; /* we don't do RTCP anyway */
  415. if (gcm_on && tag_size != 8) {
  416. policy.rtp.auth_tag_len = tag_size;
  417. }
  418. /*
  419. * read key from hexadecimal or base64 on command line into an octet
  420. * string
  421. */
  422. if (b64_input) {
  423. int pad;
  424. expected_len = (policy.rtp.cipher_key_len * 4) / 3;
  425. len = base64_string_to_octet_string(key, &pad, input_key,
  426. expected_len);
  427. if (pad != 0) {
  428. fprintf(stderr, "error: padding in base64 unexpected\n");
  429. exit(1);
  430. }
  431. } else {
  432. expected_len = policy.rtp.cipher_key_len * 2;
  433. len = hex_string_to_octet_string(key, input_key, expected_len);
  434. }
  435. /* check that hex string is the right length */
  436. if (len < expected_len) {
  437. fprintf(stderr, "error: too few digits in key/salt "
  438. "(should be %d digits, found %d)\n",
  439. expected_len, len);
  440. exit(1);
  441. }
  442. if ((int)strlen(input_key) > policy.rtp.cipher_key_len * 2) {
  443. fprintf(stderr, "error: too many digits in key/salt "
  444. "(should be %d hexadecimal digits, found %u)\n",
  445. policy.rtp.cipher_key_len * 2, (unsigned)strlen(input_key));
  446. exit(1);
  447. }
  448. printf("set master key/salt to %s/", octet_string_hex_string(key, 16));
  449. printf("%s\n", octet_string_hex_string(key + 16, 14));
  450. } else {
  451. /*
  452. * we're not providing security services, so set the policy to the
  453. * null policy
  454. *
  455. * Note that this policy does not conform to the SRTP
  456. * specification, since RTCP authentication is required. However,
  457. * the effect of this policy is to turn off SRTP, so that this
  458. * application is now a vanilla-flavored RTP application.
  459. */
  460. srtp_crypto_policy_set_null_cipher_hmac_null(&policy.rtp);
  461. srtp_crypto_policy_set_null_cipher_hmac_null(&policy.rtcp);
  462. policy.key = (uint8_t *)key;
  463. policy.ssrc.type = ssrc_specific;
  464. policy.ssrc.value = ssrc;
  465. policy.window_size = 0;
  466. policy.allow_repeat_tx = 0;
  467. policy.next = NULL;
  468. }
  469. if (prog_type == sender) {
  470. #if BEW
  471. /* bind to local socket (to match crypto policy, if need be) */
  472. memset(&local, 0, sizeof(struct sockaddr_in));
  473. local.sin_addr.s_addr = htonl(INADDR_ANY);
  474. local.sin_port = htons(port);
  475. ret = bind(sock, (struct sockaddr *)&local, sizeof(struct sockaddr_in));
  476. if (ret < 0) {
  477. fprintf(stderr, "%s: bind failed\n", argv[0]);
  478. perror("");
  479. exit(1);
  480. }
  481. #endif /* BEW */
  482. /* initialize sender's rtp and srtp contexts */
  483. snd = rtp_sender_alloc();
  484. if (snd == NULL) {
  485. fprintf(stderr, "error: malloc() failed\n");
  486. exit(1);
  487. }
  488. rtp_sender_init(snd, sock, name, ssrc);
  489. status = rtp_sender_init_srtp(snd, &policy);
  490. if (status) {
  491. fprintf(stderr, "error: srtp_create() failed with code %d\n",
  492. status);
  493. exit(1);
  494. }
  495. /* open dictionary */
  496. dict = fopen(dictfile, "r");
  497. if (dict == NULL) {
  498. fprintf(stderr, "%s: couldn't open file %s\n", argv[0], dictfile);
  499. if (ADDR_IS_MULTICAST(rcvr_addr.s_addr)) {
  500. leave_group(sock, mreq, argv[0]);
  501. }
  502. exit(1);
  503. }
  504. /* read words from dictionary, then send them off */
  505. while (!interrupted && fgets(word, MAX_WORD_LEN, dict) != NULL) {
  506. len = strlen(word) + 1; /* plus one for null */
  507. if (len > MAX_WORD_LEN)
  508. printf("error: word %s too large to send\n", word);
  509. else {
  510. rtp_sendto(snd, word, len);
  511. printf("sending word: %s", word);
  512. }
  513. usleep(USEC_RATE);
  514. }
  515. rtp_sender_deinit_srtp(snd);
  516. rtp_sender_dealloc(snd);
  517. fclose(dict);
  518. } else { /* prog_type == receiver */
  519. rtp_receiver_t rcvr;
  520. if (bind(sock, (struct sockaddr *)&name, sizeof(name)) < 0) {
  521. close(sock);
  522. fprintf(stderr, "%s: socket bind error\n", argv[0]);
  523. perror(NULL);
  524. if (ADDR_IS_MULTICAST(rcvr_addr.s_addr)) {
  525. leave_group(sock, mreq, argv[0]);
  526. }
  527. exit(1);
  528. }
  529. rcvr = rtp_receiver_alloc();
  530. if (rcvr == NULL) {
  531. fprintf(stderr, "error: malloc() failed\n");
  532. exit(1);
  533. }
  534. rtp_receiver_init(rcvr, sock, name, ssrc);
  535. status = rtp_receiver_init_srtp(rcvr, &policy);
  536. if (status) {
  537. fprintf(stderr, "error: srtp_create() failed with code %d\n",
  538. status);
  539. exit(1);
  540. }
  541. /* get next word and loop */
  542. while (!interrupted) {
  543. len = MAX_WORD_LEN;
  544. if (rtp_recvfrom(rcvr, word, &len) > -1)
  545. printf("\tword: %s\n", word);
  546. }
  547. rtp_receiver_deinit_srtp(rcvr);
  548. rtp_receiver_dealloc(rcvr);
  549. }
  550. if (ADDR_IS_MULTICAST(rcvr_addr.s_addr)) {
  551. leave_group(sock, mreq, argv[0]);
  552. }
  553. #ifdef RTPW_USE_WINSOCK2
  554. ret = closesocket(sock);
  555. #else
  556. ret = close(sock);
  557. #endif
  558. if (ret < 0) {
  559. fprintf(stderr, "%s: Failed to close socket", argv[0]);
  560. perror("");
  561. }
  562. status = srtp_shutdown();
  563. if (status) {
  564. printf("error: srtp shutdown failed with error code %d\n", status);
  565. exit(1);
  566. }
  567. #ifdef RTPW_USE_WINSOCK2
  568. WSACleanup();
  569. #endif
  570. return 0;
  571. }
  572. void usage(char *string)
  573. {
  574. printf("usage: %s [-d <debug>]* [-k <key> [-a][-e]] "
  575. "[-s | -r] dest_ip dest_port\n"
  576. "or %s -l\n"
  577. "where -a use message authentication\n"
  578. " -e <key size> use encryption (use 128 or 256 for key size)\n"
  579. " -g Use AES-GCM mode (must be used with -e)\n"
  580. " -t <tag size> Tag size to use in GCM mode (use 8 or 16)\n"
  581. " -k <key> sets the srtp master key given in hexadecimal\n"
  582. " -b <key> sets the srtp master key given in base64\n"
  583. " -s act as rtp sender\n"
  584. " -r act as rtp receiver\n"
  585. " -l list debug modules\n"
  586. " -d <debug> turn on debugging for module <debug>\n"
  587. " -w <wordsfile> use <wordsfile> for input, rather than %s\n",
  588. string, string, DICT_FILE);
  589. exit(1);
  590. }
  591. void leave_group(int sock, struct ip_mreq mreq, char *name)
  592. {
  593. int ret;
  594. ret = setsockopt(sock, IPPROTO_IP, IP_DROP_MEMBERSHIP, (void *)&mreq,
  595. sizeof(mreq));
  596. if (ret < 0) {
  597. fprintf(stderr, "%s: Failed to leave multicast group", name);
  598. perror("");
  599. }
  600. }
  601. void handle_signal(int signum)
  602. {
  603. interrupted = 1;
  604. /* Reset handler explicitly, in case we don't have sigaction() (and signal()
  605. has BSD semantics), or we don't have SA_RESETHAND */
  606. signal(signum, SIG_DFL);
  607. }
  608. int setup_signal_handler(char *name)
  609. {
  610. #ifdef HAVE_SIGACTION
  611. struct sigaction act;
  612. memset(&act, 0, sizeof(act));
  613. act.sa_handler = handle_signal;
  614. sigemptyset(&act.sa_mask);
  615. #if defined(SA_RESETHAND)
  616. act.sa_flags = SA_RESETHAND;
  617. #else
  618. act.sa_flags = 0;
  619. #endif
  620. /* Note that we're not setting SA_RESTART; we want recvfrom to return
  621. * EINTR when we signal the receiver. */
  622. if (sigaction(SIGTERM, &act, NULL) != 0) {
  623. fprintf(stderr, "%s: error setting up signal handler", name);
  624. perror("");
  625. return -1;
  626. }
  627. #else
  628. if (signal(SIGTERM, handle_signal) == SIG_ERR) {
  629. fprintf(stderr, "%s: error setting up signal handler", name);
  630. perror("");
  631. return -1;
  632. }
  633. #endif
  634. return 0;
  635. }