8 # define INTERNET_SOCKET PF_INET
11 # include <sys/socket.h>
13 # include <netinet/in.h>
14 # include <arpa/inet.h>
17 # define INVALID_SOCKET -1
18 # define INTERNET_SOCKET AF_INET
19 # define closesocket close
30 struct in_addr imr_multiaddr;
31 struct in_addr imr_interface;
39 int i, n, nfds, replen, max_replen, connected = 0;
41 struct timeval timeout = { 0, 0 };
43 struct hostent *hostp;
44 struct sockaddr_in address;
45 int enable = 1, disable = 0;
46 for(i=0; !connected && (servers[i] != NULL) && (strlen(servers[i]) > 0) ; i++)
48 printf(
"open_connection: trying to establish connection to \"%s\"\n", servers[i]);
51 perror(
"open_connection: socket");
54 if ( setsockopt(sd, SOL_SOCKET, SO_KEEPALIVE, &setkeepalive,
sizeof(setkeepalive)) == -1 )
56 perror(
"setsockopt: keepalive");
61 if ( setsockopt(sd, SOL_SOCKET, SO_SNDTIMEO, &timeout,
sizeof(timeout)) == -1 )
63 perror(
"setsockopt: sndtimeo");
68 if ( setsockopt(sd, SOL_SOCKET, SO_RCVTIMEO, &timeout,
sizeof(timeout)) == -1 )
70 perror(
"setsockopt: rcvtimeo");
80 memset(&address, 0,
sizeof(
struct sockaddr_in));
81 address.sin_family = AF_INET;
82 if ((hostp = gethostbyname(servers[i])) == NULL)
84 printf(
"open_connection: gethostbyname failed for \"%s\"\n", servers[i]);
89 memcpy(&(address.sin_addr.s_addr), hostp->h_addr, hostp->h_length);
90 address.sin_port = htons(port);
91 if (connect(sd, (
struct sockaddr*)&address,
sizeof(address)) == -1)
93 perror(
"open_connection: connect");
100 if (ioctl(sd, FIONBIO, &disable) == -1)
102 perror(
"ioctl: FIONBIO");
139 struct timeval delay = { 5, 0};
140 int numfds = FD_SETSIZE;
148 FD_SET(sd, &writefds);
149 if (select(numfds, NULL, &writefds, NULL, &delay) <= 0)
151 printf(
"send_packet: select timed out after %ld seconds\n", delay.tv_sec);
154 stat = send(sd, data, n, 0);
168 struct timeval delay = { 5, 0};
169 int numfds = FD_SETSIZE;
174 FD_SET(sd, &writefds);
175 if (select(numfds, NULL, &writefds, NULL, &delay) <= 0)
177 printf(
"send_data: select timed out after %ld seconds\n", delay.tv_sec);
181 data = (
char*)malloc(n);
185 stat = send(sd, data, n, 0);
200 int stat, nread = 0, numfds, sock_size;
201 struct sockaddr_in sockin;
203 struct timeval delay = { 180, 0};
208 FD_SET(sd, &readfds);
209 if (select(numfds, &readfds, NULL, NULL, &delay) <= 0)
211 printf(
"receive_data_pattern: select timed out after %ld seconds\n", delay.tv_sec);
214 stat = recv(sd, (
char*)data + nread, maxdata - nread, 0);
221 if (nread == maxdata)
223 printf(
"too much data\n");
226 ((
char*)data)[nread] =
'\0';
227 if (strstr((
char*)data, pattern) != NULL)
238 int stat, nread = 0, numfds, sock_size;
239 struct sockaddr_in sockin;
241 struct timeval delay = { 30, 0};
246 FD_SET(sd, &readfds);
247 if (select(numfds, &readfds, NULL, NULL, &delay) <= 0)
249 printf(
"receive_data_size: select timed out after %ld seconds\n", delay.tv_sec);
252 stat = recv(sd, (
char*)data + nread, n - nread, 0);
254 chsr * address = inet_ntoa(sockin.sin_addr);
255 unsigned long add = sockin.sin_addr.s_addr;
256 host = gethostbyaddr((
char*)&add, 4, AF_INET);
276 printf(
"receive_data: error reading header\n");
281 printf(
"receive_data: packet size mismatch (got %d, expected %d)\n", dp->
header.
len,
sizeof(dp->
data));
282 printf(
"receive_data: there is probably a mismatch in the data_packet_1_0 definitions on this and\n");
283 printf(
"receive_data: the ISIS controls sending computer\n");
289 printf(
"receive_data: error reading data\n");
295 printf(
"receive_data: error reading checksum\n");
299 memcpy(&(dp->
data), data,
sizeof(dp->
data));
306 struct sockaddr_in sockin;
313 sock_size =
sizeof(sockin);
314 stat = recvfrom(sd, dp, n, 0, (
struct sockaddr*)&sockin, &sock_size);
328 struct sockaddr_in sin;
334 memset(&sin, 0,
sizeof(sin));
336 sin.sin_addr.s_addr = INADDR_ANY;
337 sin.sin_port = htons(port);
338 if (bind(sd, (
struct sockaddr*)&sin,
sizeof(sin)) == -1)
344 if (listen(sd, num_listen) == -1)
357 struct sockaddr_in sin;
365 if ( setsockopt(sd, SOL_SOCKET, SO_REUSEPORT, &setreuse,
sizeof(setreuse)) == -1 )
367 perror(
"setsockopt: reuseport");
372 if ( setsockopt(sd, SOL_SOCKET, SO_REUSEADDR, &setreuse,
sizeof(setreuse)) == -1 )
374 perror(
"setsockopt: reuseaddr");
379 memset(&sin, 0,
sizeof(sin));
381 sin.sin_addr.s_addr = INADDR_ANY;
382 sin.sin_port = htons(port);
383 if (bind(sd, (
struct sockaddr*)&sin,
sizeof(sin)) == -1)
392 mreq.imr_interface.s_addr = INADDR_ANY;
393 if (setsockopt(sd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq,
sizeof(mreq)) == -1)
395 perror(
"setsockopt: add multicast membership");
410 struct hostent* hostp;
411 struct in_addr ifaddress;
413 typedef unsigned long in_addr_t;
416 add = inet_addr(address);
417 if ( add == (in_addr_t)-1 )
423 ifaddress.s_addr = add;
425 hostp = malloc(
sizeof(
struct hostent));
426 hostp->h_addr_list = malloc(
sizeof(
char*));
427 hostp->h_addr_list[0] = (
char*)&add;
428 hostp->h_length =
sizeof(in_addr_t);
433 memset(sockin, 0,
sizeof(
struct sockaddr_in));
434 sockin->sin_family = AF_INET;
435 sockin->sin_port = htons(port);
436 memcpy(&(sockin->sin_addr.s_addr), hostp->h_addr_list[0], hostp->h_length);
440 if (setsockopt(s, IPPROTO_IP, IP_MULTICAST_TTL, &ttl,
sizeof(ttl)) == -1)
442 perror(
"setsockopt: multicast ttl");
445 if (setsockopt(s, IPPROTO_IP, IP_MULTICAST_IF, &ifaddress,
sizeof(ifaddress)) == -1)
447 perror(
"setsockopt: multicast if");
453 if (setsockopt(s, SOL_SOCKET, SO_BROADCAST, &one,
sizeof(one)) == -1)
455 perror(
"setsockopt: broadcast");
467 char*
xml_parse(
const char* input_string,
const char* token)
469 char *start_token, *end_token, *result = NULL;
470 char *start_pos, *end_pos;
472 if (input_string == NULL || token == NULL)
476 len_tok = strlen(token);
477 start_token = malloc(len_tok+2+1);
478 if (start_token == NULL)
482 end_token = malloc(len_tok+3+1);
483 if (end_token == NULL)
487 sprintf(start_token,
"<%s>", token);
488 sprintf(end_token,
"</%s>", token);
489 start_pos = strstr(input_string, start_token);
490 end_pos = strstr(input_string, end_token);
491 if (start_pos != NULL && end_pos != NULL)
493 n = (end_pos - start_pos) - len_tok - 2;
494 result = malloc(n+1);
497 strncpy(result, start_pos + len_tok + 2, n);
508 static char buffer[1024];
509 struct timeval delay = { 2, 0};
510 int nread = 1, numfds;
515 FD_SET(sd, &readfds);
518 if (select(numfds, &readfds, NULL, NULL, &delay) > 0)
520 nread = recv(sd, buffer,
sizeof(buffer), 0);
521 buffer[nread] =
'\0';
529 static char buffer[1024];
531 va_start(ap, format);
532 vsprintf(buffer, format, ap);
535 send(sd, buffer, strlen(buffer), 0);
539 void send_mail(
const char* from,
const char* to,
const char* message)
543 const char* servers[] = {
"localhost",
"hathor.nd.rl.ac.uk",
"thoth.nd.rl.ac.uk", NULL };
544 if (gethostname(host_name,
sizeof(host_name)) != 0)
546 strcpy(host_name,
"localhost");
552 send_mess(sd,
"MAIL FROM:<%s>\r\n", from);
561 printf(
"send_mail: error opening connection\n");
char * xml_parse(const char *input_string, const char *token)
int send_packet(SOCKET sd, const char *data, int n)
int initialise_data_packet(data_header *dh, short major, short minor)
static void send_mess(SOCKET sd, const char *format,...)
int receive_data_udp(SOCKET sd, char *dp, int n)
#define ND_MULTICAST_ADDRESS
SOCKET setup_socket(unsigned short port, int num_listen)
SOCKET setup_broadcast_socket(const char *address, short port, struct sockaddr_in *sockin, int multicast)
int receive_data_pattern(SOCKET sd, void *data, int maxdata, const char *pattern)
int receive_data_size(SOCKET sd, void *data, int n)
void send_mail(const char *from, const char *to, const char *message)
static void junk_reply(sd)
int send_data(SOCKET sd, data_packet_1_0 *dp)
SOCKET setup_udp_socket(unsigned short port, int multicast)
SOCKET open_connection(const char *servers[], short port)
int receive_data(SOCKET sd, data_packet_1_0 *dp)