struct obj_conf *conf_init( void ) { struct obj_conf *conf = (struct obj_conf *) myalloc( sizeof(struct obj_conf), "conf_init" ); conf->mode = CONF_FOREGROUND; conf->port = strdup( CONF_PORT ); rand_urandom( conf->node_id, SHA_DIGEST_LENGTH ); memset( conf->null_id, '\0', SHA_DIGEST_LENGTH ); conf->cores = (unix_cpus() > 2) ? unix_cpus() : CONF_CORES; conf->quiet = CONF_VERBOSE; conf->user = strdup( CONF_USER ); conf->bootstrap_node = strdup( CONF_BOOTSTRAP_NODE ); conf->bootstrap_port = strdup( CONF_BOOTSTRAP_PORT ); #ifdef DNS conf->dns_port = strdup( CONF_DNS_PORT ); conf->dns_addr = strdup( CONF_DNS_ADDR ); #endif #ifdef WEB conf->web_port = strdup( CONF_WEB_PORT ); conf->web_addr = strdup( CONF_WEB_ADDR ); #endif return conf; }
struct obj_conf *conf_init( int argc, char **argv ) { struct obj_conf *conf = (struct obj_conf *) myalloc( sizeof(struct obj_conf) ); BEN *opts = opts_init(); BEN *value = NULL; /* Parse command line */ opts_load( opts, argc, argv ); /* Mode */ if( ben_searchDictStr( opts, "-d" ) != NULL ) { conf->mode = CONF_DAEMON; } else { conf->mode = CONF_CONSOLE; } /* Verbosity */ if( ben_searchDictStr( opts, "-v" ) != NULL ) { conf->verbosity = CONF_VERBOSE; } else if ( ben_searchDictStr( opts, "-q" ) != NULL ) { conf->verbosity = CONF_BEQUIET; } else { /* Be verbose in the console and quiet while running as a daemon. */ conf->verbosity = ( conf->mode == CONF_CONSOLE ) ? CONF_VERBOSE : CONF_BEQUIET; } /* Port */ value = ben_searchDictStr( opts, "-p" ); if( value != NULL && ben_str_size( value ) >= 1 ) { conf->port = atoi( (char *)value->v.s->s ); } else { conf->port = CONF_PORT; } if( conf->port < CONF_PORTMIN || conf->port > CONF_PORTMAX ) { conf->port = CONF_PORT; } /* Cores */ conf->cores = ( unix_cpus() > 2 ) ? unix_cpus() : CONF_CORES; if( conf->cores < 1 || conf->cores > 128 ) { fail( "Invalid number of CPU cores" ); } /* HOME */ conf_home( conf, opts ); #ifdef TUMBLEWEED /* HTML index */ value = ben_searchDictStr( opts, "-i" ); if( value != NULL && ben_str_size( value ) >= 1 ) { snprintf( conf->file, BUF_SIZE, "%s", (char *)value->v.s->s ); } else { snprintf( conf->file, BUF_SIZE, "%s", CONF_INDEX_NAME ); } if( !str_isValidFilename( conf->file ) ) { fail( "Index %s looks suspicious", conf->file ); } #endif #ifdef TORRENTKINO /* Hostname */ conf_hostname( conf, opts ); /* Realm */ value = ben_searchDictStr( opts, "-r" ); if( value != NULL && ben_str_size( value ) >= 1 ) { snprintf( conf->realm, BUF_SIZE, "%s", (char *)value->v.s->s ); conf->bool_realm = TRUE; } else { snprintf( conf->realm, BUF_SIZE, "%s", CONF_REALM ); conf->bool_realm = FALSE; } /* Compute host_id. Respect the realm. */ conf_hostid( conf->host_id, conf->hostname, conf->realm, conf->bool_realm ); /* Announce this port */ value = ben_searchDictStr( opts, "-b" ); if( value != NULL && ben_str_size( value ) >= 1 ) { conf->announce_port = atoi( (char *)value->v.s->s ); } else { conf->announce_port = CONF_ANNOUNCED_PORT; } if( conf->announce_port < CONF_PORTMIN || conf->announce_port > CONF_PORTMAX ) { fail( "Invalid announce port number. (-a)" ); } if( getuid() == 0 ) { snprintf( conf->file, BUF_SIZE, "%s/%s", conf->home, CONF_FILE ); } else { snprintf( conf->file, BUF_SIZE, "%s/.%s", conf->home, CONF_FILE ); } /* Node ID */ value = ben_searchDictStr( opts, "-n" ); if( value != NULL && ben_str_size( value ) >= 1 ) { sha1_hash( conf->node_id, (char *)value->v.s->s, ben_str_size( value ) ); } else { rand_urandom( conf->node_id, SHA1_SIZE ); } memset( conf->null_id, '\0', SHA1_SIZE ); /* Bootstrap node */ value = ben_searchDictStr( opts, "-x" ); if( value != NULL && ben_str_size( value ) >= 1 ) { snprintf( conf->bootstrap_node, BUF_SIZE, "%s", (char *)value->v.s->s ); } else { snprintf( conf->bootstrap_node, BUF_SIZE, "%s", CONF_MULTICAST ); } /* Bootstrap port */ value = ben_searchDictStr( opts, "-y" ); if( value != NULL && ben_str_size( value ) >= 1 ) { snprintf( conf->bootstrap_port, CONF_PORT_SIZE+1, "%s", value->v.s->s ); } else { snprintf( conf->bootstrap_port, CONF_PORT_SIZE+1, "%i", CONF_PORT ); } if( str_isSafePort( conf->bootstrap_port) < 0 ) { fail( "Invalid bootstrap port number. (-y)" ); } #ifdef POLARSSL /* Secret key */ value = ben_searchDictStr( opts, "-k" ); if( value != NULL && ben_str_size( value ) >= 1 ) { snprintf( conf->key, BUF_SIZE, "%s", (char *)value->v.s->s ); conf->bool_encryption = TRUE; } else { memset( conf->key, '\0', BUF_SIZE ); conf->bool_encryption = FALSE; } #endif #endif opts_free( opts ); return conf; }
struct obj_conf *conf_init( int argc, char **argv ) { struct obj_conf *conf = myalloc( sizeof( struct obj_conf ) ); int opt = 0; int i = 0; /* Defaults */ conf->mode = CONF_CONSOLE; conf->p2p_port = PORT_DHT_DEFAULT; conf->dns_port = PORT_DNS_DEFAULT; conf->verbosity = CONF_VERBOSE; conf->bootstrap_port = PORT_DHT_DEFAULT; conf->announce_port = PORT_WWW_USER; conf->cores = unix_cpus(); conf->bool_realm = FALSE; #ifdef POLARSSL conf->bool_encryption = FALSE; memset( conf->key, '\0', BUF_SIZE ); #endif memset( conf->null_id, '\0', SHA1_SIZE ); strncpy( conf->realm, CONF_REALM, BUF_OFF1 ); strncpy( conf->bootstrap_node, MULTICAST_DEFAULT, BUF_OFF1 ); rand_urandom( conf->node_id, SHA1_SIZE ); /* Arguments */ while( ( opt = getopt( argc, argv, "a:dhk:ln:p:P:qr:x:y:" ) ) != -1 ) { switch( opt ) { case 'a': conf->announce_port = str_safe_port( optarg ); break; case 'd': conf->mode = CONF_DAEMON; break; case 'h': conf_usage( argv[0] ); break; case 'k': #ifdef POLARSSL snprintf( conf->key, BUF_SIZE, "%s", optarg ); conf->bool_encryption = TRUE; #endif break; case 'l': snprintf( conf->bootstrap_node, BUF_SIZE, "%s", BOOTSTRAP_DEFAULT ); break; case 'n': sha1_hash( conf->node_id, optarg, strlen( optarg ) ); break; case 'p': conf->p2p_port = str_safe_port( optarg ); break; case 'P': conf->dns_port = str_safe_port( optarg ); break; case 'q': conf->verbosity = CONF_BEQUIET; break; case 'r': snprintf( conf->realm, BUF_SIZE, "%s", optarg ); conf->bool_realm = TRUE; break; case 'x': snprintf( conf->bootstrap_node, BUF_SIZE, "%s", optarg ); break; case 'y': conf->bootstrap_port = str_safe_port( optarg ); break; default: /* '?' */ conf_usage( argv[0] ); } } /* Get non-option values. */ for( i=optind; i<argc; i++ ) { hostname_put( argv[i], conf->node_id, conf->realm, conf->bool_realm ); } if( list_size( _main->hostname ) <= 0 ) { conf_usage( argv[0] ); } if( conf->p2p_port == 0 ) { fail( "Invalid P2P port number (-p)" ); } if( conf->dns_port == 0 ) { fail( "Invalid DNS port number (-P)" ); } if( conf->dns_port == conf->p2p_port ) { fail( "P2P port (-p) and DNS port (-P) must not be the same." ); } if( conf->bootstrap_port == 0 ) { fail( "Invalid bootstrap port number (-y)" ); } if( conf->announce_port == 0 ) { fail( "Invalid announce port number (-a)" ); } if( conf->cores < 1 || conf->cores > 128 ) { fail( "Invalid number of CPU cores" ); } return conf; }
void p2p_cron_find_random(void) { UCHAR node_id[SHA1_SIZE]; rand_urandom(node_id, SHA1_SIZE); p2p_cron_find(node_id); }