-
Notifications
You must be signed in to change notification settings - Fork 4
/
raw_socket.c
executable file
·41 lines (29 loc) · 1.01 KB
/
raw_socket.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
//
// Created by Subangkar on 07-Aug-19.
//
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/ip.h>
#include <netinet/ip_icmp.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "icmp_ping.h"
void send_raw_ip_packet(struct iphdr *ip) {
struct sockaddr_in dest_info;
int enable = 1;
// Step 1: Create a raw network socket.
int sock = socket(AF_INET, SOCK_RAW, IPPROTO_RAW);
if (sock < 0) PRINT_ERROR("Could Not create raw socket")
// Step 2: Set socket option.
if (setsockopt(sock, IPPROTO_IP, IP_HDRINCL, &enable, sizeof(enable)) < 0)
PRINT_ERROR("Could Not set socket option");
// Step 3: Provide needed information about destination.
dest_info.sin_family = AF_INET;
dest_info.sin_addr = *(struct in_addr *) &ip->daddr;
// Step 4: Send the packet out.
if (sendto(sock, ip, ntohs(ip->tot_len), 0, (struct sockaddr *) &dest_info, sizeof(dest_info)) < 0)
PRINT_ERROR("Could not send ip packet");
close(sock);
}