home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
PC World Komputer 1998 May
/
Pcwk5b98.iso
/
WEBSERVE
/
SAMBAR
/
DATA.1
/
netutils.c
< prev
next >
Wrap
C/C++ Source or Header
|
1997-04-27
|
6KB
|
287 lines
/*
** NETUTILS
**
** HTTP Wrapper for some basic Network Utilities
**
** Confidential Property of Tod Sambar
** (c) Copyright Tod Sambar 1997
** All rights reserved.
**
**
** Public Functions:
**
** netutils_init
** ipname_lookup
** whois_lookup
**
**
** History:
** Chg# Date Description Resp
** ---- ------- ------------------------------------------------------- ----
** 2FEB97 Created sambar
*/
#include <stdio.h>
#include <memory.h>
#include <string.h>
#include <stdlib.h>
#include <winsock.h>
#include <direct.h>
#include <errno.h>
#include <netutils.h>
/*
** WHOIS defines
*/
#define WHOIS_PORT 43
#define WHOIS_SERVER "198.41.0.5" /* rs.internic.net */
/*
** Network Utility RPC Commands
*/
typedef struct netutils__rpcs
{
SA_CHAR *name;
SA_INT auth;
SA_VOID *func;
SA_CHAR *descr;
} NETUTILS__RPCS;
static NETUTILS__RPCS netutils_rpcs [] =
{
{ "ipname", SA_AUTHORIZATION_ALL, (SA_VOID *)ipname_lookup,
"Lookup the DNS name associated with an IP address." },
{ "whois", SA_AUTHORIZATION_ALL, (SA_VOID *)whois_lookup,
"Perform a whois lookup give a network domain name." }
};
/*
** NETUTILS_INIT
**
** Initialize the Network Utilities.
**
** Parameters:
** sactx Sambar Server context
**
** Returns:
** SA_SUCCEED | SA_FAIL
*/
SA_RETCODE SA_PUBLIC
netutils_init(sactx)
SA_CTX *sactx;
{
int i;
/* Register the Network Uitlities RPCs with the application */
for (i = 0; i < sizeof(netutils_rpcs) / sizeof(NETUTILS__RPCS); i++)
{
if (sa_cmd_init(sactx, netutils_rpcs[i].name, netutils_rpcs[i].auth,
netutils_rpcs[i].descr, (SA_RPCFUNC)netutils_rpcs[i].func)
!= SA_SUCCEED)
{
sa_log(sactx, "Unable to initialize Network Utility RPCs");
return (SA_FAIL);
}
}
sa_log(sactx, "Network Utilities Initialized");
return (SA_SUCCEED);
}
/*
** IPNAME_LOOKUP
**
** Get the host name associated with an IP address.
**
** Parameters:
** sactx Sambar Server context
** saconn Sambar Server connection
** saparams RPC Parameters
** infop Error parameters
**
** Returns:
** SA_SUCCEED | SA_FAIL
*/
SA_RETCODE SA_PUBLIC
ipname_lookup(sactx, saconn, saparams, infop)
SA_CTX *sactx;
SA_CONN *saconn;
SA_PARAMS *saparams;
SA_INT *infop;
{
unsigned long addr;
SA_INT datalen;
SA_CHAR *data;
struct hostent *hent;
/* Get the ip address to lookup */
if ((sa_param(sactx, saparams, "ipaddr", &data, &datalen) != SA_SUCCEED) ||
(datalen == 0))
{
*infop = SA_E_INVALIDDATA;
return (SA_FAIL);
}
addr = (int)inet_addr((char *)data);
if (addr == -1)
{
*infop = SA_E_INVALIDDATA;
return (SA_FAIL);
}
hent = gethostbyaddr((char *)&addr, sizeof(addr), AF_INET);
if (hent != NULL)
{
if (sa_conn_send(saconn, hent->h_name, SA_NULLTERM) != SA_SUCCEED)
return (SA_FAIL);
}
else
{
if (sa_conn_send(saconn, "unknown", SA_NULLTERM) != SA_SUCCEED)
return (SA_FAIL);
}
return (SA_SUCCEED);
}
/*
** WHOIS_LOOKUP
**
** Perform a whois search.
**
** Parameters:
** sactx Sambar Server context
** saconn Sambar Server connection
** saparams RPC Parameters
** infop Error parameters
**
** Returns:
** SA_SUCCEED | SA_FAIL
*/
SA_RETCODE SA_PUBLIC
whois_lookup(sactx, saconn, saparams, infop)
SA_CTX *sactx;
SA_CONN *saconn;
SA_PARAMS *saparams;
SA_INT *infop;
{
SA_INT buflen;
SA_INT datalen;
SA_BOOL finished;
SOCKET sock;
unsigned long addr;
SA_CHAR *data;
struct sockaddr_in sin;
struct hostent *hent;
SA_CHAR buffer[1024];
/* Get the site name */
if ((sa_param(sactx, saparams, "sitename", &data, &datalen) != SA_SUCCEED)
|| (datalen == 0))
{
*infop = SA_E_INVALIDDATA;
return (SA_FAIL);
}
/* Get the WHOIS server address for the query */
addr = (int)inet_addr((char *)WHOIS_SERVER);
if (addr == -1)
{
*infop = SA_E_INVALIDDATA;
return (SA_FAIL);
}
hent = gethostbyaddr((char *)&addr, sizeof(addr), AF_INET);
if (hent == NULL)
{
if (sa_conn_send(saconn, "The WHOIS server is not available.",
SA_NULLTERM) != SA_SUCCEED)
{
return (SA_FAIL);
}
return (SA_SUCCEED);
}
/* Create a socket */
sock = socket(hent->h_addrtype, SOCK_STREAM, 0);
if (sock == INVALID_SOCKET)
{
if (sa_conn_send(saconn, "The WHOIS server is not available.",
SA_NULLTERM) != SA_SUCCEED)
{
return (SA_FAIL);
}
return (SA_SUCCEED);
}
/* Connect to the WHOIS server */
sin.sin_family = AF_INET;
sin.sin_port = htons((u_short)WHOIS_PORT);
memcpy(&sin.sin_addr, hent->h_addr, hent->h_length);
/* Now, connect to that WHOIS server */
if (connect(sock, (struct sockaddr *)&sin, sizeof(sin)) < 0)
{
if (sa_conn_send(saconn, "The WHOIS server is not available.",
SA_NULLTERM) != SA_SUCCEED)
{
return (SA_FAIL);
}
return (SA_SUCCEED);
}
/* Send the request. */
sprintf(buffer, "%s\r\n", data);
buflen = strlen(buffer);
if (send(sock, buffer, buflen, 0) != buflen)
{
if (sa_conn_send(saconn, "The WHOIS server is not available.",
SA_NULLTERM) != SA_SUCCEED)
{
return (SA_FAIL);
}
return (SA_SUCCEED);
}
/* Receive the response. */
finished = 0;
while (!finished)
{
buflen = recv(sock, buffer, 1024, 0);
if ((buflen < 0) && (errno == EINTR))
continue;
if (buflen == SOCKET_ERROR)
return (SA_FAIL);
if (buflen > 0)
{
if (sa_conn_send(saconn, buffer, buflen) != SA_SUCCEED)
return (SA_FAIL);
}
else
{
finished = 1;
}
}
if (!finished)
{
if (sa_conn_send(saconn, "The WHOIS server is not available.",
SA_NULLTERM) != SA_SUCCEED)
{
return (SA_FAIL);
}
}
closesocket(sock);
return (SA_SUCCEED);
}