std::string WEnvironment::getClientAddress(const WebRequest& request, const Configuration& conf) { std::string result; /* * Determine client address, taking into account proxies */ if (conf.behindReverseProxy()) { std::string clientIp = str(request.headerValue("Client-IP")); boost::trim(clientIp); std::vector<std::string> ips; if (!clientIp.empty()) boost::split(ips, clientIp, boost::is_any_of(",")); std::string forwardedFor = str(request.headerValue("X-Forwarded-For")); boost::trim(forwardedFor); std::vector<std::string> forwardedIps; if (!forwardedFor.empty()) boost::split(forwardedIps, forwardedFor, boost::is_any_of(",")); Utils::insert(ips, forwardedIps); for (unsigned i = 0; i < ips.size(); ++i) { result = ips[i]; boost::trim(result); if (!result.empty() && !isPrivateIP(result)) { break; } } } if (result.empty()) result = str(request.envValue("REMOTE_ADDR")); return result; }
int GetLWSessionKey(Packet *p, SessionKey *key) { u_int16_t sport; u_int16_t dport; int proto; /* Because the key is going to be used for hash lookups, * the lower of the values of the IP address field is * stored in the key->ip_l and the port for that ip is * stored in key->port_l. */ if (!key) return 0; #ifdef SUP_IP6 if (IS_IP4(p)) { u_int32_t *src; u_int32_t *dst; proto = p->iph->ip_proto; switch (proto) { case IPPROTO_TCP: case IPPROTO_UDP: sport = p->sp; dport = p->dp; break; case IPPROTO_ICMP: default: sport = dport = 0; break; } src = p->ip4h.ip_src.ip32; dst = p->ip4h.ip_dst.ip32; /* These comparisons are done in this fashion for performance reasons */ if (*src < *dst) { COPY4(key->ip_l, src); COPY4(key->ip_h, dst); key->port_l = sport; key->port_h = dport; } else if (*src == *dst) { COPY4(key->ip_l, src); COPY4(key->ip_h, dst); if (sport < dport) { key->port_l = sport; key->port_h = dport; } else { key->port_l = dport; key->port_h = sport; } } else { COPY4(key->ip_l, dst); key->port_l = dport; COPY4(key->ip_h, src); key->port_h = sport; } #ifdef MPLS if(pv.overlapping_IP && (p->mpls) && isPrivateIP(*src) && isPrivateIP(*dst) ) { key->mplsLabel = p->mplsHdr.label; } else { key->mplsLabel = 0; } #endif } else { /* IPv6 */ sfip_t *src; sfip_t *dst; proto = p->ip6h.next; switch (proto) { case IPPROTO_TCP: case IPPROTO_UDP: sport = p->sp; dport = p->dp; break; case IPPROTO_ICMP: default: sport = dport = 0; break; } src = &p->ip6h.ip_src; dst = &p->ip6h.ip_dst; if (sfip_fast_lt6(src, dst)) { COPY4(key->ip_l, src->ip32); key->port_l = sport; COPY4(key->ip_h, dst->ip32); key->port_h = dport; } else if (sfip_fast_eq6(src, dst)) { COPY4(key->ip_l, src->ip32); COPY4(key->ip_h, dst->ip32); if (sport < dport) { key->port_l = sport; key->port_h = dport; } else { key->port_l = dport; key->port_h = sport; } } else { COPY4(key->ip_l, dst->ip32); key->port_l = dport; COPY4(key->ip_h, src->ip32); key->port_h = sport; } #ifdef MPLS if(pv.overlapping_IP && (p->mpls)) { key->mplsLabel = p->mplsHdr.label; } else { key->mplsLabel = 0; } #endif } #else proto = p->iph->ip_proto; switch (proto) { case IPPROTO_TCP: case IPPROTO_UDP: sport = p->sp; dport = p->dp; break; case IPPROTO_ICMP: default: sport = dport = 0; break; } /* These comparisons are done in this fashion for performance reasons */ if (IP_LESSER(GET_SRC_IP(p), GET_DST_IP(p))) { IP_COPY_VALUE(key->ip_l, GET_SRC_IP(p)); key->port_l = sport; IP_COPY_VALUE(key->ip_h, GET_DST_IP(p)); key->port_h = dport; } else if (IP_EQUALITY(GET_SRC_IP(p), GET_DST_IP(p))) { IP_COPY_VALUE(key->ip_l, GET_SRC_IP(p)); IP_COPY_VALUE(key->ip_h, GET_DST_IP(p)); if (sport < dport) { key->port_l = sport; key->port_h = dport; } else { key->port_l = dport; key->port_h = sport; } } else { IP_COPY_VALUE(key->ip_l, GET_DST_IP(p)); key->port_l = dport; IP_COPY_VALUE(key->ip_h, GET_SRC_IP(p)); key->port_h = sport; } #ifdef MPLS if(pv.overlapping_IP && (p->mpls) && isPrivateIP(key->ip_l) && isPrivateIP(key->ip_h)) { key->mplsLabel = p->mplsHdr.label; } else { key->mplsLabel = 0; } #endif #endif key->protocol = proto; if (p->vh) key->vlan_tag = (u_int16_t)VTH_VLAN(p->vh); else key->vlan_tag = 0; key->pad = 0; #ifdef MPLS key->mplsPad = 0; #endif return 1; }
std::string determineHost() { std::string ip_env; // First, did the user set ROS_HOSTNAME? if ( get_environment_variable(ip_env, "ROS_HOSTNAME")) { ROSCPP_LOG_DEBUG( "determineIP: using value of ROS_HOSTNAME:%s:", ip_env.c_str()); return ip_env; } // Second, did the user set ROS_IP? if ( get_environment_variable(ip_env, "ROS_IP")) { ROSCPP_LOG_DEBUG( "determineIP: using value of ROS_IP:%s:", ip_env.c_str()); return ip_env; } // Third, try the hostname char host[1024]; memset(host,0,sizeof(host)); if(gethostname(host,sizeof(host)-1) != 0) { ROS_ERROR("determineIP: gethostname failed"); } // We don't want localhost to be our ip else if(strlen(host) && strcmp("localhost", host)) { return std::string(host); } // Fourth, fall back on interface search, which will yield an IP address #ifdef HAVE_IFADDRS_H struct ifaddrs *ifa = NULL, *ifp = NULL; int rc; if ((rc = getifaddrs(&ifp)) < 0) { ROS_FATAL("error in getifaddrs: [%s]", strerror(rc)); ROS_BREAK(); } char preferred_ip[200] = {0}; for (ifa = ifp; ifa; ifa = ifa->ifa_next) { char ip_[200]; socklen_t salen; if (!ifa->ifa_addr) continue; // evidently this interface has no ip address if (ifa->ifa_addr->sa_family == AF_INET) salen = sizeof(struct sockaddr_in); else if (ifa->ifa_addr->sa_family == AF_INET6) salen = sizeof(struct sockaddr_in6); else continue; if (getnameinfo(ifa->ifa_addr, salen, ip_, sizeof(ip_), NULL, 0, NI_NUMERICHOST) < 0) { ROSCPP_LOG_DEBUG( "getnameinfo couldn't get the ip of interface [%s]", ifa->ifa_name); continue; } //ROS_INFO( "ip of interface [%s] is [%s]", ifa->ifa_name, ip); // prefer non-private IPs over private IPs if (!strcmp("127.0.0.1", ip_) || strchr(ip_,':')) continue; // ignore loopback unless we have no other choice if (ifa->ifa_addr->sa_family == AF_INET6 && !preferred_ip[0]) strcpy(preferred_ip, ip_); else if (isPrivateIP(ip_) && !preferred_ip[0]) strcpy(preferred_ip, ip_); else if (!isPrivateIP(ip_) && (isPrivateIP(preferred_ip) || !preferred_ip[0])) strcpy(preferred_ip, ip_); } freeifaddrs(ifp); if (!preferred_ip[0]) { ROS_ERROR( "Couldn't find a preferred IP via the getifaddrs() call; I'm assuming that your IP " "address is 127.0.0.1. This should work for local processes, " "but will almost certainly not work if you have remote processes." "Report to the ROS development team to seek a fix."); return std::string("127.0.0.1"); } ROSCPP_LOG_DEBUG( "preferred IP is guessed to be %s", preferred_ip); return std::string(preferred_ip); #else // @todo Fix IP determination in the case where getifaddrs() isn't // available. ROS_ERROR( "You don't have the getifaddrs() call; I'm assuming that your IP " "address is 127.0.0.1. This should work for local processes, " "but will almost certainly not work if you have remote processes." "Report to the ROS development team to seek a fix."); return std::string("127.0.0.1"); #endif }