Skip to content

Commit

Permalink
Add ipv6 ice candidates
Browse files Browse the repository at this point in the history
  • Loading branch information
sepfy committed May 26, 2024
1 parent 4412e58 commit 9bbc7a2
Show file tree
Hide file tree
Showing 11 changed files with 350 additions and 192 deletions.
49 changes: 49 additions & 0 deletions src/address.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
#include <stdlib.h>
#include <stdint.h>
#include <stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>

#include "address.h"

int addr_ipv4_validate(const char *ipv4, size_t len, Address *addr) {
Expand All @@ -21,6 +27,49 @@ int addr_ipv4_validate(const char *ipv4, size_t len, Address *addr) {
return 0;
}
}
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;
}

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;
}
return 0;
}

int addr_equal(const Address *a, const Address *b) {
if (a->family != b->family) {
return 0;
}

switch (a->family) {
case AF_INET:
for (int i = 0; i < 4; i++) {
if (a->ipv4[i] != b->ipv4[i]) {
return 0;
}
}
break;
case AF_INET6:
break;
}

return 1;
}
10 changes: 8 additions & 2 deletions src/address.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,16 @@ struct Address {
uint8_t family;
uint16_t port;
uint8_t ipv4[4];
uint8_t ipv6[16];
uint16_t ipv6[8];

}__attribute__((packed));
};

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

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

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

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

#endif // ADDRESS_H_
Loading

0 comments on commit 9bbc7a2

Please sign in to comment.