Skip to content

Commit 71827fa

Browse files
committed
Support listening for IPv6 connections
The EchoLink proxy protocol supports only IPv4. However, the connection between the proxy and the EchoLink client doesn't need to be IPv4, so listening for incoming clients on IPv6 makes sense. It makes even more sense moving forward because proxies like this may become the only option for accessing EchoLink from IPv6-only hosts.
1 parent a8512cf commit 71827fa

File tree

2 files changed

+20
-7
lines changed

2 files changed

+20
-7
lines changed

include/conn.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,6 @@ void conn_shutdown(struct conn_handle *conn);
217217
* @param[in] conn Target network connection instance
218218
* @param[out] dest Destination ASCII string
219219
*/
220-
void conn_get_remote_addr(const struct conn_handle *conn, char dest[40]);
220+
void conn_get_remote_addr(const struct conn_handle *conn, char dest[46]);
221221

222222
#endif /* _conn_h */

src/conn.c

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ int conn_listen(struct conn_handle *conn)
226226
return -1;
227227
}
228228

229-
hints.ai_family = AF_INET;
229+
hints.ai_family = AF_UNSPEC;
230230
hints.ai_flags = AI_PASSIVE;
231231

232232
ret = getaddrinfo(conn->source_addr, conn->source_port == NULL ? "0" : conn->source_port, &hints, &res);
@@ -403,7 +403,7 @@ int conn_connect(struct conn_handle *conn, const char *addr, const char *port)
403403
goto conn_connect_free_early;
404404
}
405405

406-
priv->sock_fd = socket(AF_INET, res->ai_socktype, res->ai_protocol);
406+
priv->sock_fd = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
407407
if (priv->sock_fd == INVALID_SOCKET)
408408
{
409409
ret = SOCK_ERRNO;
@@ -782,11 +782,24 @@ void conn_shutdown(struct conn_handle *conn)
782782
mutex_unlock_shared(&priv->mutex);
783783
}
784784

785-
void conn_get_remote_addr(const struct conn_handle *conn, char dest[40])
785+
void conn_get_remote_addr(const struct conn_handle *conn, char dest[46])
786786
{
787-
struct conn_priv *priv = (struct conn_priv *)conn->priv;
788-
struct sockaddr_in *addr = (struct sockaddr_in *)&priv->remote_addr;
789-
inet_ntop(addr->sin_family, &addr->sin_addr.s_addr, dest, 40);
787+
const struct conn_priv *priv = (const struct conn_priv *)conn->priv;
788+
const struct sockaddr_in *addr = (const struct sockaddr_in *)&priv->remote_addr;
789+
const struct sockaddr_in6 *addr6 = (const struct sockaddr_in6 *)&priv->remote_addr;
790+
791+
switch (priv->remote_addr.ss_family)
792+
{
793+
case AF_INET:
794+
inet_ntop(addr->sin_family, &addr->sin_addr.s_addr, dest, 46);
795+
break;
796+
case AF_INET6:
797+
inet_ntop(addr6->sin6_family, &addr6->sin6_addr.s6_addr, dest, 46);
798+
break;
799+
default:
800+
strcpy(dest, "(unknown)");
801+
break;
802+
}
790803
}
791804

792805
int conn_in_use(struct conn_handle *conn)

0 commit comments

Comments
 (0)