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
|
void diep(char s[], int r) {
perror(s);
exit(r);
}
void die(char s[], int r) {
fprintf(stderr, "%s() failed\n", s);
exit(r);
}
#define DEST_IP "127.0.0.1"
#define DEST_PORT 37008
int send_tzsp_sample_packet() {
const int sockaddr_len=sizeof(struct sockaddr_in);
const char sample_packet[]={
/*Ether*/ 0xe0,0x91,0xf5,0xa1,0xa3,0xd8,0x00,0x11,0x2f,0xde,0x0d,0xa1,0x08,0x00,
/* IP4 */ 0x45,0x00,0x00,0x3c,0x00,0x00,0x40,0x00,0x40,0x11,0xb4,0x8d,0xc0,0xa8,0x02,0xd2,0xc0,0xa8,0x02,0x01,
/* UDP */ 0xc4,0x89,0x00,0x35,0x00,0x28,0x86,0x5d,
/* DNS */ 0x04,0x59,0x01,0x00,0x00,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x03,0x77,0x77,0x77,
0x06,0x64,0x6f,0x6f,0x64,0x6c,0x65,0x03,0x63,0x6f,0x6d,0x00,0x00,0x01,0x00,0x01
};
const int sample_packet_len=sizeof(sample_packet);
struct timeval sample_packet_time;
uint32_t ts;
int res,len;
int s; /*socket*/
struct sockaddr_in /*si_me,*/ si_other;
char buf[UDP_SEND_BUFLEN];
/* On récupère le timestamp courant pour mettre dans les headers TZSP du paquet capturé d'exemple */
printf("DEBUG : sizeof(time_t)==%i\n", sizeof(time_t));
res=gettimeofday(&sample_packet_time,NULL);
if (res==-1) die("gettimeofday",5);
/* On prépare le socket UDP */
s=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
if (s==-1) diep("socket",10);
memset((char *) &si_other, 0, sockaddr_len);
si_other.sin_family = AF_INET;
si_other.sin_port = htons(DEST_PORT);
res=inet_aton(DEST_IP, &si_other.sin_addr);
if (res==0) die("inet_aton",11);
/* On forge la trame TZSP */
//memset((char *) &buf, 0, UDP_SEND_BUFLEN);
buf[0]=0x01; /* version */
buf[1]=0x01; /* type == Packet for transmit */
buf[2]=0x00; /* Encapsuled protocol 0x0001 == Ethernet */
buf[3]=0x01;
buf[4]=0x0D; /* Tag type TAG_TIMESTAMP */
buf[5]=0x04; /* Tag length : 4 bytes */
/* buf[6,7,8,9] Timestamp on 4 bytes (network order) */
ts=htonl((uint32_t) sample_packet_time.tv_sec); /* FIXME : ce cast est une bonne idée ??? */
memcpy(buf+6, &ts, sizeof(ts));
buf[10]=0x29; /* Tag type TAG_RX_FRAME_LENGTH */
buf[11]=0x02; /* Tag length : 2 bytes */
len=htons((uint16_t) sample_packet_len);
memcpy(buf+12,&len, sizeof(len));
buf[14]=0x00; /* Tag type TAG PADDING (for alignement) */
buf[15]=0x01; /* Tag type TAG END */
/* Raw packet copy */
//TODO : assert that sample_packet_len < MAX_TZSP_PAYLOAD
memcpy(buf+16,sample_packet, sample_packet_len);
/* Envoi du paquet UDP (avec comme charge utile le TZSP) */
res=sendto(s, buf, 16+sample_packet_len, 0, (struct sockaddr *)&si_other, sockaddr_len);
if (res==-1) diep("sendto",12);
close(s);
return 0;
}
|