Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix support for link-local IPv6 addresses #32

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 22 additions & 5 deletions rsmb/src/MQTTSPacket.c
Original file line number Diff line number Diff line change
Expand Up @@ -694,18 +694,36 @@ int MQTTSPacket_sendPacketBuffer(int socket, char* addr, PacketBuffer buf)
{
char *port;
int rc = 0;
char a[strlen(addr) + 1];

FUNC_ENTRY;
port = strrchr(addr, ':') + 1;

/* grab a copy so we can modify it */
strcpy(a, addr);

/* decode and remove port */
port = strrchr(a, ':') + 1;
*(port - 1) = '\0';
if (strchr(addr, ':'))

/* check if IPv6 */
if (strchr(a, ':'))
{
struct sockaddr_in6 cliaddr6;
memset(&cliaddr6, '\0', sizeof(cliaddr6));
cliaddr6.sin6_family = AF_INET6;
if (inet_pton(cliaddr6.sin6_family, addr, &cliaddr6.sin6_addr) == 0)

/* decode and remove scope ID */
if (strchr(a, '%')) {
char *scope = strrchr(a, '%') + 1;
*(scope - 1) = '\0';
cliaddr6.sin6_scope_id = atoi(scope);
}

if (inet_pton(cliaddr6.sin6_family, a, &cliaddr6.sin6_addr) == 0)
Socket_error("inet_pton", socket);

cliaddr6.sin6_port = htons(atoi(port));

if ((rc = sendto(socket, buf.data, buf.len, 0, (const struct sockaddr*)&cliaddr6, sizeof(cliaddr6))) == SOCKET_ERROR)
Socket_error("sendto", socket);
else
Expand All @@ -715,15 +733,14 @@ int MQTTSPacket_sendPacketBuffer(int socket, char* addr, PacketBuffer buf)
{
struct sockaddr_in cliaddr;
cliaddr.sin_family = AF_INET;
if (inet_pton(cliaddr.sin_family, addr, &cliaddr.sin_addr.s_addr) == 0)
if (inet_pton(cliaddr.sin_family, a, &cliaddr.sin_addr.s_addr) == 0)
Socket_error("inet_pton", socket);
cliaddr.sin_port = htons(atoi(port));
if ((rc = sendto(socket, buf.data, buf.len, 0, (const struct sockaddr*)&cliaddr, sizeof(cliaddr))) == SOCKET_ERROR)
Socket_error("sendto", socket);
else
rc = 0;
}
*(port - 1) = ':';

FUNC_EXIT_RC(rc);
return rc;
Expand Down
15 changes: 14 additions & 1 deletion rsmb/src/Socket.c
Original file line number Diff line number Diff line change
Expand Up @@ -1769,11 +1769,15 @@ char* Socket_getaddrname(struct sockaddr* sa, int sock)
* maximum length of the address string
*/
#define ADDRLEN INET6_ADDRSTRLEN+1
/**
* maximum length of the scope string
*/
#define SCOPELEN 3
/**
* maximum length of the port string
*/
#define PORTLEN 10
static char addr_string[ADDRLEN + PORTLEN];
static char addr_string[ADDRLEN + SCOPELEN + PORTLEN];

#if defined(WIN32)
int buflen = ADDRLEN*2;
Expand All @@ -1797,6 +1801,15 @@ char* Socket_getaddrname(struct sockaddr* sa, int sock)
struct sockaddr_in6 *sin = (struct sockaddr_in6 *)sa;
if (inet_ntop(AF_INET6, &sin->sin6_addr, addr_string, ADDRLEN) == NULL)
Socket_error("inet_ntop", sock);

/* check if link-local; append scope_id to address if so */
char ll[] = "fe80::";
struct in6_addr link_local_prefix;
inet_pton(AF_INET6, ll, &link_local_prefix);
if (!memcmp(&link_local_prefix, &sin->sin6_addr, 8))
sprintf(&addr_string[strlen(addr_string)], "%%%d", sin->sin6_scope_id);

/* append port */
sprintf(&addr_string[strlen(addr_string)], ":%d", ntohs(sin->sin6_port));
}
#endif
Expand Down