ccn_setup_sockaddr_un.c
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
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
00035 #endif
00036
00037 #include <ccn/ccnd.h>
00038 #include <ccn/ccn_private.h>
00039 #include <ccn/charbuf.h>
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
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;
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
00075
00076
00077
00078
00079
00080
00081
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 }