Main Page | Modules | Alphabetical List | Data Structures | Directories | File List | Data Fields | Globals | Related Pages

sockaddr_import.c

Go to the documentation of this file.
00001 /*
00002 ** Copyright (C) 2005 by Kevin L. Mitchell <klmitch@mit.edu>
00003 **
00004 ** This program is free software; you can redistribute it and/or modify
00005 ** it under the terms of the GNU General Public License as published by
00006 ** the Free Software Foundation; either version 2 of the License, or
00007 ** (at your option) any later version.
00008 **
00009 ** This program is distributed in the hope that it will be useful,
00010 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 ** GNU General Public License for more details.
00013 **
00014 ** You should have received a copy of the GNU General Public License
00015 ** along with this program; if not, write to the Free Software
00016 ** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00017 **
00018 ** @(#)$Id: sockaddr_import.c,v 1.2 2005/10/26 12:53:59 klmitch Exp $
00019 */
00027 #include "event_int.h"
00028 
00029 #include <netinet/in.h>
00030 #include <string.h>
00031 #include <sys/socket.h>
00032 #include <sys/types.h>
00033 #include <sys/un.h>
00034 
00035 RCSTAG("@(#)$Id: sockaddr_import.c,v 1.2 2005/10/26 12:53:59 klmitch Exp $");
00036 
00037 ev_err_t
00038 sockaddr_import(ev_sockaddr_t *sa, const struct sockaddr *addr)
00039 {
00040   ev_init(); /* make sure library is initialized... */
00041 
00042   /* sanity-check arguments */
00043   if (!sa || !addr ||
00044       (addr->sa_family != AF_LOCAL && addr->sa_family != AF_INET
00045 #ifdef HAVE_SOCKADDR_IN6
00046        && addr->sa_family != AF_INET6
00047 #endif /* HAVE_SOCKADDR_IN6 */
00048        ))
00049     ev_return(EINVAL);
00050 
00051   switch (addr->sa_family) {
00052   case AF_LOCAL: /* Local address... */
00053     sa->sa_type = AT_LOCAL; /* set address type */
00054     ev_assert(strlen(((struct sockaddr_un *)addr)->sun_path) <
00055               SOCK_LOCALADDR_LEN);
00056     strncpy(sa->sa_addr.saa_localaddr, ((struct sockaddr_un *)addr)->sun_path,
00057             SOCK_LOCALADDR_LEN - 1); /* path may not be terminated */
00058     break;
00059 
00060   case AF_INET: /* IPv4 address... */
00061     sa->sa_type = AT_IPv4; /* set address type */
00062     sa->sa_addr.saa_ipaddr.saai_port =
00063       ntohs(((struct sockaddr_in *)addr)->sin_port);
00064     memcpy(sa->sa_addr.saa_ipaddr.saai_addrbuf,
00065            &((struct sockaddr_in *)addr)->sin_addr.s_addr, SOCK_ADDRBUFV4_LEN);
00066     break;
00067 
00068 #ifdef HAVE_SOCKADDR_IN6
00069   case AF_INET6: /* IPv6 address... */
00070     sa->sa_type = AT_IPv6; /* set address type */
00071     sa->sa_addr.saa_ipaddr.saai_port =
00072       ntohs(((struct sockaddr_in6 *)addr)->sin6_port);
00073     memcpy(sa->sa_addr.saa_ipaddr.saai_addrbuf,
00074            ((struct sockaddr_in6 *)addr)->sin6_addr.s6_addr,
00075            SOCK_ADDRBUFV6_LEN);
00076     break;
00077 #endif /* HAVE_SOCKADDR_IN6 */
00078   }
00079 
00080   ev_return(0);
00081 }

Generated on Wed Dec 28 23:36:56 2005 for event by  doxygen 1.4.4