Debian Codegraph @

xpa/tcp.c

raw symbols SCIP
1 /*
2 * Copyright (c) 1999-2003 Smithsonian Astrophysical Observatory
3 */
4
5 /*
6 *
7 * tcp.c -- unix tcp connection routines
8 *
9 */
10 #if HAVE_CONFIG_H
11 #include <conf.h>
12 #endif
13
14 #if HAVE_UNISTD_H
15 #include <unistd.h>
16 #endif
17 #if HAVE_STDLIB_H
18 #include <stdlib.h>
19 #endif
20 #include <stdio.h>
21 #include <errno.h>
22 #include <string.h>
23 #include <xport.h>
24 #include <tcp.h>
25
26 /* the ever-present */
27 #ifndef SZ_LINE
28 #define SZ_LINE 1024
29 #endif
30
31 /* name of user environment variable specifying this host */
32 #ifndef MYHOST
33 #define MYHOST "XPA_HOST"
34 #endif
35
36 /* other modules can set this variable and the default for various routines
37 will be "localhost" instead of the DNS-dependent hostname */
38 int use_localhost=0;
39
40 static char myhost[SZ_LINE]; /* hostname to registering access points */
41
42 /*
43 *----------------------------------------------------------------------------
44 *
45 *
46 * Private Routines and Data
47 *
48 *
49 *----------------------------------------------------------------------------
50 */
51
52 static char ipstr[SZ_LINE];
53
54 /*
55 *----------------------------------------------------------------------------
56 *
57 *
58 * Public Routines
59 *
60 *
61 *----------------------------------------------------------------------------
62 */
63
64 /*
65 *--------------------------------------------------------------
66 *
67 * Routines: gethost
68 *
69 * Purpose: Find the current hostname
70 *
71 * Results: 1 on success, 0 for failure
72 *
73 *--------------------------------------------------------------
74 */
75 #ifdef ANSI_FUNC
76 int
77 gethost (
78 char *xhost, /* (out) corresponding hostname */
79 int len /* (in) length of host buffer */
80 )
81 #else
82 int gethost(xhost, len)
83 char *xhost; /* (out) corresponding hostname */
84 int len; /* (in) length of host buffer */
85 #endif
86 {
87 char *s=NULL;
88 struct hostent *hent;
89 static int init=0;
90
91 if( use_localhost == 0 ){
92 if( init == 0 ){
93 if( (s=(char *)getenv(MYHOST)) != NULL ){
94 strncpy(myhost, s, SZ_LINE-1);
95 } else {
96 gethostname(myhost, SZ_LINE-1);
97 }
98 init++;
99 }
100 strncpy(xhost, myhost, len-1);
101 if( (hent = gethostbyname(xhost)) == NULL ){
102 return(-1);
103 }
104 strncpy(xhost, hent->h_name, len-1);
105 }
106 else{
107 strncpy(xhost, "localhost", len-1);
108 }
109 xhost[len-1] = '\0';
110 return(0);
111 }
112
113 /*
114 *----------------------------------------------------------------------------
115 *
116 * Routine: gethostip
117 *
118 * Purpose: Find the IP address corresponding to a hostname
119 *
120 * Results: ip (host byte order) on success, 0 if host is unknown
121 *
122 *---------------------------------------------------------------------------
123 */
124 #ifdef ANSI_FUNC
125 unsigned int
126 gethostip (
127 char *xhost /* (in) Hostname (human readable) */
128 )
129 #else
130 unsigned int gethostip(xhost)
131 char *xhost; /* (in) Hostname (human readable) */
132 #endif
133 {
134 struct hostent *hostent;
135 unsigned int ip;
136 char temp[SZ_LINE];
137 int saveip=0;
138 static unsigned int myip=0;
139
140
141 /* null input means current host */
142 if( (xhost == NULL) || (*xhost == '\0') || !strcmp(xhost, "$host") ){
143 if( myip != 0 )
144 return(myip);
145 saveip = 1;
146 gethost(temp, SZ_LINE);
147 }
148 else if( !strcmp(xhost, "$localhost") ){
149 strcpy(temp, "localhost");
150 } else {
151 strncpy(temp, xhost, SZ_LINE-1);
152 temp[SZ_LINE-1] = '\0';
153 }
154
155 /* special check */
156 if( !strcmp(temp, "localhost") || !strcmp(temp, "localhost.localdomain") ){
157 ip = htonl(0x7F000001);
158 goto done;
159 }
160
161 /*
162 * Try looking by address (i.e., host is something like "128.84.253.1").
163 * Do this first because it's much faster (no trip to the DNS)
164 */
165 if( (int)(ip = inet_addr(temp)) != -1 ){
166 goto done;
167 }
168
169 /*
170 * Try looking it up by name. If successful, the IP address is in
171 * hostent->h_addr_list[0]
172 */
173 if( (hostent = gethostbyname(temp)) != NULL ){
174 memcpy(&ip, hostent->h_addr_list[0], (size_t)hostent->h_length);
175 goto done;
176 }
177
178 /* could not convert */
179 ip = 0;
180 saveip = 0;
181
182 done:
183 ip = ntohl(ip);
184 if( saveip ) myip = ip;
185 return(ip);
186 }
187
188 /*
189 *----------------------------------------------------------------------------
190 *
191 * Routine: getiphost
192 *
193 * Purpose: Find the IP address in dot format corresponding to an ip
194 *
195 * Results: ip string (in static buffer) or NULL
196 *
197 *---------------------------------------------------------------------------
198 */
199 #ifdef ANSI_FUNC
200 char *
201 getiphost (unsigned int ip)
202 #else
203 char *getiphost(ip)
204 unsigned int ip;
205 #endif
206 {
207 char *s;
208 struct sockaddr_in sockbuf;
209
210 if( ip == 0x7F000001 ){
211 strcpy(ipstr, "localhost");
212 return(ipstr);
213 }
214
215 sockbuf.sin_addr.s_addr = htonl(ip);
216 s = (char *)inet_ntoa(sockbuf.sin_addr);
217 if( s != NULL ){
218 strcpy(ipstr, s);
219 return(ipstr);
220 }
221 else
222 return(NULL);
223 }