From bf14668b2a831d583196d8c5d1f58c69eefa216e Mon Sep 17 00:00:00 2001 From: ridiculousfish Date: Fri, 25 Apr 2014 17:44:49 -0700 Subject: [PATCH] Migrate machine and hostname identification from fishd.cpp to env_universal_common.cpp, so that fish can use it --- env_universal.cpp | 11 ++++ env_universal.h | 3 + env_universal_common.cpp | 124 +++++++++++++++++++++++++++++++++++++++ env_universal_common.h | 3 + fishd.cpp | 116 ------------------------------------ 5 files changed, 141 insertions(+), 116 deletions(-) diff --git a/env_universal.cpp b/env_universal.cpp index 64f398327..a83fc0ab7 100644 --- a/env_universal.cpp +++ b/env_universal.cpp @@ -495,3 +495,14 @@ void env_universal_get_names(wcstring_list_t &lst, show_exported, show_unexported); } + + +bool synchronizes_via_fishd() +{ + if (program_name && ! wcscmp(program_name, L"fishd")) + { + /* fishd always wants to use fishd */ + return true; + } + return false; +} diff --git a/env_universal.h b/env_universal.h index a77efb1f5..dd4869112 100644 --- a/env_universal.h +++ b/env_universal.h @@ -70,4 +70,7 @@ void env_universal_get_names(wcstring_list_t &list, */ void env_universal_barrier(); +/* Temporary */ +bool synchronizes_via_fishd(); + #endif diff --git a/env_universal_common.cpp b/env_universal_common.cpp index 5194e8ff1..592930bf2 100644 --- a/env_universal_common.cpp +++ b/env_universal_common.cpp @@ -27,6 +27,7 @@ #include #include #include +#include #include #ifdef HAVE_SYS_SELECT_H @@ -706,3 +707,126 @@ void env_universal_t::enqueue_all(connection_t *c) const c->unsent.push(msg); } } + + + +/** + Maximum length of hostname. Longer hostnames are truncated + */ +#define HOSTNAME_LEN 32 + +/* Length of a MAC address */ +#define MAC_ADDRESS_MAX_LEN 6 + + +/* Thanks to Jan Brittenson + http://lists.apple.com/archives/xcode-users/2009/May/msg00062.html + */ +#ifdef SIOCGIFHWADDR + +/* Linux */ +#include +static bool get_mac_address(unsigned char macaddr[MAC_ADDRESS_MAX_LEN], const char *interface = "eth0") +{ + bool result = false; + const int dummy = socket(AF_INET, SOCK_STREAM, 0); + if (dummy >= 0) + { + struct ifreq r; + strncpy((char *)r.ifr_name, interface, sizeof r.ifr_name - 1); + r.ifr_name[sizeof r.ifr_name - 1] = 0; + if (ioctl(dummy, SIOCGIFHWADDR, &r) >= 0) + { + memcpy(macaddr, r.ifr_hwaddr.sa_data, MAC_ADDRESS_MAX_LEN); + result = true; + } + close(dummy); + } + return result; +} + +#elif defined(HAVE_GETIFADDRS) + +/* OS X and BSD */ +#include +#include +static bool get_mac_address(unsigned char macaddr[MAC_ADDRESS_MAX_LEN], const char *interface = "en0") +{ + // BSD, Mac OS X + struct ifaddrs *ifap; + bool ok = false; + + if (getifaddrs(&ifap) == 0) + { + for (const ifaddrs *p = ifap; p; p = p->ifa_next) + { + if (p->ifa_addr->sa_family == AF_LINK) + { + if (p->ifa_name && p->ifa_name[0] && + ! strcmp((const char*)p->ifa_name, interface)) + { + + const sockaddr_dl& sdl = *(sockaddr_dl*)p->ifa_addr; + + size_t alen = sdl.sdl_alen; + if (alen > MAC_ADDRESS_MAX_LEN) alen = MAC_ADDRESS_MAX_LEN; + memcpy(macaddr, sdl.sdl_data + sdl.sdl_nlen, alen); + ok = true; + break; + } + } + } + freeifaddrs(ifap); + } + return ok; +} + +#else + +/* Unsupported */ +static bool get_mac_address(unsigned char macaddr[MAC_ADDRESS_MAX_LEN]) +{ + return false; +} + +#endif + +/* Function to get an identifier based on the hostname */ +bool get_hostname_identifier(std::string *result) +{ + bool success = false; + char hostname[HOSTNAME_LEN + 1] = {}; + if (gethostname(hostname, HOSTNAME_LEN) == 0) + { + result->assign(hostname); + success = true; + } + return success; +} + +/* Get a sort of unique machine identifier. Prefer the MAC address; if that fails, fall back to the hostname; if that fails, pick something. */ +std::string get_machine_identifier(void) +{ + std::string result; + unsigned char mac_addr[MAC_ADDRESS_MAX_LEN] = {}; + if (get_mac_address(mac_addr)) + { + result.reserve(2 * MAC_ADDRESS_MAX_LEN); + for (size_t i=0; i < MAC_ADDRESS_MAX_LEN; i++) + { + char buff[3]; + snprintf(buff, sizeof buff, "%02x", mac_addr[i]); + result.append(buff); + } + } + else if (get_hostname_identifier(&result)) + { + /* Hooray */ + } + else + { + /* Fallback */ + result.assign("nohost"); + } + return result; +} diff --git a/env_universal_common.h b/env_universal_common.h index 100cbfc45..cc1ddf557 100644 --- a/env_universal_common.h +++ b/env_universal_common.h @@ -228,4 +228,7 @@ public: void enqueue_all(connection_t *c) const; }; +std::string get_machine_identifier(); +bool get_hostname_identifier(std::string *result); + #endif diff --git a/fishd.cpp b/fishd.cpp index e20c8bb57..d6ab45895 100644 --- a/fishd.cpp +++ b/fishd.cpp @@ -100,9 +100,6 @@ time the original barrier request was sent have been received. #define MSG_DONTWAIT 0 #endif -/* Length of a MAC address */ -#define MAC_ADDRESS_MAX_LEN 6 - /** Small greeting to show that fishd is running */ @@ -269,119 +266,6 @@ static std::string gen_unique_nfs_filename(const std::string &filename) return newname; } - -/* Thanks to Jan Brittenson - http://lists.apple.com/archives/xcode-users/2009/May/msg00062.html -*/ -#ifdef SIOCGIFHWADDR - -/* Linux */ -#include -static bool get_mac_address(unsigned char macaddr[MAC_ADDRESS_MAX_LEN], const char *interface = "eth0") -{ - bool result = false; - const int dummy = socket(AF_INET, SOCK_STREAM, 0); - if (dummy >= 0) - { - struct ifreq r; - strncpy((char *)r.ifr_name, interface, sizeof r.ifr_name - 1); - r.ifr_name[sizeof r.ifr_name - 1] = 0; - if (ioctl(dummy, SIOCGIFHWADDR, &r) >= 0) - { - memcpy(macaddr, r.ifr_hwaddr.sa_data, MAC_ADDRESS_MAX_LEN); - result = true; - } - close(dummy); - } - return result; -} - -#elif defined(HAVE_GETIFADDRS) - -/* OS X and BSD */ -#include -#include -static bool get_mac_address(unsigned char macaddr[MAC_ADDRESS_MAX_LEN], const char *interface = "en0") -{ - // BSD, Mac OS X - struct ifaddrs *ifap; - bool ok = false; - - if (getifaddrs(&ifap) == 0) - { - for (const ifaddrs *p = ifap; p; p = p->ifa_next) - { - if (p->ifa_addr->sa_family == AF_LINK) - { - if (p->ifa_name && p->ifa_name[0] && - ! strcmp((const char*)p->ifa_name, interface)) - { - - const sockaddr_dl& sdl = *(sockaddr_dl*)p->ifa_addr; - - size_t alen = sdl.sdl_alen; - if (alen > MAC_ADDRESS_MAX_LEN) alen = MAC_ADDRESS_MAX_LEN; - memcpy(macaddr, sdl.sdl_data + sdl.sdl_nlen, alen); - ok = true; - break; - } - } - } - freeifaddrs(ifap); - } - return ok; -} - -#else - -/* Unsupported */ -static bool get_mac_address(unsigned char macaddr[MAC_ADDRESS_MAX_LEN]) -{ - return false; -} - -#endif - -/* Function to get an identifier based on the hostname */ -static bool get_hostname_identifier(std::string *result) -{ - bool success = false; - char hostname[HOSTNAME_LEN + 1] = {}; - if (gethostname(hostname, HOSTNAME_LEN) == 0) - { - result->assign(hostname); - success = true; - } - return success; -} - -/* Get a sort of unique machine identifier. Prefer the MAC address; if that fails, fall back to the hostname; if that fails, pick something. */ -static std::string get_machine_identifier(void) -{ - std::string result; - unsigned char mac_addr[MAC_ADDRESS_MAX_LEN] = {}; - if (get_mac_address(mac_addr)) - { - result.reserve(2 * MAC_ADDRESS_MAX_LEN); - for (size_t i=0; i < MAC_ADDRESS_MAX_LEN; i++) - { - char buff[3]; - snprintf(buff, sizeof buff, "%02x", mac_addr[i]); - result.append(buff); - } - } - else if (get_hostname_identifier(&result)) - { - /* Hooray */ - } - else - { - /* Fallback */ - result.assign("nohost"); - } - return result; -} - /** The number of milliseconds to wait between polls when attempting to acquire a lockfile