Blame view

Network/sender.c 2.7 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
9
10
11
  #include <string.h>
  #include <netdb.h>
  #include <arpa/inet.h>
  
  
  
1f9d795a   Speedclocker   Sniffer et sender
12
13
  int initializeSocketUDP(char* service)
  {
c35daaf6   Souheib Khinache   Organisation, cré...
14
  	int sock_id;
351d9caa   Speedclocker   Ajout de serveur/...
15
  	struct addrinfo precisions, *resultat=NULL, *origine; // On stocke dans précisions nos besoins pour le socket, dans resultat l'adresse qui respectera les reqêtes
1f9d795a   Speedclocker   Sniffer et sender
16
17
18
19
  
  	memset(&precisions, 0, sizeof precisions);
  	precisions.ai_family = AF_UNSPEC;
  	precisions.ai_socktype = SOCK_DGRAM;
351d9caa   Speedclocker   Ajout de serveur/...
20
  	//precisions.ai_flags = AI_PASSIVE;
1f9d795a   Speedclocker   Sniffer et sender
21
22
23
24
25
26
27
28
29
  	
  	if(getaddrinfo(NULL, service, &precisions, &origine)<0) { fprintf(stderr, "Erreur getaddrinfo\n"); return(-1); }
  	
  	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/...
30
  		{
1f9d795a   Speedclocker   Sniffer et sender
31
  			resultat=i;
351d9caa   Speedclocker   Ajout de serveur/...
32
33
34
  			printf("Test : %d", origine->ai_addrlen);
  		}
  
1f9d795a   Speedclocker   Sniffer et sender
35
36
37
  		printf("\n");	
  		n++;
  	}
351d9caa   Speedclocker   Ajout de serveur/...
38
39
40
  
  	struct sockaddr_in* test=(struct sockaddr_in*)(resultat->ai_addr);
  	printf("Addr : %x\n", test->sin_addr.s_addr);
1f9d795a   Speedclocker   Sniffer et sender
41
42
43
44
  	if((sock_id=socket(resultat->ai_family, resultat->ai_socktype, resultat->ai_protocol))<0) { fprintf(stderr, "Erreur socket\n"); return(-1); }
  
  	int val=1;
  	
351d9caa   Speedclocker   Ajout de serveur/...
45
46
47
48
49
  	if(setsockopt(sock_id, SOL_SOCKET, SO_REUSEADDR|SO_BROADCAST, &val, sizeof(val))<0) { fprintf(stderr, "Erreur setsockopt\n"); return(-1); }
  
  	if(bind(sock_id, resultat->ai_addr, resultat->ai_addrlen)) { fprintf(stderr, "Erreur bind\n"); return(-1); }
  
  
1f9d795a   Speedclocker   Sniffer et sender
50
  	freeaddrinfo(origine);
c35daaf6   Souheib Khinache   Organisation, cré...
51
  	return sock_id;
1f9d795a   Speedclocker   Sniffer et sender
52
53
54
  }
  
  
1f9d795a   Speedclocker   Sniffer et sender
55
56
  int sendUDPBroadcast(char* message, int port)
  {
351d9caa   Speedclocker   Ajout de serveur/...
57
58
59
  	static struct sockaddr_in addrUDP;
  	addrUDP.sin_family=AF_INET;
  	addrUDP.sin_port=(uint16_t)port;
1f9d795a   Speedclocker   Sniffer et sender
60
61
  
  	struct in_addr tmpadd={0xffffffff};	
351d9caa   Speedclocker   Ajout de serveur/...
62
63
  	addrUDP.sin_addr=tmpadd;
  	
c35daaf6   Souheib Khinache   Organisation, cré...
64
65
66
  	char tmp_str[15];
  	sprintf(tmp_str, "%d", port);
  	int sock_id=initializeSocketUDP( tmp_str );
1f9d795a   Speedclocker   Sniffer et sender
67
  
c35daaf6   Souheib Khinache   Organisation, cré...
68
69
70
71
72
73
  	printf("Initialize via Broadcast %d\n", sock_id);
  
  	if(sendto(sock_id, message, sizeof(message), 0, (struct sockaddr*) &addrUDP, sizeof(addrUDP)) < 0) { fprintf(stderr, "Erreur sendto\n"); return(-1);}
         	
  	close(sock_id);	
  	return 0;
1f9d795a   Speedclocker   Sniffer et sender
74
75
  }
  
c35daaf6   Souheib Khinache   Organisation, cré...
76
77
  
  
1f9d795a   Speedclocker   Sniffer et sender
78
79
  int sendUDPUnicast(char* address, char* message, int port)
  {
351d9caa   Speedclocker   Ajout de serveur/...
80
81
82
  	static struct sockaddr_in addrUDP;
  	addrUDP.sin_family=AF_INET;
  	addrUDP.sin_port=(uint16_t)port;
1f9d795a   Speedclocker   Sniffer et sender
83
  
351d9caa   Speedclocker   Ajout de serveur/...
84
  	inet_aton(address, &(addrUDP.sin_addr)); // Converti lcriture string de format a.b.c.d en format Internet
1f9d795a   Speedclocker   Sniffer et sender
85
  
c35daaf6   Souheib Khinache   Organisation, cré...
86
87
88
89
  	char tmp_str[15];
  	sprintf(tmp_str, "%d", port);
  	int sock_id=initializeSocketUDP( tmp_str );
  	printf("Initialize via Unicast %d\n", sock_id);
1f9d795a   Speedclocker   Sniffer et sender
90
  
351d9caa   Speedclocker   Ajout de serveur/...
91
  	if(sendto(sock_id, message, sizeof(message), 0, (struct sockaddr*) &addrUDP, sizeof(addrUDP)) < 0 ) { fprintf(stderr, "Error sendto\n"); return(-1); }
c35daaf6   Souheib Khinache   Organisation, cré...
92
93
94
  	
  	close(sock_id);
  	return 0;
1f9d795a   Speedclocker   Sniffer et sender
95
96
97
  }
  
  
c35daaf6   Souheib Khinache   Organisation, cré...
98
  /*
1f9d795a   Speedclocker   Sniffer et sender
99
100
101
  
  int main(void)
  {
351d9caa   Speedclocker   Ajout de serveur/...
102
  	sendUDPBroadcast("Broadwesh", 2020);
c35daaf6   Souheib Khinache   Organisation, cré...
103
  	sendUDPUnicast("192.168.43.174", "Uniwesh", 2020);
1f9d795a   Speedclocker   Sniffer et sender
104
  	return(1);
1f9d795a   Speedclocker   Sniffer et sender
105
  }
c35daaf6   Souheib Khinache   Organisation, cré...
106
107
  
  */