/* mzsocket: BSD/POSIX sockets library for PLT-scheme
 * winsock compatibility: implemantation of pton/ntop
 * 
 * (C) Copyright 2007-2009 Dimitris Vyzovitis <vyzo at media.mit.edu>
 * 
 * mzsocket is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published
 * by the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * mzsocket is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with mzsocket.  If not, see <http://www.gnu.org/licenses/>.
 */

static int inet_pton (int af, const char* src, void* dst) {
  switch (af) {
  case AF_INET: {
    struct sockaddr_in sa;
    int len = sizeof(sa);
    sa.sin_family = AF_INET;
    if (!WSAStringToAddress ((LPTSTR)src, af, NULL, 
                             (LPSOCKADDR)&sa, &len)) {
      memcpy (dst, &sa.sin_addr, sizeof(struct in_addr));
      return 1;
    } else return -1;
  }
#ifdef HAVE_IPV6
  case AF_INET6: {
    struct sockaddr_in6 sa;
    int len = sizeof(sa);
    sa.sin6_family = AF_INET6;
    if (!WSAStringToAddress ((LPTSTR)src, af, NULL, 
                             (LPSOCKADDR)&sa, &len)) {
      memcpy (dst, &sa.sin6_addr, sizeof(struct in6_addr));
      return 1;
    } else return -1;
  }
#endif
  }
}

static char* inet_ntop (int af, const void* src, char* dst, size_t dstlen) {
  switch (af) {
  case AF_INET: {
    struct sockaddr_in sa;
    DWORD len = dstlen;
    sa.sin_family = AF_INET;
    memcpy (&sa.sin_addr, src, sizeof(struct in_addr));
    if (!WSAAddressToString ((LPSOCKADDR)&sa, (DWORD)sizeof(sa), 
                             NULL, (LPTSTR)dst, &len ))
      return dst;
    else return NULL;
  }
#ifdef HAVE_IPV6
  case AF_INET6: {
    struct sockaddr_in6 sa;
    DWORD len = dstlen;
    sa.sin6_family = AF_INET6;
    memcpy (&sa.sin6_addr, src, sizeof(struct in6_addr) );
    if (!WSAAddressToString ((LPSOCKADDR)&sa, (DWORD)sizeof(sa), 
                             NULL, (LPTSTR)dst, &len ))
      return dst;
    else return NULL;
  }
#endif
  }
}
