Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <errno.h>
- #include <unistd.h>
- #include <netdb.h>
- #include <sys/socket.h>
- #include <netinet/in.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <netinet/ip_icmp.h>
- #include <arpa/inet.h>
- #include <string.h>
- char msg[300];
- int main()
- {
- char adr[] = "127.0.0.1";
- int fd=socket(PF_INET,SOCK_DGRAM,IPPROTO_ICMP);
- if (fd==-1) {
- return -1;
- }
- const size_t req_size=8;
- struct icmphdr req;
- req.type=8;
- req.code=0;
- req.checksum=0;
- req.un.echo.id=htons(12);
- req.un.echo.sequence=htons(1);
- struct sockaddr_in sin;
- sin.sin_family = AF_INET;
- sin.sin_addr.s_addr = inet_addr (adr);
- if (sendto(fd,&req,req_size,0,
- (struct sockaddr *) &sin,sizeof(sin))==-1) {
- return -1;
- }
- int n = recv(fd, msg, sizeof(msg), 0);
- if(n <= 0) {
- printf("recv fail\n");
- return -1;
- }
- struct icmphdr rcv_hdr;
- memcpy(&rcv_hdr, msg, sizeof rcv_hdr);
- if (rcv_hdr.type == ICMP_ECHOREPLY) {
- printf("ICMP Reply, id=0x%x, sequence = 0x%x\n",
- rcv_hdr.un.echo.id, rcv_hdr.un.echo.sequence);
- } else {
- printf("Got ICMP packet with type 0x%x ?!?\n", rcv_hdr.type);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement