6ff8d018525b73c8836fa687bf16f45ccc1a11f1 galt Wed Jun 21 23:33:34 2023 -0700 Adding a utility for using our existing geoIp infrastructure to lookup a 2-letter country code from an IP address (IPv4 or IPv6). Gives an error if the IP address is malformed, gives an error if hgcentral.geoIpCountry6 table does not exist, then uses it to lookup the country. If it is not found in our Maxmind table, then it returns "not found". Hopefully Hiram and others can use this. diff --git src/hg/lib/geoMirror.c src/hg/lib/geoMirror.c index 9b5495e..3a90532 100644 --- src/hg/lib/geoMirror.c +++ src/hg/lib/geoMirror.c @@ -154,30 +154,96 @@ char *geoSuffix = cfgOptionDefault("browser.geoSuffix",""); char fullTableName[1024]; safef(fullTableName, sizeof fullTableName, "%s%s", "geoIpNode6", geoSuffix); int defaultNode = 1; if (sqlTableExists(centralConn, fullTableName)) { defaultNode = geoMirrorDefaultNode6(centralConn, ipStr); } else { defaultNode = geoMirrorDefaultNode4(centralConn, ipStr); } return defaultNode; } +char *geoMirrorCountry6(struct sqlConnection *centralConn, char *ipStr) +/* Return 2 letter country code for given IP. user has already checked table geoIpCountry6 exists. + * Return error string otherwise. Free the response string. */ +{ +char query[1024]; +char newIpStr[NI_MAXHOST]; +struct in6_addr ip; +ZeroVar(&ip); +char ipHex[33]; + +char response[256]; +safef(response, sizeof response, "not found"); + +if (isIpv6Address(ipStr)) + safef(newIpStr, sizeof newIpStr, "%s", ipStr); +else if (isIpv4Address(ipStr)) + safef(newIpStr, sizeof newIpStr, "%s%s", IPV4MAPPED_PREFIX, ipStr); // "::ffff:" +else + { + warn("Unexpected strange ip address string: %s", ipStr); + safef(response, sizeof response, "Unexpected strange ip address string: %s", ipStr); + return cloneString(response); + } + +if (!internetIpStringToIp6(newIpStr, &ip)) + errAbort("internetIpStringToIp6 failed for %s", ipStr); + +ip6AddrToHexStr(&ip, ipHex, sizeof ipHex); + +char *geoSuffix = cfgOptionDefault("browser.geoSuffix",""); + +// We (sort-of) assume no overlaps in geoIpCountry6 table, so we can use limit 1 to make query very efficient; +// we do accomodate a range that is completely contained in another (to accomodate the hgroaming entry for testing); +// this is accomplished by "<= ipEnd" in the sql query. +// TODO The hgroaming thing is probably obsolete and testing is done with browser.geoSuffix= instead. +// If so, we may wish to remove the loop below since that was added by Larry and reformulate +// it as it was originally done by Galt. However it does not seem to affect performance so we can leave it for now. + +sqlSafef(query, sizeof query, + "select ipStart, ipEnd, countryId from geoIpCountry6%s where unhex('%s') >= ipStart and unhex('%s') <= ipEnd order by ipStart desc limit 1" + , geoSuffix, ipHex, ipHex); +char **row; +struct sqlResult *sr = sqlGetResult(centralConn, query); + +if ((row = sqlNextRow(sr)) != NULL) + { + struct in6_addr ipStart; + ip6AddrCopy((struct in6_addr *)row[0], &ipStart); + + struct in6_addr ipEnd; + ip6AddrCopy((struct in6_addr *)row[1], &ipEnd ); + + if ( + (ip6AddrCmpBits(&ipStart, &ip) <= 0) + && + (ip6AddrCmpBits(&ipEnd , &ip) >= 0) + ) + { + safef(response, sizeof response, "%s", row[2]); + } + } +sqlFreeResult(&sr); + +return cloneString(response); +} + char *geoMirrorMenu() /* Create customized geoMirror menu string for substitution of into * <!-- OPTIONAL_MIRROR_MENU --> in htdocs/inc/globalNavBar.inc * Reads hgcentral geo tables and hg.conf settings. * Free the returned string when done. */ { struct dyString *dy = dyStringNew(0); // by default replacment is just an empty string. if (geoMirrorEnabled()) { dyStringAppend(dy, "<li id=\"geoMirrorMenu\" class=\"noHighlight\"><hr></li>\n"); // Geo mirror functionality (e.g. in nav bar) char *myNode = geoMirrorNode(); /* hgcentral.gbNode table so UI can share w/ browser GEO mirror redirect code */ char **row = NULL; struct sqlConnection *conn = hConnectCentral(); // after hClade since it access hgcentral too