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
12
13
14
15
16
|
#include <string.h>
#include <netdb.h>
#include <arpa/inet.h>
int initialized=0;
int sock_id;
int initializeSocketUDP(char* service)
{
|
351d9caa
Speedclocker
Ajout de serveur/...
|
17
|
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
|
18
19
20
21
|
memset(&precisions, 0, sizeof precisions);
precisions.ai_family = AF_UNSPEC;
precisions.ai_socktype = SOCK_DGRAM;
|
351d9caa
Speedclocker
Ajout de serveur/...
|
22
|
//precisions.ai_flags = AI_PASSIVE;
|
1f9d795a
Speedclocker
Sniffer et sender
|
23
24
25
26
27
28
29
30
31
|
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/...
|
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
41
42
|
struct sockaddr_in* test=(struct sockaddr_in*)(resultat->ai_addr);
printf("Addr : %x\n", test->sin_addr.s_addr);
|
1f9d795a
Speedclocker
Sniffer et sender
|
43
44
45
46
|
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/...
|
47
48
49
50
51
|
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
|
52
53
54
55
56
57
58
59
60
|
freeaddrinfo(origine);
initialized=1;
}
int sendUDPBroadcast(char* message, int port)
{
|
351d9caa
Speedclocker
Ajout de serveur/...
|
61
62
63
|
static struct sockaddr_in addrUDP;
addrUDP.sin_family=AF_INET;
addrUDP.sin_port=(uint16_t)port;
|
1f9d795a
Speedclocker
Sniffer et sender
|
64
65
|
struct in_addr tmpadd={0xffffffff};
|
351d9caa
Speedclocker
Ajout de serveur/...
|
66
67
|
addrUDP.sin_addr=tmpadd;
|
1f9d795a
Speedclocker
Sniffer et sender
|
68
69
|
if(initialized!=1)
{
|
351d9caa
Speedclocker
Ajout de serveur/...
|
70
71
|
printf("Initialize via Broadcast\n");
char tmp_str[15];
|
1f9d795a
Speedclocker
Sniffer et sender
|
72
73
74
75
|
sprintf(tmp_str, "%d", port);
initializeSocketUDP( tmp_str );
}
|
351d9caa
Speedclocker
Ajout de serveur/...
|
76
|
if(sendto(sock_id, message, sizeof(message), 0, (struct sockaddr*) &addrUDP, sizeof(addrUDP)) < 0) { fprintf(stderr, "Erreur sendto\n"); return(-1);}
|
1f9d795a
Speedclocker
Sniffer et sender
|
77
78
79
80
|
}
int sendUDPUnicast(char* address, char* message, int port)
{
|
351d9caa
Speedclocker
Ajout de serveur/...
|
81
82
83
|
static struct sockaddr_in addrUDP;
addrUDP.sin_family=AF_INET;
addrUDP.sin_port=(uint16_t)port;
|
1f9d795a
Speedclocker
Sniffer et sender
|
84
|
|
351d9caa
Speedclocker
Ajout de serveur/...
|
85
|
inet_aton(address, &(addrUDP.sin_addr)); // Converti l'écriture string de format a.b.c.d en format Internet
|
1f9d795a
Speedclocker
Sniffer et sender
|
86
87
88
89
90
|
if(initialized!=1)
{
|
351d9caa
Speedclocker
Ajout de serveur/...
|
91
92
|
printf("Initialize via Unicast\n");
char tmp_str[15];
|
1f9d795a
Speedclocker
Sniffer et sender
|
93
94
95
96
|
sprintf(tmp_str, "%d", port);
initializeSocketUDP( tmp_str );
}
|
351d9caa
Speedclocker
Ajout de serveur/...
|
97
|
if(sendto(sock_id, message, sizeof(message), 0, (struct sockaddr*) &addrUDP, sizeof(addrUDP)) < 0 ) { fprintf(stderr, "Error sendto\n"); return(-1); }
|
1f9d795a
Speedclocker
Sniffer et sender
|
98
99
100
101
102
103
104
|
}
int main(void)
{
|
351d9caa
Speedclocker
Ajout de serveur/...
|
105
106
|
sendUDPBroadcast("Broadwesh", 2020);
sendUDPUnicast("255.255.255.255", "Uniwesh", 2020);
|
1f9d795a
Speedclocker
Sniffer et sender
|
107
|
return(1);
|
351d9caa
Speedclocker
Ajout de serveur/...
|
108
|
close(sock_id);
|
1f9d795a
Speedclocker
Sniffer et sender
|
109
|
}
|