Blame view

Network/sender.c 3.62 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
  
  
c9ce1fd2   skhinach   modifs
12
  <<<<<<< HEAD
1f9d795a   Speedclocker   Sniffer et sender
13
14
  int initializeSocketUDP(char* service)
  {
32c9271a   skhinach   modif
15
16
  	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
c9ce1fd2   skhinach   modifs
17
  =======
1f9d795a   Speedclocker   Sniffer et sender
18
  
1f9d795a   Speedclocker   Sniffer et sender
19
20
  int initializeSocketUDP(char* service)
  {
c35daaf6   Souheib Khinache   Organisation, cré...
21
  	int sock_id;
351d9caa   Speedclocker   Ajout de serveur/...
22
  	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
c9ce1fd2   skhinach   modifs
23
  >>>>>>> 52d8a1eaf954b611aaf0861144b440a15f7c517d
1f9d795a   Speedclocker   Sniffer et sender
24
25
26
27
  
  	memset(&precisions, 0, sizeof precisions);
  	precisions.ai_family = AF_UNSPEC;
  	precisions.ai_socktype = SOCK_DGRAM;
351d9caa   Speedclocker   Ajout de serveur/...
28
  	//precisions.ai_flags = AI_PASSIVE;
32c9271a   skhinach   modif
29
30
31
32
  
  
  	if(getaddrinfo("192.168.0.14", service, &precisions, &origine)<0) { fprintf(stderr, "Erreur getaddrinfo\n"); return(-1); }
  
1f9d795a   Speedclocker   Sniffer et sender
33
34
35
36
37
38
  	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/...
39
  		{
1f9d795a   Speedclocker   Sniffer et sender
40
  			resultat=i;
351d9caa   Speedclocker   Ajout de serveur/...
41
42
43
  			printf("Test : %d", origine->ai_addrlen);
  		}
  
1f9d795a   Speedclocker   Sniffer et sender
44
45
46
  		printf("\n");	
  		n++;
  	}
351d9caa   Speedclocker   Ajout de serveur/...
47
  
32c9271a   skhinach   modif
48
49
  	printf("Value : %d\n", n);
  
351d9caa   Speedclocker   Ajout de serveur/...
50
51
  	struct sockaddr_in* test=(struct sockaddr_in*)(resultat->ai_addr);
  	printf("Addr : %x\n", test->sin_addr.s_addr);
1f9d795a   Speedclocker   Sniffer et sender
52
53
54
55
  	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/...
56
57
58
59
60
  	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
61
  	freeaddrinfo(origine);
32c9271a   skhinach   modif
62
  	return sock_id;
1f9d795a   Speedclocker   Sniffer et sender
63
64
65
  }
  
  
1f9d795a   Speedclocker   Sniffer et sender
66
67
  int sendUDPBroadcast(char* message, int port)
  {
351d9caa   Speedclocker   Ajout de serveur/...
68
69
70
  	static struct sockaddr_in addrUDP;
  	addrUDP.sin_family=AF_INET;
  	addrUDP.sin_port=(uint16_t)port;
1f9d795a   Speedclocker   Sniffer et sender
71
72
  
  	struct in_addr tmpadd={0xffffffff};	
351d9caa   Speedclocker   Ajout de serveur/...
73
74
  	addrUDP.sin_addr=tmpadd;
  	
32c9271a   skhinach   modif
75
76
77
  	char tmp_str[15];
  	sprintf(tmp_str, "%d", port);
  	int sock_id=initializeSocketUDP( tmp_str );
1f9d795a   Speedclocker   Sniffer et sender
78
  
32c9271a   skhinach   modif
79
  	printf("Initialize via Broadcast %d\n", sock_id);
c9ce1fd2   skhinach   modifs
80
  <<<<<<< HEAD
32c9271a   skhinach   modif
81
82
83
84
  	
  	int status;
  	if((status=sendto(sock_id, message, sizeof(message), 0, (struct sockaddr*) &addrUDP, sizeof(addrUDP))) < 0) { fprintf(stderr, "Erreur sendto %d\n", status); return(-1);}       
  
c9ce1fd2   skhinach   modifs
85
  =======
c35daaf6   Souheib Khinache   Organisation, cré...
86
87
88
  
  	if(sendto(sock_id, message, sizeof(message), 0, (struct sockaddr*) &addrUDP, sizeof(addrUDP)) < 0) { fprintf(stderr, "Erreur sendto\n"); return(-1);}
         	
c9ce1fd2   skhinach   modifs
89
  >>>>>>> 52d8a1eaf954b611aaf0861144b440a15f7c517d
32c9271a   skhinach   modif
90
91
  	close(sock_id);	
  	return 0;
1f9d795a   Speedclocker   Sniffer et sender
92
93
  }
  
32c9271a   skhinach   modif
94
95
  
  
1f9d795a   Speedclocker   Sniffer et sender
96
97
  int sendUDPUnicast(char* address, char* message, int port)
  {
351d9caa   Speedclocker   Ajout de serveur/...
98
99
100
  	static struct sockaddr_in addrUDP;
  	addrUDP.sin_family=AF_INET;
  	addrUDP.sin_port=(uint16_t)port;
1f9d795a   Speedclocker   Sniffer et sender
101
  
351d9caa   Speedclocker   Ajout de serveur/...
102
  	inet_aton(address, &(addrUDP.sin_addr)); // Converti lcriture string de format a.b.c.d en format Internet
1f9d795a   Speedclocker   Sniffer et sender
103
  
32c9271a   skhinach   modif
104
105
106
107
  	char tmp_str[15];
  	sprintf(tmp_str, "%d", port);
  	int sock_id=initializeSocketUDP( tmp_str );
  	printf("Initialize via Unicast %d\n", sock_id);
c9ce1fd2   skhinach   modifs
108
  <<<<<<< HEAD
1f9d795a   Speedclocker   Sniffer et sender
109
  
32c9271a   skhinach   modif
110
  	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
111
  
c9ce1fd2   skhinach   modifs
112
  =======
1f9d795a   Speedclocker   Sniffer et sender
113
  
351d9caa   Speedclocker   Ajout de serveur/...
114
  	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é...
115
116
117
  	
  	close(sock_id);
  	return 0;
1f9d795a   Speedclocker   Sniffer et sender
118
  }
c9ce1fd2   skhinach   modifs
119
  >>>>>>> 52d8a1eaf954b611aaf0861144b440a15f7c517d
1f9d795a   Speedclocker   Sniffer et sender
120
  
32c9271a   skhinach   modif
121
122
123
  	close(sock_id);
  	return 0;
  }
1f9d795a   Speedclocker   Sniffer et sender
124
  
c9ce1fd2   skhinach   modifs
125
  <<<<<<< HEAD
1f9d795a   Speedclocker   Sniffer et sender
126
  
c9ce1fd2   skhinach   modifs
127
128
  =======
  >>>>>>> 52d8a1eaf954b611aaf0861144b440a15f7c517d
32c9271a   skhinach   modif
129
  /*
1f9d795a   Speedclocker   Sniffer et sender
130
131
132
  
  int main(void)
  {
351d9caa   Speedclocker   Ajout de serveur/...
133
  	sendUDPBroadcast("Broadwesh", 2020);
32c9271a   skhinach   modif
134
  	sendUDPUnicast("192.168.43.174", "Uniwesh", 2020);
1f9d795a   Speedclocker   Sniffer et sender
135
  	return(1);
1f9d795a   Speedclocker   Sniffer et sender
136
  }
32c9271a   skhinach   modif
137
138
  
  */