我正在尝试使用 libcisco 在 SRTP 上使用 UDP 协议实现简单的多播应用程序,这是链接。 https://github.com/cisco/libsrtp
所以在我的应用程序中,我从客户端接收到的服务器端发送“Hello From Server”。但是从客户端我无法向服务器发送任何数据。在服务器端,它只是在 rtp_recvfrom() 中等待。
这是我的代码
服务器端
struct sockaddr_in server_addr;
int addr_len;
int retcode;
int iOptVal;
int iOptLen;
int datalen = 1024;
struct in_addr localInterface;
rtp_sender_t snd;
srtp_policy_t policy;
srtp_err_status_t status;
int len;
int expected_len;
int do_list_mods = 0;
uint32_t ssrc = 0xdeadbeef;
uint8_t key[30] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09,
0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15,
0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D };
status = srtp_init();
if (status) {
printf("error: srtp initialization failed with error code %d\n",
status);
exit(1);
}
memset(&policy, 0x0, sizeof(srtp_policy_t));
srtp_crypto_policy_set_rtp_default(&policy.rtp);
srtp_crypto_policy_set_rtcp_default(&policy.rtcp);
policy.key = key;
policy.ssrc.type = ssrc_specific;
policy.ssrc.value = ssrc;
policy.window_size = 0;
policy.allow_repeat_tx = 0;
policy.next = NULL;
memset(out_buf,0x00,sizeof(out_buf));
memset(in_buf,0x00,sizeof(in_buf));
memset((char*) &server_addr, 0, sizeof(server_addr));
client_s = socket(AF_INET, SOCK_DGRAM, 0);
if (client_s < 0)
{
printf("*** ERROR - socket() failed \n");
exit(-1);
}
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = inet_addr("226.1.1.1");
server_addr.sin_port = htons(PORT_NUM);
localInterface.s_addr = inet_addr("127.0.0.1");
if (setsockopt(client_s, IPPROTO_IP, IP_MULTICAST_IF, (char*) &localInterface,
sizeof(localInterface)) < 0) {
perror("Setting local interface error");
exit(1);
} else
printf("Setting the local interface...OK\n");
snd = rtp_sender_alloc();
if (snd == NULL) {
fprintf(stderr, "error: malloc() failed\n");
exit(1);
}
rtp_sender_init( snd, client_s, server_addr, ssrc);
status = rtp_sender_init_srtp(snd, &policy);
if (status) {
fprintf(stderr, "error: srtp_create() failed with code %d\n",
status);
exit(1);
}
strcpy(out_buf, "Hello from server");
datalen = strlen(out_buf) + 1;
rtp_sendto( snd, out_buf, datalen);
datalen = 1024;
rtp_recvfrom(snd, in_buf, &datalen);
printf("Received server: %s \n", in_buf);
rtp_sender_deinit_srtp( snd);
rtp_sender_dealloc( snd);
客户端:
struct sockaddr_in server_addr, client_addr;
struct in_addr client_ip_addr;
int addr_len;
int retcode;
srtp_policy_t policy;
srtp_err_status_t status;
int datalen = 1024;
uint32_t ssrc = 0xdeadbeef;
uint8_t key[30] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09,
0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15,
0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D };
status = srtp_init();
if (status) {
printf("error: srtp initialization failed with error code %d\n",
status);
exit(1);
}
memset(&policy, 0x0, sizeof(srtp_policy_t));
srtp_crypto_policy_set_rtp_default(&policy.rtp);
srtp_crypto_policy_set_rtcp_default(&policy.rtcp);
policy.key = key;
policy.ssrc.type = ssrc_specific;
policy.ssrc.value = ssrc;
policy.window_size = 0;
policy.allow_repeat_tx = 0;
policy.next = NULL;
struct ip_mreq group;
memset(out_buf,0x00,sizeof(out_buf));
memset(in_buf,0x00,sizeof(in_buf));
server_s = socket(AF_INET, SOCK_DGRAM, 0);
if (server_s < 0)
{
printf("*** ERROR - socket() failed \n");
exit(-1);
}
int reuse = 1;
if (setsockopt(server_s, SOL_SOCKET, SO_REUSEADDR, (char*) &reuse,
sizeof(reuse)) < 0) {
perror("Setting SO_REUSEADDR error");
close(server_s);
exit(1);
} else
printf("Setting SO_REUSEADDR...OK.\n");
server_addr.sin_family = AF_INET; // Address family to use
server_addr.sin_port = htons(PORT_NUM);// Port number to use
server_addr.sin_addr.s_addr = htonl(INADDR_ANY);// Listen on any IP address
retcode = bind(server_s, (struct sockaddr*) &server_addr, sizeof(server_addr));
if (retcode < 0)
{
printf("*** ERROR - bind() failed \n");
exit(-1);
}
group.imr_multiaddr.s_addr = inet_addr("226.1.1.1");
group.imr_interface.s_addr = inet_addr("127.0.0.1");
if (setsockopt(server_s, IPPROTO_IP, IP_ADD_MEMBERSHIP, (char*) &group,
sizeof(group)) < 0) {
perror("Adding multicast group error");
close(server_s);
exit(1);
} else
printf("Adding multicast group...OK.\n");
rtp_receiver_t rcvr;
rcvr = rtp_receiver_alloc();
if (rcvr == NULL) {
fprintf(stderr, "error: malloc() failed\n");
exit(1);
}
rtp_receiver_init( rcvr, server_s, server_addr, ssrc);
status = rtp_receiver_init_srtp(rcvr, &policy);
if (status) {
fprintf(stderr, "error: srtp_create() failed with code %d\n",
status);
exit(1);
}
printf("Waiting for recvfrom() to complete... \n");
rtp_recvfrom(rcvr, in_buf, &datalen);
printf("Received client: %s \n", in_buf);
strcpy(out_buf, "hello from client");
datalen = strlen(out_buf) + 1;
rtp_sendto( rcvr, out_buf, datalen);
rtp_receiver_deinit_srtp( rcvr);
rtp_receiver_dealloc( rcvr);
我认为客户端的 rtp_receiver_init() 可能存在问题,我也尝试使用 client_addr 地址而不是 server_addr,但它给了我无效的参数错误。对此的任何帮助都非常感谢。