Skip to content

Commit 85a345e

Browse files
committed
Added ICMP sockets + tests
1 parent 4e7bf9e commit 85a345e

1 file changed

Lines changed: 58 additions & 8 deletions

File tree

src/wolfip.c

Lines changed: 58 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@
2222
#include <stdint.h>
2323
#include <string.h>
2424
#include <stddef.h>
25+
#include <unistd.h>
26+
#ifdef WOLF_POSIX
27+
#include <poll.h>
28+
#include <sys/socket.h>
29+
#include <netinet/in.h>
30+
#endif
2531
#include "wolfip.h"
2632
#include "config.h"
2733

@@ -1995,6 +2001,26 @@ int wolfIP_sock_connect(struct wolfIP *s, int sockfd, const struct wolfIP_sockad
19952001
if ((sockfd & ~MARK_ICMP_SOCKET) >= MAX_ICMPSOCKETS)
19962002
return -WOLFIP_EINVAL;
19972003

2004+
ts = &s->icmpsockets[sockfd & ~MARK_ICMP_SOCKET];
2005+
if ((sin->sin_family != AF_INET) || (addrlen < sizeof(struct wolfIP_sockaddr_in)))
2006+
return -WOLFIP_EINVAL;
2007+
ts->remote_ip = ee32(sin->sin_addr.s_addr);
2008+
if_idx = wolfIP_route_for_ip(s, ts->remote_ip);
2009+
conf = wolfIP_ipconf_at(s, if_idx);
2010+
ts->if_idx = (uint8_t)if_idx;
2011+
if (ts->local_ip == 0 && conf && conf->ip != IPADDR_ANY)
2012+
ts->local_ip = conf->ip;
2013+
else if (ts->local_ip == 0) {
2014+
struct ipconf *primary = wolfIP_primary_ipconf(s);
2015+
if (primary && primary->ip != IPADDR_ANY)
2016+
ts->local_ip = primary->ip;
2017+
}
2018+
return 0;
2019+
} else if (sockfd & MARK_ICMP_SOCKET) {
2020+
struct ipconf *conf;
2021+
if ((sockfd & ~MARK_ICMP_SOCKET) >= MAX_ICMPSOCKETS)
2022+
return -WOLFIP_EINVAL;
2023+
19982024
ts = &s->icmpsockets[sockfd & ~MARK_ICMP_SOCKET];
19992025
if ((sin->sin_family != AF_INET) || (addrlen < sizeof(struct wolfIP_sockaddr_in)))
20002026
return -WOLFIP_EINVAL;
@@ -2409,10 +2435,6 @@ int wolfIP_sock_setsockopt(struct wolfIP *s, int sockfd, int level, int optname,
24092435
ts->recv_ttl = enable ? 1 : 0;
24102436
return 0;
24112437
}
2412-
(void)level;
2413-
(void)optname;
2414-
(void)optval;
2415-
(void)optlen;
24162438
return 0;
24172439
}
24182440

@@ -2442,10 +2464,6 @@ int wolfIP_sock_getsockopt(struct wolfIP *s, int sockfd, int level, int optname,
24422464
*optlen = sizeof(int);
24432465
return 0;
24442466
}
2445-
(void)level;
2446-
(void)optname;
2447-
(void)optval;
2448-
(void)optlen;
24492467
return 0;
24502468
}
24512469
int wolfIP_sock_close(struct wolfIP *s, int sockfd)
@@ -2680,6 +2698,38 @@ int wolfIP_sock_bind(struct wolfIP *s, int sockfd, const struct wolfIP_sockaddr
26802698
}
26812699
}
26822700
return 0;
2701+
} else if (sockfd & MARK_ICMP_SOCKET) {
2702+
if ((sockfd & ~MARK_ICMP_SOCKET) >= MAX_ICMPSOCKETS)
2703+
return -WOLFIP_EINVAL;
2704+
ts = &s->icmpsockets[sockfd & ~MARK_ICMP_SOCKET];
2705+
if (ts->src_port != 0)
2706+
return -1;
2707+
if ((sin->sin_family != AF_INET) || (addrlen < sizeof(struct wolfIP_sockaddr_in)))
2708+
return -1;
2709+
{
2710+
ip4 prev_ip = ts->local_ip;
2711+
uint16_t prev_id = ts->src_port;
2712+
uint16_t new_id = ee16(sin->sin_port);
2713+
ts->if_idx = (uint8_t)if_idx;
2714+
if (bind_ip != IPADDR_ANY)
2715+
ts->local_ip = bind_ip;
2716+
else if (conf && conf->ip != IPADDR_ANY)
2717+
ts->local_ip = conf->ip;
2718+
else {
2719+
struct ipconf *primary = wolfIP_primary_ipconf(s);
2720+
if (primary && primary->ip != IPADDR_ANY)
2721+
ts->local_ip = primary->ip;
2722+
}
2723+
ts->src_port = new_id;
2724+
if (wolfIP_filter_notify_socket_event(
2725+
WOLFIP_FILT_BINDING, s, ts,
2726+
ts->local_ip, new_id, IPADDR_ANY, 0) != 0) {
2727+
ts->local_ip = prev_ip;
2728+
ts->src_port = prev_id;
2729+
return -1;
2730+
}
2731+
}
2732+
return 0;
26832733
} else return -1;
26842734

26852735
}

0 commit comments

Comments
 (0)