[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[GNUnet-SVN] r3979 - in GNUnet/src: setup/gtk transports util/string
From: |
grothoff |
Subject: |
[GNUnet-SVN] r3979 - in GNUnet/src: setup/gtk transports util/string |
Date: |
Mon, 18 Dec 2006 17:03:22 -0800 (PST) |
Author: grothoff
Date: 2006-12-18 17:03:18 -0800 (Mon, 18 Dec 2006)
New Revision: 3979
Modified:
GNUnet/src/setup/gtk/wizard_gtk.h
GNUnet/src/transports/ip.c
GNUnet/src/transports/ip6.c
GNUnet/src/util/string/string.c
Log:
dos2unix
Modified: GNUnet/src/setup/gtk/wizard_gtk.h
===================================================================
--- GNUnet/src/setup/gtk/wizard_gtk.h 2006-12-18 19:24:44 UTC (rev 3978)
+++ GNUnet/src/setup/gtk/wizard_gtk.h 2006-12-19 01:03:18 UTC (rev 3979)
@@ -30,7 +30,7 @@
#include "gnunet_setup_lib.h"
#include "wizard_util.h"
-
+
int gtk_wizard_mainsetup_gtk(int argc,
char * const * argv,
struct PluginHandle * self,
Modified: GNUnet/src/transports/ip.c
===================================================================
--- GNUnet/src/transports/ip.c 2006-12-18 19:24:44 UTC (rev 3978)
+++ GNUnet/src/transports/ip.c 2006-12-19 01:03:18 UTC (rev 3979)
@@ -1,372 +1,372 @@
-/*
- This file is part of GNUnet.
- (C) 2001, 2002, 2004, 2005, 2006 Christian Grothoff (and other
contributing authors)
-
- GNUnet is free software; you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published
- by the Free Software Foundation; either version 2, or (at your
- option) any later version.
-
- GNUnet is distributed in the hope that it will be useful, but
- WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- General Public License for more details.
-
- You should have received a copy of the GNU General Public License
- along with GNUnet; see the file COPYING. If not, write to the
- Free Software Foundation, Inc., 59 Temple Place - Suite 330,
- Boston, MA 02111-1307, USA.
-*/
-
-/**
- * @file transports/ip.c
- * @brief code to determine the IP of the local machine
- *
- *
- * Determine the IP of the local machine. We have many
- * ways to get that IP:
- * a) from the interface (ifconfig)
- * b) via DNS from our HOSTNAME (environment)
- * c) from the configuration (HOSTNAME specification or static IP)
- *
- * Which way applies depends on the OS, the configuration
- * (dynDNS? static IP? NAT?) and at the end what the user
- * needs.
- *
- * @author Christian Grothoff
- * @author Tzvetan Horozov
- */
-
-#include <stdlib.h>
-#include "platform.h"
-#include "gnunet_util.h"
-#include "ip.h"
-
-/* maximum length of hostname */
-#define MAX_HOSTNAME 1024
-
-/**
- * Obtain the identity information for the current node
- * (connection information), conInfo.
- * @return SYSERR on failure, OK on success
- */
-static int getAddressFromHostname(struct GE_Context * ectx,
- IPaddr * identity) {
- char hostname[MAX_HOSTNAME];
- int ret;
-
- if (0 != gethostname(hostname, MAX_HOSTNAME)) {
- GE_LOG_STRERROR(ectx,
- GE_ERROR | GE_ADMIN | GE_USER | GE_BULK,
- "gethostname");
- return SYSERR;
- }
- ret = get_host_by_name(ectx,
- hostname,
- identity);
- return ret;
-}
-
-#if LINUX || SOMEBSD || MINGW
-#define MAX_INTERFACES 16
-static int getAddressFromIOCTL(struct GC_Configuration * cfg,
- struct GE_Context * ectx,
- IPaddr * identity) {
- char * interfaces;
-#ifndef MINGW
- struct ifreq ifr[MAX_INTERFACES];
- struct ifconf ifc;
- int sockfd,ifCount;
-#else
- DWORD dwIP;
-#endif
- int i;
-
- if (-1 == GC_get_configuration_value_string(cfg,
- "NETWORK",
- "INTERFACE",
- "eth0",
- &interfaces)) {
- GE_LOG(ectx,
- GE_ERROR | GE_BULK | GE_USER,
- _("No interface specified in section `%s' under `%s'!\n"),
- "NETWORK",
- "INTERFACE");
- return SYSERR; /* that won't work! */
- }
-#ifndef MINGW
- sockfd = SOCKET(PF_INET, SOCK_DGRAM, 0);
- if (sockfd == -1) {
- FREE(interfaces);
- GE_LOG_STRERROR(ectx,
- GE_ERROR | GE_ADMIN | GE_USER | GE_BULK,
- "socket");
- return SYSERR;
- }
- memset(&ifc,
- 0,
- sizeof(struct ifconf));
- ifc.ifc_len = sizeof(ifr);
- ifc.ifc_buf = (char*)𝔦
-
- if (ioctl(sockfd,
- SIOCGIFCONF,
- &ifc) == -1) {
- GE_LOG_STRERROR(ectx,
- GE_WARNING | GE_ADMIN | GE_USER | GE_BULK,
- "ioctl");
- if (0 != CLOSE(sockfd))
- GE_LOG_STRERROR(ectx,
- GE_WARNING | GE_ADMIN | GE_BULK,
- "close");
- FREE(interfaces);
- return SYSERR;
- }
- ifCount = ifc.ifc_len / sizeof(struct ifreq);
-
- /* first, try to find exatly matching interface */
- for (i=0;i<ifCount;i++){
- if (ioctl(sockfd, SIOCGIFADDR, &ifr[i]) != 0)
- continue;
- if (ioctl(sockfd, SIOCGIFFLAGS, &ifr[i]) != 0)
- continue;
- if (!(ifr[i].ifr_flags & IFF_UP))
- continue;
- if (strcmp((char*) interfaces,
- (char*) ifr[i].ifr_name) != 0)
- continue;
- memcpy(identity,
- &(((struct sockaddr_in *)&ifr[i].ifr_addr)->sin_addr),
- sizeof(struct in_addr));
- if (0 != CLOSE(sockfd))
- GE_LOG_STRERROR(ectx,
- GE_WARNING | GE_ADMIN | GE_BULK,
- "close");
- FREE(interfaces);
- return OK;
- }
- GE_LOG(ectx,
- GE_WARNING | GE_ADMIN | GE_USER | GE_BULK,
- _("Could not find interface `%s' using `%s', "
- "trying to find another interface.\n"),
- interfaces,
- "ioctl");
- /* if no such interface exists, take any interface but loopback */
- for (i=0;i<ifCount;i++){
- if (ioctl(sockfd, SIOCGIFADDR, &ifr[i]) != 0)
- continue;
- if (ioctl(sockfd, SIOCGIFFLAGS, &ifr[i]) != 0)
- continue;
- if (!(ifr[i].ifr_flags & IFF_UP))
- continue;
- if (strncmp("lo",
- (char*) ifr[i].ifr_name, 2) == 0)
- continue;
- memcpy(identity,
- &(((struct sockaddr_in *)&ifr[i].ifr_addr)->sin_addr),
- sizeof(struct in_addr));
- if (0 != CLOSE(sockfd))
- GE_LOG_STRERROR(ectx,
- GE_WARNING | GE_ADMIN | GE_BULK,
- "close");
- FREE(interfaces);
- return OK;
- }
-
- if (0 != CLOSE(sockfd))
- GE_LOG_STRERROR(ectx,
- GE_WARNING | GE_ADMIN | GE_BULK,
- "close");
- GE_LOG(ectx,
- GE_WARNING | GE_USER | GE_BULK,
- _("Could not obtain IP for interface `%s' using `%s'.\n"),
- interfaces,
- "ioctl");
- FREE(interfaces);
- return SYSERR;
-#else /* MinGW */
-
- /* Win 98 or Win NT SP 4 */
- if (GNGetIpAddrTable)
- {
- PMIB_IFTABLE pTable;
- PMIB_IPADDRTABLE pAddrTable;
- DWORD dwIfIdx;
- unsigned int iAddrCount = 0;
-
- dwIP = 0;
-
- EnumNICs(&pTable, &pAddrTable);
-
- for(dwIfIdx=0; dwIfIdx < pTable->dwNumEntries; dwIfIdx++) {
- unsigned long long l;
- BYTE bPhysAddr[MAXLEN_PHYSADDR];
-
- l = _atoi64(interfaces);
-
- memset(bPhysAddr, 0, MAXLEN_PHYSADDR);
- memcpy(bPhysAddr,
- pTable->table[dwIfIdx].bPhysAddr,
- pTable->table[dwIfIdx].dwPhysAddrLen);
-
- if (memcmp(bPhysAddr, &l, sizeof(l)) == 0) {
- for(i = 0; i < pAddrTable->dwNumEntries; i++) {
- if (pAddrTable->table[i].dwIndex
- == pTable->table[dwIfIdx].dwIndex) {
- iAddrCount++;
- dwIP = pAddrTable->table[i].dwAddr;
- }
- }
- }
- }
-
- if (! iAddrCount)
- {
- GE_LOG(ectx, GE_WARNING | GE_BULK | GE_USER,
- _("Could not find an IP address for "
- "interface `%s'.\n"),
- interfaces);
-
- GlobalFree(pTable);
- GlobalFree(pAddrTable);
- return SYSERR;
- }
- else if (iAddrCount > 1)
- GE_LOG(ectx, GE_WARNING | GE_BULK | GE_USER,
- _("There is more than one IP address specified"
- " for interface `%s'.\nGNUnet will "
- "use %u.%u.%u.%u.\n"),
- interfaces,
- PRIP(ntohl(dwIP)));
-
- identity->addr = dwIP;
-
- GlobalFree(pTable);
- GlobalFree(pAddrTable);
- }
- else /* Win 95 */
- {
- SOCKET s;
- HOSTENT *pHost;
- SOCKADDR_IN theHost;
-
- s = SOCKET(PF_INET, SOCK_STREAM, 0);
- pHost = GETHOSTBYNAME("www.example.com");
- if (! pHost) {
- GE_LOG(ectx, GE_ERROR | GE_BULK | GE_USER,
- _("Could not resolve `%s' to "
- "determine our IP address: %s\n"),
- "www.example.com",
- STRERROR(errno));
- return SYSERR;
- }
-
- theHost.sin_family = AF_INET;
- theHost.sin_port = htons(80);
- theHost.sin_addr.S_un.S_addr
- = *((unsigned long *) pHost->h_addr_list[0]);
- if (CONNECT(s,
- (SOCKADDR *) &theHost,
- sizeof(theHost)) == SOCKET_ERROR) {
- GE_LOG_STRERROR(ectx, GE_ERROR | GE_BULK | GE_USER,
- "connect");
- return SYSERR;
- }
-
- i = sizeof(theHost);
- if (GETSOCKNAME(s,
- (SOCKADDR *) &theHost,
- &i) == SOCKET_ERROR) {
- GE_LOG_STRERROR(ectx, GE_ERROR | GE_BULK | GE_USER,
- "getsockname");
- return SYSERR;
- }
- closesocket(s);
- identity->addr = theHost.sin_addr.S_un.S_addr;
- }
-
- GE_LOG(ectx, GE_DEBUG | GE_REQUEST | GE_USER,
- _("GNUnet now uses the IP address %u.%u.%u.%u.\n"),
- PRIP(ntohl(identity->addr)));
-
- return OK;
-#endif
-}
-
-#endif
-
-/**
- * Get the IP address for the local machine.
- * @return SYSERR on error, OK on success
- */
-static int getAddress(struct GC_Configuration * cfg,
- struct GE_Context * ectx,
- IPaddr * address){
- char * ipString;
- int retval;
-
- retval = SYSERR;
- if (GC_have_configuration_value(cfg,
- "NETWORK",
- "IP")) {
- ipString = NULL;
- GC_get_configuration_value_string(cfg,
- "NETWORK",
- "IP",
- "",
- &ipString);
- if (strlen(ipString) > 0) {
- retval = get_host_by_name(ectx,
- ipString,
- address);
- }
- FREE(ipString);
- }
-#if LINUX || SOMEBSD || MINGW
- if (retval == SYSERR)
- if (OK == getAddressFromIOCTL(cfg,
- ectx,
- address))
- retval = OK;
-#endif
- if (retval == SYSERR)
- retval = getAddressFromHostname(ectx,
- address);
- return retval;
-}
-
-/**
- * Get the IP address for the local machine.
- * @return SYSERR on error, OK on success
- */
-int getPublicIPAddress(struct GC_Configuration * cfg,
- struct GE_Context * ectx,
- IPaddr * address) {
- static IPaddr myAddress;
- static cron_t last;
- static cron_t lastError;
- cron_t now;
-
- now = get_time();
- if (last + cronMINUTES < now) {
- if (lastError + 30 * cronSECONDS > now)
- return SYSERR;
- if (SYSERR == getAddress(cfg,
- ectx,
- &myAddress)) {
- GE_LOG(ectx,
- GE_WARNING | GE_USER | GE_BULK,
- _("Failed to obtain my (external) %s address!\n"),
- "IP");
- lastError = now;
- return SYSERR;
- }
- last = now;
- }
- memcpy(address,
- &myAddress,
- sizeof(IPaddr));
- return OK;
-}
-
-/* end of ip.c */
+/*
+ This file is part of GNUnet.
+ (C) 2001, 2002, 2004, 2005, 2006 Christian Grothoff (and other
contributing authors)
+
+ GNUnet is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published
+ by the Free Software Foundation; either version 2, or (at your
+ option) any later version.
+
+ GNUnet is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with GNUnet; see the file COPYING. If not, write to the
+ Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ Boston, MA 02111-1307, USA.
+*/
+
+/**
+ * @file transports/ip.c
+ * @brief code to determine the IP of the local machine
+ *
+ *
+ * Determine the IP of the local machine. We have many
+ * ways to get that IP:
+ * a) from the interface (ifconfig)
+ * b) via DNS from our HOSTNAME (environment)
+ * c) from the configuration (HOSTNAME specification or static IP)
+ *
+ * Which way applies depends on the OS, the configuration
+ * (dynDNS? static IP? NAT?) and at the end what the user
+ * needs.
+ *
+ * @author Christian Grothoff
+ * @author Tzvetan Horozov
+ */
+
+#include <stdlib.h>
+#include "platform.h"
+#include "gnunet_util.h"
+#include "ip.h"
+
+/* maximum length of hostname */
+#define MAX_HOSTNAME 1024
+
+/**
+ * Obtain the identity information for the current node
+ * (connection information), conInfo.
+ * @return SYSERR on failure, OK on success
+ */
+static int getAddressFromHostname(struct GE_Context * ectx,
+ IPaddr * identity) {
+ char hostname[MAX_HOSTNAME];
+ int ret;
+
+ if (0 != gethostname(hostname, MAX_HOSTNAME)) {
+ GE_LOG_STRERROR(ectx,
+ GE_ERROR | GE_ADMIN | GE_USER | GE_BULK,
+ "gethostname");
+ return SYSERR;
+ }
+ ret = get_host_by_name(ectx,
+ hostname,
+ identity);
+ return ret;
+}
+
+#if LINUX || SOMEBSD || MINGW
+#define MAX_INTERFACES 16
+static int getAddressFromIOCTL(struct GC_Configuration * cfg,
+ struct GE_Context * ectx,
+ IPaddr * identity) {
+ char * interfaces;
+#ifndef MINGW
+ struct ifreq ifr[MAX_INTERFACES];
+ struct ifconf ifc;
+ int sockfd,ifCount;
+#else
+ DWORD dwIP;
+#endif
+ int i;
+
+ if (-1 == GC_get_configuration_value_string(cfg,
+ "NETWORK",
+ "INTERFACE",
+ "eth0",
+ &interfaces)) {
+ GE_LOG(ectx,
+ GE_ERROR | GE_BULK | GE_USER,
+ _("No interface specified in section `%s' under `%s'!\n"),
+ "NETWORK",
+ "INTERFACE");
+ return SYSERR; /* that won't work! */
+ }
+#ifndef MINGW
+ sockfd = SOCKET(PF_INET, SOCK_DGRAM, 0);
+ if (sockfd == -1) {
+ FREE(interfaces);
+ GE_LOG_STRERROR(ectx,
+ GE_ERROR | GE_ADMIN | GE_USER | GE_BULK,
+ "socket");
+ return SYSERR;
+ }
+ memset(&ifc,
+ 0,
+ sizeof(struct ifconf));
+ ifc.ifc_len = sizeof(ifr);
+ ifc.ifc_buf = (char*)𝔦
+
+ if (ioctl(sockfd,
+ SIOCGIFCONF,
+ &ifc) == -1) {
+ GE_LOG_STRERROR(ectx,
+ GE_WARNING | GE_ADMIN | GE_USER | GE_BULK,
+ "ioctl");
+ if (0 != CLOSE(sockfd))
+ GE_LOG_STRERROR(ectx,
+ GE_WARNING | GE_ADMIN | GE_BULK,
+ "close");
+ FREE(interfaces);
+ return SYSERR;
+ }
+ ifCount = ifc.ifc_len / sizeof(struct ifreq);
+
+ /* first, try to find exatly matching interface */
+ for (i=0;i<ifCount;i++){
+ if (ioctl(sockfd, SIOCGIFADDR, &ifr[i]) != 0)
+ continue;
+ if (ioctl(sockfd, SIOCGIFFLAGS, &ifr[i]) != 0)
+ continue;
+ if (!(ifr[i].ifr_flags & IFF_UP))
+ continue;
+ if (strcmp((char*) interfaces,
+ (char*) ifr[i].ifr_name) != 0)
+ continue;
+ memcpy(identity,
+ &(((struct sockaddr_in *)&ifr[i].ifr_addr)->sin_addr),
+ sizeof(struct in_addr));
+ if (0 != CLOSE(sockfd))
+ GE_LOG_STRERROR(ectx,
+ GE_WARNING | GE_ADMIN | GE_BULK,
+ "close");
+ FREE(interfaces);
+ return OK;
+ }
+ GE_LOG(ectx,
+ GE_WARNING | GE_ADMIN | GE_USER | GE_BULK,
+ _("Could not find interface `%s' using `%s', "
+ "trying to find another interface.\n"),
+ interfaces,
+ "ioctl");
+ /* if no such interface exists, take any interface but loopback */
+ for (i=0;i<ifCount;i++){
+ if (ioctl(sockfd, SIOCGIFADDR, &ifr[i]) != 0)
+ continue;
+ if (ioctl(sockfd, SIOCGIFFLAGS, &ifr[i]) != 0)
+ continue;
+ if (!(ifr[i].ifr_flags & IFF_UP))
+ continue;
+ if (strncmp("lo",
+ (char*) ifr[i].ifr_name, 2) == 0)
+ continue;
+ memcpy(identity,
+ &(((struct sockaddr_in *)&ifr[i].ifr_addr)->sin_addr),
+ sizeof(struct in_addr));
+ if (0 != CLOSE(sockfd))
+ GE_LOG_STRERROR(ectx,
+ GE_WARNING | GE_ADMIN | GE_BULK,
+ "close");
+ FREE(interfaces);
+ return OK;
+ }
+
+ if (0 != CLOSE(sockfd))
+ GE_LOG_STRERROR(ectx,
+ GE_WARNING | GE_ADMIN | GE_BULK,
+ "close");
+ GE_LOG(ectx,
+ GE_WARNING | GE_USER | GE_BULK,
+ _("Could not obtain IP for interface `%s' using `%s'.\n"),
+ interfaces,
+ "ioctl");
+ FREE(interfaces);
+ return SYSERR;
+#else /* MinGW */
+
+ /* Win 98 or Win NT SP 4 */
+ if (GNGetIpAddrTable)
+ {
+ PMIB_IFTABLE pTable;
+ PMIB_IPADDRTABLE pAddrTable;
+ DWORD dwIfIdx;
+ unsigned int iAddrCount = 0;
+
+ dwIP = 0;
+
+ EnumNICs(&pTable, &pAddrTable);
+
+ for(dwIfIdx=0; dwIfIdx < pTable->dwNumEntries; dwIfIdx++) {
+ unsigned long long l;
+ BYTE bPhysAddr[MAXLEN_PHYSADDR];
+
+ l = _atoi64(interfaces);
+
+ memset(bPhysAddr, 0, MAXLEN_PHYSADDR);
+ memcpy(bPhysAddr,
+ pTable->table[dwIfIdx].bPhysAddr,
+ pTable->table[dwIfIdx].dwPhysAddrLen);
+
+ if (memcmp(bPhysAddr, &l, sizeof(l)) == 0) {
+ for(i = 0; i < pAddrTable->dwNumEntries; i++) {
+ if (pAddrTable->table[i].dwIndex
+ == pTable->table[dwIfIdx].dwIndex) {
+ iAddrCount++;
+ dwIP = pAddrTable->table[i].dwAddr;
+ }
+ }
+ }
+ }
+
+ if (! iAddrCount)
+ {
+ GE_LOG(ectx, GE_WARNING | GE_BULK | GE_USER,
+ _("Could not find an IP address for "
+ "interface `%s'.\n"),
+ interfaces);
+
+ GlobalFree(pTable);
+ GlobalFree(pAddrTable);
+ return SYSERR;
+ }
+ else if (iAddrCount > 1)
+ GE_LOG(ectx, GE_WARNING | GE_BULK | GE_USER,
+ _("There is more than one IP address specified"
+ " for interface `%s'.\nGNUnet will "
+ "use %u.%u.%u.%u.\n"),
+ interfaces,
+ PRIP(ntohl(dwIP)));
+
+ identity->addr = dwIP;
+
+ GlobalFree(pTable);
+ GlobalFree(pAddrTable);
+ }
+ else /* Win 95 */
+ {
+ SOCKET s;
+ HOSTENT *pHost;
+ SOCKADDR_IN theHost;
+
+ s = SOCKET(PF_INET, SOCK_STREAM, 0);
+ pHost = GETHOSTBYNAME("www.example.com");
+ if (! pHost) {
+ GE_LOG(ectx, GE_ERROR | GE_BULK | GE_USER,
+ _("Could not resolve `%s' to "
+ "determine our IP address: %s\n"),
+ "www.example.com",
+ STRERROR(errno));
+ return SYSERR;
+ }
+
+ theHost.sin_family = AF_INET;
+ theHost.sin_port = htons(80);
+ theHost.sin_addr.S_un.S_addr
+ = *((unsigned long *) pHost->h_addr_list[0]);
+ if (CONNECT(s,
+ (SOCKADDR *) &theHost,
+ sizeof(theHost)) == SOCKET_ERROR) {
+ GE_LOG_STRERROR(ectx, GE_ERROR | GE_BULK | GE_USER,
+ "connect");
+ return SYSERR;
+ }
+
+ i = sizeof(theHost);
+ if (GETSOCKNAME(s,
+ (SOCKADDR *) &theHost,
+ &i) == SOCKET_ERROR) {
+ GE_LOG_STRERROR(ectx, GE_ERROR | GE_BULK | GE_USER,
+ "getsockname");
+ return SYSERR;
+ }
+ closesocket(s);
+ identity->addr = theHost.sin_addr.S_un.S_addr;
+ }
+
+ GE_LOG(ectx, GE_DEBUG | GE_REQUEST | GE_USER,
+ _("GNUnet now uses the IP address %u.%u.%u.%u.\n"),
+ PRIP(ntohl(identity->addr)));
+
+ return OK;
+#endif
+}
+
+#endif
+
+/**
+ * Get the IP address for the local machine.
+ * @return SYSERR on error, OK on success
+ */
+static int getAddress(struct GC_Configuration * cfg,
+ struct GE_Context * ectx,
+ IPaddr * address){
+ char * ipString;
+ int retval;
+
+ retval = SYSERR;
+ if (GC_have_configuration_value(cfg,
+ "NETWORK",
+ "IP")) {
+ ipString = NULL;
+ GC_get_configuration_value_string(cfg,
+ "NETWORK",
+ "IP",
+ "",
+ &ipString);
+ if (strlen(ipString) > 0) {
+ retval = get_host_by_name(ectx,
+ ipString,
+ address);
+ }
+ FREE(ipString);
+ }
+#if LINUX || SOMEBSD || MINGW
+ if (retval == SYSERR)
+ if (OK == getAddressFromIOCTL(cfg,
+ ectx,
+ address))
+ retval = OK;
+#endif
+ if (retval == SYSERR)
+ retval = getAddressFromHostname(ectx,
+ address);
+ return retval;
+}
+
+/**
+ * Get the IP address for the local machine.
+ * @return SYSERR on error, OK on success
+ */
+int getPublicIPAddress(struct GC_Configuration * cfg,
+ struct GE_Context * ectx,
+ IPaddr * address) {
+ static IPaddr myAddress;
+ static cron_t last;
+ static cron_t lastError;
+ cron_t now;
+
+ now = get_time();
+ if (last + cronMINUTES < now) {
+ if (lastError + 30 * cronSECONDS > now)
+ return SYSERR;
+ if (SYSERR == getAddress(cfg,
+ ectx,
+ &myAddress)) {
+ GE_LOG(ectx,
+ GE_WARNING | GE_USER | GE_BULK,
+ _("Failed to obtain my (external) %s address!\n"),
+ "IP");
+ lastError = now;
+ return SYSERR;
+ }
+ last = now;
+ }
+ memcpy(address,
+ &myAddress,
+ sizeof(IPaddr));
+ return OK;
+}
+
+/* end of ip.c */
Modified: GNUnet/src/transports/ip6.c
===================================================================
--- GNUnet/src/transports/ip6.c 2006-12-18 19:24:44 UTC (rev 3978)
+++ GNUnet/src/transports/ip6.c 2006-12-19 01:03:18 UTC (rev 3979)
@@ -1,168 +1,168 @@
-/*
- This file is part of GNUnet.
- (C) 2001, 2002, 2004, 2005, 2006 Christian Grothoff (and other
contributing authors)
-
- GNUnet is free software; you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published
- by the Free Software Foundation; either version 2, or (at your
- option) any later version.
-
- GNUnet is distributed in the hope that it will be useful, but
- WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- General Public License for more details.
-
- You should have received a copy of the GNU General Public License
- along with GNUnet; see the file COPYING. If not, write to the
- Free Software Foundation, Inc., 59 Temple Place - Suite 330,
- Boston, MA 02111-1307, USA.
-*/
-
-/**
- * @file transports/ip6.c
- * @brief code to determine the IP(v6) of the local machine
- *
- * Todo:
- * * scanning of network devices for IPv6 (first: find good
- * API to do it, doesn't seem to exist!)
- *
- * @author Christian Grothoff
- * @author Tzvetan Horozov
- */
-
-#include <stdlib.h>
-#include "platform.h"
-#include "gnunet_util.h"
-#include "ip6.h"
-
-/* maximum length of hostname */
-#define MAX_HOSTNAME 1024
-
-/**
- * Obtain the identity information for the current node
- * (connection information), conInfo.
- * @return SYSERR on failure, OK on success
- */
-static int getAddress6FromHostname(struct GE_Context * ectx,
- IP6addr * identity) {
- char hostname[MAX_HOSTNAME];
- struct hostent * ip;
-
- if (0 != gethostname(hostname, MAX_HOSTNAME)) {
- GE_LOG_STRERROR(ectx,
- GE_ERROR | GE_ADMIN | GE_USER | GE_BULK,
- "gethostname");
- return SYSERR;
- }
- /* GE_LOG(ectx, GE_DEBUG | GE_REQUEST | GE_USER,
- " looking up $HOSTNAME (%s) to obtain local IP\n",
- hostname); */
-
- ip = gethostbyname2(hostname, AF_INET6);
- if (ip == NULL) {
- GE_LOG(ectx,
- GE_ERROR | GE_ADMIN | GE_USER | GE_BULK,
- _("Could not find IP of host `%s': %s\n"),
- hostname,
- hstrerror(h_errno));
- return SYSERR;
- }
- if (ip->h_addrtype != AF_INET6) {
- GE_BREAK(ectx,
- 0);
- return SYSERR;
- }
- GE_ASSERT(ectx,
- sizeof(struct in6_addr) == sizeof(identity->addr));
- memcpy(&identity->addr[0],
- ip->h_addr_list[0],
- sizeof(struct in6_addr));
- return OK;
-}
-
-/**
- * Get the IP address for the local machine.
- * @return SYSERR on error, OK on success
- */
-static int getAddress6(struct GC_Configuration * cfg,
- struct GE_Context * ectx,
- IP6addr * address){
- char * ipString;
- int retval;
- struct hostent * ip; /* for the lookup of the IP in gnunet.conf */
-
- retval = SYSERR;
- if (GC_have_configuration_value(cfg,
- "NETWORK",
- "IP6")) {
- ipString = NULL;
- GC_get_configuration_value_string(cfg,
- "NETWORK",
- "IP6",
- "",
- &ipString);
- if (strlen(ipString) > 0) {
- ip = gethostbyname2(ipString,
- AF_INET6);
- if (ip == NULL) {
- GE_LOG(ectx,
- GE_ERROR | GE_USER | GE_BULK,
- _("Could not resolve `%s': %s\n"),
- ipString,
- hstrerror(h_errno));
- } else if (ip->h_addrtype != AF_INET6) {
- GE_ASSERT(ectx,
- 0);
- retval = SYSERR;
- } else {
- GE_ASSERT(ectx,
- sizeof(struct in6_addr) == sizeof(address->addr));
- memcpy(&address->addr[0],
- ip->h_addr_list[0],
- sizeof(struct in6_addr));
- retval = OK;
- }
- }
- FREE(ipString);
- }
- if (retval == SYSERR)
- retval = getAddress6FromHostname(ectx,
- address);
- return retval;
-}
-
-/**
- * Get the IPv6 address for the local machine.
- * @return SYSERR on error, OK on success
- */
-int getPublicIP6Address(struct GC_Configuration * cfg,
- struct GE_Context * ectx,
- IP6addr * address) {
- static IP6addr myAddress;
- static cron_t last;
- static cron_t lastError;
- cron_t now;
-
- now = get_time();
- if (last + cronMINUTES < now) {
- if (lastError + 30 * cronSECONDS > now)
- return SYSERR;
- if (SYSERR == getAddress6(cfg,
- ectx,
- &myAddress)) {
- lastError = now;
- GE_LOG(ectx,
- GE_WARNING | GE_USER | GE_BULK,
- _("Failed to obtain my (external) %s address!\n"),
- "IPv6");
- return SYSERR;
- }
- last = now;
- }
- memcpy(address,
- &myAddress,
- sizeof(IP6addr));
- return OK;
-}
-
-/* end of ip6.c */
+/*
+ This file is part of GNUnet.
+ (C) 2001, 2002, 2004, 2005, 2006 Christian Grothoff (and other
contributing authors)
+
+ GNUnet is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published
+ by the Free Software Foundation; either version 2, or (at your
+ option) any later version.
+
+ GNUnet is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with GNUnet; see the file COPYING. If not, write to the
+ Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ Boston, MA 02111-1307, USA.
+*/
+
+/**
+ * @file transports/ip6.c
+ * @brief code to determine the IP(v6) of the local machine
+ *
+ * Todo:
+ * * scanning of network devices for IPv6 (first: find good
+ * API to do it, doesn't seem to exist!)
+ *
+ * @author Christian Grothoff
+ * @author Tzvetan Horozov
+ */
+
+#include <stdlib.h>
+#include "platform.h"
+#include "gnunet_util.h"
+#include "ip6.h"
+
+/* maximum length of hostname */
+#define MAX_HOSTNAME 1024
+
+/**
+ * Obtain the identity information for the current node
+ * (connection information), conInfo.
+ * @return SYSERR on failure, OK on success
+ */
+static int getAddress6FromHostname(struct GE_Context * ectx,
+ IP6addr * identity) {
+ char hostname[MAX_HOSTNAME];
+ struct hostent * ip;
+
+ if (0 != gethostname(hostname, MAX_HOSTNAME)) {
+ GE_LOG_STRERROR(ectx,
+ GE_ERROR | GE_ADMIN | GE_USER | GE_BULK,
+ "gethostname");
+ return SYSERR;
+ }
+ /* GE_LOG(ectx, GE_DEBUG | GE_REQUEST | GE_USER,
+ " looking up $HOSTNAME (%s) to obtain local IP\n",
+ hostname); */
+
+ ip = gethostbyname2(hostname, AF_INET6);
+ if (ip == NULL) {
+ GE_LOG(ectx,
+ GE_ERROR | GE_ADMIN | GE_USER | GE_BULK,
+ _("Could not find IP of host `%s': %s\n"),
+ hostname,
+ hstrerror(h_errno));
+ return SYSERR;
+ }
+ if (ip->h_addrtype != AF_INET6) {
+ GE_BREAK(ectx,
+ 0);
+ return SYSERR;
+ }
+ GE_ASSERT(ectx,
+ sizeof(struct in6_addr) == sizeof(identity->addr));
+ memcpy(&identity->addr[0],
+ ip->h_addr_list[0],
+ sizeof(struct in6_addr));
+ return OK;
+}
+
+/**
+ * Get the IP address for the local machine.
+ * @return SYSERR on error, OK on success
+ */
+static int getAddress6(struct GC_Configuration * cfg,
+ struct GE_Context * ectx,
+ IP6addr * address){
+ char * ipString;
+ int retval;
+ struct hostent * ip; /* for the lookup of the IP in gnunet.conf */
+
+ retval = SYSERR;
+ if (GC_have_configuration_value(cfg,
+ "NETWORK",
+ "IP6")) {
+ ipString = NULL;
+ GC_get_configuration_value_string(cfg,
+ "NETWORK",
+ "IP6",
+ "",
+ &ipString);
+ if (strlen(ipString) > 0) {
+ ip = gethostbyname2(ipString,
+ AF_INET6);
+ if (ip == NULL) {
+ GE_LOG(ectx,
+ GE_ERROR | GE_USER | GE_BULK,
+ _("Could not resolve `%s': %s\n"),
+ ipString,
+ hstrerror(h_errno));
+ } else if (ip->h_addrtype != AF_INET6) {
+ GE_ASSERT(ectx,
+ 0);
+ retval = SYSERR;
+ } else {
+ GE_ASSERT(ectx,
+ sizeof(struct in6_addr) == sizeof(address->addr));
+ memcpy(&address->addr[0],
+ ip->h_addr_list[0],
+ sizeof(struct in6_addr));
+ retval = OK;
+ }
+ }
+ FREE(ipString);
+ }
+ if (retval == SYSERR)
+ retval = getAddress6FromHostname(ectx,
+ address);
+ return retval;
+}
+
+/**
+ * Get the IPv6 address for the local machine.
+ * @return SYSERR on error, OK on success
+ */
+int getPublicIP6Address(struct GC_Configuration * cfg,
+ struct GE_Context * ectx,
+ IP6addr * address) {
+ static IP6addr myAddress;
+ static cron_t last;
+ static cron_t lastError;
+ cron_t now;
+
+ now = get_time();
+ if (last + cronMINUTES < now) {
+ if (lastError + 30 * cronSECONDS > now)
+ return SYSERR;
+ if (SYSERR == getAddress6(cfg,
+ ectx,
+ &myAddress)) {
+ lastError = now;
+ GE_LOG(ectx,
+ GE_WARNING | GE_USER | GE_BULK,
+ _("Failed to obtain my (external) %s address!\n"),
+ "IPv6");
+ return SYSERR;
+ }
+ last = now;
+ }
+ memcpy(address,
+ &myAddress,
+ sizeof(IP6addr));
+ return OK;
+}
+
+/* end of ip6.c */
Modified: GNUnet/src/util/string/string.c
===================================================================
--- GNUnet/src/util/string/string.c 2006-12-18 19:24:44 UTC (rev 3978)
+++ GNUnet/src/util/string/string.c 2006-12-19 01:03:18 UTC (rev 3979)
@@ -1,360 +1,360 @@
/*
This file is part of GNUnet.
- (C) 2005, 2006 Christian Grothoff (and other contributing authors)
-
- GNUnet is free software; you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published
- by the Free Software Foundation; either version 2, or (at your
- option) any later version.
-
- GNUnet is distributed in the hope that it will be useful, but
- WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- General Public License for more details.
-
- You should have received a copy of the GNU General Public License
- along with GNUnet; see the file COPYING. If not, write to the
- Free Software Foundation, Inc., 59 Temple Place - Suite 330,
- Boston, MA 02111-1307, USA.
-*/
-
-/**
- * @file util/string/string.c
- * @brief string functions
- * @author Nils Durner
- * @author Christian Grothoff
- */
-
-#include "gnunet_util_string.h"
-#include "platform.h"
-#include <iconv.h>
-
-
-int SNPRINTF(char * buf,
- size_t size,
- const char * format,
- ...) {
- int ret;
- va_list args;
-
- va_start(args, format);
- ret = VSNPRINTF(buf,
- size,
- format,
- args);
- va_end(args);
- GE_ASSERT(NULL, ret <= size);
- return ret;
-}
-
-#if !HAVE_STRLCPY
-/**
- * @brief Copy a %NUL terminated string into a sized buffer
- * @author Linus Torvalds
- * @param dest Where to copy the string to
- * @param src Where to copy the string from
- * @param size size of destination buffer
- * @remarks Compatible with *BSD: the result is always a valid
- * NUL-terminated string that fits in the buffer (unless,
- * of course, the buffer size is zero). It does not pad
- * out the result like strncpy() does.
- */
-size_t strlcpy(char * dest,
- const char * src,
- size_t size) {
- size_t ret;
-
- GE_ASSERT(NULL, dest != NULL);
- GE_ASSERT(NULL, size > 0);
- GE_ASSERT(NULL, src != NULL);
- ret = strlen(src);
-
- if (size) {
- size_t len = (ret >= size) ? size-1 : ret;
- memcpy(dest, src, len);
- dest[len] = '\0';
- }
- return ret;
-}
-#endif
-
-#if !HAVE_STRLCAT
-/**
- * @brief Append a length-limited, %NUL-terminated string to another
- * @author Linus Torvalds
- * @param dest The string to be appended to
- * @param src The string to append to it
- * @param count The size of the destination buffer.
- */
-size_t strlcat(char * dest,
- const char * src,
- size_t count) {
- size_t dsize;
- size_t len;
- size_t res;
-
- GE_ASSERT(NULL, dest != NULL);
- GE_ASSERT(NULL, src != NULL);
- GE_ASSERT(NULL, count > 0);
- dsize = strlen(dest);
- len = strlen(src);
- res = dsize + len;
- GE_ASSERT(NULL, dsize < count);
-
- dest += dsize;
- count -= dsize;
- if (len >= count)
- len = count-1;
- memcpy(dest, src, len);
- dest[len] = 0;
- return res;
-}
-#endif
-
-/**
- * Give relative time in human-readable fancy format.
- * @param delta time in milli seconds
- */
-char * string_get_fancy_time_interval(unsigned long long delta) {
- const char * unit = _(/* time unit */ "ms");
- char * ret;
-
- if (delta > 5 * 1000) {
- delta = delta / 1000;
- unit = _(/* time unit */ "s");
- if (delta > 5 * 60) {
- delta = delta / 60;
- unit = _(/* time unit */ "m");
- if (delta > 5 * 60) {
- delta = delta / 60;
- unit = _(/* time unit */ "h");
- if (delta > 5 * 24) {
- delta = delta / 24;
- unit = _(/* time unit */ " days");
- }
- }
- }
- }
- ret = MALLOC(32);
- SNPRINTF(ret,
- 32,
- "%llu%s",
- delta,
- unit);
- return ret;
-}
-
-/**
- * Convert a given filesize into a fancy human-readable format.
- */
-char * string_get_fancy_byte_size(unsigned long long size) {
-const char * unit = _(/* size unit */ "b");
- char * ret;
-
- if (size > 5 * 1024) {
- size = size / 1024;
- unit = _(/* size unit */ "KiB");
- if (size > 5 * 1024) {
- size = size / 1024;
- unit = _(/* size unit */ "MiB");
- if (size > 5 * 1024) {
- size = size / 1024;
- unit = _(/* size unit */ "GiB");
- if (size > 5 * 1024) {
- size = size / 1024;
- unit = _(/* size unit */ "TiB");
- }
- }
- }
- }
- ret = MALLOC(32);
- SNPRINTF(ret,
- 32,
- "%llu%s",
- size,
- unit);
- return ret;
-}
-
-
-
-
-/**
- * Convert the len characters long character sequence
- * given in input that is in the given charset
- * to UTF-8.
- * @return the converted string (0-terminated),
- * if conversion fails, a copy of the orignal
- * string is returned.
- */
-char * string_convertToUtf8(struct GE_Context * ectx,
- const char * input,
- size_t len,
- const char * charset) {
- char * ret;
-#if ENABLE_NLS
- size_t tmpSize;
- size_t finSize;
- char * tmp;
- char * itmp;
- iconv_t cd;
-
- cd = iconv_open("UTF-8", charset);
- if (cd == (iconv_t) -1) {
- GE_LOG_STRERROR(ectx,
- GE_USER | GE_ADMIN | GE_WARNING | GE_BULK,
- "iconv_open");
- ret = MALLOC(len+1);
- memcpy(ret, input, len);
- ret[len] = '\0';
- return ret;
- }
- tmpSize = 3 * len + 4;
- tmp = MALLOC(tmpSize);
- itmp = tmp;
- finSize = tmpSize;
- if (iconv(cd,
- (char**) &input,
- &len,
- &itmp,
- &finSize) == (size_t)-1) {
- GE_LOG_STRERROR(ectx,
- GE_USER | GE_WARNING | GE_BULK,
- "iconv");
- iconv_close(cd);
- FREE(tmp);
- ret = MALLOC(len+1);
- memcpy(ret, input, len);
- ret[len] = '\0';
- return ret;
- }
- ret = MALLOC(tmpSize - finSize + 1);
- memcpy(ret,
- tmp,
- tmpSize - finSize);
- ret[tmpSize - finSize] = '\0';
- FREE(tmp);
- if (0 != iconv_close(cd))
- GE_LOG_STRERROR(ectx,
- GE_ADMIN | GE_WARNING | GE_REQUEST,
- "iconv_close");
- return ret;
-#else
- ret = MALLOC(len+1);
- memcpy(ret, input, len);
- ret[len] = '\0';
- return ret;
-#endif
-}
-
-
-
-
-/**
- * Complete filename (a la shell) from abbrevition.
- * @param fil the name of the file, may contain ~/ or
- * be relative to the current directory
- * @returns the full file name,
- * NULL is returned on error
- */
-char * string_expandFileName(struct GE_Context * ectx,
- const char * fil) {
- char * buffer;
-#ifndef MINGW
- size_t len;
- size_t n;
- char * fm;
- const char * fil_ptr;
-#else
- char * fn;
- long lRet;
-#endif
-
- if (fil == NULL)
- return NULL;
-
-#ifndef MINGW
- if (fil[0] == DIR_SEPARATOR)
- /* absolute path, just copy */
- return STRDUP(fil);
- if (fil[0] == '~') {
- fm = getenv("HOME");
- if (fm == NULL) {
- GE_LOG(ectx,
- GE_USER | GE_ADMIN | GE_WARNING | GE_IMMEDIATE,
- _("Failed to expand `$HOME': environment variable `HOME' not
set"));
- return NULL;
- }
- fm = STRDUP(fm);
- /* do not copy '~' */
- fil_ptr = fil + 1;
-
- /* skip over dir seperator to be consistent */
- if (fil_ptr[0] == DIR_SEPARATOR)
- fil_ptr++;
- } else {
- /* relative path */
- fil_ptr = fil;
- len = 512;
+ (C) 2005, 2006 Christian Grothoff (and other contributing authors)
+
+ GNUnet is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published
+ by the Free Software Foundation; either version 2, or (at your
+ option) any later version.
+
+ GNUnet is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with GNUnet; see the file COPYING. If not, write to the
+ Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ Boston, MA 02111-1307, USA.
+*/
+
+/**
+ * @file util/string/string.c
+ * @brief string functions
+ * @author Nils Durner
+ * @author Christian Grothoff
+ */
+
+#include "gnunet_util_string.h"
+#include "platform.h"
+#include <iconv.h>
+
+
+int SNPRINTF(char * buf,
+ size_t size,
+ const char * format,
+ ...) {
+ int ret;
+ va_list args;
+
+ va_start(args, format);
+ ret = VSNPRINTF(buf,
+ size,
+ format,
+ args);
+ va_end(args);
+ GE_ASSERT(NULL, ret <= size);
+ return ret;
+}
+
+#if !HAVE_STRLCPY
+/**
+ * @brief Copy a %NUL terminated string into a sized buffer
+ * @author Linus Torvalds
+ * @param dest Where to copy the string to
+ * @param src Where to copy the string from
+ * @param size size of destination buffer
+ * @remarks Compatible with *BSD: the result is always a valid
+ * NUL-terminated string that fits in the buffer (unless,
+ * of course, the buffer size is zero). It does not pad
+ * out the result like strncpy() does.
+ */
+size_t strlcpy(char * dest,
+ const char * src,
+ size_t size) {
+ size_t ret;
+
+ GE_ASSERT(NULL, dest != NULL);
+ GE_ASSERT(NULL, size > 0);
+ GE_ASSERT(NULL, src != NULL);
+ ret = strlen(src);
+
+ if (size) {
+ size_t len = (ret >= size) ? size-1 : ret;
+ memcpy(dest, src, len);
+ dest[len] = '\0';
+ }
+ return ret;
+}
+#endif
+
+#if !HAVE_STRLCAT
+/**
+ * @brief Append a length-limited, %NUL-terminated string to another
+ * @author Linus Torvalds
+ * @param dest The string to be appended to
+ * @param src The string to append to it
+ * @param count The size of the destination buffer.
+ */
+size_t strlcat(char * dest,
+ const char * src,
+ size_t count) {
+ size_t dsize;
+ size_t len;
+ size_t res;
+
+ GE_ASSERT(NULL, dest != NULL);
+ GE_ASSERT(NULL, src != NULL);
+ GE_ASSERT(NULL, count > 0);
+ dsize = strlen(dest);
+ len = strlen(src);
+ res = dsize + len;
+ GE_ASSERT(NULL, dsize < count);
+
+ dest += dsize;
+ count -= dsize;
+ if (len >= count)
+ len = count-1;
+ memcpy(dest, src, len);
+ dest[len] = 0;
+ return res;
+}
+#endif
+
+/**
+ * Give relative time in human-readable fancy format.
+ * @param delta time in milli seconds
+ */
+char * string_get_fancy_time_interval(unsigned long long delta) {
+ const char * unit = _(/* time unit */ "ms");
+ char * ret;
+
+ if (delta > 5 * 1000) {
+ delta = delta / 1000;
+ unit = _(/* time unit */ "s");
+ if (delta > 5 * 60) {
+ delta = delta / 60;
+ unit = _(/* time unit */ "m");
+ if (delta > 5 * 60) {
+ delta = delta / 60;
+ unit = _(/* time unit */ "h");
+ if (delta > 5 * 24) {
+ delta = delta / 24;
+ unit = _(/* time unit */ " days");
+ }
+ }
+ }
+ }
+ ret = MALLOC(32);
+ SNPRINTF(ret,
+ 32,
+ "%llu%s",
+ delta,
+ unit);
+ return ret;
+}
+
+/**
+ * Convert a given filesize into a fancy human-readable format.
+ */
+char * string_get_fancy_byte_size(unsigned long long size) {
+const char * unit = _(/* size unit */ "b");
+ char * ret;
+
+ if (size > 5 * 1024) {
+ size = size / 1024;
+ unit = _(/* size unit */ "KiB");
+ if (size > 5 * 1024) {
+ size = size / 1024;
+ unit = _(/* size unit */ "MiB");
+ if (size > 5 * 1024) {
+ size = size / 1024;
+ unit = _(/* size unit */ "GiB");
+ if (size > 5 * 1024) {
+ size = size / 1024;
+ unit = _(/* size unit */ "TiB");
+ }
+ }
+ }
+ }
+ ret = MALLOC(32);
+ SNPRINTF(ret,
+ 32,
+ "%llu%s",
+ size,
+ unit);
+ return ret;
+}
+
+
+
+
+/**
+ * Convert the len characters long character sequence
+ * given in input that is in the given charset
+ * to UTF-8.
+ * @return the converted string (0-terminated),
+ * if conversion fails, a copy of the orignal
+ * string is returned.
+ */
+char * string_convertToUtf8(struct GE_Context * ectx,
+ const char * input,
+ size_t len,
+ const char * charset) {
+ char * ret;
+#if ENABLE_NLS
+ size_t tmpSize;
+ size_t finSize;
+ char * tmp;
+ char * itmp;
+ iconv_t cd;
+
+ cd = iconv_open("UTF-8", charset);
+ if (cd == (iconv_t) -1) {
+ GE_LOG_STRERROR(ectx,
+ GE_USER | GE_ADMIN | GE_WARNING | GE_BULK,
+ "iconv_open");
+ ret = MALLOC(len+1);
+ memcpy(ret, input, len);
+ ret[len] = '\0';
+ return ret;
+ }
+ tmpSize = 3 * len + 4;
+ tmp = MALLOC(tmpSize);
+ itmp = tmp;
+ finSize = tmpSize;
+ if (iconv(cd,
+ (char**) &input,
+ &len,
+ &itmp,
+ &finSize) == (size_t)-1) {
+ GE_LOG_STRERROR(ectx,
+ GE_USER | GE_WARNING | GE_BULK,
+ "iconv");
+ iconv_close(cd);
+ FREE(tmp);
+ ret = MALLOC(len+1);
+ memcpy(ret, input, len);
+ ret[len] = '\0';
+ return ret;
+ }
+ ret = MALLOC(tmpSize - finSize + 1);
+ memcpy(ret,
+ tmp,
+ tmpSize - finSize);
+ ret[tmpSize - finSize] = '\0';
+ FREE(tmp);
+ if (0 != iconv_close(cd))
+ GE_LOG_STRERROR(ectx,
+ GE_ADMIN | GE_WARNING | GE_REQUEST,
+ "iconv_close");
+ return ret;
+#else
+ ret = MALLOC(len+1);
+ memcpy(ret, input, len);
+ ret[len] = '\0';
+ return ret;
+#endif
+}
+
+
+
+
+/**
+ * Complete filename (a la shell) from abbrevition.
+ * @param fil the name of the file, may contain ~/ or
+ * be relative to the current directory
+ * @returns the full file name,
+ * NULL is returned on error
+ */
+char * string_expandFileName(struct GE_Context * ectx,
+ const char * fil) {
+ char * buffer;
+#ifndef MINGW
+ size_t len;
+ size_t n;
+ char * fm;
+ const char * fil_ptr;
+#else
+ char * fn;
+ long lRet;
+#endif
+
+ if (fil == NULL)
+ return NULL;
+
+#ifndef MINGW
+ if (fil[0] == DIR_SEPARATOR)
+ /* absolute path, just copy */
+ return STRDUP(fil);
+ if (fil[0] == '~') {
+ fm = getenv("HOME");
+ if (fm == NULL) {
+ GE_LOG(ectx,
+ GE_USER | GE_ADMIN | GE_WARNING | GE_IMMEDIATE,
+ _("Failed to expand `$HOME': environment variable `HOME' not
set"));
+ return NULL;
+ }
+ fm = STRDUP(fm);
+ /* do not copy '~' */
+ fil_ptr = fil + 1;
+
+ /* skip over dir seperator to be consistent */
+ if (fil_ptr[0] == DIR_SEPARATOR)
+ fil_ptr++;
+ } else {
+ /* relative path */
+ fil_ptr = fil;
+ len = 512;
errno = ERANGE;
fm = NULL;
- while (errno == ERANGE) {
- buffer = MALLOC(len);
- if (getcwd(buffer, len) != NULL) {
- fm = buffer;
- break;
- } else {
- GE_LOG_STRERROR(ectx,
- GE_USER | GE_WARNING | GE_IMMEDIATE,
- "getcwd");
- FREE(buffer);
- buffer = getenv("PWD"); /* alternative */
- if (buffer == NULL)
- return NULL; /* fatal */
- fm = STRDUP(buffer);
- }
- }
- }
- n = strlen(fm) + 1 + strlen(fil_ptr) + 1;
- buffer = MALLOC(n);
- SNPRINTF(buffer, n,
- "%s/%s", fm, fil_ptr);
- FREE(fm);
- return buffer;
-#else
- fn = MALLOC(MAX_PATH + 1);
-
- if ((lRet = plibc_conv_to_win_path(fil, fn)) != ERROR_SUCCESS) {
- SetErrnoFromWinError(lRet);
- GE_LOG_STRERROR(ectx,
- GE_USER | GE_WARNING | GE_IMMEDIATE,
- "plibc_conv_to_win_path");
- return NULL;
- }
- /* is the path relative? */
- if ( (strncmp(fn + 1, ":\\", 2) != 0) &&
- (strncmp(fn, "\\\\", 2) != 0)) {
- char szCurDir[MAX_PATH + 1];
- lRet = GetCurrentDirectory(MAX_PATH + 1, szCurDir);
- if (lRet + strlen(fn) + 1 > (MAX_PATH + 1)) {
- SetErrnoFromWinError(ERROR_BUFFER_OVERFLOW);
- GE_LOG_STRERROR(ectx,
- GE_USER | GE_WARNING | GE_IMMEDIATE,
- "GetCurrentDirectory");
- return NULL;
- }
- buffer = MALLOC(MAX_PATH + 1);
- SNPRINTF(buffer,
- MAX_PATH+1,
- "%s\\%s", szCurDir, fn);
- FREE(fn);
- fn = buffer;
- }
-
- return fn;
-#endif
-}
-
-
-
-/* end of string.c */
+ while (errno == ERANGE) {
+ buffer = MALLOC(len);
+ if (getcwd(buffer, len) != NULL) {
+ fm = buffer;
+ break;
+ } else {
+ GE_LOG_STRERROR(ectx,
+ GE_USER | GE_WARNING | GE_IMMEDIATE,
+ "getcwd");
+ FREE(buffer);
+ buffer = getenv("PWD"); /* alternative */
+ if (buffer == NULL)
+ return NULL; /* fatal */
+ fm = STRDUP(buffer);
+ }
+ }
+ }
+ n = strlen(fm) + 1 + strlen(fil_ptr) + 1;
+ buffer = MALLOC(n);
+ SNPRINTF(buffer, n,
+ "%s/%s", fm, fil_ptr);
+ FREE(fm);
+ return buffer;
+#else
+ fn = MALLOC(MAX_PATH + 1);
+
+ if ((lRet = plibc_conv_to_win_path(fil, fn)) != ERROR_SUCCESS) {
+ SetErrnoFromWinError(lRet);
+ GE_LOG_STRERROR(ectx,
+ GE_USER | GE_WARNING | GE_IMMEDIATE,
+ "plibc_conv_to_win_path");
+ return NULL;
+ }
+ /* is the path relative? */
+ if ( (strncmp(fn + 1, ":\\", 2) != 0) &&
+ (strncmp(fn, "\\\\", 2) != 0)) {
+ char szCurDir[MAX_PATH + 1];
+ lRet = GetCurrentDirectory(MAX_PATH + 1, szCurDir);
+ if (lRet + strlen(fn) + 1 > (MAX_PATH + 1)) {
+ SetErrnoFromWinError(ERROR_BUFFER_OVERFLOW);
+ GE_LOG_STRERROR(ectx,
+ GE_USER | GE_WARNING | GE_IMMEDIATE,
+ "GetCurrentDirectory");
+ return NULL;
+ }
+ buffer = MALLOC(MAX_PATH + 1);
+ SNPRINTF(buffer,
+ MAX_PATH+1,
+ "%s\\%s", szCurDir, fn);
+ FREE(fn);
+ fn = buffer;
+ }
+
+ return fn;
+#endif
+}
+
+
+
+/* end of string.c */
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- [GNUnet-SVN] r3979 - in GNUnet/src: setup/gtk transports util/string,
grothoff <=