Back to index

php5  5.3.10
inet.c
Go to the documentation of this file.
00001 #include "config.w32.h"
00002 #if (_WIN32_WINNT < 0x0600) /* Vista/2k8 have these functions */
00003 #include "php.h"
00004 #include <winsock2.h>
00005 #include <windows.h>
00006 #include <Ws2tcpip.h>
00007 
00008 #include "inet.h"
00009 
00010 PHPAPI int inet_pton(int af, const char* src, void* dst)
00011 {
00012        int address_length;
00013        struct sockaddr_storage sa;
00014        struct sockaddr_in *sin = (struct sockaddr_in *)&sa;
00015        struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)&sa;
00016 
00017        switch (af) {
00018               case AF_INET:
00019                      address_length = sizeof (struct sockaddr_in);
00020                      break;
00021 
00022               case AF_INET6:
00023                      address_length = sizeof (struct sockaddr_in6);
00024                      break;
00025 
00026               default:
00027                      return -1;
00028        }
00029 
00030        if (WSAStringToAddress ((LPTSTR) src, af, NULL, (LPSOCKADDR) &sa, &address_length) == 0) {
00031               switch (af) {
00032                      case AF_INET:
00033                             memcpy (dst, &sin->sin_addr, sizeof (struct in_addr));
00034                             break;
00035 
00036                      case AF_INET6:
00037                             memcpy (dst, &sin6->sin6_addr, sizeof (struct in6_addr));
00038                             break;
00039               }
00040               return 1;
00041        }
00042 
00043        return 0;
00044 }
00045 
00046 PHPAPI const char* inet_ntop(int af, const void* src, char* dst, size_t size)
00047 {
00048        int address_length;
00049        DWORD string_length = size;
00050        struct sockaddr_storage sa;
00051        struct sockaddr_in *sin = (struct sockaddr_in *)&sa;
00052        struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)&sa;
00053 
00054        memset (&sa, 0, sizeof (sa));
00055        switch (af) {
00056               case AF_INET:
00057                      address_length = sizeof (struct sockaddr_in);
00058                      sin->sin_family = af;
00059                      memcpy (&sin->sin_addr, src, sizeof (struct in_addr));
00060                      break;
00061 
00062               case AF_INET6:
00063                      address_length = sizeof (struct sockaddr_in6);
00064                      sin6->sin6_family = af;
00065                      memcpy (&sin6->sin6_addr, src, sizeof (struct in6_addr));
00066                      break;
00067 
00068               default:
00069                      return NULL;
00070        }
00071 
00072        if (WSAAddressToString ((LPSOCKADDR) &sa, address_length, NULL, dst, &string_length) == 0) {
00073               return dst;
00074        }
00075 
00076        return NULL;
00077 }
00078 
00079 int inet_aton(const char *cp, struct in_addr *inp) {
00080   inp->s_addr = inet_addr(cp);
00081 
00082   if (inp->s_addr == INADDR_NONE) {
00083           return 0;
00084   }
00085 
00086   return 1;
00087 }
00088 #endif