MyString convert_ipaddr_to_hostname(const condor_sockaddr& addr) { MyString ret; MyString default_domain; if (!param(default_domain, "DEFAULT_DOMAIN_NAME")) { dprintf(D_HOSTNAME, "NO_DNS: DEFAULT_DOMAIN_NAME must be defined in your " "top-level config file\n"); return ret; } ret = addr.to_ip_string(); for (int i = 0; i < ret.Length(); ++i) { if (ret[i] == '.' || ret[i] == ':') ret.setChar(i, '-'); } ret += "."; ret += default_domain; // Hostnames can't begin with -, as per RFC 1123 // ipv6 zero-compression could cause this, esp. for the loopback addr if (ret[0] == '-') { ret = "0" + ret; } return ret; }
void append_arg(char const *arg,MyString &result) { if(result.Length()) { result += " "; } ASSERT(arg); if(!*arg) { result += "''"; //empty arg } while(*arg) { switch(*arg) { case ' ': case '\t': case '\n': case '\r': case '\'': if(result.Length() && result[result.Length()-1] == '\'') { //combine preceeding quoted section with this one, //so we do not introduce a repeated quote. result.setChar(result.Length()-1,'\0'); } else { result += '\''; } if(*arg == '\'') { result += '\''; //repeat the quote to escape it } result += *(arg++); result += '\''; break; default: result += *(arg++); } } }
// Remove/replace characters from the string so it can be used as an attribute name // it changes the string that is passed to it. first leading an trailing spaces // are removed, then Characters that are invalid in compatible classads // (basically anthing but [a-zA-Z0-9_]) is replaced with chReplace. // if chReplace is 0, then invalid characters are removed. // if compact is true, then multiple consecutive runs of chReplace // are changed to a single instance. // return value is the length of the resulting string. // int cleanStringForUseAsAttr(MyString &str, char chReplace/*=0*/, bool compact/*=true*/) { // have 0 mean 'remove' since we can't actually use it as a replacement char // we'll actually implement it by replacing invalid chars with spaces, // and then compacting to remove all of the spaces. if (0 == chReplace) { chReplace = ' '; compact = true; } // trim the input and replace invalid chars with chReplace str.trim(); for (int ii = 0; ii < str.Length(); ++ii) { char ch = str[ii]; if (ch == '_' || (ch >= '0' && ch <= '9') || (ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z')) continue; str.setChar(ii,chReplace); } // if compact, convert runs of chReplace with a single instance, // unless chReplace is ' ', then remove them entirely. if (compact) { if (chReplace == ' ') str.replaceString(" ",""); else { MyString tmp; tmp += chReplace; tmp += chReplace; str.replaceString(tmp.Value(), tmp.Value()+1); } } str.trim(); return str.Length(); }
bool getClassAdNoTypes( Stream *sock, classad::ClassAd& ad ) { classad::ClassAdParser parser; int numExprs = 0; // Initialization clears Coverity warning string buffer; classad::ClassAd *upd=NULL; MyString inputLine; ad.Clear( ); // Reinsert CurrentTime, emulating the special version in old // ClassAds if ( !compat_classad::ClassAd::m_strictEvaluation ) { ad.Insert( ATTR_CURRENT_TIME " = time()" ); } sock->decode( ); if( !sock->code( numExprs ) ) { return false; } // pack exprs into classad buffer = "["; for( int i = 0 ; i < numExprs ; i++ ) { if( !sock->get( inputLine ) ) { return( false ); } if(strcmp(inputLine.Value(),SECRET_MARKER) ==0 ){ char *secret_line = NULL; if( !sock->get_secret(secret_line) ) { dprintf(D_FULLDEBUG, "Failed to read encrypted ClassAd expression.\n"); break; } inputLine = secret_line; free( secret_line ); } if ( strncmp( inputLine.Value(), "ConcurrencyLimit.", 17 ) == 0 ) { inputLine.setChar( 16, '_' ); } buffer += string(inputLine.Value()) + ";"; } buffer += "]"; // parse ad if( !( upd = parser.ParseClassAd( buffer ) ) ) { return( false ); } // put exprs into ad ad.Update( *upd ); delete upd; return true; }
condor_sockaddr convert_hostname_to_ipaddr(const MyString& fullname) { MyString hostname; MyString default_domain; bool truncated = false; if (param(default_domain, "DEFAULT_DOMAIN_NAME")) { MyString dotted_domain = "."; dotted_domain += default_domain; int pos = fullname.find(dotted_domain.Value()); if (pos != -1) { truncated = true; hostname = fullname.Substr(0, pos - 1); } } if (!truncated) hostname = fullname; // detects if hostname is IPv6 // // hostname is NODNS coded address // // for example, // it could be 127-0-0-1 (127.0.0.1) as IPv4 address // it could be fe80-3577--1234 ( fe80:3577::1234) as IPv6 address // // it is IPv6 address // 1) if there are 7 '-' // 2) if there are '--' which means compaction of zeroes in IPv6 adress char target_char; bool ipv6 = false; if (hostname.find("--") != -1) ipv6 = true; else { int dash_count = 0; for (int i = 0; i < hostname.Length(); ++i) if (hostname[i] == '-') ++dash_count; if (dash_count == 7) ipv6 = true; } if (ipv6) target_char = ':'; else target_char ='.'; // converts hostname to IP address string for (int i = 0; i < hostname.Length(); ++i) { if (hostname[i] == '-') hostname.setChar(i, target_char); } condor_sockaddr ret; ret.from_ip_string(hostname); return ret; }
bool create_name_for_VM(ClassAd *ad, MyString& vmname) { if( !ad ) { return false; } int cluster_id = 0; if( ad->LookupInteger(ATTR_CLUSTER_ID, cluster_id) != 1 ) { dprintf(D_ALWAYS, "%s cannot be found in job classAd\n", ATTR_CLUSTER_ID); return false; } int proc_id = 0; if( ad->LookupInteger(ATTR_PROC_ID, proc_id) != 1 ) { dprintf(D_ALWAYS, "%s cannot be found in job classAd\n", ATTR_PROC_ID); return false; } MyString stringattr; if( ad->LookupString(ATTR_USER, stringattr) != 1 ) { dprintf(D_ALWAYS, "%s cannot be found in job classAd\n", ATTR_USER); return false; } // replace '@' with '_' int pos = -1; while( (pos = stringattr.find("@") ) >= 0 ) { stringattr.setChar(pos, '_'); } vmname = stringattr; vmname += "_"; vmname += cluster_id; vmname += "_"; vmname += proc_id; return true; }
/* This function has a unit test. */ int is_valid_sinful( const char *sinful ) { dprintf(D_HOSTNAME, "Checking if %s is a sinful address\n", sinful); char addrbuf[INET6_ADDRSTRLEN]; const char* acc = sinful; const char* tmp; const char* addr_begin; const char* addr_end; if (!acc) return FALSE; if (*acc != '<') { dprintf(D_HOSTNAME, "%s is not a sinful address: does not begin with \"<\"\n", sinful); return FALSE; } acc++; if (*acc == '[') { dprintf(D_HOSTNAME, "%s is an ipv6 address\n", sinful); tmp = strchr(acc, ']'); if (!tmp) { dprintf(D_HOSTNAME, "%s is not a sinful address: could not find closing \"]\"\n", sinful); return FALSE; } addr_begin = acc + 1; addr_end = tmp; // too long if (addr_end - addr_begin > INET6_ADDRSTRLEN) { dprintf(D_HOSTNAME, "%s is not a sinful address: addr too long %d\n", sinful, (int)(addr_end - addr_begin)); return FALSE; } strncpy(addrbuf, addr_begin, addr_end - addr_begin); addrbuf[addr_end - addr_begin] = '\0'; dprintf(D_HOSTNAME, "tring to convert %s using inet_pton, %s\n", sinful, addrbuf); in6_addr tmp_addr; if (inet_pton(AF_INET6, addrbuf, &tmp_addr) <= 0) { dprintf(D_HOSTNAME, "%s is not a sinful address: inet_pton(AF_INET6, %s) failed\n", sinful, addrbuf); return FALSE; } acc = tmp + 1; } else { MyString ipaddr = acc; int colon_pos = ipaddr.FindChar(':'); if(colon_pos == -1) { return false; } ipaddr.setChar(colon_pos, 0); if( ! is_ipv4_addr_implementation(ipaddr.Value(),NULL,NULL,false) ) { return FALSE; } acc = acc + colon_pos; } if (*acc != ':') { dprintf(D_HOSTNAME, "%s is not a sinful address: no colon found\n", sinful); return FALSE; } tmp = strchr(acc, '>'); if (!tmp) { dprintf(D_HOSTNAME, "%s is not a sinful address: no closing \">\" found\n", sinful); return FALSE; } dprintf(D_HOSTNAME, "%s is a sinful address!\n", sinful); return TRUE; }