dev-serveez
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[dev-serveez] IPv6 prep


From: Thien-Thi Nguyen
Subject: [dev-serveez] IPv6 prep
Date: Fri, 20 May 2011 11:54:23 +0200

One of the goals for Serveez 0.2.0 is to be "prepared for IPv6",
even if proper IPv6 support is not (yet) included.  The idea is
to make the libserveez interface upward compatible with a future
release when that support will be included.

To me (a networking newbie), this means that addresses should
explicitly include family (AF_INET vs IF_INET6) info.  Are there
other implications wrt the libserveez API?

Currently (see API-HACKING), there are eight functions that take
‘in_addr_t’ that i'm thinking to convert to take ‘svz_address_t *’
instead.  Any objections?  The alternative is to keep those
functions as-is, and introduce new IPv6-specific variants.  If
you prefer that approach, could you explain why?

BTW, here is the wip sketch of src/libserveez/address.[hc].
Comments welcome!

/*
 * address.h - abstract address for IPv4, IPv6
 *
 * Copyright (C) 2011 Thien-Thi Nguyen
 *
 * This is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3, or (at your option)
 * any later version.
 *
 * This software 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this package.  If not, see <http://www.gnu.org/licenses/>.
 */

#ifndef __ADDRESS_H__
#define __ADDRESS_H__ 1

/* begin svzint */
#include "libserveez/defines.h"
/* end svzint */

typedef struct svz_address svz_address_t;

__BEGIN_DECLS

int svz_set_addr (svz_address_t *addr, int family, const void *bits);
int svz_address_family (const svz_address_t *addr);
int svz_address_to (void *dest, const svz_address_t *addr);

__END_DECLS

#endif /* not __ADDRESS_H__ */

/* address.h ends here */
/*
 * address.c - abstract address for IPv4, IPv6
 *
 * Copyright (C) 2011 Thien-Thi Nguyen
 *
 * This is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3, or (at your option)
 * any later version.
 *
 * This software 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this package.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "config.h"
#include <string.h>
#include <sys/types.h>
#include "networking-headers.h"
#include "misc-macros.h"
#ifdef HAVE_NETINET_IP6_H
#include <netinet/ip6.h>
#define IPV6_OK  1
#else
#define IPV6_OK  0
#endif
#include "libserveez/address.h"

struct svz_address
{
  sa_family_t family;
  union
  {
    in_addr_t in4;
#if IPV6_OK
    struct in6_addr in6;
#endif
  } u;
};

/**
 * Make address @var{addr} hold an IP address in @var{family},
 * represented by @var{bits}.  @var{family} must be one of:
 *
 * @table @code
 * @item AF_INET
 * An IPv4 address; @var{bits} is @code{in_addr_t *}.
 * @item AF_INET6
 * (if supported by your system) An IPv6 address;
 * @var{bits} is @code{struct in6_addr *}.
 * @end table
 *
 * The addresses are expected in network byte order.
 * Return 0 on success, -1 if @var{family} is not recognized.
 */
int
svz_set_addr (svz_address_t *addr, int family, const void *bits)
{
  switch (family)
    {
    case AF_INET:
      memcpy (&addr->u.in4, bits, sizeof (in_addr_t));
      break;
#if IPV6_OK
    case AF_INET6:
      memcpy (&addr->u.in6, bits, sizeof (struct in6_addr *));
      break;
#endif
    default:
      addr->family = AF_UNSPEC;
      return -1;
    }
  addr->family = family;
  return 0;
}

/**
 * Return the address family of @var{addr}.
 */
int
svz_address_family (const svz_address_t *addr)
{
  return addr->family;
}

/**
 * Copy the address bits out of @var{addr} to @var{dest}.
 * Return 0 on success, -1 if either @var{addr} or @var{dest}
 * is @code{NULL}, or the @var{addr} family is @code{AF_UNSPEC}.
 */
int
svz_address_to (void *dest, const svz_address_t *addr)
{
  if (!addr || !dest || AF_UNSPEC == addr->family)
    return -1;

  switch (addr->family)
    {
    case AF_INET:
      memcpy (dest, &addr->u.in6, sizeof (in_addr_t));
      break;
#if IPV6_OK
    case AF_INET6:
      memcpy (dest, &addr->u.in6, sizeof (struct in6_addr));
      break;
#endif
    default:
      /* Should not get here!  */
      return -1;
    }
  return 0;
}

/* address.c ends here */

reply via email to

[Prev in Thread] Current Thread [Next in Thread]