5 #include <netinet/in.h> 6 #include <netinet/ip.h> 7 #include <netinet/ip_icmp.h> 24 main (
int argc,
char **argv)
29 char *
interface =
NULL;
32 while ((ch = getopt(argc, argv,
"h?" "I:" "d")) != EOF) {
53 if ((sd = socket (AF_INET6, SOCK_RAW, IPPROTO_IPIP)) < 0) {
54 perror (
"Failed to get socket descriptor ");
59 snprintf(ifr.ifr_name,
sizeof(ifr.ifr_name),
"%s", interface);
62 if (setsockopt (sd, SOL_SOCKET, SO_BINDTODEVICE, (
void *) &ifr,
sizeof (ifr)) < 0) {
63 perror (
"SO_BINDTODEVICE failed");
66 if (debug) printf(
"Binding to interface %s\n", interface);
69 struct sockaddr_in6 src_addr;
70 socklen_t addrlen =
sizeof(src_addr);
71 char source[INET6_ADDRSTRLEN+1];
73 uint8_t inpack[IP_MAXPACKET];
75 if ((len = recvfrom(sd, inpack,
sizeof(inpack), 0, (
struct sockaddr *)&src_addr, &addrlen)) < 0) {
76 perror(
"recvfrom failed ");
78 if (inet_ntop(AF_INET6, &src_addr.sin6_addr, source, INET6_ADDRSTRLEN) ==
NULL) {
79 perror(
"inet_ntop() failed.");
84 struct iphdr *
ip = (
struct iphdr *)inpack;
89 ip->saddr = ip->daddr;
92 switch (ip->protocol) {
94 if (debug) printf (
"ICMP Echo request from %s\n", source);
95 icmp = (
struct icmphdr *)&ip[1];
96 icmp->type = ICMP_ECHOREPLY;
99 fprintf(stderr,
"Unsupported protocol %d", ip->protocol);
101 if (len = sendto(sd, inpack, len, 0, (
struct sockaddr *)&src_addr, addrlen) < 0) {
102 perror(
"sendto failed ");
108 return (EXIT_SUCCESS);
Optimized string handling code, including c11-compliant "safe C library" variants.
clib_memset(h->entries, 0, sizeof(h->entries[0]) *entries)
int main(int argc, char **argv)