Skip to content

Commit

Permalink
Fix ice ufrag length issuse and set DTLS to passive
Browse files Browse the repository at this point in the history
  • Loading branch information
sepfy committed Sep 24, 2023
1 parent 6ec50f7 commit a318068
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 30 deletions.
39 changes: 21 additions & 18 deletions src/agent.c
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ static int agent_create_turn_addr(Agent *agent, Address *serv_addr, const char *
udp_blocking_timeout(&udp_socket, 5000);
memset(&send_msg, 0, sizeof(send_msg));
stun_msg_create(&send_msg, STUN_METHOD_ALLOCATE);
stun_msg_write_attr(&send_msg, STUN_ATTR_REQUESTED_TRANSPORT, sizeof(attr), (char*)&attr); // UDP
stun_msg_write_attr(&send_msg, STUN_ATTR_TYPE_REQUESTED_TRANSPORT, sizeof(attr), (char*)&attr); // UDP
stun_msg_write_attr(&send_msg, STUN_ATTR_TYPE_USERNAME, strlen(username), (char*)username);

do {
Expand All @@ -105,7 +105,7 @@ static int agent_create_turn_addr(Agent *agent, Address *serv_addr, const char *

memset(&send_msg, 0, sizeof(send_msg));
stun_msg_create(&send_msg, STUN_METHOD_ALLOCATE);
stun_msg_write_attr(&send_msg, STUN_ATTR_REQUESTED_TRANSPORT, sizeof(attr), (char*)&attr); // UDP
stun_msg_write_attr(&send_msg, STUN_ATTR_TYPE_REQUESTED_TRANSPORT, sizeof(attr), (char*)&attr); // UDP
stun_msg_write_attr(&send_msg, STUN_ATTR_TYPE_USERNAME, strlen(username), (char*)username);
stun_msg_write_attr(&send_msg, STUN_ATTR_TYPE_NONCE, strlen(recv_msg.nonce), recv_msg.nonce);
stun_msg_write_attr(&send_msg, STUN_ATTR_TYPE_REALM, strlen(recv_msg.realm), recv_msg.realm);
Expand Down Expand Up @@ -192,8 +192,8 @@ void agent_get_local_description(Agent *agent, char *description, int length) {
memset(agent->local_ufrag, 0, sizeof(agent->local_ufrag));
memset(agent->local_upwd, 0, sizeof(agent->local_upwd));

utils_random_string(agent->local_ufrag, ICE_UFRAG_LENGTH);
utils_random_string(agent->local_upwd, ICE_UPWD_LENGTH);
utils_random_string(agent->local_ufrag, 4);
utils_random_string(agent->local_upwd, 24);

snprintf(description, length, "a=ice-ufrag:%s\na=ice-pwd:%s\n", agent->local_ufrag, agent->local_upwd);
ncandidates = agent->local_candidates_count;
Expand Down Expand Up @@ -238,7 +238,7 @@ void agent_process_stun_request(Agent *agent, StunMessage *stun_msg) {
header = (StunHeader *)msg.buf;
memcpy(header->transaction_id, agent->transaction_id, sizeof(header->transaction_id));

char username[64];
char username[584];

snprintf(username, sizeof(username), "%s:%s", agent->local_ufrag, agent->remote_ufrag);

Expand All @@ -247,7 +247,7 @@ void agent_process_stun_request(Agent *agent, StunMessage *stun_msg) {
stun_set_mapped_address(mapped_address, NULL, &agent->nominated_pair->remote->addr);
stun_msg_write_attr(&msg, STUN_ATTR_TYPE_MAPPED_ADDRESS, 8, mapped_address);
stun_msg_write_attr(&msg, STUN_ATTR_TYPE_USERNAME, strlen(username), username);
stun_msg_finish(&msg, STUN_CREDENTIAL_SHORT_TERM, agent->local_upwd, ICE_UPWD_LENGTH);
stun_msg_finish(&msg, STUN_CREDENTIAL_SHORT_TERM, agent->local_upwd, strlen(agent->local_upwd));

udp_socket_sendto(&agent->udp_socket, &agent->nominated_pair->remote->addr, msg.buf, msg.size);
agent->binding_request_time = utils_get_timestamp();
Expand Down Expand Up @@ -328,26 +328,27 @@ a=candidate:1 1 UDP 1 36.231.28.50 38143 typ srflx

LOGD("Set remote description:\n%s", description);

char *line = strtok(description, "\r\n");
char *line_start = description;
char *line_end = NULL;

while (line) {
while ((line_end = strstr(line_start, "\r\n")) != NULL) {

if (strncmp(line, "a=ice-ufrag:", strlen("a=ice-ufrag:")) == 0) {
if (strncmp(line_start, "a=ice-ufrag:", strlen("a=ice-ufrag:")) == 0) {

memcpy(agent->remote_ufrag, line + strlen("a=ice-ufrag:"), ICE_UFRAG_LENGTH);
strncpy(agent->remote_ufrag, line_start + strlen("a=ice-ufrag:"), line_end - line_start - strlen("a=ice-ufrag:"));

} else if (strncmp(line, "a=ice-pwd:", strlen("a=ice-pwd:")) == 0) {
} else if (strncmp(line_start, "a=ice-pwd:", strlen("a=ice-pwd:")) == 0) {

memcpy(agent->remote_upwd, line + strlen("a=ice-pwd:"), ICE_UPWD_LENGTH);
strncpy(agent->remote_upwd, line_start + strlen("a=ice-pwd:"), line_end - line_start - strlen("a=ice-pwd:"));

} else if (strncmp(line, "a=candidate:", strlen("a=candidate:")) == 0) {
} else if (strncmp(line_start, "a=candidate:", strlen("a=candidate:")) == 0) {

if (ice_candidate_from_description(&agent->remote_candidates[agent->remote_candidates_count], line) == 0) {
if (ice_candidate_from_description(&agent->remote_candidates[agent->remote_candidates_count], line_start) == 0) {
agent->remote_candidates_count++;
}
}

line = strtok(NULL, "\r\n");
line_start = line_end + 2;
}

LOGD("remote ufrag: %s", agent->remote_ufrag);
Expand Down Expand Up @@ -379,14 +380,16 @@ int agent_connectivity_check(Agent *agent) {
agent_recv(agent, buf, sizeof(buf));

stun_msg_create(&msg, STUN_CLASS_REQUEST | STUN_METHOD_BINDING);
char username[64];
char username[584];
memset(username, 0, sizeof(username));
snprintf(username, sizeof(username), "%s:%s", agent->remote_ufrag, agent->local_ufrag);

stun_msg_write_attr(&msg, STUN_ATTR_TYPE_USERNAME, strlen(username), username);
stun_msg_write_attr(&msg, STUN_ATTR_TYPE_PRIORITY, 4, (char *)&agent->nominated_pair->priority);
stun_msg_write_attr(&msg, STUN_ATTR_TYPE_PRIORITY, 4, (char *)&agent->nominated_pair->priority);
uint64_t tie_breaker = utils_get_timestamp();
stun_msg_write_attr(&msg, STUN_ATTR_TYPE_USE_CANDIDATE, 0, NULL);
stun_msg_finish(&msg, STUN_CREDENTIAL_SHORT_TERM, agent->remote_upwd, ICE_UPWD_LENGTH);
stun_msg_write_attr(&msg, STUN_ATTR_TYPE_ICE_CONTROLLED, 8, (char *)&tie_breaker);
stun_msg_finish(&msg, STUN_CREDENTIAL_SHORT_TERM, agent->remote_upwd, strlen(agent->remote_upwd));

LOGD("send binding request to remote ip: %d.%d.%d.%d, port: %d", agent->nominated_pair->remote->addr.ipv4[0], agent->nominated_pair->remote->addr.ipv4[1], agent->nominated_pair->remote->addr.ipv4[2], agent->nominated_pair->remote->addr.ipv4[3], agent->nominated_pair->remote->addr.port);

Expand Down
4 changes: 2 additions & 2 deletions src/dtls_srtp.c
Original file line number Diff line number Diff line change
Expand Up @@ -494,12 +494,12 @@ int dtls_srtp_read(DtlsSrtp *dtls_srtp, unsigned char *buf, size_t len) {
return ret;
}

int dtls_srtp_validate(uint8_t *buf) {
int dtls_srtp_probe(uint8_t *buf) {

if(buf == NULL)
return 0;

LOGD("DTLS content type: %d, version: %d, epoch: %d, sequence: %d, length: %d (%.4x)", header->content_type, header->version, header->epoch, ntohs(header->seqnum_hi), ntohs(header->length), header->length);
//LOGD("DTLS content type: %d, version: %d, epoch: %d, sequence: %d, length: %d (%.4x)", header->content_type, header->version, header->epoch, ntohs(header->seqnum_hi), ntohs(header->length), header->length);

return ((*buf >= 20) && (*buf <= 64));
}
Expand Down
2 changes: 1 addition & 1 deletion src/dtls_srtp.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ void dtls_srtp_encrypt_rtp_packet(DtlsSrtp *dtls_srtp, uint8_t *packet, int *byt

void dtls_srtp_sctp_to_dtls(DtlsSrtp *dtls_srtp, uint8_t *packet, int bytes);

int dtls_srtp_validate(uint8_t *buf);
int dtls_srtp_probe(uint8_t *buf);

void dtls_srtp_decrypt_rtp_packet(DtlsSrtp *dtls_srtp, uint8_t *packet, int *bytes);

Expand Down
4 changes: 2 additions & 2 deletions src/ice.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
#include "address.h"
#include "stun.h"

#define ICE_UFRAG_LENGTH 4
#define ICE_UPWD_LENGTH 24
#define ICE_UFRAG_LENGTH 256
#define ICE_UPWD_LENGTH 256

typedef enum IceCandidateState {

Expand Down
12 changes: 6 additions & 6 deletions src/peer_connection.c
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ static void peer_connection_state_new(PeerConnection *pc) {

sdp_append_h264(&pc->local_sdp);
sdp_append(&pc->local_sdp, "a=fingerprint:sha-256 %s", pc->dtls_srtp.local_fingerprint);
sdp_append(&pc->local_sdp, "a=setup:actpass");
sdp_append(&pc->local_sdp, "a=setup:passive");
strcat(pc->local_sdp.content, description);
}

Expand All @@ -297,22 +297,22 @@ static void peer_connection_state_new(PeerConnection *pc) {

sdp_append_pcma(&pc->local_sdp);
sdp_append(&pc->local_sdp, "a=fingerprint:sha-256 %s", pc->dtls_srtp.local_fingerprint);
sdp_append(&pc->local_sdp, "a=setup:actpass");
sdp_append(&pc->local_sdp, "a=setup:passive");
strcat(pc->local_sdp.content, description);
break;

case CODEC_PCMU:

sdp_append_pcmu(&pc->local_sdp);
sdp_append(&pc->local_sdp, "a=fingerprint:sha-256 %s", pc->dtls_srtp.local_fingerprint);
sdp_append(&pc->local_sdp, "a=setup:actpass");
sdp_append(&pc->local_sdp, "a=setup:passive");
strcat(pc->local_sdp.content, description);
break;

case CODEC_OPUS:
sdp_append_opus(&pc->local_sdp);
sdp_append(&pc->local_sdp, "a=fingerprint:sha-256 %s", pc->dtls_srtp.local_fingerprint);
sdp_append(&pc->local_sdp, "a=setup:actpass");
sdp_append(&pc->local_sdp, "a=setup:passive");
strcat(pc->local_sdp.content, description);

default:
Expand All @@ -322,7 +322,7 @@ static void peer_connection_state_new(PeerConnection *pc) {
if (pc->config.datachannel) {
sdp_append_datachannel(&pc->local_sdp);
sdp_append(&pc->local_sdp, "a=fingerprint:sha-256 %s", pc->dtls_srtp.local_fingerprint);
sdp_append(&pc->local_sdp, "a=setup:actpass");
sdp_append(&pc->local_sdp, "a=setup:passive");
strcat(pc->local_sdp.content, description);
}

Expand Down Expand Up @@ -409,7 +409,7 @@ int peer_connection_loop(PeerConnection *pc) {
dtls_srtp_decrypt_rtcp_packet(&pc->dtls_srtp, pc->agent_buf, &pc->agent_ret);
peer_connection_incoming_rtcp(pc, pc->agent_buf, pc->agent_ret);

} else if (dtls_srtp_validate(pc->agent_buf)) {
} else if (dtls_srtp_probe(pc->agent_buf)) {

int ret = dtls_srtp_read(&pc->dtls_srtp, pc->temp_buf, sizeof(pc->temp_buf));
LOGD("Got DTLS data %d", ret);
Expand Down
2 changes: 1 addition & 1 deletion src/stun.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ typedef enum StunAttrType {
STUN_ATTR_TYPE_REALM = 0x0014,
STUN_ATTR_TYPE_NONCE = 0x0015,
STUN_ATTR_TYPE_XOR_RELAYED_ADDRESS = 0x0016,
STUN_ATTR_REQUESTED_TRANSPORT = 0x0019,
STUN_ATTR_TYPE_REQUESTED_TRANSPORT = 0x0019,
STUN_ATTR_TYPE_XOR_MAPPED_ADDRESS = 0x0020,
STUN_ATTR_TYPE_PRIORITY = 0x0024,
STUN_ATTR_TYPE_USE_CANDIDATE = 0x0025,
Expand Down

0 comments on commit a318068

Please sign in to comment.