Commit ef0d4aee authored by Pavel Tvrdík's avatar Pavel Tvrdík

Socktest: Refactore settings of setsockopts

parent 1a5fd716
......@@ -35,6 +35,33 @@ struct my_packet
#define SK_IP 1
#define SK_UDP 2
#define set(fd, level, optname, value) \
({ \
if (setsockopt(fd, level, optname, &value, sizeof(value)) < 0) \
ERR(#optname); \
printf("Set \t" #level "(" #optname ") to "); \
for (int i = 0; i < sizeof(value); i++) \
printf("%02X ", *(((char *) &value) + i)); \
printf("\n"); \
})
#define enable(fd, level, optname) \
({ \
int one = 1; \
if (setsockopt(fd, level, optname, &one, sizeof(one)) < 0) \
ERR(#optname); \
printf("Enable \t" #level "(" #optname ")\n"); \
})
#define disable(fd, level, optname) \
({ \
int zero = 0; \
if (setsockopt(fd, level, optname, &zero, sizeof(zero)) < 0) \
ERR(#optname); \
printf("Disable\t" #level "(" #optname ")\n"); \
})
#ifdef IPV4
#define AF_IP AF_INET
......@@ -112,30 +139,22 @@ void
init_ttl(int fd, int ttl)
{
#ifdef IPV4
if (setsockopt(fd, IPPROTO_IP, IP_TTL, &ttl, sizeof(ttl)) < 0)
ERR("IP_TTL");
set(fd, IPPROTO_IP, IP_TTL, ttl);
#else
if (setsockopt(fd, IPPROTO_IPV6, IPV6_UNICAST_HOPS, &ttl, sizeof(ttl)) < 0)
ERR("IPV6_UNICAST_HOPS");
set(fd, IPPROTO_IPV6, IPV6_UNICAST_HOPS, ttl);
#endif
}
void
init_dontroute(int fd)
{
int one = 1;
if (setsockopt(fd, SOL_SOCKET, SO_DONTROUTE, &one, sizeof(one)) < 0)
ERR("SO_DONTROUTE");
enable(fd, SOL_SOCKET, SO_DONTROUTE);
}
void
init_bcast(int fd)
{
int one = 1;
if (setsockopt(fd, SOL_SOCKET, SO_BROADCAST, &one, sizeof(one)) < 0)
ERR("SO_BROADCAST");
enable(fd, SOL_SOCKET, SO_BROADCAST);
}
int
......
......@@ -6,54 +6,40 @@
void
init_maddr(int fd, int ifindex, struct in_addr laddr, struct in_addr maddr, int ttl_, int mship)
{
unsigned char zero = 0;
unsigned char ttl = ttl_;
struct ip_mreq mreq = { .imr_interface = laddr, .imr_multiaddr = maddr };
if (setsockopt(fd, IPPROTO_IP, IP_MULTICAST_TTL, &ttl, sizeof(ttl)) < 0)
ERR("IP_MULTICAST_TTL");
set(fd, IPPROTO_IP, IP_MULTICAST_TTL, ttl);
if (setsockopt(fd, IPPROTO_IP, IP_MULTICAST_LOOP, &zero, sizeof(zero)) < 0)
ERR("IP_MULTICAST_LOOP");
disable(fd, IPPROTO_IP, IP_MULTICAST_LOOP);
/* This defines where should we send _outgoing_ multicasts */
if (setsockopt(fd, IPPROTO_IP, IP_MULTICAST_IF, &cf_ifaddr, sizeof(cf_ifaddr)) < 0)
ERR("IP_MULTICAST_IF");
set(fd, IPPROTO_IP, IP_MULTICAST_IF, cf_ifaddr);
if (!mship)
return;
/* And this one sets interface for _receiving_ multicasts from */
if (setsockopt(fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq, sizeof(mreq)) < 0)
ERR("IP_ADD_MEMBERSHIP");
set(fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, mreq);
}
void
init_pktinfo(int fd)
{
int ok = 1;
if (setsockopt(fd, IPPROTO_IP, IP_RECVDSTADDR, &ok, sizeof(ok)) < 0)
ERR("IP_RECVDSTADDR");
if (setsockopt(fd, IPPROTO_IP, IP_RECVIF, &ok, sizeof(ok)) < 0)
ERR("IP_RECVIF");
enable(fd, IPPROTO_IP, IP_RECVDSTADDR);
enable(fd, IPPROTO_IP, IP_RECVIF);
#ifdef IP_RECVTTL
if (setsockopt(fd, IPPROTO_IP, IP_RECVTTL, &ok, sizeof(ok)) < 0)
ERR("IP_RECVTTL");
enable(fd, IPPROTO_IP, IP_RECVTTL);
#endif
}
void
init_hdrincl(int fd)
{
int ok = 1;
if (setsockopt(fd, IPPROTO_IP, IP_HDRINCL, &ok, sizeof(ok)) < 0)
ERR("IP_HDRINCL");
enable(fd, IPPROTO_IP, IP_HDRINCL);
}
......
......@@ -4,37 +4,27 @@ void
init_maddr(int fd, int ifindex, struct in_addr laddr, struct in_addr maddr, int ttl, int mship)
{
struct ip_mreqn mreq;
int zero = 0;
mreq.imr_ifindex = ifindex;
mreq.imr_address = laddr;
mreq.imr_multiaddr = maddr;
if (setsockopt(fd, IPPROTO_IP, IP_MULTICAST_TTL, &ttl, sizeof(ttl)) < 0)
ERR("IP_MULTICAST_TTL");
if (setsockopt(fd, IPPROTO_IP, IP_MULTICAST_LOOP, &zero, sizeof(zero)) < 0)
ERR("IP_MULTICAST_LOOP");
if (setsockopt(fd, IPPROTO_IP, IP_MULTICAST_IF, &mreq, sizeof(mreq)) < 0)
ERR("IP_MULTICAST_IF");
set(fd, IPPROTO_IP, IP_MULTICAST_TTL, ttl);
disable(fd, IPPROTO_IP, IP_MULTICAST_LOOP);
set(fd,IPPROTO_IP, IP_MULTICAST_IF, mreq);
if (!mship)
return;
if (setsockopt(fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq, sizeof(mreq)) < 0)
ERR("IP_ADD_MEMBERSHIP");
set(fd,IPPROTO_IP, IP_ADD_MEMBERSHIP, mreq);
}
void
init_pktinfo(int fd)
{
int ok = 1;
if (setsockopt(fd, IPPROTO_IP, IP_PKTINFO, &ok, sizeof(ok)) < 0)
ERR("IP_PKTINFO");
if (setsockopt(fd, IPPROTO_IP, IP_RECVTTL, &ok, sizeof(ok)) < 0)
ERR("IP_RECVTTL");
enable(fd, IPPROTO_IP, IP_PKTINFO);
enable(fd, IPPROTO_IP, IP_RECVTTL);
}
......
......@@ -4,33 +4,23 @@ void
init_maddr(int fd, int ifindex, struct in6_addr laddr, struct in6_addr maddr, int ttl, int mship)
{
struct ipv6_mreq mreq = { .ipv6mr_multiaddr = maddr, .ipv6mr_interface = ifindex };
int zero = 0;
if (setsockopt(fd, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, &ttl, sizeof(ttl)) < 0)
ERR("IPV6_MULTICAST_HOPS");
if (setsockopt(fd, IPPROTO_IPV6, IPV6_MULTICAST_LOOP, &zero, sizeof(zero)) < 0)
ERR("IPV6_MULTICAST_LOOP");
if (setsockopt(fd, IPPROTO_IPV6, IPV6_MULTICAST_IF, &index, sizeof(index)) < 0)
ERR("IPV6_MULTICAST_IF");
set(fd, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, ttl);
disable(fd, IPPROTO_IPV6, IPV6_MULTICAST_LOOP);
set(fd, IPPROTO_IPV6, IPV6_MULTICAST_IF, ifindex);
if (!mship)
return;
if (setsockopt(fd, IPPROTO_IPV6, IPV6_JOIN_GROUP, &mreq, sizeof(mreq)) < 0)
ERR("IPV6_JOIN_GROUP");
set(fd, IPPROTO_IPV6, IPV6_JOIN_GROUP, mreq);
}
void
init_pktinfo(int fd)
{
int ok = 1;
if (setsockopt(fd, IPPROTO_IPV6, IPV6_RECVPKTINFO, &ok, sizeof(ok)) < 0)
ERR("IPV6_RECVPKTINFO");
if (setsockopt(fd, IPPROTO_IPV6, IPV6_RECVHOPLIMIT, &ok, sizeof(ok)) < 0)
ERR("IPV6_RECVHOPLIMIT");
enable(fd, IPPROTO_IPV6, IPV6_RECVPKTINFO);
enable(fd, IPPROTO_IPV6, IPV6_RECVHOPLIMIT);
}
struct in6_pktinfo *recv_pi = NULL;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment