Hash :
94046104
Author :
Thomas de Grivel
Date :
2020-02-24T15:19:27
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include <strings.h>
#include <sys/types.h>
#ifdef WIN32
# include "../common/win32.h"
#else
# include <netdb.h>
# include <netinet/in.h>
# include <sys/socket.h>
# include <arpa/inet.h>
# include <sys/wait.h>
# include <sys/select.h>
#endif
#include "../common/broadcast.h"
#include "../common/nieme_str.h"
#include "iplist.h"
#include "servlist.h"
#define MAXDATASIZE 100 /* Tampon d'entrée */
int nbserver = 0;
/*//////////////////////////////////////////////////////////////////////////
/ Envoi d'un paquet en Broadcast et reception du paquet d'authentification /
//////////////////////////////////////////////////////////////////////////*/
int pw13cc_Bcast_init()
{
int socketDesc; /* Socket Descriptor */
/* Create socket from which to send */
if ((socketDesc = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
{
perror("open error on socket");
exit(1);
}
return(socketDesc);
}
void * pw13cc_Bcast (void *servlist,
void (*callback) (pident, void*),
void *param)
{
pident *serverlist = (pident*) servlist;
int socketDesc;
int addrLength;
struct sockaddr_in destinationAddr, myAddr, responseAddr;
struct hostent *hostNameBufPtr;
char outMessageBuf[128];
char inMessageBuf[128];
struct timeval timeOut;
fd_set readReadySet;
int msgCount;
int trueFlag = 0x1;
pident elt;
int nb_bcast = 100;
socketDesc = pw13cc_Bcast_init ();
display (*serverlist);
/* BIND the socket on our end to any available port (i.e.
leave it up to the system to assign a port (used for
replys */
myAddr.sin_family = AF_INET;
myAddr.sin_addr.s_addr = htonl(INADDR_ANY);
myAddr.sin_port = htons (PW13_BROADCAST_CLIENT_PORT);
if (bind(socketDesc, (struct sockaddr *) &myAddr, sizeof(myAddr))
< 0)
{
perror("bind error");
goto end_Bcast;
}
/* And build BROADCAST address */
destinationAddr.sin_family = AF_INET;
destinationAddr.sin_addr.s_addr = htonl(INADDR_BROADCAST);
destinationAddr.sin_port = htons(PW13_BROADCAST_SERVER_PORT);
/* Set the socket to permit multicasts on the local LAN */
if (setsockopt(socketDesc, SOL_SOCKET, SO_BROADCAST,
(void*) &trueFlag, sizeof(trueFlag)) != 0)
{
perror("setsockopt error");
goto end_Bcast;
}
while(nb_bcast--)
{
/* Broadcast message */
strcpy(outMessageBuf, "#1#1#");
if (sendto(socketDesc, outMessageBuf, strlen(outMessageBuf)+1, 0,
(struct sockaddr *) &destinationAddr,
sizeof (destinationAddr)) < 0)
{
perror("socket send error");
printf("1");
}
/* And wait for up to 1 seconds for replys to come back */
/* Set timeOut value for receive to 1 seconds */
timeOut.tv_sec = 1L;
timeOut.tv_usec = 0L;
/* Read responses from hosts accepting the multicast until a read
finally times-out. */
FD_ZERO(&readReadySet);
FD_SET(socketDesc, &readReadySet);
msgCount = 0;
while (select(socketDesc+1,&readReadySet, NULL, NULL, &timeOut) > 0)
{
addrLength= sizeof(responseAddr);
if (recvfrom(socketDesc, inMessageBuf,
sizeof(inMessageBuf), 0,
(struct sockaddr *)&responseAddr,
(socklen_t*)&addrLength) < 0)
{
perror("socket read error");
printf("2");
goto end_Bcast;
}
/* Convert the address of the responding machine to its symbolic
format */
if( !strncmp(inMessageBuf,"#1#2#",4) )
{
int dejadanslaliste;
if ((hostNameBufPtr =
gethostbyaddr ((void*) &responseAddr.sin_addr.s_addr,
sizeof(responseAddr.sin_addr.s_addr),
AF_INET)) == NULL)
{
perror("error gethostbyaddr");
}
else
{
printf("Message received from %s\n", hostNameBufPtr->h_name);
}
printf ("recu un paquet de %s\n",
inet_ntoa(responseAddr.sin_addr));
elt = addserverlist (serverlist,
new_ident (inet_ntoa(responseAddr.sin_addr),
atoi(nieme_str(inMessageBuf,3))),
&dejadanslaliste);
if (!dejadanslaliste && callback)
callback (elt, param);
printf("Received Message=%s\n\n",inMessageBuf);
msgCount++;
}
FD_SET (socketDesc, &readReadySet);
sleep (1);
} /* while */
printf ("Received %d messages\n",msgCount);
}
end_Bcast:
close (socketDesc);
return NULL;
}