testexecmgmt/ucc/Source/PppdGateway/socket_helper.c
changeset 0 3da2a79470a7
equal deleted inserted replaced
-1:000000000000 0:3da2a79470a7
       
     1 /*
       
     2 * Copyright (c) 2005-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:  
       
    15 * System Includes
       
    16 *
       
    17 */
       
    18 
       
    19 
       
    20 
       
    21 #include <stdio.h>
       
    22 #include <stdlib.h>
       
    23 #include <string.h>
       
    24 #include <assert.h>
       
    25 #include <ctype.h>
       
    26 #include <errno.h>
       
    27 
       
    28 #ifdef WIN32
       
    29 #include <winsock2.h>
       
    30 #else
       
    31 #include <sys/types.h>
       
    32 #include <sys/socket.h>
       
    33 #include <netinet/in.h>
       
    34 #include <netdb.h>
       
    35 #endif
       
    36 
       
    37 /*******************************************************************************
       
    38  *
       
    39  * Local Includes
       
    40  *
       
    41  ******************************************************************************/
       
    42 #include "socket_helper.h"
       
    43 
       
    44 /*******************************************************************************
       
    45  *
       
    46  * Definition
       
    47  *
       
    48  ******************************************************************************/
       
    49 
       
    50 /*******************************************************************************
       
    51  *
       
    52  * Implementation
       
    53  *
       
    54  ******************************************************************************/
       
    55 struct sockaddr_in getsockaddr( const char* ip, const char* port)
       
    56 {	
       
    57 	int isip;
       
    58 	struct sockaddr_in sockaddr;
       
    59 	int b1, b2, b3, b4;
       
    60 	unsigned long addr = 0;
       
    61 
       
    62 	// set the basics
       
    63 	sockaddr.sin_family = AF_INET;		
       
    64 	sockaddr.sin_port = htons(atoi(port));
       
    65 
       
    66 	// if IP is NULL then set to INADDR_ANY otherwise work it out
       
    67 	if( ip == NULL ) {
       
    68 #ifdef WIN32
       
    69 		sockaddr.sin_addr.S_un.S_addr = INADDR_ANY;
       
    70 #else
       
    71 		sockaddr.sin_addr.s_addr = INADDR_ANY;
       
    72 #endif
       
    73 		return sockaddr;
       
    74 	} 
       
    75 
       
    76 	// otherwise we see if this is an IP address
       
    77 	isip = is_ip_address( ip );
       
    78 	if( isip ) {
       
    79 		sscanf( ip, "%d.%d.%d.%d", &b1, &b2, &b3,&b4 );
       
    80 		addr = ((b1<<24)&0xFF000000);
       
    81 		addr |= ((b2<<16)&0x00FF0000);
       
    82 		addr |= ((b3<<8)&0x0000FF00);
       
    83 		addr |= ((b4)&0x000000FF);
       
    84 #ifdef WIN32
       
    85 		sockaddr.sin_addr.S_un.S_addr = htonl(addr);
       
    86 #else
       
    87 		sockaddr.sin_addr.s_addr = htonl(addr);
       
    88 #endif
       
    89 
       
    90 	} else {
       
    91 		struct hostent *hp = gethostbyname(ip);
       
    92 		memcpy(&sockaddr.sin_addr,hp->h_addr,hp->h_length); 	
       
    93 	}
       
    94 
       
    95 	return sockaddr;
       
    96 }
       
    97 
       
    98 int is_ip_address( const char* aAddr )
       
    99 {
       
   100   int i;
       
   101   int dot_count = 0;
       
   102 
       
   103   // Our method for checking whether this is an IP address is to check that
       
   104   // we have digits and points (3 to be precise).
       
   105   for( i = 0; aAddr[i] != 0; i++ ) {
       
   106     if( (aAddr[i] != '.') && (isdigit(aAddr[i]) == 0) ) {
       
   107       return 0;
       
   108     }
       
   109     if( aAddr[i] == '.' ) {
       
   110       dot_count++;
       
   111       if( dot_count > 3 ) {
       
   112 	return 0;
       
   113       }
       
   114     }
       
   115   }
       
   116   return 1;
       
   117 }
       
   118 
       
   119 /*******************************************************************************
       
   120  *
       
   121  * GetSocketError - gets the last socket error - for xdev
       
   122  *
       
   123  ******************************************************************************/
       
   124 int GetSocketError()
       
   125 {
       
   126 #ifdef WIN32
       
   127 	return WSAGetLastError();
       
   128 #else
       
   129 	return errno;
       
   130 #endif
       
   131 }