ccn_setup_sockaddr_un.c

Go to the documentation of this file.
00001 /**
00002  * @file ccn_setup_sockaddr_un.c
00003  * @brief
00004  * 
00005  * Part of the CCNx C Library.
00006  *
00007  * Copyright (C) 2009 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 <stdio.h>
00022 #include <stdlib.h>
00023 #include <string.h>
00024 #include <arpa/inet.h>
00025 #include <netinet/in.h>
00026 #include <netdb.h>
00027 #include <sys/socket.h>
00028 #include <sys/un.h>
00029 #if defined(NEED_GETADDRINFO_COMPAT)
00030 #include "getaddrinfo.h"
00031 #include "dummyin6.h"
00032 #endif
00033 #ifndef AI_ADDRCONFIG
00034 #define AI_ADDRCONFIG 0 /*IEEE Std 1003.1-2001/Cor 1-2002, item XSH/TC1/D6/20*/
00035 #endif
00036 
00037 #include <ccn/ccnd.h>
00038 #include <ccn/ccn_private.h>
00039 #include <ccn/charbuf.h>
00040 
00041 /**
00042  * Set up a unix-domain socket address for contacting ccnd.
00043  *
00044  * If the environment variable CCN_LOCAL_SOCKNAME is set and
00045  * not empty, it supplies the name stem; otherwise the compiled-in
00046  * default is used.
00047  *
00048  * If portstr is NULL or empty, the environment variable CCN_LOCAL_PORT is
00049  * checked. If the portstr specifies something other than the ccnx registered
00050  * port number, the socket name is modified accordingly. 
00051  * @param portstr - numeric port; use NULL for default.
00052  */
00053 void
00054 ccn_setup_sockaddr_un(const char *portstr, struct sockaddr_un *result)
00055 {
00056     struct sockaddr_un *sa = result;
00057     const char *sockname = getenv("CCN_LOCAL_SOCKNAME");
00058     if (sockname == NULL || sockname[0] == 0)
00059         sockname = CCN_DEFAULT_LOCAL_SOCKNAME; /* /tmp/.ccnd.sock */
00060     memset(sa, 0, sizeof(*sa));
00061     sa->sun_family = AF_UNIX;
00062     if (portstr == NULL || portstr[0] == 0)
00063         portstr = getenv(CCN_LOCAL_PORT_ENVNAME);
00064     if (portstr != NULL && atoi(portstr) > 0 &&
00065           atoi(portstr) != atoi(CCN_DEFAULT_UNICAST_PORT))
00066         snprintf(sa->sun_path, sizeof(sa->sun_path), "%s.%s",
00067                  sockname, portstr);
00068     else
00069         snprintf(sa->sun_path, sizeof(sa->sun_path), "%s",
00070                  sockname);
00071 }
00072 
00073 /**
00074  * Set up a Internet socket address for contacting ccnd.
00075  *
00076  * The name must be of the form "tcp[4|6][:port]"
00077  * If there is no port specified, the environment variable CCN_LOCAL_PORT is
00078  * checked. Bad port specifications will result in the default port (9695)
00079  * being used.  If neither "4" nor "6" is present, the code will prefer the IPv4
00080  * localhost address.
00081  * @returns 0 on success, -1 on error
00082  */
00083 int
00084 ccn_setup_sockaddr_in(const char *name, struct sockaddr *result, int length)
00085 {
00086     struct addrinfo hints = {0};
00087     struct addrinfo *ai = NULL;
00088     char *port;
00089     char *nameonly = strdup(name);
00090     int ans = -1;
00091     int res;
00092 
00093     port = strchr(nameonly, ':');
00094     if (port)
00095         *port++ = 0;
00096     if (port == NULL || port[0] == 0)
00097         port = getenv(CCN_LOCAL_PORT_ENVNAME);
00098     if (port == NULL || port[0] == 0)
00099         port = CCN_DEFAULT_UNICAST_PORT;
00100     memset(result, 0, length);
00101     hints.ai_family = AF_UNSPEC;
00102     if (strcasecmp(nameonly, "tcp6") == 0) hints.ai_family = AF_INET6;
00103     if (strcasecmp(nameonly, "tcp4") == 0) hints.ai_family = AF_INET;
00104     hints.ai_socktype = SOCK_STREAM;
00105     hints.ai_flags = AI_ADDRCONFIG;
00106     hints.ai_protocol = 0;
00107     res = getaddrinfo(NULL, port, &hints, &ai);
00108     if (res != 0 || ai->ai_addrlen > length)
00109         goto Bail;
00110     memcpy(result, ai->ai_addr, ai->ai_addrlen);
00111     ans = 0;
00112 Bail:
00113     free(nameonly);
00114     freeaddrinfo(ai);
00115     return (ans);
00116 }
Generated on Tue Aug 21 14:54:18 2012 for Content-Centric Networking in C by  doxygen 1.6.3