-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathudp.c
More file actions
60 lines (45 loc) · 1.86 KB
/
udp.c
File metadata and controls
60 lines (45 loc) · 1.86 KB
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
/*
* name: udp.c
* this file is part of the "Net-Injector" project. It defines the write_udp() function
* Copyright (C) 2002 by Jean Philippe GUILLEMIN <jp.guillemin@free.fr>
* license: This software is under GPL license
* date: 04 17 2003
* rev: 0.8
0 7 8 15 16 23 24 31
+--------+--------+--------+--------+
| Source | Destination |
| Port | Port |
+--------+--------+--------+--------+
| | |
| Length | Checksum |
+--------+--------+--------+--------+
|
| data octets ...
+---------------- ...
*/
#include "neti.h"
/* function that create the UDP header and data field
the result is a pointer (*udp_packet) to the begining of UDP header
*/
int write_udp(char *udp_packet, int size, u_int32_t ip_saddr, u_int32_t ip_daddr){
struct udphdr *udph;
struct pseudohdr *pseudoh;
pseudoh = (struct pseudohdr*) udp_packet; /* address of pseudo-header */
udph = (struct udphdr*) (udp_packet+PSEUDOHDR_SIZE); /* address of udp-header */
data = (char*) (udp_packet+PSEUDOHDR_SIZE+UDPHDR_SIZE); /* address of begining of payload */
memset(data, 0, myhdr->data_size);
memset(udp_packet, 0, PSEUDOHDR_SIZE+size);
memcpy (data, payload, myhdr->data_size);
/*filling UDP header */
udph->source = myhdr->inet_src_port;
udph->dest = myhdr->inet_dst_port;
udph->len = UDPHDR_SIZE + myhdr->data_size; /* size of UDP packet */
udph->check = 0; /* checksum will be set later */
/*filling PSEUDO header */
memcpy(&pseudoh->saddr, &ip_saddr, 4);
memcpy(&pseudoh->daddr, &ip_daddr, 4);
pseudoh->protocol = 17; /* layer 4 protocole */
pseudoh->lenght = htons(size);
udph->check = sum((u_int16_t *) udp_packet, PSEUDOHDR_SIZE + size);
return EXIT_SUCCESS;
}