Skip to content

Commit

Permalink
Add mdns and refactor socket
Browse files Browse the repository at this point in the history
  • Loading branch information
sepfy committed Jul 22, 2024
1 parent 00211ec commit 5c53b62
Show file tree
Hide file tree
Showing 21 changed files with 667 additions and 717 deletions.
88 changes: 39 additions & 49 deletions src/address.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,70 +6,60 @@
#include <arpa/inet.h>
#include <string.h>

#include "utils.h"
#include "address.h"

int addr_ipv4_validate(const char *ipv4, size_t len, Address *addr) {

int start = 0;
int index = 0;
for (int i = 0; i < len; i++) {

if (ipv4[i] != '.' && (ipv4[i] < '0' || ipv4[i] > '9')) {
return 0;
} else if (ipv4[i] == '.') {

addr->ipv4[index++] = atoi(ipv4 + start);
start = i + 1;
} else if (i == (len - 1)) {

addr->ipv4[index++] = atoi(ipv4 + start);
} else if (index == 4) {
return 0;
}
void addr_set_family(Address *addr, int family) {
switch (family) {
case AF_INET6:
addr->family = AF_INET6;
break;
case AF_INET:
default:
addr->family = AF_INET;
break;
}
addr->family = AF_INET;
return 1;
}

int addr_ipv6_validate(const char *ipv6, size_t len, Address *addr) {
int ret;
struct sockaddr_in6 sa6;
char astring[INET6_ADDRSTRLEN];
ret = inet_pton(AF_INET6, ipv6, &(sa6.sin6_addr));
inet_ntop(AF_INET6, &(sa6.sin6_addr), astring, INET6_ADDRSTRLEN);
memcpy(addr->ipv6, sa6.sin6_addr.s6_addr, 16);
return ret;
void addr_set_port(Address *addr, uint16_t port) {
addr->port = port;
switch (addr->family) {
case AF_INET6:
addr->sin6.sin6_port = htons(port);
break;
case AF_INET:
default:
addr->sin.sin_port = htons(port);
break;
}
}

int addr_to_text(const Address *addr, char *buf, size_t len) {

switch (addr->family) {
case AF_INET:
return inet_ntop(AF_INET, addr->ipv4, buf, len) != NULL;
break;
case AF_INET6:
return inet_ntop(AF_INET6, addr->ipv6, buf, len) != NULL;
break;
int addr_from_string(const char *buf, Address *addr) {
if (inet_pton(AF_INET, buf, &(addr->sin.sin_addr)) == 1) {
addr_set_family(addr, AF_INET);
return 1;
} else if (inet_pton(AF_INET6, buf, &(addr->sin6.sin6_addr)) == 1) {
addr_set_family(addr, AF_INET6);
return 1;
}
return 0;
}

int addr_equal(const Address *a, const Address *b) {
if (a->family != b->family) {
return 0;
}
int addr_to_string(const Address *addr, char *buf, size_t len) {

switch (a->family) {
case AF_INET:
for (int i = 0; i < 4; i++) {
if (a->ipv4[i] != b->ipv4[i]) {
return 0;
}
}
break;
memset(buf, 0, sizeof(len));
switch (addr->family) {
case AF_INET6:
break;
return inet_ntop(AF_INET6, &addr->sin6.sin6_addr, buf, len) != NULL;
case AF_INET:
default:
return inet_ntop(AF_INET, &addr->sin.sin_addr, buf, len) != NULL;
}
return 0;
}

int addr_equal(const Address *a, const Address *b) {
// TODO
return 1;
}
24 changes: 15 additions & 9 deletions src/address.h
Original file line number Diff line number Diff line change
@@ -1,24 +1,30 @@
#ifndef ADDRESS_H_
#define ADDRESS_H_

#include <netinet/in.h>
#include <sys/socket.h>
#include <stdint.h>

typedef struct Address Address;

struct Address {
#define ADDRSTRLEN INET6_ADDRSTRLEN

typedef struct Address {
uint8_t family;
struct sockaddr_in sin;
struct sockaddr_in6 sin6;
uint16_t port;
uint8_t ipv4[4];
uint16_t ipv6[8];
} Address;

void addr_set_family(Address *addr, int family);

void addr_set_port(Address *addr, uint16_t port);

};
int addr_inet6_validate(const char *ipv6, size_t len, Address *addr);

int addr_ipv6_validate(const char *ipv6, size_t len, Address *addr);
int addr_inet_validate(const char *ipv4, size_t len, Address *addr);

int addr_ipv4_validate(const char *ipv4, size_t len, Address *addr);
int addr_to_string(const Address *addr, char *buf, size_t len);

int addr_to_text(const Address *addr, char *buf, size_t len);
int addr_from_string(const char *str, Address *addr);

int addr_equal(const Address *a, const Address *b);

Expand Down
45 changes: 23 additions & 22 deletions src/agent.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#include <sys/socket.h>

#include <pthread.h>
#include "udp.h"
#include "socket.h"
#include "utils.h"
#include "stun.h"
#include "ice.h"
Expand All @@ -21,14 +21,14 @@
static int agent_create_sockets(Agent *agent) {

int ret;
if ((ret = udp_socket_create(&agent->udp_sockets[0], AF_INET)) < 0) {
if ((ret = udp_socket_open(&agent->udp_sockets[0], AF_INET, 0)) < 0) {
LOGE("Failed to create UDP socket.");
return ret;
}
LOGI("create IPv4 UDP socket: %d", agent->udp_sockets[0].fd);

#if CONFIG_IPV6
if ((ret = udp_socket_create(&agent->udp_sockets[1], AF_INET6)) < 0) {
if ((ret = udp_socket_open(&agent->udp_sockets[1], AF_INET6, 0)) < 0) {
LOGE("Failed to create IPv6 UDP socket.");
return ret;
}
Expand Down Expand Up @@ -92,15 +92,15 @@ static int agent_create_host_addr(Agent *agent) {

udp_socket = &agent->udp_sockets[0];
if (ports_get_host_addr(&udp_socket->bind_addr)) {
LOGD("addr: %d.%d.%d.%d", udp_socket->bind_addr.ipv4[0], udp_socket->bind_addr.ipv4[1], udp_socket->bind_addr.ipv4[2], udp_socket->bind_addr.ipv4[3]);
//LOGD("addr: %d.%d.%d.%d", udp_socket->bind_addr.ipv4[0], udp_socket->bind_addr.ipv4[1], udp_socket->bind_addr.ipv4[2], udp_socket->bind_addr.ipv4[3]);
IceCandidate *ice_candidate = agent->local_candidates + agent->local_candidates_count++;
ice_candidate_create(ice_candidate, agent->local_candidates_count, ICE_CANDIDATE_TYPE_HOST, &udp_socket->bind_addr);
}

#if CONFIG_IPV6
udp_socket = &agent->udp_sockets[1];
if (ports_get_host_addr(&udp_socket->bind_addr)) {
LOGD("addr: %x:%x:%x:%x:%x:%x:%x:%x", udp_socket->bind_addr.ipv6[0], udp_socket->bind_addr.ipv6[1], udp_socket->bind_addr.ipv6[2], udp_socket->bind_addr.ipv6[3], udp_socket->bind_addr.ipv6[4], udp_socket->bind_addr.ipv6[5], udp_socket->bind_addr.ipv6[6], udp_socket->bind_addr.ipv6[7]);
//LOGD("addr: %x:%x:%x:%x:%x:%x:%x:%x", udp_socket->bind_addr.ipv6[0], udp_socket->bind_addr.ipv6[1], udp_socket->bind_addr.ipv6[2], udp_socket->bind_addr.ipv6[3], udp_socket->bind_addr.ipv6[4], udp_socket->bind_addr.ipv6[5], udp_socket->bind_addr.ipv6[6], udp_socket->bind_addr.ipv6[7]);
IceCandidate *ice_candidate = agent->local_candidates + agent->local_candidates_count++;
ice_candidate_create(ice_candidate, agent->local_candidates_count, ICE_CANDIDATE_TYPE_HOST, &udp_socket->bind_addr);
}
Expand Down Expand Up @@ -234,8 +234,10 @@ void agent_deinit(Agent *agent) {
*/
void agent_gather_candidate(Agent *agent, const char *urls, const char *username, const char *credential) {

char *port = NULL;
char *pos;
int port;
char hostname[64];
char addr_string[ADDRSTRLEN];
int i;
int addr_type[1] = {AF_INET}; // ipv6 no need stun
Address resolved_addr;
Expand All @@ -247,24 +249,27 @@ void agent_gather_candidate(Agent *agent, const char *urls, const char *username

do {

if ((port = strstr(urls + 5, ":")) == NULL) {
if ((pos = strstr(urls + 5, ":")) == NULL) {
break;
}

resolved_addr.port = atoi(port + 1);
if (resolved_addr.port <= 0) {
port = atoi(pos + 1);
if (port <= 0) {
break;
LOGE("Cannot parse port");
}
LOGI("resolved_addr.port: %d", resolved_addr.port);

snprintf(hostname, port - urls - 5 + 1, "%s", urls + 5);
snprintf(hostname, pos - urls - 5 + 1, "%s", urls + 5);

for (i = 0; i < sizeof(addr_type) / sizeof(addr_type[0]); i++) {
resolved_addr.family = addr_type[i];
if (ports_resolve_addr(hostname, &resolved_addr) == 0) {
LOGI("resolved_addr.ipv4: %d.%d.%d.%d",
resolved_addr.ipv4[0], resolved_addr.ipv4[1], resolved_addr.ipv4[2], resolved_addr.ipv4[3]);

if (ports_resolve_addr(hostname, &resolved_addr) != 0) {
continue;
}

addr_set_port(&resolved_addr, port);
addr_to_string(&resolved_addr, addr_string, sizeof(addr_string));
LOGI("stun/turn server %s:%d", addr_string, port);

if (strncmp(urls, "stun:", 5) == 0) {
LOGD("create stun addr");
Expand Down Expand Up @@ -453,6 +458,7 @@ a=candidate:1 1 UDP 1 36.231.28.50 38143 typ srflx

int agent_connectivity_check(Agent *agent) {

char addr_string[ADDRSTRLEN];
uint8_t buf[1400];
StunMessage msg;

Expand All @@ -465,13 +471,8 @@ int agent_connectivity_check(Agent *agent) {

if (agent->nominated_pair->conncheck % AGENT_CONNCHECK_PERIOD == 0) {

if (agent->nominated_pair->remote->addr.family == AF_INET) {
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);
} else {
char astring[INET6_ADDRSTRLEN];
addr_to_text(&(agent->nominated_pair->remote->addr), astring, INET6_ADDRSTRLEN);
LOGD("send binding request to remote ip: %s, port: %d", astring, agent->nominated_pair->remote->addr.port);
}
addr_to_string(&agent->nominated_pair->remote->addr, addr_string, sizeof(addr_string));
LOGD("send binding request to remote ip: %s, port: %d", addr_string, agent->nominated_pair->remote->addr.port);
agent_create_binding_request(agent, &msg);
agent_socket_send(agent, &agent->nominated_pair->remote->addr, msg.buf, msg.size);
}
Expand Down
2 changes: 1 addition & 1 deletion src/agent.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

#include <pthread.h>

#include "udp.h"
#include "socket.h"
#include "utils.h"
#include "stun.h"
#include "ice.h"
Expand Down
2 changes: 1 addition & 1 deletion src/dtls_srtp.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#include "mbedtls/ssl.h"
#include "dtls_srtp.h"
#include "address.h"
#include "udp.h"
#include "socket.h"
#include "config.h"
#include "utils.h"

Expand Down
1 change: 0 additions & 1 deletion src/dtls_srtp.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@

#include <srtp2/srtp.h>

#include "udp.h"
#include "address.h"

#define SRTP_MASTER_KEY_LENGTH 16
Expand Down
Loading

0 comments on commit 5c53b62

Please sign in to comment.