1 karl 1.9 //%2006////////////////////////////////////////////////////////////////////////
|
2 schuur 1.1 //
|
3 martin 1.6 // Copyright (c) 2000, 2001, 2002 BMC Software; Hewlett-Packard Development
4 // Company, L.P.; IBM Corp.; The Open Group; Tivoli Systems.
5 // Copyright (c) 2003 BMC Software; Hewlett-Packard Development Company, L.P.;
|
6 schuur 1.1 // IBM Corp.; EMC Corporation, The Open Group.
|
7 martin 1.6 // Copyright (c) 2004 BMC Software; Hewlett-Packard Development Company, L.P.;
8 // IBM Corp.; EMC Corporation; VERITAS Software Corporation; The Open Group.
9 // Copyright (c) 2005 Hewlett-Packard Development Company, L.P.; IBM Corp.;
10 // EMC Corporation; VERITAS Software Corporation; The Open Group.
|
11 karl 1.9 // Copyright (c) 2006 Hewlett-Packard Development Company, L.P.; IBM Corp.;
12 // EMC Corporation; Symantec Corporation; The Open Group.
|
13 schuur 1.1 //
14 // Permission is hereby granted, free of charge, to any person obtaining a copy
15 // of this software and associated documentation files (the "Software"), to
16 // deal in the Software without restriction, including without limitation the
17 // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
18 // sell copies of the Software, and to permit persons to whom the Software is
19 // furnished to do so, subject to the following conditions:
|
20 dave.sudlik 1.12 //
|
21 schuur 1.1 // THE ABOVE COPYRIGHT NOTICE AND THIS PERMISSION NOTICE SHALL BE INCLUDED IN
22 // ALL COPIES OR SUBSTANTIAL PORTIONS OF THE SOFTWARE. THE SOFTWARE IS PROVIDED
23 // "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
24 // LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
25 // PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
26 // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
27 // ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 //
30 //==============================================================================
31 //
32 //%/////////////////////////////////////////////////////////////////////////////
33
34 /*!
35 \file ip.c
36 \brief General TCP/IP routines.
37 */
38
|
39 ks.madhusudan 1.12.8.1 #include "cmpir_common.h"
|
40 schuur 1.1 #include <stdio.h>
41 #include <stdlib.h>
|
42 ks.madhusudan 1.12.8.1
43 #if defined PEGASUS_OS_TYPE_WINDOWS
44 #include <winsock2.h>
45 #elif defined PEGASUS_PLATFORM_ZOS_ZSERIES_IBM
|
46 r.kieninger 1.3 #define _XOPEN_SOURCE_EXTENDED 1
47 #include <arpa/inet.h>
|
48 ks.madhusudan 1.12.8.1 #else
49 #include <error.h>
|
50 konrad.r 1.4 #include <strings.h>
|
51 schuur 1.1 #include <sys/socket.h>
52 #include <netinet/in.h>
53 #include <netdb.h>
54 #include <unistd.h>
|
55 ks.madhusudan 1.12.8.1 #endif
|
56 schuur 1.1
|
57 ks.madhusudan 1.12.8.1 #include <errno.h>
58 #include <string.h>
59 #include <sys/types.h>
|
60 konrad.r 1.5
61 #ifndef CMPI_VER_100
62 #define CMPI_VER_100
63 #endif
64
|
65 schuur 1.1 #include <Pegasus/Provider/CMPI/cmpimacs.h>
66 #include <Pegasus/Provider/CMPI/cmpidt.h>
67 #include <Pegasus/Provider/CMPI/cmpift.h>
68
69 #include "ip.h"
70 #include "tcpcomm.h"
|
71 ks.madhusudan 1.12.8.1 #include "debug.h"
|
72 schuur 1.1
73 struct linger __linger = {
|
74 ks.madhusudan 1.12.8.1 1,
75 15
|
76 schuur 1.1 };
77
|
78 ks.madhusudan 1.12.8.1 #ifdef PEGASUS_OS_TYPE_WINDOWS
79 CMPI_IMPORT CMPIBrokerExtFT *CMPI_BrokerExt_Ftab;
80 #else
|
81 schuur 1.1 extern CMPIBrokerExtFT *CMPI_BrokerExt_Ftab;
|
82 ks.madhusudan 1.12.8.1 #endif
83
|
84 konrad.r 1.7 static int _die = 0;
|
85 schuur 1.1 /****************************************************************************/
86
|
87 ms.aruran 1.10 static struct hostent * _getHostByName (
88 const char *hname,
89 struct hostent *hbuf,
90 char *tmphbuf,
91 int hbuflen)
92 {
93 struct hostent *hptr;
|
94 ks.madhusudan 1.12.8.1 int herr=0,rc=0;
95
|
96 ms.aruran 1.10 #ifdef PEGASUS_PLATFORM_ZOS_ZSERIES_IBM
97 extern int h_errno;
98 #endif
99
100 #if defined(PEGASUS_OS_LINUX)
101 rc = gethostbyname_r (hname,hbuf,tmphbuf,hbuflen,&hptr,&herr);
102 #elif defined(PEGASUS_OS_SOLARIS)
103 hptr = gethostbyname_r(hname,hbuf,tmphbuf,hbuflen,&herr);
104 #else
105 hptr = gethostbyname(hname);
106 #endif
107
108 if (hptr==NULL)
109 {
|
110 ks.madhusudan 1.12.8.1
111 #ifdef PEGASUS_OS_TYPE_WINDOWS
112 const char* lpMsgBuf=NULL;
113 DWORD winErrorCode;
114
115 winErrorCode = GetLastError();
116 FormatMessage(
117 FORMAT_MESSAGE_ALLOCATE_BUFFER |
118 FORMAT_MESSAGE_FROM_SYSTEM,
119 NULL,
120 winErrorCode,
121 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
122 (LPTSTR) &lpMsgBuf,
123 0, NULL );
124 #endif
125
|
126 ms.aruran 1.10 error_at_line (0, 0, __FILE__, __LINE__,
127 #ifdef PEGASUS_PLATFORM_ZOS_ZSERIES_IBM
128 strerror (h_errno));
|
129 ks.madhusudan 1.12.8.1 #elif defined PEGASUS_OS_TYPE_WINDOWS
130 (char *)lpMsgBuf);
|
131 ms.aruran 1.10 #else
132 hstrerror (h_errno));
133 #endif
|
134 ks.madhusudan 1.12.8.1
|
135 dave.sudlik 1.12 }
|
136 ms.aruran 1.10
137 return hptr;
138 }
139
|
140 ks.madhusudan 1.12.8.1 #ifdef PEGASUS_OS_TYPE_WINDOWS
141 void winStartNetwork(void)
142 {
143 WSADATA winData;
144 WSAStartup ( 0x0002, &winData );
145 }
146 #endif
|
147 ms.aruran 1.10
|
148 ks.madhusudan 1.12.8.1 int open_connection ( const char * address, int port, int print_errmsg )
|
149 schuur 1.1 {
|
150 ms.aruran 1.10 int sockfd;
151 struct sockaddr_in sin;
152 struct hostent * server_host_name;
153 struct hostent hbuf;
154 char tempbuf[8192];
|
155 r.kieninger 1.3 // masking unability to transform an ip-address via gethostbyname()
156 #ifdef PEGASUS_PLATFORM_ZOS_ZSERIES_IBM
|
157 ks.madhusudan 1.12.8.1 extern int h_errno;
158 in_addr_t broker_ip_address;
159 broker_ip_address = inet_addr(address);
|
160 schuur 1.1
|
161 ks.madhusudan 1.12.8.1 if ( broker_ip_address != INADDR_NONE )
162 {
163 // HERE COMES THE CALL TO GETHOSTBYADDR
164 server_host_name = gethostbyaddr(
165 &(broker_ip_address),
166 sizeof(broker_ip_address), AF_INET);
167 if (server_host_name == NULL )
168 {
169 if (print_errmsg == PEGASUS_PRINT_ERROR_MESSAGE)
170 {
171 error_at_line ( 0, 0, __FILE__, __LINE__,strerror(h_errno));
172 return -1;
173 }
174 }
175 }
176 else
177 {
|
178 r.kieninger 1.3 #endif
|
179 ms.aruran 1.10 if ((server_host_name = _getHostByName (
180 address,
181 &hbuf,
182 tempbuf,
|
183 dave.sudlik 1.12 sizeof(tempbuf))) == NULL)
|
184 ks.madhusudan 1.12.8.1 {
185 return -1;
186 }
|
187 r.kieninger 1.3 // masking end of if case for differing between ip-address and host
188 #ifdef PEGASUS_PLATFORM_ZOS_ZSERIES_IBM
|
189 ks.madhusudan 1.12.8.1 }
|
190 r.kieninger 1.3 #endif
191
|
192 ks.madhusudan 1.12.8.1 sin.sin_family = AF_INET;
193 sin.sin_port = htons ( port );
194 sin.sin_addr.s_addr =
195 ( (struct in_addr *) ( server_host_name->h_addr ) )->s_addr;
|
196 schuur 1.1
|
197 ks.madhusudan 1.12.8.1 if ( ( sockfd = socket ( PF_INET,
198 SOCK_STREAM,
|
199 r.kieninger 1.3 #ifdef PEGASUS_PLATFORM_ZOS_ZSERIES_IBM
|
200 ks.madhusudan 1.12.8.1 0 ) ) == -1 )
|
201 r.kieninger 1.3 #else
|
202 ks.madhusudan 1.12.8.1 IPPROTO_TCP ) ) == PEGASUS_CMPIR_INVALID_SOCKET )
|
203 r.kieninger 1.3 #endif
|
204 ks.madhusudan 1.12.8.1 {
205 if (print_errmsg == PEGASUS_PRINT_ERROR_MESSAGE)
206 {
207 error_at_line ( 0, errno, __FILE__, __LINE__,
208 "failed to create socket" );
209 }
210 return -1;
211 }
|
212 schuur 1.1
|
213 ks.madhusudan 1.12.8.1 setsockopt ( sockfd,
214 SOL_SOCKET,
215 SO_LINGER,
216 &__linger,
217 sizeof ( struct linger ) );
|
218 schuur 1.1
|
219 ks.madhusudan 1.12.8.1 if ( connect ( sockfd, (struct sockaddr *) &sin, sizeof ( sin ) ) == -1 )
220 {
221 PEGASUS_CMPIR_CLOSESOCKET(sockfd);
222 if (print_errmsg == PEGASUS_PRINT_ERROR_MESSAGE)
223 {
224 error_at_line ( 0, errno, __FILE__, __LINE__,
225 "could not connect to %s:%d",
226 address,
227 port );
228 }
229 return -1;
230 }
231 return sockfd;
232 }
|
233 schuur 1.1
|
234 ks.madhusudan 1.12.8.1 CMPI_EXPORT void accept_connections (
235 int port,
236 void (* __connection_handler) ( int ),
237 int multithreaded )
|
238 schuur 1.1 {
|
239 ks.madhusudan 1.12.8.1 CMPI_THREAD_TYPE t;
240 int in_socket, listen_socket;
241 struct sockaddr_in sin;
242 int sin_len = sizeof ( sin );
|
243 schuur 1.1
|
244 ks.madhusudan 1.12.8.1 int ru = 1;
245 #ifdef PEGASUS_OS_TYPE_WINDOWS
246 winStartNetwork();
247 #endif
248 listen_socket = PEGASUS_CMPIR_SOCKET(PF_INET,SOCK_STREAM,IPPROTO_TCP);
249
250 setsockopt (
251 listen_socket,
252 SOL_SOCKET,
253 SO_REUSEADDR, (char *) &ru,
254 sizeof ( ru ) );
255
256 memset(&sin,0,sin_len);
257 sin.sin_family = AF_INET;
258 sin.sin_addr.s_addr = INADDR_ANY;
259 sin.sin_port = htons ( port );
|
260 schuur 1.1
|
261 ks.madhusudan 1.12.8.1 if ( bind ( listen_socket, (struct sockaddr *) &sin, sin_len ) ||
|
262 r.kieninger 1.3 #ifdef PEGASUS_PLATFORM_ZOS_ZSERIES_IBM
|
263 ks.madhusudan 1.12.8.1 listen ( listen_socket, 15 ) )
264 {
|
265 r.kieninger 1.3 #else
|
266 ks.madhusudan 1.12.8.1 listen ( listen_socket, 0 ) )
267 {
|
268 r.kieninger 1.3 #endif
|
269 ks.madhusudan 1.12.8.1 error_at_line ( -1, PEGASUS_CMPIR_WSAGETLASTERROR, __FILE__, __LINE__,
270 "cannot listen on port %d", port );
271 }
|
272 konrad.r 1.7 _die = 0;
|
273 ks.madhusudan 1.12.8.1 while ( ( in_socket = accept ( listen_socket,
274 (struct sockaddr *) &sin,
275 (size_t *) &sin_len ) ) > 0 )
276 {
277 if (_die == 1)
278 {
279 PEGASUS_CMPIR_CLOSESOCKET(in_socket);
280 break;
281 }
282 setsockopt (
283 in_socket,
284 SOL_SOCKET,
285 SO_LINGER,
286 &__linger,
287 sizeof ( struct linger ) );
288
289 if ( multithreaded )
290 {
291
292 t=CMPI_BrokerExt_Ftab->newThread(
293 (void *(PEGASUS_CMPIR_STDCALL*)(void *))__connection_handler,
294 ks.madhusudan 1.12.8.1 (void *) in_socket,1);
295
296 }
297 else
298 __connection_handler ( in_socket );
299 }
300 if (in_socket < 0)
301 {
302 error_at_line ( -1, errno, __FILE__, __LINE__, "invalid socket descriptor (%d) ", in_socket);
303 }
304 PEGASUS_CMPIR_CLOSESOCKET (listen_socket);
305 listen_socket = 0;
306 _die = 0;
307
|
308 schuur 1.1 }
309
310
|
311 dave.sudlik 1.12 int close_connection (int port )
|
312 konrad.r 1.7 {
|
313 ks.madhusudan 1.12.8.1 int socket = 0;
|
314 konrad.r 1.7
|
315 ks.madhusudan 1.12.8.1 _die = 1;
|
316 konrad.r 1.7
|
317 ks.madhusudan 1.12.8.1 // "tickle" the connection.
318 socket = open_connection("127.0.0.1", port,PEGASUS_PRINT_ERROR_MESSAGE);
319 if (socket)
320 {
321 PEGASUS_CMPIR_CLOSESOCKET (socket);
|
322 dave.sudlik 1.12
|
323 ks.madhusudan 1.12.8.1 while (_die == 1)
324 {
325 PEGASUS_CMPIR_SLEEP(1);
326 }
327 }
328 return _die;
|
329 konrad.r 1.7 }
|
330 ks.madhusudan 1.12.8.1
|
331 schuur 1.1 void get_peer_address ( int socket, char * buf )
332 {
333 #define UC(b) ( ( (int) b ) & 0xFF )
334
|
335 ks.madhusudan 1.12.8.1 struct sockaddr_in sin;
336 socklen_t sinlen = sizeof ( sin );
337 char * p = (char *) &sin.sin_addr;
|
338 schuur 1.1
|
339 ks.madhusudan 1.12.8.1 getpeername ( socket, (struct sockaddr *) &sin, &sinlen );
340 sprintf ( buf, "%d.%d.%d.%d", UC(p[0]), UC(p[1]), UC(p[2]), UC(p[3]) );
|
341 schuur 1.1 }
342
343 /****************************************************************************/
344
345 /*** Local Variables: ***/
346 /*** mode: C ***/
347 /*** c-basic-offset: 8 ***/
348 /*** End: ***/
|