ping(1)

时间:2023-03-09 01:10:31
ping(1)
/*

ping program for learning IP protocol

    author: jeff
date: 2014/10/25 */ #include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <errno.h> static int ICMP_create();
static int opt_set(int sockfd); int
main(int argc, char* argv[])
{
int sockfd = ICMP_create();
int optset = ;
if(sockfd != -)
printf("create socket ICMP success!\n");
else
printf("create socket ICMP failed [%d]!\n", errno); optset = opt_set(sockfd);
if(optset != -)
printf("sock set success\n");
else
printf("sock set failed\n"); return ;
} static int ICMP_create()
{
return socket(AF_INET, SOCK_RAW, IPPROTO_ICMP);
} static int opt_set(int sockfd)
{
int on =;
return setsockopt(sockfd, IPPROTO_IP, IP_HDRINCL, &on, sizeof(on));
}