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

sockaddr_export.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_export.c,v 1.3 2005/12/29 04:33:58 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_export.c,v 1.3 2005/12/29 04:33:58 klmitch Exp $");
00036 
00037 ev_err_t
00038 sockaddr_export(const ev_sockaddr_t *sa, struct sockaddr *addr, int *len)
00039 {
00040   int t_len;
00041 
00042   ev_init(); /* make sure library is initialized... */
00043 
00044   /* sanity-check arguments */
00045   if (!sa || !addr || !len)
00046     ev_return(EINVAL);
00047 
00048   /* Clear out the address structure... */
00049   memset(addr, 0, *len);
00050 
00051   switch (sa->sa_type) {
00052 #ifdef HAVE_SOCKADDR_IN6
00053   case AT_IPv6:
00054     if (*len < sizeof(struct sockaddr_in6)) /* verify the size */
00055       ev_return(ENOSPC);
00056     *len = sizeof(struct sockaddr_in6); /* reset the size */
00057     ((struct sockaddr_in6 *)addr)->sin6_family = AF_INET6; /* set family */
00058     /* Copy the port over... */
00059     ((struct sockaddr_in6 *)addr)->sin6_port = htons(sa_ipaddrport(sa));
00060     /* Copy the address over... */
00061     memcpy(((struct sockaddr_in6 *)addr)->sin6_addr.s6_addr,
00062            sa_ipaddrbuf(sa), SOCK_ADDRBUFV6_LEN);
00063     break;
00064 #endif /* HAVE_SOCKADDR_IN6 */
00065 
00066   case AT_IPv4:
00067     if (*len < sizeof(struct sockaddr_in)) /* verify the size */
00068       ev_return(ENOSPC);
00069     *len = sizeof(struct sockaddr_in); /* reset the size */
00070     ((struct sockaddr_in *)addr)->sin_family = AF_INET; /* set family */
00071     /* Copy the port over... */
00072     ((struct sockaddr_in *)addr)->sin_port = htons(sa_ipaddrport(sa));
00073     /* Copy the address over... */
00074     memcpy(&((struct sockaddr_in *)addr)->sin_addr.s_addr,
00075            sa_ipaddrbuf(sa), SOCK_ADDRBUFV4_LEN);
00076     break;
00077 
00078   case AT_LOCAL:
00079     if (*len < (sizeof(struct sockaddr_un) - SOCK_LOCALADDR_LEN +
00080                 (t_len = strlen(sa_localaddr(sa))))) /* verify the size */
00081       ev_return(ENOSPC);
00082     /* reset the size */
00083     *len = sizeof(struct sockaddr_un) - SOCK_LOCALADDR_LEN + t_len;
00084     ((struct sockaddr_un *)addr)->sun_family = AF_LOCAL; /* set family */
00085     /* Copy the address over... */
00086     memcpy(((struct sockaddr_un *)addr)->sun_path, sa_localaddr(sa), t_len);
00087     /* String should already be terminated due to memset() above */
00088     break;
00089 
00090   default:
00091     ev_return(ENOSYS);
00092     break;
00093   }
00094 
00095 #ifdef HAVE_SOCKADDR_SA_LEN
00096   addr->sa_len = *len; /* set the length member */
00097 #endif /* HAVE_SOCKADDR_SA_LEN */
00098 
00099   ev_return(0);
00100 }

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