"Fossies" - the Fresh Open Source Software Archive

Member "libspf2-1.2.10/src/libspf2/spf_win32.c" (28 Jan 2012, 3347 Bytes) of package /linux/privat/libspf2-1.2.10.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "spf_win32.c" see the Fossies "Dox" file reference documentation.

    1 /* 
    2  * This program is free software; you can redistribute it and/or modify
    3  * it under the terms of either:
    4  * 
    5  *   a) The GNU Lesser General Public License as published by the Free
    6  *      Software Foundation; either version 2.1, or (at your option) any
    7  *      later version,
    8  * 
    9  *   OR
   10  * 
   11  *   b) The two-clause BSD license.
   12  *
   13  * These licenses can be found with the distribution in the file LICENSES
   14  */
   15 
   16 #ifdef _WIN32
   17 
   18 #include "spf_win32_internal.h"
   19 
   20 int SPF_win32_startup()
   21 {
   22     WORD wVersionRequested;
   23     WSADATA wsaData;
   24     int err;
   25  
   26     wVersionRequested = MAKEWORD( 2, 2 );
   27  
   28     err = WSAStartup( wVersionRequested, &wsaData );
   29     if ( err != 0 ) 
   30         return 0;
   31  
   32     if ( LOBYTE( wsaData.wVersion ) != 2 ||
   33         HIBYTE( wsaData.wVersion ) != 2 )
   34     {
   35         WSACleanup();
   36         return 0;
   37     }
   38 
   39     return 1;
   40 }
   41 
   42 int SPF_win32_cleanup()
   43 {
   44     return WSACleanup();
   45 }
   46 
   47 char *inet_ntop( int af, const void *src, char *dst, size_t size )
   48 {
   49     void    *pSrc_sockaddr;
   50     struct  sockaddr_in     src_sockaddr;
   51     struct  sockaddr_in6    src6_sockaddr;
   52     DWORD   src_size;
   53     DWORD   temp;
   54     int     result;
   55     DWORD   error;
   56 
   57     switch( af )
   58     {
   59     case AF_INET:
   60         src_sockaddr.sin_family = AF_INET;
   61         src_sockaddr.sin_port = 0;
   62         memcpy( &src_sockaddr.sin_addr, src, sizeof( struct in_addr ) );
   63         pSrc_sockaddr = &src_sockaddr;
   64         src_size = sizeof( struct sockaddr_in );
   65         break;
   66     case AF_INET6:
   67         src6_sockaddr.sin6_family = AF_INET6;
   68         src6_sockaddr.sin6_port = 0;
   69         src6_sockaddr.sin6_flowinfo = 0;
   70         src6_sockaddr.sin6_scope_id = 0;
   71         memcpy( &src6_sockaddr.sin6_addr, src, sizeof( struct in6_addr ) );
   72         pSrc_sockaddr = &src6_sockaddr;
   73         src_size = sizeof( struct sockaddr_in6 );
   74         break;
   75     default:
   76         return NULL;
   77     }
   78 
   79     temp = size;
   80     result = WSAAddressToStringA( (LPSOCKADDR)pSrc_sockaddr, src_size, 
   81         NULL, dst, &temp );
   82     
   83     error = GetLastError();
   84 
   85     if (result == 0)
   86         // Success
   87         return dst;
   88     else
   89         // Failure
   90         return NULL;
   91 }
   92 
   93 int inet_pton( int af, const char *src, void *dst )
   94 {
   95     /* IPv6 is largest buffer, so use it for both */
   96     struct  sockaddr_in6    dst_sockaddr;
   97     struct  sockaddr_in6    *pDst_sockaddr;
   98     int     dst_size;
   99     int     result;
  100     DWORD   error;
  101 
  102     pDst_sockaddr = &dst_sockaddr;
  103 
  104     switch( af )
  105     {
  106     case AF_INET:
  107         dst_size = sizeof( struct sockaddr_in );
  108         break;
  109     case AF_INET6:
  110         dst_size = sizeof( struct sockaddr_in6 );
  111         break;
  112     default:
  113         return 0;
  114     }
  115 
  116     result = WSAStringToAddressA( src, af, NULL, 
  117         (LPSOCKADDR)pDst_sockaddr, &dst_size );
  118 
  119     if ( result != 0 )
  120     {
  121         error = GetLastError();
  122         return error;
  123         return 0;
  124     }
  125 
  126     switch( af )
  127     {
  128     case AF_INET:
  129         memcpy( dst, &((struct sockaddr_in*)pDst_sockaddr)->sin_addr, 
  130             sizeof( struct in_addr ) );
  131         break;
  132     case AF_INET6:
  133         memcpy( dst, &pDst_sockaddr->sin6_addr, 
  134             sizeof( struct in6_addr ) );
  135         break;
  136     }
  137 
  138     return 1;
  139 }
  140 
  141 int gethostnameFQDN( char *name, int namelen )
  142 {
  143     int result;
  144     int fullnamelen;
  145     struct hostent *he;
  146 
  147     result = gethostname( name, namelen );
  148 
  149     if ( result == 0 )
  150     {
  151         he = gethostbyname( name );
  152 
  153         if (he != NULL)
  154         {
  155             fullnamelen = strlen( he->h_name );
  156 
  157             if (fullnamelen < namelen)
  158                 strcpy( name, he->h_name );
  159         }
  160     }
  161 
  162     return result;
  163 }
  164 
  165 #endif