Salut,

J'essaie d'envoyer une trame ip en C, mais le format TCP n'a pas l'air bon (pourtant j'ai l'impression d'avoir bien mis tous les champs au bon endroit...).

Voici le problème (dans wireshark):
http://img166.imageshack.us/img166/2989/iptcpdn7.png

Et voici le code:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
#include <arpa/inet.h>
#include <string.h>
#include <stdio.h>
 
typedef struct ipheader {
	unsigned char hl:4, v:4; /* this means that each member is 4 bits */
	unsigned char tos;
	unsigned short int len;
	unsigned short int id;
	unsigned short int off;
	unsigned char ttl;
	unsigned char p;
	unsigned short int sum;
	unsigned int src;
	unsigned int dst;
} * t_ipheader; /* total ip header length: 20 bytes (=160 bits) */
 
typedef struct tcpheader {
	unsigned short int sport;
	unsigned short int dport;
	unsigned int seq;
	unsigned int ack;
 	unsigned char x2:4, off:4;
	unsigned char flags;
	unsigned short int win;
	unsigned short int sum;
	unsigned short int urp;
} * t_tcpheader; /* total tcp header length: 20 bytes (=160 bits) */
 
void fill_ipheader(t_ipheader header) {
	//t_ipheader header = (t_ipheader) malloc(sizeof(ipheader));
	header -> hl = 5; // header is 5 * 32 bits
	header -> v = 4; // ip v4
	header -> tos = 0; // type of service, don't care
	header -> len = sizeof(struct ipheader) + sizeof(struct tcpheader);
	header -> id = 0; // id, don't care
	header -> off = 0; // first fragment
	header -> ttl = 1; // on lan, 1 router, which provide wifi
	header -> p = 6; // tcp protocol;
	header -> sum = 0;
	header -> src = inet_addr("192.168.0.1");
	header -> dst = inet_addr("192.168.0.1");
}
 
void fill_tcpheader(t_tcpheader header) {
	//t_tcpheader header = (t_tcpheader) malloc(sizeof(tcpheader));
	header -> sport = 1863;
	header -> dport = 59575;
	header -> seq = 127;
	header -> ack = 0;
	header -> x2 = 0;
	header -> off = 0x8;
	header -> flags = 18;
	header -> win = 0xfd91;
	header -> sum = 0;
	header -> urp = 0;
}
 
int main() {
	int s = socket (PF_INET, SOCK_RAW, IPPROTO_TCP);
	char datagram[4096];
	memset (datagram, 0, 4096);
	fill_ipheader((t_ipheader)datagram);
	fill_tcpheader((t_tcpheader)(datagram + sizeof(struct ipheader)));
 
	char* offset = datagram + sizeof(struct ipheader) + sizeof(struct tcpheader);
	offset[0] = 5;
 
	struct sockaddr_in sin;
 
	sin.sin_family = AF_INET;
	sin.sin_port = htons (1863);
	sin.sin_addr.s_addr = inet_addr ("192.168.0.1");
 
 {				/* lets do it the ugly way.. */
    int one = 1;
    const int *val = &one;
    if (setsockopt (s, IPPROTO_IP, IP_HDRINCL, val, sizeof (one)) < 0)
      printf ("Warning: Cannot set HDRINCL!\n");
  }
 
	if (sendto (s, datagram, ((t_ipheader)datagram)->len, 0,
	    		(struct sockaddr *) &sin, sizeof (sin)) < 0)	
		printf ("error\n");
	else
		printf (".");
 
}