Skip to content

Commit d1590b0

Browse files
committed
xmpp: validate resolved IPv4 addresses
(cherry picked from commit 15ed735)
1 parent 9f98878 commit d1590b0

1 file changed

Lines changed: 28 additions & 24 deletions

File tree

modules/xmpp/network.c

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,30 @@
3737
#include "../../sr_module.h"
3838
#include "../../resolve.h"
3939

40+
static int net_resolve_ipv4(char *server, struct in_addr *addr)
41+
{
42+
struct hostent *host;
43+
44+
if (inet_aton(server, addr))
45+
return 0;
46+
47+
LM_DBG("resolving %s...\n", server);
48+
49+
host = resolvehost(server,0);
50+
if (!host) {
51+
LM_ERR("resolving %s failed (%s).\n", server, hstrerror(h_errno));
52+
return -1;
53+
}
54+
if (host->h_addrtype != AF_INET || host->h_length != sizeof(*addr) ||
55+
host->h_addr_list[0] == NULL) {
56+
LM_ERR("invalid address family or length for %s\n", server);
57+
return -1;
58+
}
59+
60+
memcpy(addr, host->h_addr_list[0], sizeof(*addr));
61+
return 0;
62+
}
63+
4064
int net_listen(char *server, int port)
4165
{
4266
int fd;
@@ -47,18 +71,8 @@ int net_listen(char *server, int port)
4771
sin.sin_family = AF_INET;
4872
sin.sin_port = htons(port);
4973

50-
if (!inet_aton(server, &sin.sin_addr)) {
51-
struct hostent *host;
52-
53-
LM_DBG("resolving %s...\n", server);
54-
55-
if (!(host = resolvehost(server,0))) {
56-
LM_ERR("resolving %s failed (%s).\n", server,
57-
hstrerror(h_errno));
58-
return -1;
59-
}
60-
memcpy(&sin.sin_addr, host->h_addr_list[0], host->h_length);
61-
}
74+
if (net_resolve_ipv4(server, &sin.sin_addr) < 0)
75+
return -1;
6276

6377
if ((fd = socket(PF_INET, SOCK_STREAM, 0)) < 0) {
6478
LM_ERR("socket() failed: %s\n", strerror(errno));
@@ -95,18 +109,8 @@ int net_connect(char *server, int port)
95109
sin.sin_family = AF_INET;
96110
sin.sin_port = htons(port);
97111

98-
if (!inet_aton(server, &sin.sin_addr)) {
99-
struct hostent *host;
100-
101-
LM_DBG("resolving %s...\n", server);
102-
103-
if (!(host = resolvehost(server,0))) {
104-
LM_ERR("resolving %s failed (%s).\n", server,
105-
hstrerror(h_errno));
106-
return -1;
107-
}
108-
memcpy(&sin.sin_addr, host->h_addr_list[0], host->h_length);
109-
}
112+
if (net_resolve_ipv4(server, &sin.sin_addr) < 0)
113+
return -1;
110114

111115
if ((fd = socket(PF_INET, SOCK_STREAM, 0)) < 0) {
112116
LM_ERR("socket() failed: %s\n", strerror(errno));

0 commit comments

Comments
 (0)