1 /***********************************************************************/
2 /* Open Visualization Data Explorer                                    */
3 /* (C) Copyright IBM Corp. 1989,1999                                   */
4 /* ALL RIGHTS RESERVED                                                 */
5 /* This code licensed under the                                        */
6 /*    "IBM PUBLIC LICENSE - Open Visualization Data Explorer"          */
7 /***********************************************************************/
8 
9 #include <dxconfig.h>
10 
11 
12 #include <dx/dx.h>
13 
14 #include <stdio.h>
15 #include <ctype.h>
16 #include <sys/types.h>
17 
18 #if defined(HAVE_UNISTD_H)
19 #include <unistd.h>
20 #endif
21 
22 #if defined(HAVE_SYS_TIMEB_H)
23 #include <sys/timeb.h>
24 #elif defined(HAVE_SYS_TIME_H)
25 #include <sys/time.h>
26 #endif
27 
28 #if defined(HAVE_NETINET_IN_H)
29 #include <netinet/in.h>
30 #endif
31 
32 #if defined(HAVE_ARPA_INET_H)
33 #include <arpa/inet.h>
34 #endif
35 
36 #if defined(HAVE_NETDB_H)
37 #include <netdb.h>
38 #endif
39 
40 #if defined(HAVE_SYS_SOCKET_H)
41 #include <sys/socket.h>
42 #endif
43 
44 #if defined(HAVE_SYS_UN_H)
45 #include <sys/un.h>
46 #endif
47 
48 #include <errno.h>
49 
50 #if defined(HAVE_SYS_SELECT_H)
51 #include <sys/select.h>
52 #endif
53 
54 #if defined(HAVE_STRING_H)
55 #include <string.h>
56 #endif
57 
58 #define verbose 0
59 
60 int
_dxfHostIsLocal(char * host)61 _dxfHostIsLocal(char *host)
62 {
63     char localHostname[BUFSIZ];
64     char localhostHostname[BUFSIZ];
65     char remoteHostname[BUFSIZ];
66     struct hostent *he;
67     int  hostnameFound;
68 #ifndef DXD_OS_NON_UNIX
69     if (strcmp ("unix", host) == 0)
70 	return 1;
71 #else
72     char tmpHost[MAXHOSTNAMELEN], *p;
73 
74     if(!host || !strlen(host) || !_stricmp("localhost", host) || !_stricmp("localPC", host))
75 	return 1;
76     strcpy(tmpHost, host);
77     p = strstr(tmpHost, ":0");
78     if(p) {
79 	tmpHost[p-tmpHost] = '\0';
80     }
81     if(!tmpHost || !strlen(tmpHost) || !_stricmp("localhost", tmpHost) || !_stricmp("localPC", tmpHost))
82 	return 1;
83     gethostname(localHostname, BUFSIZ);
84     if(strcmp(tmpHost, localHostname) == 0)
85 	return 1;  /*  default to localhost    */
86 #endif
87 
88     /* Local host names do not have to resolve even in UNIX.
89        If it doesn't resolve, then assume localhost instead
90        of returning error.
91      */
92 
93     he = gethostbyname (host);
94     if (he == NULL) {
95 		return 1;   /* Default to LocalHost */
96     }
97     strcpy(remoteHostname, he->h_name);
98 
99     if (gethostname(localHostname, BUFSIZ) >= 0 && (he=gethostbyname(localHostname)) != NULL)
100     {
101 	strcpy(localHostname, he->h_name);
102 	hostnameFound = 1;
103     }
104     else
105     {
106 #ifdef DXD_OS_NON_UNIX
107 	return 1;   /* Default to LocalHost */
108 #endif
109 	hostnameFound = 0;
110     }
111 
112     he = gethostbyname ("localhost");
113     if (he == NULL) {
114 #ifdef DXD_OS_NON_UNIX
115 	return 1;   /* Default to LocalHost */
116 #else
117 	herror("localhost");
118 	return(0);
119 #endif
120     }
121     strcpy(localhostHostname, he->h_name);
122 
123     return ((hostnameFound && strcmp (localHostname, remoteHostname) == 0) ||
124 	    strcmp (localhostHostname, remoteHostname) == 0);
125 }
126 
127 /*
128  * Initiate a connection to a server.
129  */
DXConnectToServer(char * host,int pport)130 int DXConnectToServer(char *host, int pport)
131 {
132     int sock;
133     struct sockaddr_in server;
134 #if DXD_SOCKET_UNIXDOMAIN_OK
135     struct sockaddr_un userver;
136 #endif
137     struct hostent *hp;
138     int length;
139 #ifndef DXD_HAS_WINSOCKETS
140     struct hostent *gethostbyname();
141 #endif
142     ushort port = pport;
143 
144 #if DXD_SOCKET_UNIXDOMAIN_OK
145     if (_dxfHostIsLocal(host))
146     {
147 	memset((char *)&userver, 0, sizeof(userver));
148 	userver.sun_family = AF_INET;
149 	sprintf(userver.sun_path, "/tmp/.DX-unix/DX%d", port);
150 	length = sizeof (userver) - sizeof(userver.sun_path) + strlen (userver.sun_path);
151 
152 	sock = socket(AF_UNIX, SOCK_STREAM, 0);
153 	if (sock < 0)
154 	    return(-1);
155 
156 	if (connect(sock, (struct sockaddr *) &userver, length) >= 0)
157 	{
158 	    return(sock);
159 	}
160 	close (sock);
161 	sock = -1;
162     }
163 #endif
164 
165     memset((char *)&server, 0, sizeof(server));
166     server.sin_family = AF_INET;
167     server.sin_port = htons(port);
168 
169     /* get host address */
170     if (isdigit(host[0]))
171 	server.sin_addr.s_addr = inet_addr(host);
172     else
173     {
174 	hp = gethostbyname(host);
175 	if (hp == NULL)
176 	    return(-1);
177 	memcpy(&server.sin_addr, hp->h_addr, hp->h_length);
178     }
179 
180     sock = socket(AF_INET, SOCK_STREAM, 0);
181     if (sock < 0)
182 	return(-1);
183 
184     if (connect(sock, (struct sockaddr*)&server, sizeof(server)) < 0)
185     {
186 	close(sock);
187 	return(-1);
188     }
189 
190     return(sock);
191 }
192