Blame view

Network/sender.c 2.6 KB
1f9d795a   Speedclocker   Sniffer et sender
1
2
3
4
  #include <sys/types.h>
  #include <sys/socket.h>
  #include <stdio.h>
  #include <stdlib.h>
351d9caa   Speedclocker   Ajout de serveur/...
5
  #include <unistd.h>
1f9d795a   Speedclocker   Sniffer et sender
6
7
8
  #include <string.h>
  #include <netdb.h>
  #include <arpa/inet.h>
32c9271a   skhinach   modif
9
  #include <errno.h>
1f9d795a   Speedclocker   Sniffer et sender
10
11
  
  
1a2c5f60   skhinach   Quelques modifs f...
12
  int initializeSocketUDP(char* service, char* adresse)
1f9d795a   Speedclocker   Sniffer et sender
13
  {
32c9271a   skhinach   modif
14
15
  	int sock_id;
  	struct addrinfo precisions, *resultat=NULL, *origine; // On stocke dans précisions nos besoins pour le socket, dans resultat l'adresse qui respectera les requêtes
1f9d795a   Speedclocker   Sniffer et sender
16
  
1f9d795a   Speedclocker   Sniffer et sender
17
18
19
20
  
  	memset(&precisions, 0, sizeof precisions);
  	precisions.ai_family = AF_UNSPEC;
  	precisions.ai_socktype = SOCK_DGRAM;
32c9271a   skhinach   modif
21
  
a69a94a7   skhinach   Ajout de gestion ...
22
  /*"172.26.145.35"*/
a629f0c7   Antoine Moreau   Fin.
23
  	if(getaddrinfo(adresse, service, &precisions, &origine)<0) { fprintf(stderr, "Erreur getaddrinfo\n"); return(-1); }
1f9d795a   Speedclocker   Sniffer et sender
24
25
26
  	
  	for(struct addrinfo* i=origine; i!=NULL && resultat==NULL; i=i->ai_next)
  	{
1f9d795a   Speedclocker   Sniffer et sender
27
  		if(i->ai_family==AF_INET)
351d9caa   Speedclocker   Ajout de serveur/...
28
  		{
1f9d795a   Speedclocker   Sniffer et sender
29
  			resultat=i;
351d9caa   Speedclocker   Ajout de serveur/...
30
31
  			printf("Test : %d", origine->ai_addrlen);
  		}
1f9d795a   Speedclocker   Sniffer et sender
32
  	}
351d9caa   Speedclocker   Ajout de serveur/...
33
  
351d9caa   Speedclocker   Ajout de serveur/...
34
35
  	struct sockaddr_in* test=(struct sockaddr_in*)(resultat->ai_addr);
  	printf("Addr : %x\n", test->sin_addr.s_addr);
1f9d795a   Speedclocker   Sniffer et sender
36
37
38
39
  	if((sock_id=socket(resultat->ai_family, resultat->ai_socktype, resultat->ai_protocol))<0) { fprintf(stderr, "Erreur socket\n"); return(-1); }
  
  	int val=1;
  	
a69a94a7   skhinach   Ajout de gestion ...
40
41
42
43
  	if(setsockopt(sock_id, SOL_SOCKET, SO_BROADCAST, &val, sizeof(val))<0) { fprintf(stderr, "Erreur setsockopt\n"); return(-1); }
  
  	if(setsockopt(sock_id, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val))<0) { fprintf(stderr, "Erreur setsockopt\n"); return(-1); }
  
351d9caa   Speedclocker   Ajout de serveur/...
44
  
1f9d795a   Speedclocker   Sniffer et sender
45
  	freeaddrinfo(origine);
32c9271a   skhinach   modif
46
  	return sock_id;
1f9d795a   Speedclocker   Sniffer et sender
47
48
49
  }
  
  
1f9d795a   Speedclocker   Sniffer et sender
50
51
  int sendUDPBroadcast(char* message, int port)
  {
351d9caa   Speedclocker   Ajout de serveur/...
52
53
  	static struct sockaddr_in addrUDP;
  	addrUDP.sin_family=AF_INET;
a69a94a7   skhinach   Ajout de gestion ...
54
  	addrUDP.sin_port=htons(port); // Converti au format réseau
1f9d795a   Speedclocker   Sniffer et sender
55
56
  
  	struct in_addr tmpadd={0xffffffff};	
351d9caa   Speedclocker   Ajout de serveur/...
57
58
  	addrUDP.sin_addr=tmpadd;
  	
32c9271a   skhinach   modif
59
60
  	char tmp_str[15];
  	sprintf(tmp_str, "%d", port);
1a2c5f60   skhinach   Quelques modifs f...
61
  	int sock_id=initializeSocketUDP( tmp_str , "255.255.255.255");
1f9d795a   Speedclocker   Sniffer et sender
62
  
32c9271a   skhinach   modif
63
  	printf("Initialize via Broadcast %d\n", sock_id);
32c9271a   skhinach   modif
64
  	
08f8ef8d   skhinach   modifs
65
  	if(sendto(sock_id, message, sizeof(message), 0, (struct sockaddr*) &addrUDP, sizeof(addrUDP)) < 0) { fprintf(stderr, "Erreur sendto\n"); return(-1);}       
32c9271a   skhinach   modif
66
  
32c9271a   skhinach   modif
67
68
  	close(sock_id);	
  	return 0;
1f9d795a   Speedclocker   Sniffer et sender
69
70
  }
  
32c9271a   skhinach   modif
71
72
  
  
1f9d795a   Speedclocker   Sniffer et sender
73
74
  int sendUDPUnicast(char* address, char* message, int port)
  {
351d9caa   Speedclocker   Ajout de serveur/...
75
76
  	static struct sockaddr_in addrUDP;
  	addrUDP.sin_family=AF_INET;
08f8ef8d   skhinach   modifs
77
  	addrUDP.sin_port=htons(port);
1f9d795a   Speedclocker   Sniffer et sender
78
  
351d9caa   Speedclocker   Ajout de serveur/...
79
  	inet_aton(address, &(addrUDP.sin_addr)); // Converti lcriture string de format a.b.c.d en format Internet
1f9d795a   Speedclocker   Sniffer et sender
80
  
32c9271a   skhinach   modif
81
82
  	char tmp_str[15];
  	sprintf(tmp_str, "%d", port);
1a2c5f60   skhinach   Quelques modifs f...
83
  	int sock_id=initializeSocketUDP( tmp_str , address);
32c9271a   skhinach   modif
84
  	printf("Initialize via Unicast %d\n", sock_id);
1f9d795a   Speedclocker   Sniffer et sender
85
  
32c9271a   skhinach   modif
86
  	if(sendto(sock_id, message, sizeof(message), 0, (struct sockaddr*) &addrUDP, sizeof(addrUDP)) < 0 ) { fprintf(stderr, "Error sendto %d\n", errno); return(-1); }
1f9d795a   Speedclocker   Sniffer et sender
87
  
32c9271a   skhinach   modif
88
  	close(sock_id);
08f8ef8d   skhinach   modifs
89
  	
32c9271a   skhinach   modif
90
91
  	return 0;
  }