|
22 | 22 | #include <stdint.h> |
23 | 23 | #include <string.h> |
24 | 24 | #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 |
25 | 31 | #include "wolfip.h" |
26 | 32 | #include "config.h" |
27 | 33 |
|
@@ -1995,6 +2001,26 @@ int wolfIP_sock_connect(struct wolfIP *s, int sockfd, const struct wolfIP_sockad |
1995 | 2001 | if ((sockfd & ~MARK_ICMP_SOCKET) >= MAX_ICMPSOCKETS) |
1996 | 2002 | return -WOLFIP_EINVAL; |
1997 | 2003 |
|
| 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 | + |
1998 | 2024 | ts = &s->icmpsockets[sockfd & ~MARK_ICMP_SOCKET]; |
1999 | 2025 | if ((sin->sin_family != AF_INET) || (addrlen < sizeof(struct wolfIP_sockaddr_in))) |
2000 | 2026 | return -WOLFIP_EINVAL; |
@@ -2409,10 +2435,6 @@ int wolfIP_sock_setsockopt(struct wolfIP *s, int sockfd, int level, int optname, |
2409 | 2435 | ts->recv_ttl = enable ? 1 : 0; |
2410 | 2436 | return 0; |
2411 | 2437 | } |
2412 | | - (void)level; |
2413 | | - (void)optname; |
2414 | | - (void)optval; |
2415 | | - (void)optlen; |
2416 | 2438 | return 0; |
2417 | 2439 | } |
2418 | 2440 |
|
@@ -2442,10 +2464,6 @@ int wolfIP_sock_getsockopt(struct wolfIP *s, int sockfd, int level, int optname, |
2442 | 2464 | *optlen = sizeof(int); |
2443 | 2465 | return 0; |
2444 | 2466 | } |
2445 | | - (void)level; |
2446 | | - (void)optname; |
2447 | | - (void)optval; |
2448 | | - (void)optlen; |
2449 | 2467 | return 0; |
2450 | 2468 | } |
2451 | 2469 | 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 |
2680 | 2698 | } |
2681 | 2699 | } |
2682 | 2700 | 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; |
2683 | 2733 | } else return -1; |
2684 | 2734 |
|
2685 | 2735 | } |
|
0 commit comments