static struct chirp_client * connect_to_host( const char *host, time_t stoptime ) { struct chirp_client *c; if(!table) { table = hash_table_create(0,0); if(!table) return 0; } c = hash_table_lookup(table,host); if(c) return c; if(!strncmp(host,"CONDOR",6)) { c = chirp_client_connect_condor(stoptime); } else { c = chirp_client_connect(host,1,stoptime); } if(c) { /* If a default replication factor was set earlier, then it must be re-applied when re-connecting after a failure. */ if(chirp_reli_default_nreps>0) { chirp_client_setrep(c,"@@@",chirp_reli_default_nreps,stoptime); } hash_table_insert(table,host,c); return c; } else { return 0; } }
struct chirp_stream *chirp_stream_open(const char *hostport, const char *path, chirp_stream_mode_t mode, time_t stoptime) { struct chirp_stream *stream; struct chirp_client *client; int result; client = chirp_client_connect(hostport, 1, stoptime); if(!client) return 0; if(mode == CHIRP_STREAM_READ) { result = chirp_client_getstream(client, path, stoptime); } else { result = chirp_client_putstream(client, path, stoptime); } if(result >= 0) { stream = malloc(sizeof(*stream)); stream->client = client; stream->mode = mode; stream->buffer_length = 4096; stream->buffer_valid = 0; stream->buffer_position = 0; stream->buffer = malloc(stream->buffer_length); return stream; } else { chirp_client_disconnect(client); return 0; } }
struct chirp_client * chirp_client_connect_starter() { FILE *file; int fields; struct chirp_client *client; char *default_filename; char host[CONDOR_HOSTNAME_MAX]; char cookie[CHIRP_LINE_MAX]; MyString path; int port; int result; const char *dir; if ((default_filename = getenv("_CONDOR_CHIRP_CONFIG"))) { path.formatstr( "%s", default_filename ); } else { if (NULL == (dir = getenv("_CONDOR_SCRATCH_DIR"))) { dir = "."; } path.formatstr( "%s%c%s",dir,DIR_DELIM_CHAR,".chirp.config"); } file = safe_fopen_wrapper_follow(path.Value(),"r"); if(!file) { fprintf(stderr, "Can't open %s file\n",path.Value()); return 0; } fields = fscanf(file,"%s %d %s",host,&port,cookie); fclose(file); if(fields!=3) { errno = EINVAL; return 0; } client = chirp_client_connect(host,port); if(!client) return 0; result = chirp_client_cookie(client,cookie); if(result!=0) { DISCONNECT_AND_RETURN(client, 0); } return client; }
DLLEXPORT struct chirp_client * chirp_client_connect_default() { FILE *file; int fields; int save_errno; struct chirp_client *client; char *default_filename; char host[CHIRP_LINE_MAX]; char cookie[CHIRP_LINE_MAX]; int port; int result; if (!(default_filename = getenv("_CONDOR_CHIRP_CONFIG"))) { default_filename = ".chirp.config"; } file = fopen(default_filename,"r"); if(!file) return 0; fields = fscanf(file,"%s %d %s",host,&port,cookie); fclose(file); if(fields!=3) { errno = EINVAL; return 0; } client = chirp_client_connect(host,port); if(!client) return 0; result = chirp_client_cookie(client,cookie); if(result!=0) { save_errno = errno; chirp_client_disconnect(client); errno = save_errno; return 0; } return client; }
struct chirp_client *chirp_client_connect_condor(time_t stoptime) { FILE *file; int fields; int save_errno; struct chirp_client *client; char host[CHIRP_LINE_MAX]; char hostport[CHIRP_LINE_MAX]; char cookie[CHIRP_LINE_MAX]; int port; int result; file = fopen("chirp.config", "r"); if(!file) return 0; fields = fscanf(file, "%s %d %s", host, &port, cookie); fclose(file); if(fields != 3) { errno = EINVAL; return 0; } sprintf(hostport, "%s:%d", host, port); client = chirp_client_connect(hostport, 0, stoptime); if(!client) return 0; result = chirp_client_cookie(client, cookie, stoptime); if(result != 0) { save_errno = errno; chirp_client_disconnect(client); errno = save_errno; return 0; } return client; }
DLLEXPORT struct chirp_client * chirp_client_connect_url( const char *url, const char **path_part) { struct chirp_client *client; char const *str; char *host = NULL; int port = 0; if(strncmp(url,"chirp:",6)) { //bare file name *path_part = url; return chirp_client_connect_default(); } url += 6; // "chirp:" if(*url != '/' && *url != '\\' && *url != ';' && *url != '.' \ && (str = strchr(url,':'))) { char *end; port = strtol(str+1,&end,10); if(port && end > str+1 && (*end == '\0' || *end == '/' || *end == '\\' || *end == '.' || *end == ';')) { //URL form chirp:host.name:port... //Note that we try to avoid getting here on a "sloppy" //relative path that happens to contain a ':' but //which is not followed by a valid port/path. host = (char *)malloc(str-url+1); if ( ! host) { errno = ENOMEM; return NULL; } strncpy(host,url,str-url); host[str-url] = '\0'; url = end; } } while(*url == ';') { //parse connection parameters char param[CHIRP_LINE_MAX]; char value[CHIRP_LINE_MAX]; url = read_url_param(++url,param,sizeof(param)); if(!url) { errno = EINVAL; if (host) free(host); return NULL; } if(*url == '=') { url = read_url_param(++url,value,sizeof(value)); if(!url) { errno = EINVAL; if (host) free(host); return NULL; } } else *value = '\0'; //No connection parameters are defined at this time! //Handle them here when they are defined. } *path_part = url; if(!host) { //URL must be in form 'chirp:path' client = chirp_client_connect_default(); } else { client = chirp_client_connect(host,port); } free(host); return client; }