From bffb9edfd02096b50980b570d984c9007b7a84c8 Mon Sep 17 00:00:00 2001 From: Josh Durgin Date: Fri, 15 Apr 2011 15:00:07 -0700 Subject: [PATCH] addr_parsing: move implementation out of header Also include missing headers and make it linkable with C, since it's duplicated from mount.ceph. Signed-off-by: Josh Durgin --- src/Makefile.am | 3 +- src/include/addr_parsing.c | 138 +++++++++++++++++++++++++++++++++++++ src/include/addr_parsing.h | 121 ++------------------------------ 3 files changed, 147 insertions(+), 115 deletions(-) create mode 100644 src/include/addr_parsing.c diff --git a/src/Makefile.am b/src/Makefile.am index 0ef97935f4f83..127b941a34d3c 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -624,7 +624,8 @@ libcommon_files = \ common/hex.cc \ common/entity_name.cc \ common/ceph_crypto.cc \ - common/pidfile.cc + common/pidfile.cc \ + include/addr_parsing.c if WITH_PROFILER libcommon_files += perfglue/cpu_profiler.cc diff --git a/src/include/addr_parsing.c b/src/include/addr_parsing.c new file mode 100644 index 0000000000000..767fea442aa2a --- /dev/null +++ b/src/include/addr_parsing.c @@ -0,0 +1,138 @@ +// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*- +// vim: ts=8 sw=2 smarttab +/* + * Ceph - scalable distributed file system + * + * Copyright (C) 2011 New Dream Network + * + * This is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License version 2.1, as published by the Free Software + * Foundation. See file COPYING. + * + */ + +#include +#include +#include +#include +#include + +#define BUF_SIZE 128 + +int safe_cat(char **pstr, int *plen, int pos, const char *str2) +{ + int len2 = strlen(str2); + + while (*plen < pos + len2 + 1) { + *plen += BUF_SIZE; + *pstr = (char *)realloc(*pstr, (size_t)*plen); + + if (!*pstr) { + printf("Out of memory\n"); + exit(1); + } + } + + strcpy((*pstr)+pos, str2); + + return pos + len2; +} + +char *mount_resolve_dest(char *orig_str) +{ + char *new_str; + char *tok, *p, *port_str; + int len, pos; + + len = BUF_SIZE; + new_str = (char *)malloc(len); + + p = new_str; + pos = 0; + + tok = strtok(orig_str, ","); + + while (tok) { + struct addrinfo hint; + struct addrinfo *res, *ores; + char *firstcolon, *lastcolon, *bracecolon; + int r; + int brackets = 0; + + firstcolon = strchr(tok, ':'); + lastcolon = strrchr(tok, ':'); + bracecolon = strstr(tok, "]:"); + + port_str = 0; + if (firstcolon && firstcolon == lastcolon) { + /* host:port or a.b.c.d:port */ + *firstcolon = 0; + port_str = firstcolon + 1; + } else if (bracecolon) { + /* {ipv6addr}:port */ + port_str = bracecolon + 1; + *port_str = 0; + port_str++; + } + if (port_str && !*port_str) + port_str = NULL; + + if (*tok == '[' && + tok[strlen(tok)-1] == ']') { + tok[strlen(tok)-1] = 0; + tok++; + brackets = 1; + } + + //printf("name '%s' port '%s'\n", tok, port_str); + + memset(&hint, 0, sizeof(hint)); + hint.ai_socktype = SOCK_STREAM; + hint.ai_protocol = IPPROTO_TCP; + + r = getaddrinfo(tok, port_str, &hint, &res); + if (r < 0) { + printf("server name not found: %s (%s)\n", tok, strerror(errno)); + free(new_str); + return 0; + } + + /* build resolved addr list */ + ores = res; + while (res) { + char host[40], port[40]; + getnameinfo(res->ai_addr, res->ai_addrlen, + host, sizeof(host), + port, sizeof(port), + NI_NUMERICSERV | NI_NUMERICHOST); + /*printf(" host %s port %s flags %d family %d socktype %d proto %d sanonname %s\n", + host, port, + res->ai_flags, res->ai_family, res->ai_socktype, res->ai_protocol, + res->ai_canonname);*/ + if (res->ai_family == AF_INET6) + brackets = 1; /* always surround ipv6 addrs with brackets */ + if (brackets) + pos = safe_cat(&new_str, &len, pos, "["); + pos = safe_cat(&new_str, &len, pos, host); + if (brackets) + pos = safe_cat(&new_str, &len, pos, "]"); + if (port_str) { + pos = safe_cat(&new_str, &len, pos, ":"); + pos = safe_cat(&new_str, &len, pos, port); + } + res = res->ai_next; + if (res) + pos = safe_cat(&new_str, &len, pos, ","); + } + freeaddrinfo(ores); + + tok = strtok(NULL, ","); + if (tok) + pos = safe_cat(&new_str, &len, pos, ","); + + } + + //printf("new_str is '%s'\n", new_str); + return new_str; +} diff --git a/src/include/addr_parsing.h b/src/include/addr_parsing.h index 9d94580583943..026dcef2234ff 100644 --- a/src/include/addr_parsing.h +++ b/src/include/addr_parsing.h @@ -9,127 +9,20 @@ #ifndef ADDR_PARSING_H_ #define ADDR_PARSING_H_ -#define BUF_SIZE 128 -static int safe_cat(char **pstr, int *plen, int pos, const char *str2) -{ - int len2 = strlen(str2); +#ifdef __cplusplus +extern "C" { +#endif - while (*plen < pos + len2 + 1) { - *plen += BUF_SIZE; - *pstr = (char *)realloc(*pstr, (size_t)*plen); - - if (!*pstr) { - printf("Out of memory\n"); - exit(1); - } - } - - strcpy((*pstr)+pos, str2); - - return pos + len2; -} +int safe_cat(char **pstr, int *plen, int pos, const char *str2); /* * returns a string allocated by malloc; caller must free */ -char *mount_resolve_dest(char *orig_str) -{ - char *new_str; - char *tok, *p, *port_str; - int len, pos; - - len = BUF_SIZE; - new_str = (char *)malloc(len); - - p = new_str; - pos = 0; - - tok = strtok(orig_str, ","); - - while (tok) { - struct addrinfo hint; - struct addrinfo *res, *ores; - char *firstcolon, *lastcolon, *bracecolon; - int r; - int brackets = 0; - - firstcolon = strchr(tok, ':'); - lastcolon = strrchr(tok, ':'); - bracecolon = strstr(tok, "]:"); +char *mount_resolve_dest(char *orig_str); - port_str = 0; - if (firstcolon && firstcolon == lastcolon) { - /* host:port or a.b.c.d:port */ - *firstcolon = 0; - port_str = firstcolon + 1; - } else if (bracecolon) { - /* {ipv6addr}:port */ - port_str = bracecolon + 1; - *port_str = 0; - port_str++; - } - if (port_str && !*port_str) - port_str = NULL; - - if (*tok == '[' && - tok[strlen(tok)-1] == ']') { - tok[strlen(tok)-1] = 0; - tok++; - brackets = 1; - } - - //printf("name '%s' port '%s'\n", tok, port_str); - - memset(&hint, 0, sizeof(hint)); - hint.ai_socktype = SOCK_STREAM; - hint.ai_protocol = IPPROTO_TCP; - - r = getaddrinfo(tok, port_str, &hint, &res); - if (r < 0) { - printf("server name not found: %s (%s)\n", tok, strerror(errno)); - free(new_str); - return 0; - } - - /* build resolved addr list */ - ores = res; - while (res) { - char host[40], port[40]; - getnameinfo(res->ai_addr, res->ai_addrlen, - host, sizeof(host), - port, sizeof(port), - NI_NUMERICSERV | NI_NUMERICHOST); - /*printf(" host %s port %s flags %d family %d socktype %d proto %d sanonname %s\n", - host, port, - res->ai_flags, res->ai_family, res->ai_socktype, res->ai_protocol, - res->ai_canonname);*/ - if (res->ai_family == AF_INET6) - brackets = 1; /* always surround ipv6 addrs with brackets */ - if (brackets) - pos = safe_cat(&new_str, &len, pos, "["); - pos = safe_cat(&new_str, &len, pos, host); - if (brackets) - pos = safe_cat(&new_str, &len, pos, "]"); - if (port_str) { - pos = safe_cat(&new_str, &len, pos, ":"); - pos = safe_cat(&new_str, &len, pos, port); - } - res = res->ai_next; - if (res) - pos = safe_cat(&new_str, &len, pos, ","); - } - freeaddrinfo(ores); - - tok = strtok(NULL, ","); - if (tok) - pos = safe_cat(&new_str, &len, pos, ","); - - } - - //printf("new_str is '%s'\n", new_str); - return new_str; +#ifdef __cplusplus } - +#endif #endif /* ADDR_PARSING_H_ */ -- 2.39.5