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
|
/*
wingate.c
Author: Liraz Siri <liraz@bigfoot.com>
Copyright (c) 1998 Liraz Siri <liraz@bigfoot.com>, Ariel, Israel
All rights reserved
Created: Created: Mon Nov 2 02:51:02 IST 1998
This is a moronic wingate 'detector' wooooo. I can't believe I'm actually
writing this.
*/
#include "BASS.h"
#include "network.h"
#include "wingate.h"
#include "log.h"
int wingate_handlehost(struct sockaddr_in *addr, char *host, int timer)
{
int sockfd;
int i, n, bytes;
struct sockaddr_in raddr;
u_char response[MAX_RESPONSE_SIZE];
memset(response, 0, sizeof(response));
memcpy(&raddr, addr, sizeof(raddr));
raddr.sin_port = htons(23);
if((sockfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0 ||
tconnect(sockfd, (struct sockaddr *)&raddr, sizeof(struct sockaddr),
timer) < 0) goto fail;
for(bytes = 0; bytes<MAX_RESPONSE_SIZE; bytes+=n)
{
arm(timer);
if(!setjmp(jmpbuf))
{
if((n = read(sockfd, response + bytes, MAX_RESPONSE_SIZE - bytes)) <= 0)
{ errno = EPROTO; goto fail; }
alarm(0);
for(i = 0; i<n; i++)
{
/* Detect telnet negotiation which is obviously NOT wingate */
if(response[bytes+i] == 255 && n - i >= 3)
{
u_char negotiate[4];
unsigned char *cp = (unsigned char *)&response[bytes+i+1];
negotiate[0] = 255;
negotiate[1] = (*cp == 251 || *cp == 252)? 254:
(*cp == 253 || *cp == 254)? 252:0;
negotiate[2] = *(++cp);
if(negotiate[1])
{
write(sockfd, negotiate, 3);
i+=2;
}
}
response[bytes+i] = (char)tolower((int)response[bytes+i]);
}
fflush(stdout);
if( strstr(response, "gate>") != NULL )
{
log("wingate - [%s] wingate ready and available",
network_getname(host));
close(sockfd);
return 0;
}
} else
{
errno = ETIMEDOUT;
goto fail;
}
}
fail:
close(sockfd);
return -1;
}
|