-
Notifications
You must be signed in to change notification settings - Fork 0
/
AFDXTransmitter.c
199 lines (157 loc) · 5.47 KB
/
AFDXTransmitter.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <pcap.h>
#include<sys/socket.h>
#include<arpa/inet.h> // for inet_ntoa()
#include<netinet/udp.h> //Provides declarations for udp header
#include<netinet/ip.h> //Provides declarations for ip header
/* ethernet headers are always exactly 14 bytes [1] */
#define SIZE_ETHERNET 14
/* Ethernet addresses are 6 bytes */
#define ETHER_ADDR_LEN 6
struct ethernetHeader {
u_char ether_dhost[ETHER_ADDR_LEN]; /* destination host address */
u_char ether_shost[ETHER_ADDR_LEN]; /* source host address */
u_short ether_type; /* IP? ARP? RARP? etc */
};
struct ipheader{
unsigned char ip_header_len:4; // 4-bit header length (in 32-bit words) normally=5 (Means 20 Bytes may be 24 also)
unsigned char ip_version :4; // 4-bit IPv4 version
unsigned char ip_tos; // IP type of service
unsigned short ip_total_length; // Total length
unsigned short ip_id; // Unique identifier
unsigned char ip_frag_offset :5; // Fragment offset field
unsigned char ip_more_fragment :1;
unsigned char ip_dont_fragment :1;
unsigned char ip_reserved_zero :1;
unsigned char ip_frag_offset1; //fragment offset
unsigned char ip_ttl; // Time to live
unsigned char ip_protocol; // Protocol(TCP,UDP etc)
unsigned short ip_checksum; // IP checksum
unsigned int ip_srcaddr; // Source address
unsigned int ip_destaddr; // Source addres
}; //total ip header panjangnya 20 bytes
struct udpheader{
unsigned short source_port; // Source port no.
unsigned short dest_port; // Dest. port no.
unsigned short udp_length; // Udp packet length
unsigned short udp_checksum; // Udp checksum (optional)
};
int main(int argc, char **argv)
{
pcap_t *adhandle;
char errbuf[PCAP_ERRBUF_SIZE];
u_char packet[100];
int i=0;
u_char *dev;
bpf_u_int32 pNet; /* ip address*/
struct bpf_program fp; /* to hold compiled program */
struct ipheader *v4hdr=NULL;
struct udpheader *uhdr=NULL;
struct ethernetHeader *ethdr=NULL;
printf("\n------------------------------------------------------------------------\n");
printf("\nAFDX TRANSMITTER PROTOTYPE\n");
printf("\n------------------------------------------------------------------------\n");
//Pilih interface yang dipakai
if(argv[1] == NULL)
{
dev="eth0";
printf ("\nUsing interface 'eth0' as default. Try command './sendpacket <interface>' to specify interface\n ");
} else
{
dev = argv[1];
printf ("\nusing interface %s\n.",dev);
}
/* Open the adapter */
if ((adhandle = pcap_open_live(dev, // name of the device
65536, // portion of the packet to capture. It doesn't matter in this case
1, // promiscuous mode (nonzero means promiscuous)
1000, // read timeout
errbuf // error buffer
)) == NULL)
{
fprintf(stderr,"\nUnable to open the adapter. %s is not supported by WinPcap\n", argv[1]);
return 2;
}
pcap_compile(adhandle, &fp, "len <= 100", 1, pNet);
pcap_setfilter(adhandle, &fp);
//ethernet header 14 bytes 0 - 13
ethdr = (struct ethernetHeader*)(packet);
unsigned char vlinkID;
/* To define virtual link ID */
printf("\nPlease enter Virtual Link ID : ");
scanf("%hhu",&vlinkID);
printf("\n------------------------------------------------------------------------\n");
/* mac destination set sesuai AFDX part 7 */
ethdr->ether_dhost[0]=0;
ethdr->ether_dhost[1]=0;
ethdr->ether_dhost[2]=0;
ethdr->ether_dhost[3]=0;
ethdr->ether_dhost[4]=0;
ethdr->ether_dhost[5]=vlinkID; /* Virtual link ID */
/* mac source to set sesuai AFDX part 7 */
ethdr->ether_shost[0]=1;
ethdr->ether_shost[1]=0;
ethdr->ether_shost[2]=0;
ethdr->ether_shost[3]=0;
ethdr->ether_shost[4]=0;
ethdr->ether_shost[5]=vlinkID; /* Virtual link ID */
//type IP
ethdr->ether_type=htons(0x8000);
//header IP
v4hdr = (struct ipheader *)&packet[14]; //lets point to the ip header portion byte ke 15(14) adalah awal header IP
v4hdr->ip_version=4;
v4hdr->ip_header_len=5;
v4hdr->ip_tos = 0;
v4hdr->ip_total_length = htons ( 100 );
v4hdr->ip_id = htons(2);
v4hdr->ip_frag_offset = 0;
v4hdr->ip_frag_offset1 = 0;
v4hdr->ip_reserved_zero = 0;
v4hdr->ip_dont_fragment = 1;
v4hdr->ip_more_fragment = 0;
v4hdr->ip_ttl = 8;
v4hdr->ip_protocol = IPPROTO_UDP;
v4hdr->ip_srcaddr = inet_addr("0.0.0.0");
v4hdr->ip_destaddr = inet_addr("0.0.0.0");
v4hdr->ip_checksum = htons(29320); //manual checksum
//header UDP
uhdr = (struct udpheader *)&packet[34];
uhdr->dest_port = htons(1);
uhdr->source_port = htons(1);
uhdr->udp_length = htons(64);
uhdr->udp_checksum = 0;
/* Payload */
for(i=42;i<150;i++)
{
packet[i]= i;
}
if (vlinkID == 01 ) {
/* Send down the packet */
printf("\nPacket Sent...\n");
if (pcap_sendpacket(adhandle, // Adapter
packet, // buffer with the packet
150 // packet size
) != 0)
{
fprintf(stderr,"\nError sending the packet: %s\n", pcap_geterr(adhandle));
return 3;
}
}
else if (vlinkID == 04 ) {
/* Send down the packet */
printf("\nPacket Sent...\n");
if (pcap_sendpacket(adhandle, // Adapter
packet, // buffer with the packet
150 // packet size
) != 0)
{
fprintf(stderr,"\nError sending the packet: %s\n", pcap_geterr(adhandle));
return 3;
}
else {printf("\nVirtual Link ID %.2x is not defined in this device or already used in another device\n",vlinkID);}
printf("\n------------------------------------------------------------------------\n");
pcap_close(adhandle);
return 0;
}