ccn_sockaddrutil.c

Go to the documentation of this file.
00001 /**
00002  * @file ccn_sockaddrutil.c
00003  * @brief sockaddr utilities
00004  * 
00005  * Part of the CCNx C Library.
00006  *
00007  * Copyright (C) 2010 Palo Alto Research Center, Inc.
00008  *
00009  * This library is free software; you can redistribute it and/or modify it
00010  * under the terms of the GNU Lesser General Public License version 2.1
00011  * as published by the Free Software Foundation.
00012  * This library is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
00015  * Lesser General Public License for more details. You should have received
00016  * a copy of the GNU Lesser General Public License along with this library;
00017  * if not, write to the Free Software Foundation, Inc., 51 Franklin Street,
00018  * Fifth Floor, Boston, MA 02110-1301 USA.
00019  */
00020  
00021 #include <string.h>
00022 #include <sys/types.h>
00023 #include <sys/socket.h>
00024 #include <netinet/in.h>
00025 #include <arpa/inet.h>
00026 #include <ccn/charbuf.h>
00027 #include <ccn/sockaddrutil.h>
00028 
00029 /**
00030  * Append a printable representation of sa (sans any port info) to the charbuf.
00031  *
00032  * IPv6 addresses will be enclosed in square braces, as in the host part
00033  * of a URI.
00034  * @returns the port number (0 if no port number is available), or -1 for
00035  *        in case of an error.
00036  */
00037 int
00038 ccn_charbuf_append_sockaddr(struct ccn_charbuf *c, const struct sockaddr *sa)
00039 {
00040     const unsigned char *rawaddr = NULL;
00041     const char *s = NULL;
00042     const char *closer = "";
00043     const struct sockaddr_in *addr4 = NULL;
00044     const struct sockaddr_in6 *addr6 = NULL;
00045     size_t savlen = c->length;
00046     socklen_t sz = 80;
00047     int port = 0;
00048     
00049     if (sa == NULL)
00050         return(-1);
00051     switch (sa->sa_family) {
00052         case AF_INET:
00053             addr4 = (struct sockaddr_in *)sa;
00054             rawaddr = (const unsigned char *)&addr4->sin_addr.s_addr;
00055             port = htons(addr4->sin_port);break;
00056         case AF_INET6:
00057             addr6 = (struct sockaddr_in6 *)sa;
00058             rawaddr = (const unsigned char *)&addr6->sin6_addr;
00059             port = htons(addr6->sin6_port);
00060             ccn_charbuf_append_string(c, "[");
00061             closer = "]";
00062             break;
00063         default:
00064             return(-1);
00065     }
00066     s = inet_ntop(sa->sa_family, rawaddr, 
00067                   (void *)ccn_charbuf_reserve(c, sz), sz);
00068     if (s == NULL) {
00069         c->length = savlen;
00070         return(-1);
00071     }
00072     c->length += strlen(s);
00073     ccn_charbuf_append_string(c, closer);
00074     return(port);
00075 }

Generated on Thu Feb 16 00:44:00 2012 for Content-Centric Networking in C by  doxygen 1.5.6