2
0

rtpw.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701
  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((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.ekt = NULL;
  411. policy.next = NULL;
  412. policy.window_size = 128;
  413. policy.allow_repeat_tx = 0;
  414. policy.rtp.sec_serv = sec_servs;
  415. policy.rtcp.sec_serv = sec_serv_none; /* we don't do RTCP anyway */
  416. if (gcm_on && tag_size != 8) {
  417. policy.rtp.auth_tag_len = tag_size;
  418. }
  419. /*
  420. * read key from hexadecimal or base64 on command line into an octet
  421. * string
  422. */
  423. if (b64_input) {
  424. int pad;
  425. expected_len = (policy.rtp.cipher_key_len * 4) / 3;
  426. len = base64_string_to_octet_string(key, &pad, input_key,
  427. expected_len);
  428. if (pad != 0) {
  429. fprintf(stderr, "error: padding in base64 unexpected\n");
  430. exit(1);
  431. }
  432. } else {
  433. expected_len = policy.rtp.cipher_key_len * 2;
  434. len = hex_string_to_octet_string(key, input_key, expected_len);
  435. }
  436. /* check that hex string is the right length */
  437. if (len < expected_len) {
  438. fprintf(stderr, "error: too few digits in key/salt "
  439. "(should be %d digits, found %d)\n",
  440. expected_len, len);
  441. exit(1);
  442. }
  443. if ((int)strlen(input_key) > policy.rtp.cipher_key_len * 2) {
  444. fprintf(stderr, "error: too many digits in key/salt "
  445. "(should be %d hexadecimal digits, found %u)\n",
  446. policy.rtp.cipher_key_len * 2, (unsigned)strlen(input_key));
  447. exit(1);
  448. }
  449. printf("set master key/salt to %s/", octet_string_hex_string(key, 16));
  450. printf("%s\n", octet_string_hex_string(key + 16, 14));
  451. } else {
  452. /*
  453. * we're not providing security services, so set the policy to the
  454. * null policy
  455. *
  456. * Note that this policy does not conform to the SRTP
  457. * specification, since RTCP authentication is required. However,
  458. * the effect of this policy is to turn off SRTP, so that this
  459. * application is now a vanilla-flavored RTP application.
  460. */
  461. srtp_crypto_policy_set_null_cipher_hmac_null(&policy.rtp);
  462. srtp_crypto_policy_set_null_cipher_hmac_null(&policy.rtcp);
  463. policy.key = (uint8_t *)key;
  464. policy.ssrc.type = ssrc_specific;
  465. policy.ssrc.value = ssrc;
  466. policy.window_size = 0;
  467. policy.allow_repeat_tx = 0;
  468. policy.ekt = NULL;
  469. policy.next = NULL;
  470. }
  471. if (prog_type == sender) {
  472. #if BEW
  473. /* bind to local socket (to match crypto policy, if need be) */
  474. memset(&local, 0, sizeof(struct sockaddr_in));
  475. local.sin_addr.s_addr = htonl(INADDR_ANY);
  476. local.sin_port = htons(port);
  477. ret = bind(sock, (struct sockaddr *)&local, sizeof(struct sockaddr_in));
  478. if (ret < 0) {
  479. fprintf(stderr, "%s: bind failed\n", argv[0]);
  480. perror("");
  481. exit(1);
  482. }
  483. #endif /* BEW */
  484. /* initialize sender's rtp and srtp contexts */
  485. snd = rtp_sender_alloc();
  486. if (snd == NULL) {
  487. fprintf(stderr, "error: malloc() failed\n");
  488. exit(1);
  489. }
  490. rtp_sender_init(snd, sock, name, ssrc);
  491. status = rtp_sender_init_srtp(snd, &policy);
  492. if (status) {
  493. fprintf(stderr, "error: srtp_create() failed with code %d\n",
  494. status);
  495. exit(1);
  496. }
  497. /* open dictionary */
  498. dict = fopen(dictfile, "r");
  499. if (dict == NULL) {
  500. fprintf(stderr, "%s: couldn't open file %s\n", argv[0], dictfile);
  501. if (ADDR_IS_MULTICAST(rcvr_addr.s_addr)) {
  502. leave_group(sock, mreq, argv[0]);
  503. }
  504. exit(1);
  505. }
  506. /* read words from dictionary, then send them off */
  507. while (!interrupted && fgets(word, MAX_WORD_LEN, dict) != NULL) {
  508. len = strlen(word) + 1; /* plus one for null */
  509. if (len > MAX_WORD_LEN)
  510. printf("error: word %s too large to send\n", word);
  511. else {
  512. rtp_sendto(snd, word, len);
  513. printf("sending word: %s", word);
  514. }
  515. usleep(USEC_RATE);
  516. }
  517. rtp_sender_deinit_srtp(snd);
  518. rtp_sender_dealloc(snd);
  519. fclose(dict);
  520. } else { /* prog_type == receiver */
  521. rtp_receiver_t rcvr;
  522. if (bind(sock, (struct sockaddr *)&name, sizeof(name)) < 0) {
  523. close(sock);
  524. fprintf(stderr, "%s: socket bind error\n", argv[0]);
  525. perror(NULL);
  526. if (ADDR_IS_MULTICAST(rcvr_addr.s_addr)) {
  527. leave_group(sock, mreq, argv[0]);
  528. }
  529. exit(1);
  530. }
  531. rcvr = rtp_receiver_alloc();
  532. if (rcvr == NULL) {
  533. fprintf(stderr, "error: malloc() failed\n");
  534. exit(1);
  535. }
  536. rtp_receiver_init(rcvr, sock, name, ssrc);
  537. status = rtp_receiver_init_srtp(rcvr, &policy);
  538. if (status) {
  539. fprintf(stderr, "error: srtp_create() failed with code %d\n",
  540. status);
  541. exit(1);
  542. }
  543. /* get next word and loop */
  544. while (!interrupted) {
  545. len = MAX_WORD_LEN;
  546. if (rtp_recvfrom(rcvr, word, &len) > -1)
  547. printf("\tword: %s\n", word);
  548. }
  549. rtp_receiver_deinit_srtp(rcvr);
  550. rtp_receiver_dealloc(rcvr);
  551. }
  552. if (ADDR_IS_MULTICAST(rcvr_addr.s_addr)) {
  553. leave_group(sock, mreq, argv[0]);
  554. }
  555. #ifdef RTPW_USE_WINSOCK2
  556. ret = closesocket(sock);
  557. #else
  558. ret = close(sock);
  559. #endif
  560. if (ret < 0) {
  561. fprintf(stderr, "%s: Failed to close socket", argv[0]);
  562. perror("");
  563. }
  564. status = srtp_shutdown();
  565. if (status) {
  566. printf("error: srtp shutdown failed with error code %d\n", status);
  567. exit(1);
  568. }
  569. #ifdef RTPW_USE_WINSOCK2
  570. WSACleanup();
  571. #endif
  572. return 0;
  573. }
  574. void usage(char *string)
  575. {
  576. printf("usage: %s [-d <debug>]* [-k <key> [-a][-e]] "
  577. "[-s | -r] dest_ip dest_port\n"
  578. "or %s -l\n"
  579. "where -a use message authentication\n"
  580. " -e <key size> use encryption (use 128 or 256 for key size)\n"
  581. " -g Use AES-GCM mode (must be used with -e)\n"
  582. " -t <tag size> Tag size to use in GCM mode (use 8 or 16)\n"
  583. " -k <key> sets the srtp master key given in hexadecimal\n"
  584. " -b <key> sets the srtp master key given in base64\n"
  585. " -s act as rtp sender\n"
  586. " -r act as rtp receiver\n"
  587. " -l list debug modules\n"
  588. " -d <debug> turn on debugging for module <debug>\n"
  589. " -w <wordsfile> use <wordsfile> for input, rather than %s\n",
  590. string, string, DICT_FILE);
  591. exit(1);
  592. }
  593. void leave_group(int sock, struct ip_mreq mreq, char *name)
  594. {
  595. int ret;
  596. ret = setsockopt(sock, IPPROTO_IP, IP_DROP_MEMBERSHIP, (void *)&mreq,
  597. sizeof(mreq));
  598. if (ret < 0) {
  599. fprintf(stderr, "%s: Failed to leave multicast group", name);
  600. perror("");
  601. }
  602. }
  603. void handle_signal(int signum)
  604. {
  605. interrupted = 1;
  606. /* Reset handler explicitly, in case we don't have sigaction() (and signal()
  607. has BSD semantics), or we don't have SA_RESETHAND */
  608. signal(signum, SIG_DFL);
  609. }
  610. int setup_signal_handler(char *name)
  611. {
  612. #if HAVE_SIGACTION
  613. struct sigaction act;
  614. memset(&act, 0, sizeof(act));
  615. act.sa_handler = handle_signal;
  616. sigemptyset(&act.sa_mask);
  617. #if defined(SA_RESETHAND)
  618. act.sa_flags = SA_RESETHAND;
  619. #else
  620. act.sa_flags = 0;
  621. #endif
  622. /* Note that we're not setting SA_RESTART; we want recvfrom to return
  623. * EINTR when we signal the receiver. */
  624. if (sigaction(SIGTERM, &act, NULL) != 0) {
  625. fprintf(stderr, "%s: error setting up signal handler", name);
  626. perror("");
  627. return -1;
  628. }
  629. #else
  630. if (signal(SIGTERM, handle_signal) == SIG_ERR) {
  631. fprintf(stderr, "%s: error setting up signal handler", name);
  632. perror("");
  633. return -1;
  634. }
  635. #endif
  636. return 0;
  637. }