// ip: network-byte order // port: network-byte order char * ipport_to_string(const unsigned int ip, const unsigned short port) { static char buf[24]; struct in_addr inaddr; buf[0] = '<'; buf[1] = '\0'; if (ip == INADDR_ANY) { strcat(buf, my_ip_string()); } else { inaddr.s_addr = ip; strcat(buf, inet_ntoa(inaddr)); } sprintf(&buf[strlen(buf)], ":%d>", ntohs(port)); return buf; }
UdpWakeOnLanWaker::UdpWakeOnLanWaker ( char const *mac, char const *subnet, unsigned short port ) throw () : WakerBase (), m_port ( port ) { strncpy ( m_mac, mac, STRING_MAC_ADDRESS_LENGTH-1 ); m_mac[STRING_MAC_ADDRESS_LENGTH-1] = '\0'; strncpy ( m_subnet, subnet, MAX_IP_ADDRESS_LENGTH-1 ); m_subnet[MAX_IP_ADDRESS_LENGTH-1] = '\0'; strncpy ( m_public_ip, my_ip_string (), MAX_IP_ADDRESS_LENGTH-1 ); m_public_ip[MAX_IP_ADDRESS_LENGTH-1] = '\0'; m_can_wake = initialize (); }
char const * SharedPortEndpoint::GetMyLocalAddress() { if( !m_listening ) { return NULL; } if( m_local_addr.IsEmpty() ) { Sinful sinful; // port 0 is used as an indicator that no SharedPortServer // address is included in this address. This address should // never be shared with anybody except for local commands // and daemons who can then form a connection to us via // direct access to our named socket. sinful.setPort("0"); sinful.setHost(my_ip_string()); sinful.setSharedPortID( m_local_id.Value() ); std::string alias; if( param(alias,"HOST_ALIAS") ) { sinful.setAlias(alias.c_str()); } m_local_addr = sinful.getSinful(); } return m_local_addr.Value(); }
void ConvertDefaultIPToSocketIP(char const *attr_name,char const *old_expr_string,char **new_expr_string,Stream& s) { *new_expr_string = NULL; if( !enable_convert_default_IP_to_socket_IP ) { return; } if(!is_sender_ip_attr(attr_name)) { return; } char const *my_default_ip = my_ip_string(); char const *my_sock_ip = s.my_ip_str(); if(!my_default_ip || !my_sock_ip) { return; } if(strcmp(my_default_ip,my_sock_ip) == 0) { return; } condor_sockaddr sock_addr; //if(is_loopback_net_str(my_sock_ip)) { if (sock_addr.from_ip_string(my_sock_ip) && sock_addr.is_loopback()) { // We must be talking to another daemon on the same // machine as us. We don't want to replace the default IP // with this one, since nobody outside of this machine // will be able to contact us on that IP. return; } if( !IPMatchesNetworkInterfaceSetting(my_sock_ip) ) { return; } char const *ref = strstr(old_expr_string,my_default_ip); if(ref) { // the match must not be followed by any trailing digits // GOOD: <MMM.MMM.M.M:port?p> (where M is a matching digit) // BAD: <MMM.MMM.M.MN:port?p> (where N is a non-matching digit) if( isdigit(ref[strlen(my_default_ip)]) ) { ref = NULL; } } if(ref) { // Replace the default IP address with the one I am actually using. int pos = ref-old_expr_string; // position of the reference int my_default_ip_len = strlen(my_default_ip); int my_sock_ip_len = strlen(my_sock_ip); *new_expr_string = (char *)malloc(strlen(old_expr_string) + my_sock_ip_len - my_default_ip_len + 1); ASSERT(*new_expr_string); strncpy(*new_expr_string, old_expr_string,pos); strcpy(*new_expr_string+pos, my_sock_ip); strcpy(*new_expr_string+pos+my_sock_ip_len, old_expr_string+pos+my_default_ip_len); dprintf(D_NETWORK,"Replaced default IP %s with connection IP %s " "in outgoing ClassAd attribute %s.\n", my_default_ip,my_sock_ip,attr_name); } }
int main( int argc, char *argv[] ) { const char *filename=0; char *pool=0; int command=-1; int i; bool use_tcp = false; bool with_ack = false; bool allow_multiple = false; param_functions *p_funcs = NULL; myDistro->Init( argc, argv ); config(); p_funcs = get_param_functions(); for( i=1; i<argc; i++ ) { if(!strcmp(argv[i],"-help")) { usage(argv[0]); exit(0); } else if(!strcmp(argv[i],"-pool")) { i++; if(!argv[i]) { fprintf(stderr,"-pool requires an argument.\n\n"); usage(argv[0]); exit(1); } pool = argv[i]; } else if(!strncmp(argv[i],"-tcp",strlen(argv[i]))) { use_tcp = true; } else if(!strncmp(argv[i],"-multiple",strlen(argv[i]))) { // We don't set allow_multiple=true by default, because // existing users (e.g. glideinWMS) have stray blank lines // in the input file. allow_multiple = true; } else if(!strcmp(argv[i],"-version")) { version(); exit(0); } else if(!strcmp(argv[i],"-debug")) { // dprintf to console Termlog = 1; p_funcs = get_param_functions(); dprintf_config ("TOOL", p_funcs); } else if(argv[i][0]!='-' || !strcmp(argv[i],"-")) { if(command==-1) { command = getCollectorCommandNum(argv[i]); if(command==-1) { fprintf(stderr,"Unknown command name %s\n\n",argv[i]); usage(argv[0]); exit(1); } } else if(!filename) { filename = argv[i]; } else { fprintf(stderr,"Extra argument: %s\n\n",argv[i]); usage(argv[0]); exit(1); } } else { fprintf(stderr,"Unknown argument: %s\n\n",argv[i]); usage(argv[0]); exit(1); } } FILE *file; ClassAdList ads; Daemon *collector; Sock *sock; switch( command ) { case UPDATE_STARTD_AD_WITH_ACK: with_ack = true; break; } if( with_ack ) { use_tcp = true; } if(!filename || !strcmp(filename,"-")) { file = stdin; filename = "(stdin)"; } else { file = safe_fopen_wrapper_follow(filename,"r"); } if(!file) { fprintf(stderr,"couldn't open %s: %s\n",filename,strerror(errno)); return 1; } while(!feof(file)) { int eof=0,error=0,empty=0; char const *delim = "\n"; if( !allow_multiple ) { delim = "***"; } ClassAd *ad = new ClassAd(file,const_cast<char *>(delim),eof,error,empty); if(error) { fprintf(stderr,"couldn't parse ClassAd in %s\n",filename); delete ad; return 1; } if( empty ) { delete ad; break; } if( !allow_multiple && ads.Length() > 0 ) { fprintf(stderr,"ERROR: failed to parse '%s' as a ClassAd attribute\n",delim); delete ad; return 1; } ads.Insert(ad); } if(ads.Length() == 0) { fprintf(stderr,"%s is empty\n",filename); return 1; } CollectorList * collectors; if ( pool ) { collector = new Daemon( DT_COLLECTOR, pool, 0 ); collectors = new CollectorList(); collectors->append (collector); } else { collectors = CollectorList::create(); } bool had_error = false; collectors->rewind(); while (collectors->next(collector)) { dprintf(D_FULLDEBUG,"locating collector %s...\n", collector->name()); if(!collector->locate()) { fprintf(stderr,"couldn't locate collector: %s\n",collector->error()); had_error = true; continue; } dprintf(D_FULLDEBUG,"collector is %s located at %s\n", collector->hostname(),collector->addr()); sock = NULL; ClassAd *ad; int success_count = 0; int failure_count = 0; ads.Rewind(); while( (ad=ads.Next()) ) { // If there's no "MyAddress", generate one.. if( !ad->Lookup( ATTR_MY_ADDRESS ) ) { MyString tmp; tmp.formatstr( "<%s:0>", my_ip_string() ); ad->Assign( ATTR_MY_ADDRESS, tmp.Value() ); } if ( use_tcp ) { if( !sock ) { sock = collector->startCommand(command,Stream::reli_sock,20); } else { // Use existing connection. sock->encode(); sock->put(command); } } else { // We must open a new UDP socket each time. delete sock; sock = collector->startCommand(command,Stream::safe_sock,20); } int result = 0; if ( sock ) { result += ad->put( *sock ); result += sock->end_of_message(); } if ( result != 2 ) { fprintf(stderr,"failed to send classad to %s\n",collector->addr()); had_error = true; failure_count++; delete sock; sock = NULL; continue; } if( with_ack ) { sock->decode(); int ok = 0; if( !sock->get(ok) || !sock->end_of_message() ) { fprintf(stderr,"failed to get ack from %s\n",collector->addr()); had_error = true; failure_count++; delete sock; sock = NULL; continue; } // ack protocol does not allow for multiple updates, // so close the socket now delete sock; sock = NULL; } success_count++; } if( sock ) { CondorVersionInfo const *ver = sock->get_peer_version(); if( !ver || ver->built_since_version(7,7,3) ) { // graceful hangup so the collector knows we are done sock->encode(); command = DC_NOP; sock->put(command); sock->end_of_message(); } delete sock; sock = NULL; } printf("Sent %d of %d ad%s to %s.\n", success_count, success_count + failure_count, success_count+failure_count == 1 ? "" : "s", collector->name()); } delete collectors; return (had_error)?1:0; }
bool IOProxy::init( const char *config_file ) { FILE *file=0; int fd=-1; server = new ReliSock; if ( !server ) { dprintf(D_ALWAYS,"IOProxy: couldn't create socket\n"); return false; } /* passing FALSE to bind means this is an incomming connection. * however, here we are going to pass TRUE because only machines * on this host need to connect to the ioproxy (chirp) socket, * so there is no need to register it with a ccb broker. **/ if(!server->bind(TRUE)) { dprintf(D_ALWAYS,"IOProxy: couldn't bind: %s\n",strerror(errno)); return false; } if(!server->listen()) { dprintf(D_ALWAYS,"IOProxy: couldn't listen: %s\n",strerror(errno)); return false; } cookie = cookie_create(IO_PROXY_COOKIE_SIZE); if(!cookie) { dprintf(D_ALWAYS,"IOProxy: couldn't create cookie: %s\n",strerror(errno)); goto failure; } fd = safe_open_wrapper_follow(config_file, O_CREAT|O_TRUNC|O_WRONLY, 0700); if(fd<0) { dprintf(D_ALWAYS, "IOProxy: couldn't write to %s: %s\n", config_file, strerror(errno)); goto failure; } file = fdopen(fd,"w"); if(!file) { dprintf(D_ALWAYS,"IOProxy: couldn't create I/O stream: %s\n",strerror(errno)); goto failure; } fprintf(file,"%s %d %s\n",my_ip_string(),server->get_port(),cookie); fclose(file); daemonCore->Register_Socket( server, "IOProxy listen socket", (SocketHandlercpp) &IOProxy::connect_callback, "IOProxy connect callback", this ); socket_registered = true; return true; failure: if(cookie) free(cookie); if(file) fclose(file); unlink(config_file); server->close(); return false; }