-
Notifications
You must be signed in to change notification settings - Fork 0
/
send.c
62 lines (51 loc) · 1.46 KB
/
send.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/**
* Julia Matuszewska 324093
**/
#include "send.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <errno.h>
#include <sys/time.h>
#include <assert.h>
#include <arpa/inet.h>
#include <netinet/ip.h>
#include <sys/socket.h>
u_int16_t compute_icmp_checksum(const void *buff, int length) {
assert (length % 2 == 0);
u_int32_t sum;
const u_int16_t* ptr = buff;
for (sum = 0; length > 0; length -= 2) {
sum += *ptr++;
}
sum = (sum >> 16) + (sum & 0xffff);
return (u_int16_t)(~(sum + (sum >> 16)));
}
struct icmp create_packet(uint16_t id, int seq) {
struct icmp icmp_header;
icmp_header.icmp_type = ICMP_ECHO;
icmp_header.icmp_code = 0;
icmp_header.icmp_hun.ih_idseq.icd_seq = seq;
icmp_header.icmp_hun.ih_idseq.icd_id = id;
icmp_header.icmp_cksum = 0;
icmp_header.icmp_cksum = compute_icmp_checksum((u_int16_t*) &icmp_header,
sizeof(icmp_header));
return icmp_header;
}
int send_packet(int sockfd, struct sockaddr_in *recipient, uint16_t id, int ttl,
int seq) {
/* Setting socket options */
if (setsockopt(sockfd, IPPROTO_IP, IP_TTL, &ttl, sizeof(int)) < 0)
return -1;
struct icmp icmp_header = create_packet(id, seq);
/* Sending the packet */
return sendto(
sockfd,
&icmp_header,
sizeof(icmp_header),
0,
(struct sockaddr *)recipient,
sizeof(*recipient)
);
}