mirror of
https://github.com/obgm/libcoap.git
synced 2026-11-25 00:21:09 +03:00
coap_io.c: platform-specific network I/O for FreeBSD
Processing of ancillary data for IPv4 works different on FreeBSD. This change uses IP_RECVDSTADDR instead of IP_PKTINFO on FreeBSD. Setting a the source address with IP_SENDSRCADDR always failed with error 22 (Invalid argument) for sendmsg() and thus has not been implemented. Note that for IPv4, this is not a big problem as there is usually only one interface address even if bound to INADDR_ANY.
This commit is contained in:
1 parent
11e905ed52
commit
92ecd52454
1 file changed
+36
-7
+36
-7
@@ -38,6 +38,24 @@
|
||||
#include "coap_io.h"
|
||||
|
||||
#ifdef WITH_POSIX
|
||||
/* define generic PKTINFO for IPv4 */
|
||||
#if defined(IP_PKTINFO)
|
||||
# define GEN_IP_PKTINFO IP_PKTINFO
|
||||
#elif defined(IP_RECVDSTADDR)
|
||||
# define GEN_IP_PKTINFO IP_RECVDSTADDR
|
||||
#else
|
||||
# error "Need IP_PKTINFO or IP_RECVDSTADDR to request ancillary data from OS."
|
||||
#endif /* IP_PKTINFO */
|
||||
|
||||
/* define generic KTINFO for IPv6 */
|
||||
#ifdef IPV6_RECVPKTINFO
|
||||
# define GEN_IPV6_PKTINFO IPV6_RECVPKTINFO
|
||||
#elif defined(IPV6_PKTINFO)
|
||||
# define GEN_IPV6_PKTINFO IPV6_PKTINFO
|
||||
#else
|
||||
# error "Need IPV6_PKTINFO or IPV6_RECVPKTINFO to request ancillary data from OS."
|
||||
#endif /* IPV6_RECVPKTINFO */
|
||||
|
||||
struct coap_packet_t {
|
||||
coap_if_handle_t hnd; /**< the interface handle */
|
||||
coap_address_t src; /**< the packet's source address */
|
||||
@@ -133,17 +151,12 @@ coap_new_endpoint(const coap_address_t *addr, int flags) {
|
||||
on = 1;
|
||||
switch(addr->addr.sa.sa_family) {
|
||||
case AF_INET:
|
||||
if (setsockopt(sockfd, IPPROTO_IP, IP_PKTINFO, &on, sizeof(on)) < 0)
|
||||
if (setsockopt(sockfd, IPPROTO_IP, GEN_IP_PKTINFO, &on, sizeof(on)) < 0)
|
||||
coap_log(LOG_ALERT, "coap_new_endpoint: setsockopt IP_PKTINFO\n");
|
||||
break;
|
||||
case AF_INET6:
|
||||
#ifdef IPV6_RECVPKTINFO
|
||||
if (setsockopt(sockfd, IPPROTO_IPV6, IPV6_RECVPKTINFO, &on, sizeof(on)) < 0)
|
||||
coap_log(LOG_ALERT, "coap_new_endpoint: setsockopt IPV6_RECVPKTINFO\n");
|
||||
#else /* IPV6_RECVPKTINFO */
|
||||
if (setsockopt(sockfd, IPPROTO_IPV6, IPV6_PKTINFO, &on, sizeof(on)) < 0)
|
||||
if (setsockopt(sockfd, IPPROTO_IPV6, GEN_IPV6_PKTINFO, &on, sizeof(on)) < 0)
|
||||
coap_log(LOG_ALERT, "coap_new_endpoint: setsockopt IPV6_PKTINFO\n");
|
||||
#endif /* IPV6_RECVPKTINFO */
|
||||
break;
|
||||
default:
|
||||
coap_log(LOG_ALERT, "coap_new_endpoint: unsupported sa_family\n");
|
||||
@@ -291,6 +304,7 @@ coap_network_send(struct coap_context_t *context UNUSED_PARAM,
|
||||
break;
|
||||
}
|
||||
case AF_INET: {
|
||||
#if defined(IP_PKTINFO)
|
||||
struct cmsghdr *cmsg;
|
||||
struct in_pktinfo *pktinfo;
|
||||
|
||||
@@ -316,6 +330,7 @@ coap_network_send(struct coap_context_t *context UNUSED_PARAM,
|
||||
&local_interface->addr.addr.sin.sin_addr,
|
||||
local_interface->addr.size);
|
||||
}
|
||||
#endif /* IP_PKTINFO */
|
||||
break;
|
||||
}
|
||||
default:
|
||||
@@ -491,6 +506,7 @@ coap_network_read(coap_endpoint_t *ep, coap_packet_t **packet) {
|
||||
}
|
||||
|
||||
/* local interface for IPv4 */
|
||||
#if defined(IP_PKTINFO)
|
||||
if (cmsg->cmsg_level == SOL_IP && cmsg->cmsg_type == IP_PKTINFO) {
|
||||
union {
|
||||
unsigned char *c;
|
||||
@@ -508,6 +524,19 @@ coap_network_read(coap_endpoint_t *ep, coap_packet_t **packet) {
|
||||
|
||||
break;
|
||||
}
|
||||
#elif defined(IP_RECVDSTADDR)
|
||||
if (cmsg->cmsg_level == IPPROTO_IP && cmsg->cmsg_type == IP_RECVDSTADDR) {
|
||||
(*packet)->ifindex = 0;
|
||||
|
||||
memcpy(&(*packet)->dst.addr.sin.sin_addr,
|
||||
CMSG_DATA(cmsg), sizeof(struct in_addr));
|
||||
|
||||
(*packet)->src.size = mhdr.msg_namelen;
|
||||
assert(memcmp(&(*packet)->src.addr.st, mhdr.msg_name, (*packet)->src.size) == 0);
|
||||
|
||||
break;
|
||||
}
|
||||
#endif /* IP_PKTINFO */
|
||||
}
|
||||
|
||||
if (!is_local_if(&ep->addr, &(*packet)->dst)) {
|
||||
|
||||
Reference in new issue
Block a user