|
| 1 | +#include <zephyr/kernel.h> |
| 2 | +#include <zephyr/net/socket.h> |
| 3 | +#include <zephyr/logging/log.h> |
| 4 | +#include <zephyr/net/net_ip.h> |
| 5 | + |
| 6 | +#include <config.h> |
| 7 | +#include "srtp.h" |
| 8 | +#include "rtp.h" |
| 9 | +#include "util.h" |
| 10 | + |
| 11 | +#define MAX_WORD_LEN 128 |
| 12 | +#define MAX_KEY_LEN 96 |
| 13 | + |
| 14 | +LOG_MODULE_REGISTER(rtpw_sample, LOG_LEVEL_INF); |
| 15 | + |
| 16 | +int main(void) |
| 17 | +{ |
| 18 | + char word[MAX_WORD_LEN]; |
| 19 | + int sock, ret; |
| 20 | + struct in_addr rcvr_addr; |
| 21 | + struct sockaddr_in name; |
| 22 | + char key[MAX_KEY_LEN]; |
| 23 | + srtp_policy_t policy; |
| 24 | + srtp_err_status_t status; |
| 25 | + uint32_t ssrc = 0xdeadbeef; /* ssrc value hardcoded for now */ |
| 26 | + |
| 27 | + memset(&policy, 0x0, sizeof(srtp_policy_t)); |
| 28 | + |
| 29 | + LOG_INF("Using %s [0x%x]\n", srtp_get_version_string(), srtp_get_version()); |
| 30 | + |
| 31 | + /* initialize srtp library */ |
| 32 | + status = srtp_init(); |
| 33 | + if (status) { |
| 34 | + LOG_ERR("srtp initialization failed with error code %d.", status); |
| 35 | + return (0); |
| 36 | + } |
| 37 | + |
| 38 | + /* set address */ |
| 39 | + LOG_INF("peer IPv4 address: %s.", CONFIG_NET_CONFIG_PEER_IPV4_ADDR); |
| 40 | + LOG_INF("my IPv4 address: %s.", CONFIG_NET_CONFIG_MY_IPV4_ADDR); |
| 41 | + |
| 42 | + if (inet_pton(AF_INET, CONFIG_NET_CONFIG_PEER_IPV4_ADDR, &rcvr_addr) != 1) { |
| 43 | + LOG_ERR("Invalid IPv4 address: %s.", CONFIG_NET_CONFIG_PEER_IPV4_ADDR); |
| 44 | + return -1; |
| 45 | + } |
| 46 | + |
| 47 | + /* open socket */ |
| 48 | + sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP); |
| 49 | + if (sock < 0) { |
| 50 | + LOG_ERR("couldn't open socket."); |
| 51 | + return (0); |
| 52 | + } |
| 53 | + |
| 54 | + memset(&name, 0, sizeof(struct sockaddr_in)); |
| 55 | + name.sin_addr = rcvr_addr; |
| 56 | + name.sin_family = PF_INET; |
| 57 | + name.sin_port = htons(CONFIG_NET_SAMPLE_SRTP_SERVER_PORT); |
| 58 | + |
| 59 | + /* set up the srtp policy and master key */ |
| 60 | + /* |
| 61 | + * create policy structure, using the default mechanisms. |
| 62 | + */ |
| 63 | + srtp_crypto_policy_set_rtp_default(&policy.rtp); |
| 64 | + srtp_crypto_policy_set_rtcp_default(&policy.rtcp); |
| 65 | + policy.ssrc.type = ssrc_specific; |
| 66 | + policy.ssrc.value = ssrc; |
| 67 | + policy.key = (uint8_t *)key; |
| 68 | + policy.next = NULL; |
| 69 | + policy.window_size = 128; |
| 70 | + policy.allow_repeat_tx = 0; |
| 71 | + policy.rtp.sec_serv = sec_serv_conf_and_auth; |
| 72 | + policy.rtcp.sec_serv = sec_serv_none; /* we don't do RTCP anyway */ |
| 73 | + size_t key_len = hex2bin(CONFIG_NET_SAMPLE_SRTP_KEY, strlen(CONFIG_NET_SAMPLE_SRTP_KEY), key, sizeof(key)); |
| 74 | + |
| 75 | + /* check that hex string is the right length */ |
| 76 | + if (key_len != policy.rtp.cipher_key_len) { |
| 77 | + LOG_ERR("wrong number of digits in key (should be %d digits, found %d).", |
| 78 | + 2 * policy.rtp.cipher_key_len, 2 * key_len); |
| 79 | + return (0); |
| 80 | + } |
| 81 | + |
| 82 | +#ifdef CONFIG_NET_SAMPLE_SENDER |
| 83 | + /* initialize sender's rtp and srtp contexts */ |
| 84 | + rtp_sender_t snd; |
| 85 | + snd = rtp_sender_alloc(); |
| 86 | + if (snd == NULL) { |
| 87 | + LOG_ERR("malloc() failed."); |
| 88 | + return (0); |
| 89 | + } |
| 90 | + rtp_sender_init(snd, sock, name, ssrc); |
| 91 | + status = rtp_sender_init_srtp(snd, &policy); |
| 92 | + if (status) { |
| 93 | + LOG_ERR("srtp_create() failed with code %d.", status); |
| 94 | + return (0); |
| 95 | + } |
| 96 | + |
| 97 | + size_t word_len; |
| 98 | + /* Up-count the buffer, then send them off */ |
| 99 | + for (int i = 0; i < 1000; i++) { |
| 100 | + sprintf(word, "SRTP test%d.", i); |
| 101 | + word_len = strlen(word) + 1; /* plus one for null */ |
| 102 | + |
| 103 | + if (word_len > MAX_WORD_LEN) { |
| 104 | + LOG_ERR("word %s too large to send.", word); |
| 105 | + } else { |
| 106 | + rtp_sendto(snd, word, word_len); |
| 107 | + LOG_INF("sending word: %s", word); |
| 108 | + } |
| 109 | + k_msleep(500); |
| 110 | + } |
| 111 | + |
| 112 | + rtp_sender_deinit_srtp(snd); |
| 113 | + rtp_sender_dealloc(snd); |
| 114 | + |
| 115 | +#elif defined(CONFIG_NET_SAMPLE_RECEIVER) |
| 116 | + rtp_receiver_t rcvr; |
| 117 | + |
| 118 | + if (bind(sock, (struct sockaddr *)&name, sizeof(name)) < 0) { |
| 119 | + close(sock); |
| 120 | + LOG_ERR("socket bind error."); |
| 121 | + return (0); |
| 122 | + } |
| 123 | + |
| 124 | + rcvr = rtp_receiver_alloc(); |
| 125 | + if (rcvr == NULL) { |
| 126 | + LOG_ERR("malloc() failed."); |
| 127 | + return (0); |
| 128 | + } |
| 129 | + rtp_receiver_init(rcvr, sock, name, ssrc); |
| 130 | + status = rtp_receiver_init_srtp(rcvr, &policy); |
| 131 | + if (status) { |
| 132 | + LOG_ERR("srtp_create() failed with code %d.", status); |
| 133 | + return (0); |
| 134 | + } |
| 135 | + |
| 136 | + /* get next word and loop */ |
| 137 | + size_t word_len; |
| 138 | + while (true) { |
| 139 | + len = MAX_WORD_LEN; |
| 140 | + if (rtp_recvfrom(rcvr, word, &word_len) > -1) { |
| 141 | + LOG_INF("receiving word: %s", word); |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + rtp_receiver_deinit_srtp(rcvr); |
| 146 | + rtp_receiver_dealloc(rcvr); |
| 147 | +#else |
| 148 | +#error "Either SENDER or RECEIVER should be defined." |
| 149 | +#endif |
| 150 | + ret = close(sock); |
| 151 | + if (ret < 0) { |
| 152 | + LOG_ERR("Failed to close socket"); |
| 153 | + } |
| 154 | + |
| 155 | + status = srtp_shutdown(); |
| 156 | + if (status) { |
| 157 | + LOG_ERR("srtp shutdown failed with error code %d.", status); |
| 158 | + return (0); |
| 159 | + } |
| 160 | + return 0; |
| 161 | +} |
0 commit comments