freetds AT lists.ibiblio.org
Subject: FreeTDS Development Group
List archive
- From: Craig Spannring <cts AT internetcds.com>
- To: "TDS Development Group" <freetds AT franklin.oit.unc.edu>
- Subject: Re: interfaces file
- Date: Wed, 11 Nov 1998 21:22:19 -0800 (PST)
The following code is meant to improve the way the interfaces file is
handled.
The hostname search order is-
1) A logical server from the file set by the
set_interfaces_file_loc() function.
2) A logical server from the $HOME/.interfaces file.
3) A logical server from the $SYBASE/interfaces file.
4) The actual server name if no matching logical server was found.
If it uses defaults to an actual name instead of logical name it will
use port 1433 for TDS4.2 and port 4000 for any other version of TDS.
The port can be overridden with the DBLIB_PORT environment variable.
Note- The code replaces the existing get_server_info() function.
/* ============================== lookup_host() ==============================
*
* Def: Given a servername and port name or number, lookup the
* hostname and service. The server ip will be stored in the
* string 'servername' in dotted-decimal notation. The service port
* number will be stored in string form in the 'port' parameter.
*
* If we can't determine both the IP address and port number then
* 'ip' and 'port' will be set to empty strings.
*
* Ret: void
*
* ===========================================================================
*/
static void lookup_host(
const char *servername, /* (I) name of the server */
const char *portname, /* (I) name or number of the port */
char *ip, /* (O) dotted-decimal ip address of server */
char *port) /* (O) port number of the service */
{
struct hostent *host = gethostbyname(servername);
struct servent *service = getservbyname(portname, "tcp");
int num = 0;
if (service==NULL)
{
num = atoi(portname);
}
else
{
num = ntohs(service->s_port);
}
if (host==NULL || num==0)
{
ip[0] = '\0';
port[0] = '\0';
}
else
{
struct in_addr *ptr = (struct in_addr *) host->h_addr;
strncpy(ip, inet_ntoa(*ptr), 17);
sprintf(port, "%d", num);
}
} /* lookup_host() */
/* ========================= search_interface_file() =========================
*
* Def: Open and read the file 'file' searching for a logical server
* by the name of 'host'. If one is found then lookup
* the IP address and port number and store them in 'ip_addr', and
* 'ip_port'.
*
* Ret: void
*
* ===========================================================================
*/
static void search_interface_file(
const char *dir, /* (I) Name of base directory for interface file */
const char *file, /* (I) Name of the interface file */
const char *host, /* (I) Logical host to search for */
char *ip_addr, /* (O) dotted-decimal IP address */
char *ip_port) /* (O) Port number for database server */
{
char pathname[PATH_MAX+1];
char line[255];
char tmp_ip[sizeof(line)];
char tmp_port[sizeof(line)];
FILE *in;
char *field;
int found=0;
ip_addr[0] = '\0';
ip_port[0] = '\0';
line[0] = '\0';
tmp_ip[0] = '\0';
tmp_port[0] = '\0';
/*
* create the full pathname to the interface file
*/
if (file==NULL || file[0]=='\0')
{
pathname[0] = '\0';
}
else
{
if (dir==NULL || dir[0]=='\0')
{
pathname[0] = '\0';
}
else
{
strncpy(pathname, dir, sizeof(pathname));
strncat(pathname, "/", sizeof(pathname));
}
strncat(pathname, file, sizeof(pathname));
pathname[sizeof(pathname)-1] = '\0';
}
/*
* parse the interfaces file and find the server and port
*/
if ((in = fopen(pathname,"r"))==NULL)
{
return;
}
while (fgets(line,sizeof(line)-1,in))
{
if (line[0]=='#') continue; /* comment */
if (!isspace(line[0]))
{
field = strtok(line,"\n\t ");
if (!strcmp(field,host)) found=1;
else found=0;
}
else if (found && isspace(line[0]))
{
field = strtok(line,"\n\t ");
if (field!=NULL && !strcmp(field,"query"))
{
field = strtok(NULL,"\n\t "); /* tcp */
field = strtok(NULL,"\n\t "); /* ether */
field = strtok(NULL,"\n\t "); /* host */
strcpy(tmp_ip,field);
field = strtok(NULL,"\n\t "); /* port */
strcpy(tmp_port,field);
}
}
}
fclose(in);
/*
* Look up the host and service
*/
lookup_host(tmp_ip, tmp_port, ip_addr, ip_port);
} /* search_interface_file() */
/* ============================ get_server_info() ============================
*
* Def: Try to find the IP number and port for a (possibly) logical server
* name.
*
* Note: It is the callers responsibility to supply large enough buffers
* to hold the ip and port numbers. ip_addr should be at least 17
* bytes long and ip_port should be at least 6 bytes long.
*
* Ret: True if it found the server, false otherwise.
*
* ===========================================================================
*/
int get_server_info(
char *server, /* (I) logical or physical server name */
char *ip_addr, /* (O) string representation of IP address */
char *ip_port) /* (O) string representation of port number */
{
ip_addr[0] = '\0';
ip_port[0] = '\0';
/*
* Look for the server in the interf_file iff interf_file has been set.
*/
if (ip_addr[0]=='\0' && interf_file[0]!='\0')
{
search_interface_file("", interf_file, ip_addr, server, ip_port);
}
/*
* if we haven't found the server yet then look for a $HOME/.interfaces
file
*/
if (ip_addr[0]=='\0')
{
char *home = getenv("HOME");
if (home!=NULL && home[0]!='\0')
{
search_interface_file(home, ".interfaces", server, ip_addr, ip_port);
}
}
/*
* if we haven't found the server yet then look in $SYBBASE/interfaces file
*/
if (ip_addr[0]=='\0')
{
char *sybase = getenv("SYBASE");
if (sybase!=NULL && sybase[0]!='\0')
{
search_interface_file(sybase, "interfaces", server, ip_addr,
ip_port);
}
}
/*
* If we still don't have the server and port then assume the user
* typed an actual server name.
*/
if (ip_addr[0]=='\0')
{
char *tmp_port;
/*
* Make a guess about the port number
*/
#ifdef TDS42
tmp_port = "1433";
#else
tmp_port = "4000";
#endif
/* FIX ME -- Need a symbolic constant for the environment variable */
if (getenv("DBLIB_PORT")!=NULL && getenv("DBLIB_PORT")[0]!='\0')
{
tmp_port = getenv("DBLIB_PORT");
}
/*
* lookup the host and service
*/
lookup_host(server, tmp_port, ip_addr, ip_port);
}
return ip_addr[0]!='\0' && ip_port[0]!='\0';
} /* get_server_info() */
-
interfaces file,
Craig Spannring, 11/11/1998
- <Possible follow-up(s)>
- Re: interfaces file, Brian Bruns, 11/11/1998
- Re: interfaces file, Gregg Jensen, 11/11/1998
- Re: interfaces file, Craig Spannring, 11/11/1998
- Re: interfaces file, Craig Spannring, 11/12/1998
- Re: interfaces file, Brian Bruns, 11/12/1998
Archive powered by MHonArc 2.6.24.