Bonjour,

je développe un programme permettant d'envoyer des sockets raw. Le programme envoi bien la requête mais le kernel ajoute son propre entête IP.

L'option hdrincl permet de spécifier que l'entête IP ne doit pas être ajouté. Cependant lorsque je modifie cette option j'ai l'erreur suivante :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
 
Sendto error: Invalid argument
Errno = 22.
La documentation suivante http://developer.apple.com/mac/libra...man4/ip.4.html

indique qu'il faut bien remplir tous les champs de l'entête IP pour que cette option fonctionne, ce que je pense faire.

Voici mon 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
 
int main (int argc, char *argv[])
{
	struct ip iphdr;
 
	iphdr.ip_hl = sizeof(struct ip)/4;
	iphdr.ip_v = IPVERSION;
	iphdr.ip_tos = 0;
	iphdr.ip_len = ntohs(20);
	iphdr.ip_id = ntohs(0);
	iphdr.ip_off = ntohs(0);
	iphdr.ip_ttl = 200;
	iphdr.ip_p = IPPROTO_IP;
	inet_aton("192.168.1.2",&iphdr.ip_src);
	inet_aton("127.0.0.1",&iphdr.ip_dst);
	iphdr.ip_sum = ntohs(checksum((char * ) &iphdr, 20));
 
	printf("%02x\n", htons(iphdr.ip_sum));
	int s;
 
	if((s = socket(PF_INET,SOCK_RAW,IPPROTO_ICMP)) == -1)
	{
		perror("Socket creation error");
		printf("Errno = %d.\n", errno);
	}
	else
	{
		struct sockaddr_in to;
		int hincl = 0;
 
		if(setsockopt(s, IPPROTO_IP, IP_HDRINCL, &hincl, sizeof(hincl)) == -1)
		{
			perror("Setsockopt error");
			printf("Errno = %d\n",errno);
		}
 
		to.sin_family = AF_INET;
		to.sin_port = 0;
		to.sin_addr.s_addr = inet_addr("127.0.0.1");
		size_t i;
		for(i = 0; i < sizeof(to.sin_zero); ++i) {
			to.sin_zero[i] = 0;
		}
 
		if(sendto(s, &iphdr, sizeof(struct ip), 0, (struct sockaddr *) &to,
							sizeof(struct sockaddr_in)) == -1)
		{
			perror("Sendto error");
			printf("Errno = %d.\n", errno);
		}
		else {
			printf("Datagram sent...\n");
		}
	}
	return 0;
}
Auriez-vous une idée d'où le problème pourrait venir.

PS : J'utilise Mac OSx 10.6.2.

Merci.