Blame view

Network/sender.c 2.96 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;
351d9caa   Speedclocker   Ajout de serveur/...
21
  	//precisions.ai_flags = AI_PASSIVE;
32c9271a   skhinach   modif
22
  
a69a94a7   skhinach   Ajout de gestion ...
23
  /*"172.26.145.35"*/
0f1eb4c5   skhinach   test
24
  	if(getaddrinfo("192.168.43.174", service, &precisions, &origine)<0) { fprintf(stderr, "Erreur getaddrinfo\n"); return(-1); }
32c9271a   skhinach   modif
25
  
1f9d795a   Speedclocker   Sniffer et sender
26
27
28
29
30
31
  	int n=0;
  	
  	for(struct addrinfo* i=origine; i!=NULL && resultat==NULL; i=i->ai_next)
  	{
  		printf("%d - ", n);
  		if(i->ai_family==AF_INET)
351d9caa   Speedclocker   Ajout de serveur/...
32
  		{
1f9d795a   Speedclocker   Sniffer et sender
33
  			resultat=i;
351d9caa   Speedclocker   Ajout de serveur/...
34
35
36
  			printf("Test : %d", origine->ai_addrlen);
  		}
  
1f9d795a   Speedclocker   Sniffer et sender
37
38
39
  		printf("\n");	
  		n++;
  	}
351d9caa   Speedclocker   Ajout de serveur/...
40
  
32c9271a   skhinach   modif
41
42
  	printf("Value : %d\n", n);
  
351d9caa   Speedclocker   Ajout de serveur/...
43
44
  	struct sockaddr_in* test=(struct sockaddr_in*)(resultat->ai_addr);
  	printf("Addr : %x\n", test->sin_addr.s_addr);
1f9d795a   Speedclocker   Sniffer et sender
45
46
47
48
  	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 ...
49
50
51
52
  	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/...
53
54
55
56
  
  	if(bind(sock_id, resultat->ai_addr, resultat->ai_addrlen)) { fprintf(stderr, "Erreur bind\n"); return(-1); }
  
  
1f9d795a   Speedclocker   Sniffer et sender
57
  	freeaddrinfo(origine);
32c9271a   skhinach   modif
58
  	return sock_id;
1f9d795a   Speedclocker   Sniffer et sender
59
60
61
  }
  
  
1f9d795a   Speedclocker   Sniffer et sender
62
63
  int sendUDPBroadcast(char* message, int port)
  {
351d9caa   Speedclocker   Ajout de serveur/...
64
65
  	static struct sockaddr_in addrUDP;
  	addrUDP.sin_family=AF_INET;
a69a94a7   skhinach   Ajout de gestion ...
66
  	addrUDP.sin_port=htons(port); // Converti au format réseau
1f9d795a   Speedclocker   Sniffer et sender
67
68
  
  	struct in_addr tmpadd={0xffffffff};	
351d9caa   Speedclocker   Ajout de serveur/...
69
70
  	addrUDP.sin_addr=tmpadd;
  	
32c9271a   skhinach   modif
71
72
  	char tmp_str[15];
  	sprintf(tmp_str, "%d", port);
1a2c5f60   skhinach   Quelques modifs f...
73
  	int sock_id=initializeSocketUDP( tmp_str , "255.255.255.255");
1f9d795a   Speedclocker   Sniffer et sender
74
  
32c9271a   skhinach   modif
75
  	printf("Initialize via Broadcast %d\n", sock_id);
32c9271a   skhinach   modif
76
  	
08f8ef8d   skhinach   modifs
77
  	if(sendto(sock_id, message, sizeof(message), 0, (struct sockaddr*) &addrUDP, sizeof(addrUDP)) < 0) { fprintf(stderr, "Erreur sendto\n"); return(-1);}       
32c9271a   skhinach   modif
78
  
c35daaf6   Souheib Khinache   Organisation, cré...
79
  
32c9271a   skhinach   modif
80
81
  	close(sock_id);	
  	return 0;
1f9d795a   Speedclocker   Sniffer et sender
82
83
  }
  
32c9271a   skhinach   modif
84
85
  
  
1f9d795a   Speedclocker   Sniffer et sender
86
87
  int sendUDPUnicast(char* address, char* message, int port)
  {
351d9caa   Speedclocker   Ajout de serveur/...
88
89
  	static struct sockaddr_in addrUDP;
  	addrUDP.sin_family=AF_INET;
08f8ef8d   skhinach   modifs
90
  	addrUDP.sin_port=htons(port);
1f9d795a   Speedclocker   Sniffer et sender
91
  
351d9caa   Speedclocker   Ajout de serveur/...
92
  	inet_aton(address, &(addrUDP.sin_addr)); // Converti lcriture string de format a.b.c.d en format Internet
1f9d795a   Speedclocker   Sniffer et sender
93
  
32c9271a   skhinach   modif
94
95
  	char tmp_str[15];
  	sprintf(tmp_str, "%d", port);
1a2c5f60   skhinach   Quelques modifs f...
96
  	int sock_id=initializeSocketUDP( tmp_str , address);
32c9271a   skhinach   modif
97
  	printf("Initialize via Unicast %d\n", sock_id);
1f9d795a   Speedclocker   Sniffer et sender
98
  
32c9271a   skhinach   modif
99
  	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
100
  
32c9271a   skhinach   modif
101
  	close(sock_id);
08f8ef8d   skhinach   modifs
102
  	
32c9271a   skhinach   modif
103
104
  	return 0;
  }
1f9d795a   Speedclocker   Sniffer et sender
105
  
32c9271a   skhinach   modif
106
  /*
1f9d795a   Speedclocker   Sniffer et sender
107
108
109
  
  int main(void)
  {
351d9caa   Speedclocker   Ajout de serveur/...
110
  	sendUDPBroadcast("Broadwesh", 2020);
32c9271a   skhinach   modif
111
  	sendUDPUnicast("192.168.43.174", "Uniwesh", 2020);
1f9d795a   Speedclocker   Sniffer et sender
112
  	return(1);
1f9d795a   Speedclocker   Sniffer et sender
113
  }
32c9271a   skhinach   modif
114
115
  
  */
a69a94a7   skhinach   Ajout de gestion ...