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