2005-01-19 20:16:09 +03:00
|
|
|
/* ***** BEGIN LICENSE BLOCK *****
|
|
|
|
* Version: MPL 1.1
|
|
|
|
*
|
|
|
|
* The contents of this file are subject to the Mozilla Public License Version
|
|
|
|
* 1.1 (the "License"); you may not use this file except in compliance with
|
|
|
|
* the License. You may obtain a copy of the License at
|
|
|
|
* http://www.mozilla.org/MPL/
|
|
|
|
*
|
|
|
|
* Software distributed under the License is distributed on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|
|
|
* for the specific language governing rights and limitations under the
|
|
|
|
* License.
|
|
|
|
*
|
|
|
|
* The Original Code is SniffURI.
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is
|
|
|
|
* Erik van der Poel <erik@vanderpoel.org>.
|
|
|
|
* Portions created by the Initial Developer are Copyright (C) 1998-2005
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
|
|
|
* Bruce Robson <bns_robson@hotmail.com>
|
|
|
|
*
|
|
|
|
* ***** END LICENSE BLOCK ***** */
|
|
|
|
|
|
|
|
#include "all.h"
|
2000-02-01 21:24:20 +03:00
|
|
|
|
|
|
|
static int connectCount = 0;
|
|
|
|
static int dnsCount = 0;
|
|
|
|
|
|
|
|
static char *
|
|
|
|
getHostName(void)
|
|
|
|
{
|
|
|
|
size_t alloc;
|
|
|
|
char *hostName;
|
|
|
|
|
|
|
|
alloc = 512;
|
|
|
|
hostName = calloc(alloc, 1);
|
|
|
|
if (!hostName)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2005-01-19 20:16:09 +03:00
|
|
|
#if HAVE_GETHOSTNAME
|
|
|
|
if (gethostname(hostName, alloc) != 0)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "gethostname failed\n");
|
|
|
|
free(hostName);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
#if HAVE_SYSINFO
|
2000-02-01 21:24:20 +03:00
|
|
|
while (1)
|
|
|
|
{
|
2005-01-19 20:16:09 +03:00
|
|
|
long size;
|
|
|
|
|
2000-02-01 21:24:20 +03:00
|
|
|
size = sysinfo(SI_HOSTNAME, hostName, alloc);
|
|
|
|
if (size < 0)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "sysinfo failed\n");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
else if (size > alloc)
|
|
|
|
{
|
|
|
|
alloc = size + 1;
|
|
|
|
hostName = realloc(hostName, alloc);
|
|
|
|
if (!hostName)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2005-01-19 20:16:09 +03:00
|
|
|
#endif
|
2000-02-17 10:01:52 +03:00
|
|
|
#endif
|
2000-02-01 21:24:20 +03:00
|
|
|
|
|
|
|
return hostName;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2005-01-21 00:09:40 +03:00
|
|
|
getSocketAndIPAddress(App *app, unsigned char *hostName, int port,
|
2000-02-01 21:24:20 +03:00
|
|
|
struct sockaddr_in *addr)
|
|
|
|
{
|
2005-01-19 20:16:09 +03:00
|
|
|
#if HAVE_GETHOSTBYNAME_R
|
2000-02-01 21:24:20 +03:00
|
|
|
char buf[512];
|
|
|
|
int err;
|
2000-02-17 10:01:52 +03:00
|
|
|
#endif
|
2000-02-01 21:24:20 +03:00
|
|
|
struct hostent host;
|
2005-01-19 20:16:09 +03:00
|
|
|
struct protoent *proto;
|
2000-02-01 21:24:20 +03:00
|
|
|
struct hostent *ret;
|
|
|
|
unsigned short shortPort;
|
|
|
|
int sock;
|
|
|
|
struct timeval theTime;
|
|
|
|
|
2005-01-19 20:16:09 +03:00
|
|
|
proto = getprotobyname("tcp");
|
|
|
|
if (!proto)
|
|
|
|
{
|
2005-01-19 22:10:41 +03:00
|
|
|
perror("getprotobyname");
|
2005-01-21 00:09:40 +03:00
|
|
|
viewReport(app, "getprotobyname failed");
|
|
|
|
viewReport(app, strerror(errno) ? strerror(errno) : "NULL");
|
2005-01-19 20:16:09 +03:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
sock = socket(PF_INET, SOCK_STREAM, proto->p_proto);
|
2000-02-01 21:24:20 +03:00
|
|
|
if (sock < 0)
|
|
|
|
{
|
2005-01-19 22:10:41 +03:00
|
|
|
perror("socket");
|
2005-01-21 00:09:40 +03:00
|
|
|
viewReport(app, "socket failed");
|
|
|
|
viewReport(app, strerror(errno) ? strerror(errno) : "NULL");
|
2000-02-01 21:24:20 +03:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2005-01-21 00:09:40 +03:00
|
|
|
viewReport(app, "calling gethostbyname_r() on");
|
|
|
|
viewReport(app, (char *) hostName);
|
2000-02-01 21:24:20 +03:00
|
|
|
|
2005-01-21 00:09:40 +03:00
|
|
|
app->status(app, "gethostbyname_r", __FILE__, __LINE__);
|
2000-02-01 21:24:20 +03:00
|
|
|
|
|
|
|
gettimeofday(&theTime, NULL);
|
|
|
|
|
|
|
|
/* XXX implement my own DNS lookup to do timeouts? */
|
|
|
|
/* XXX implement my own DNS lookup to try again? */
|
2005-01-19 20:16:09 +03:00
|
|
|
|
|
|
|
#if HAVE_GETHOSTBYNAME_R
|
|
|
|
#if HAVE_GETHOSTBYNAME_R_SOLARIS
|
2000-02-01 21:24:20 +03:00
|
|
|
ret = gethostbyname_r((char *) hostName, &host, buf, sizeof(buf), &err);
|
|
|
|
if (!ret)
|
2005-01-19 20:16:09 +03:00
|
|
|
#else
|
|
|
|
gethostbyname_r((const char *) hostName, &host, buf, sizeof(buf), &ret,
|
|
|
|
&err);
|
|
|
|
if (!ret)
|
|
|
|
#endif
|
|
|
|
#else
|
|
|
|
threadMutexLock();
|
2000-02-17 10:01:52 +03:00
|
|
|
ret = gethostbyname((char *) hostName);
|
|
|
|
if (ret != NULL)
|
|
|
|
{
|
|
|
|
host = *ret;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
2000-02-01 21:24:20 +03:00
|
|
|
{
|
2005-01-21 00:09:40 +03:00
|
|
|
app->time(app, appTimeGetHostByNameFailure, &theTime);
|
|
|
|
app->status(app, "gethostbyname_r failed", __FILE__, __LINE__);
|
|
|
|
viewReportHTML(app, "failed<br><hr>");
|
2000-02-01 21:24:20 +03:00
|
|
|
close(sock);
|
2005-01-19 20:16:09 +03:00
|
|
|
#if !defined(HAVE_GETHOSTBYNAME_R)
|
|
|
|
threadMutexUnlock();
|
|
|
|
#endif
|
2000-02-01 21:24:20 +03:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2005-01-21 00:09:40 +03:00
|
|
|
app->time(app, appTimeGetHostByNameSuccess, &theTime);
|
2000-02-01 21:24:20 +03:00
|
|
|
|
2005-01-21 00:09:40 +03:00
|
|
|
app->status(app, "gethostbyname_r succeeded", __FILE__, __LINE__);
|
2000-02-01 21:24:20 +03:00
|
|
|
|
2005-01-21 00:09:40 +03:00
|
|
|
viewReportHTML(app, "succeeded<br><hr>");
|
2000-02-01 21:24:20 +03:00
|
|
|
|
|
|
|
memset(addr, 0, sizeof(*addr));
|
|
|
|
addr->sin_family = host.h_addrtype /* PF_INET */;
|
|
|
|
shortPort = port;
|
|
|
|
addr->sin_port = htons(shortPort);
|
|
|
|
memcpy(&addr->sin_addr, host.h_addr, host.h_length /* 4 */);
|
|
|
|
|
2005-01-19 20:16:09 +03:00
|
|
|
#if HAVE_GETHOSTBYNAME_R
|
|
|
|
threadMutexLock();
|
|
|
|
#endif
|
|
|
|
dnsCount++;
|
|
|
|
threadMutexUnlock();
|
|
|
|
|
2000-02-01 21:24:20 +03:00
|
|
|
return sock;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2005-01-21 00:09:40 +03:00
|
|
|
netListen(App *app, unsigned char **host, unsigned short *port)
|
2000-02-01 21:24:20 +03:00
|
|
|
{
|
|
|
|
unsigned char *hostName;
|
|
|
|
struct sockaddr_in name;
|
2005-01-19 20:16:09 +03:00
|
|
|
socklen_t namelen = sizeof(name);
|
2000-02-01 21:24:20 +03:00
|
|
|
int fd;
|
|
|
|
|
|
|
|
hostName = (unsigned char *) getHostName();
|
|
|
|
if (!hostName)
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2005-01-21 00:09:40 +03:00
|
|
|
fd = getSocketAndIPAddress(app, hostName, *port, &name);
|
2000-02-01 21:24:20 +03:00
|
|
|
if (fd < 0)
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (host)
|
|
|
|
{
|
|
|
|
*host = hostName;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
free(hostName);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (bind(fd, (struct sockaddr *) &name, sizeof(name)))
|
|
|
|
{
|
|
|
|
perror("bind");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (listen(fd, 5))
|
|
|
|
{
|
|
|
|
perror("listen");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!*port)
|
|
|
|
{
|
|
|
|
if (getsockname(fd, (struct sockaddr *) &name, &namelen))
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
*port = ntohs(name.sin_port);
|
|
|
|
}
|
|
|
|
|
|
|
|
return fd;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
netAccept(int fd)
|
|
|
|
{
|
|
|
|
int newFD;
|
2005-01-19 20:16:09 +03:00
|
|
|
socklen_t addrlen = sizeof(struct sockaddr);
|
2000-02-01 21:24:20 +03:00
|
|
|
struct sockaddr addr;
|
|
|
|
|
|
|
|
while ((newFD = accept(fd, &addr, &addrlen)) < 0)
|
|
|
|
{
|
|
|
|
if (errno != EINTR)
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return newFD;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2005-01-21 00:09:40 +03:00
|
|
|
netConnect(App *app, unsigned char *hostName, int port)
|
2000-02-01 21:24:20 +03:00
|
|
|
{
|
|
|
|
struct sockaddr_in addr;
|
|
|
|
int sock;
|
|
|
|
struct timeval theTime;
|
|
|
|
|
2005-01-21 00:09:40 +03:00
|
|
|
sock = getSocketAndIPAddress(app, hostName, port, &addr);
|
2000-02-01 21:24:20 +03:00
|
|
|
if (sock < 0)
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2005-01-21 00:09:40 +03:00
|
|
|
viewReport(app, "calling connect()");
|
2000-02-01 21:24:20 +03:00
|
|
|
|
2005-01-21 00:09:40 +03:00
|
|
|
app->status(app, "connect", __FILE__, __LINE__);
|
2000-02-01 21:24:20 +03:00
|
|
|
|
|
|
|
gettimeofday(&theTime, NULL);
|
|
|
|
|
|
|
|
if (connect(sock, (struct sockaddr *) &addr, sizeof(addr)) == -1)
|
|
|
|
{
|
2005-01-21 00:09:40 +03:00
|
|
|
app->time(app, appTimeConnectFailure, &theTime);
|
2000-02-01 21:24:20 +03:00
|
|
|
|
|
|
|
/* XXX try again if Connection timed out? */
|
|
|
|
/* XXX try again if Connection refused? */
|
|
|
|
if
|
|
|
|
(
|
|
|
|
(errno != ETIMEDOUT) &&
|
|
|
|
(errno != ECONNREFUSED)
|
|
|
|
)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "cannot connect to %s at %d: ",
|
|
|
|
hostName, port);
|
|
|
|
perror(NULL);
|
|
|
|
}
|
|
|
|
close(sock);
|
2005-01-21 00:09:40 +03:00
|
|
|
app->status(app, "connect failed", __FILE__, __LINE__);
|
|
|
|
viewReport(app, "failed:");
|
|
|
|
viewReport(app, strerror(errno) ? strerror(errno) : "NULL");
|
|
|
|
viewReportHTML(app, "<hr>");
|
2000-02-01 21:24:20 +03:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2005-01-21 00:09:40 +03:00
|
|
|
app->time(app, appTimeConnectSuccess, &theTime);
|
2000-02-01 21:24:20 +03:00
|
|
|
|
2005-01-21 00:09:40 +03:00
|
|
|
app->status(app, "connect succeeded", __FILE__, __LINE__);
|
2000-02-01 21:24:20 +03:00
|
|
|
|
2005-01-21 00:09:40 +03:00
|
|
|
viewReportHTML(app, "succeeded<br><hr>");
|
2000-02-01 21:24:20 +03:00
|
|
|
|
2005-01-19 20:16:09 +03:00
|
|
|
threadMutexLock();
|
2000-02-01 21:24:20 +03:00
|
|
|
connectCount++;
|
2005-01-19 20:16:09 +03:00
|
|
|
threadMutexUnlock();
|
2000-02-01 21:24:20 +03:00
|
|
|
|
|
|
|
return sock;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
netGetConnectCount(void)
|
|
|
|
{
|
|
|
|
return connectCount;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
netGetDNSCount(void)
|
|
|
|
{
|
|
|
|
return dnsCount;
|
|
|
|
}
|
2005-01-19 20:16:09 +03:00
|
|
|
|
|
|
|
int
|
|
|
|
netInit(void)
|
|
|
|
{
|
|
|
|
#if WINDOWS
|
|
|
|
int ret;
|
|
|
|
WSADATA wsaData;
|
|
|
|
|
|
|
|
ret = WSAStartup(0x0001, &wsaData);
|
|
|
|
if (ret)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "WSAStartup failed\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|