(file) Return to ip.c CVS log (file) (dir) Up to [Pegasus] / pegasus / src / Pegasus / ProviderManager2 / CMPIR

  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.12.1 #include "cmpir_common.h"
 40 schuur        1.1       #include <stdio.h>
 41                         #include <stdlib.h>
 42 ks.madhusudan 1.12.12.1 
 43                         #if defined PEGASUS_OS_TYPE_WINDOWS
 44                         #include <winsock2.h>
 45 r.kieninger   1.3       #else
 46 ks.madhusudan 1.12.12.1 #if defined PEGASUS_PLATFORM_ZOS_ZSERIES_IBM
 47 r.kieninger   1.3       #include <arpa/inet.h>
 48 ks.madhusudan 1.12.12.1 #else
 49                         #include <error.h>
 50 r.kieninger   1.3       #endif
 51 konrad.r      1.4       #include <strings.h>
 52 schuur        1.1       #include <sys/socket.h>
 53                         #include <netinet/in.h>
 54                         #include <netdb.h>
 55                         #include <unistd.h>
 56 ks.madhusudan 1.12.12.1 #endif
 57 schuur        1.1       
 58 ks.madhusudan 1.12.12.1 #include <errno.h>
 59                         #include <string.h>
 60                         #include <sys/types.h>
 61 konrad.r      1.5       
 62                         #ifndef CMPI_VER_100
 63                         #define CMPI_VER_100
 64                         #endif
 65                         
 66 schuur        1.1       #include <Pegasus/Provider/CMPI/cmpimacs.h>
 67                         #include <Pegasus/Provider/CMPI/cmpidt.h>
 68                         #include <Pegasus/Provider/CMPI/cmpift.h>
 69                         
 70                         #include "ip.h"
 71                         #include "tcpcomm.h"
 72 ks.madhusudan 1.12.12.1 #include "debug.h"
 73 schuur        1.1       
 74                         struct linger __linger = {
 75 ks.madhusudan 1.12.12.1     1,
 76                             15
 77 schuur        1.1       };
 78                         
 79 ks.madhusudan 1.12.12.1 PEGASUS_IMPORT extern CMPIBrokerExtFT *CMPI_BrokerExt_Ftab;
 80                         
 81 konrad.r      1.7       static int _die = 0;
 82 schuur        1.1       /****************************************************************************/
 83                         
 84 ms.aruran     1.10      static struct hostent * _getHostByName (
 85                             const char *hname,
 86                             struct hostent *hbuf,
 87                             char *tmphbuf,
 88                             int hbuflen)
 89                         {
 90                             struct hostent  *hptr;
 91 ks.madhusudan 1.12.12.1     int herr=0,rc=0;
 92                         
 93 ms.aruran     1.10      #ifdef PEGASUS_PLATFORM_ZOS_ZSERIES_IBM
 94                             extern int h_errno;
 95                         #endif
 96                         
 97                         #if defined(PEGASUS_OS_LINUX)
 98                             rc = gethostbyname_r (hname,hbuf,tmphbuf,hbuflen,&hptr,&herr);
 99                         #elif defined(PEGASUS_OS_SOLARIS)
100                             hptr = gethostbyname_r(hname,hbuf,tmphbuf,hbuflen,&herr);
101                         #else
102                             hptr = gethostbyname(hname);
103                         #endif
104                         
105                             if (hptr==NULL)
106                             {
107 ks.madhusudan 1.12.12.1 
108                         #ifdef PEGASUS_OS_TYPE_WINDOWS
109                                 const char* lpMsgBuf=NULL;
110                                 DWORD winErrorCode;
111                         
112                                 winErrorCode = GetLastError();
113                                 FormatMessage(
114                                     FORMAT_MESSAGE_ALLOCATE_BUFFER |
115                                     FORMAT_MESSAGE_FROM_SYSTEM,
116                                     NULL,
117                                     winErrorCode,
118                                     MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
119                                     (LPTSTR) &lpMsgBuf,
120                                     0, NULL );
121                         #endif
122                         
123 ms.aruran     1.10              error_at_line (0, 0, __FILE__, __LINE__,
124                         #ifdef PEGASUS_PLATFORM_ZOS_ZSERIES_IBM
125                                 strerror (h_errno));
126 ks.madhusudan 1.12.12.1 #elif defined PEGASUS_OS_TYPE_WINDOWS
127                                 (char *)lpMsgBuf);
128 ms.aruran     1.10      #else
129                                 hstrerror (h_errno));
130                         #endif
131 ks.madhusudan 1.12.12.1 
132 dave.sudlik   1.12          }
133 ms.aruran     1.10      
134                             return hptr;
135                         }
136                         
137 ks.madhusudan 1.12.12.1 #ifdef PEGASUS_OS_TYPE_WINDOWS
138                         void winStartNetwork(void)
139                         {
140                             WSADATA winData;
141                             WSAStartup ( 0x0002, &winData );
142                         }
143                         #endif
144 ms.aruran     1.10      
145 ks.madhusudan 1.12.12.1 int open_connection ( const char * address, int port, int print_errmsg )
146 schuur        1.1       {
147 ms.aruran     1.10          int sockfd;
148                             struct sockaddr_in sin;
149                             struct hostent * server_host_name;
150                             struct hostent hbuf;
151                             char tempbuf[8192];
152 r.kieninger   1.3       // masking unability to transform an ip-address via gethostbyname()
153                         #ifdef PEGASUS_PLATFORM_ZOS_ZSERIES_IBM
154 ks.madhusudan 1.12.12.1     extern int h_errno;
155                             in_addr_t broker_ip_address;
156                             broker_ip_address = inet_addr(address);
157 schuur        1.1       
158 ks.madhusudan 1.12.12.1     if ( broker_ip_address != INADDR_NONE )
159                             {
160                                 // HERE COMES THE CALL TO GETHOSTBYADDR
161                                 server_host_name = gethostbyaddr(
162                                     &(broker_ip_address),
163                                     sizeof(broker_ip_address), AF_INET);
164                                 if (server_host_name == NULL )
165                                 {
166                                     if (print_errmsg == PEGASUS_PRINT_ERROR_MESSAGE)
167                                     {
168                                     error_at_line ( 0, 0, __FILE__, __LINE__,strerror(h_errno));
169                                     return -1;
170                                     }
171                                 }
172                             }
173                             else
174                             {
175 r.kieninger   1.3       #endif
176 ms.aruran     1.10          if ((server_host_name = _getHostByName (
177                                 address,
178                                 &hbuf,
179                                 tempbuf,
180 dave.sudlik   1.12              sizeof(tempbuf))) == NULL)
181 ks.madhusudan 1.12.12.1         {
182                                     return -1;
183                                 }
184 r.kieninger   1.3       // masking end of if case for differing between ip-address and host
185                         #ifdef PEGASUS_PLATFORM_ZOS_ZSERIES_IBM
186 ks.madhusudan 1.12.12.1     }
187 r.kieninger   1.3       #endif
188                         
189 ks.madhusudan 1.12.12.1     sin.sin_family      = AF_INET;
190                             sin.sin_port        = htons ( port );
191                             sin.sin_addr.s_addr =
192                                 ( (struct in_addr *) ( server_host_name->h_addr ) )->s_addr;
193 schuur        1.1       
194 ks.madhusudan 1.12.12.1     if ( ( sockfd = socket ( PF_INET,
195                                 SOCK_STREAM,
196 r.kieninger   1.3       #ifdef PEGASUS_PLATFORM_ZOS_ZSERIES_IBM
197 ks.madhusudan 1.12.12.1         0 ) ) == -1 )
198 r.kieninger   1.3       #else
199 ks.madhusudan 1.12.12.1         IPPROTO_TCP ) ) == PEGASUS_CMPIR_INVALID_SOCKET )
200 r.kieninger   1.3       #endif
201 ks.madhusudan 1.12.12.1     {
202                                 if (print_errmsg == PEGASUS_PRINT_ERROR_MESSAGE)
203                                 {
204                                     error_at_line ( 0, errno, __FILE__, __LINE__,
205                                         "failed to create socket" );
206                                 }
207                                 return -1;
208                             }
209 schuur        1.1       
210 ks.madhusudan 1.12.12.1     setsockopt ( sockfd,
211                                 SOL_SOCKET,
212                                 SO_LINGER,
213                                 &__linger,
214                                 sizeof ( struct linger ) );
215 schuur        1.1       
216 ks.madhusudan 1.12.12.1     if ( connect ( sockfd, (struct sockaddr *) &sin, sizeof ( sin ) ) == -1 )
217                             {
218                                 //invokes close(socket) on unix & closesocket(socket) on windows
219                                 PEGASUS_CMPIR_CLOSESOCKET(sockfd);
220                                 if (print_errmsg == PEGASUS_PRINT_ERROR_MESSAGE)
221                                 {
222                                     error_at_line ( 0, errno, __FILE__, __LINE__,
223                                         "could not connect to %s:%d",
224                                         address,
225                                         port );
226                                 }
227                                 return -1;
228                             }
229                             return sockfd;
230                         }
231 schuur        1.1       
232 ks.madhusudan 1.12.12.1 PEGASUS_EXPORT void accept_connections (
233                             int port,
234                             void (* __connection_handler) ( int ),
235                             int multithreaded )
236 schuur        1.1       {
237 ks.madhusudan 1.12.12.1     CMPI_THREAD_TYPE t;
238                             int in_socket, listen_socket;
239                             struct sockaddr_in sin;
240                             int sin_len = sizeof ( sin );
241 schuur        1.1       
242 ks.madhusudan 1.12.12.1     int ru = 1;
243                         #ifdef PEGASUS_OS_TYPE_WINDOWS
244                             winStartNetwork();
245                         #endif
246                             listen_socket = socket(PF_INET,SOCK_STREAM,IPPROTO_TCP);
247                         
248                             setsockopt (
249                                 listen_socket,
250                                 SOL_SOCKET,
251                                 SO_REUSEADDR, (char *) &ru,
252                                 sizeof ( ru ) );
253                         
254                             memset(&sin,0,sin_len);
255                             sin.sin_family = AF_INET;
256                             sin.sin_addr.s_addr = INADDR_ANY;
257                             sin.sin_port = htons ( port );
258 schuur        1.1       
259 ks.madhusudan 1.12.12.1     if ( bind ( listen_socket, (struct sockaddr *) &sin, sin_len ) ||
260 r.kieninger   1.3       #ifdef PEGASUS_PLATFORM_ZOS_ZSERIES_IBM
261 ks.madhusudan 1.12.12.1         listen ( listen_socket, 15 ) )
262                             {
263 r.kieninger   1.3       #else
264 ks.madhusudan 1.12.12.1         listen ( listen_socket, 0 ) )
265                             {
266 r.kieninger   1.3       #endif
267 ks.madhusudan 1.12.12.1         error_at_line ( -1, PEGASUS_CMPIR_WSAGETLASTERROR, __FILE__, __LINE__,
268                                     "cannot listen on port %d", port );
269                             }
270 konrad.r      1.7           _die = 0;
271 ks.madhusudan 1.12.12.1     while ( ( in_socket = accept ( listen_socket,
272                                 (struct sockaddr *) &sin,
273                                 (size_t *) &sin_len ) ) > 0 )
274                             {
275                                 if (_die == 1)
276                                 {
277                                     //invokes close(socket) on unix & closesocket(socket) on windows
278                                     PEGASUS_CMPIR_CLOSESOCKET(in_socket);
279                                     break;
280                                 }
281                                 setsockopt (
282                                     in_socket,
283                                     SOL_SOCKET,
284                                     SO_LINGER,
285                                     &__linger,
286                                     sizeof ( struct linger ) );
287                         
288                                 if ( multithreaded )
289                                 {
290                         
291                                     t=CMPI_BrokerExt_Ftab->newThread(
292 ks.madhusudan 1.12.12.1                 (void *(PEGASUS_CMPIR_STDCALL*)(void *))__connection_handler,
293                                         (void *) in_socket,1);
294                         
295                                 }
296                                  else
297                                     __connection_handler ( in_socket );
298                                 }
299                                 if (in_socket < 0)
300                                 {
301                                     error_at_line ( -1, errno, __FILE__, __LINE__, "invalid socket descriptor (%d) ", in_socket);
302                                 }
303                         
304                                 //invokes close(socket) on unix & closesocket(socket) on windows
305                                 PEGASUS_CMPIR_CLOSESOCKET (listen_socket);
306                                 listen_socket = 0;
307                                 _die = 0;
308                         
309 schuur        1.1       }
310                         
311                         
312 dave.sudlik   1.12      int close_connection (int port )
313 konrad.r      1.7       {
314 ks.madhusudan 1.12.12.1     int socket = 0;
315 konrad.r      1.7       
316 ks.madhusudan 1.12.12.1     _die = 1;
317 konrad.r      1.7       
318 ks.madhusudan 1.12.12.1     // "tickle" the connection.
319                             socket = open_connection("127.0.0.1", port,PEGASUS_PRINT_ERROR_MESSAGE);
320                             if (socket)
321                             {
322                                 //invokes close(socket) on unix & closesocket(socket) on windows
323                                 PEGASUS_CMPIR_CLOSESOCKET (socket);
324 dave.sudlik   1.12      
325 ks.madhusudan 1.12.12.1         while (_die == 1)
326                                 {
327                                     //sleep(1) on unix & Sleep(1) on windows
328                                     PEGASUS_CMPIR_SLEEP(1);
329                                 }
330                             }
331                             return _die;
332 konrad.r      1.7       }
333 ks.madhusudan 1.12.12.1 
334 schuur        1.1       void get_peer_address ( int socket, char * buf )
335                         {
336                         #define UC(b)   ( ( (int) b ) & 0xFF )
337                         
338 ks.madhusudan 1.12.12.1     struct sockaddr_in sin;
339                             socklen_t sinlen = sizeof ( sin );
340                             char * p = (char *) &sin.sin_addr;
341 schuur        1.1       
342 ks.madhusudan 1.12.12.1     getpeername ( socket, (struct sockaddr *) &sin, &sinlen );
343                             sprintf ( buf, "%d.%d.%d.%d", UC(p[0]), UC(p[1]), UC(p[2]), UC(p[3]) );
344 schuur        1.1       }
345                         
346                         /****************************************************************************/
347                         
348                         /*** Local Variables:  ***/
349                         /*** mode: C           ***/
350                         /*** c-basic-offset: 8 ***/
351                         /*** End:              ***/

No CVS admin address has been configured
Powered by
ViewCVS 0.9.2