static int sim_init_net(char *netconf, FILE * out) { DEBUG("reading %s", netconf); if (read_netconf(netconf, out) < 0) return -1; if (connect_ports() < 0) return -2; if (set_default_port(NULL) < 0) return -3; return 0; }
int parse_as_url(char * in, url u) { char * result; char port_as_string[10]; int temp; // Get the first token, try to treat it as a protocol. result = strtok(in, ":/"); if (UTILS_DEBUG) printf("token is %s -- this should be the protocol\n", result); u -> protocol = parse_as_protocol(result); if (u -> protocol < 0) { printf("I'm sorry, I couldn't parse ""%s"" as a protocol.\n", result); return (-1); } else if (u -> protocol != HTTP) { printf("I'm sorry, only HTTP is supported in this client.\n"); return(-1); } if (UTILS_DEBUG) printf("u -> protocol is %d\n", u -> protocol); // Get the second token. See whether it has a : in it. // If so, treat it as a hostname and port; // If not, treat it as just a hostname. if (u -> hostname != NULL) free(u -> hostname); result = strtok(NULL, "/"); if (UTILS_DEBUG) printf("result is %s\n", result); if (result == NULL) { printf("Oops, ran out of URL to parse.\n"); return(-1); } // I'll look for a : using strcspn, because I am not sure what using // strtok or strstr would do to the secret magic index variable. C // string functions are weird that way. int charstilcolon = strcspn(result, ":"); int resultlength = strlen(result); if (UTILS_DEBUG) printf("token is %s -- this should include the port if present\n", result); if (charstilcolon == resultlength) // Just the hostname. { // Copy the hostname into the struct, and set port to default. if (UTILS_DEBUG) printf("rest of string is %s -- port not present\n", result); u -> hostname = (char *) malloc((resultlength+3) * sizeof(char)); strcpy(u -> hostname, result); // u -> hostname = strdup(result); if (UTILS_DEBUG) { printf("--------------\n"); pretty_print(u); printf("--------------\n"); } set_default_port(u); if (UTILS_DEBUG) printf("port number is %d (default)\n", u -> port); } else { // First put in the hostname... that's chars 0 through temp-1, // and slap a null after. if (UTILS_DEBUG) { printf("rest of string is %s -- port is present\n", result); printf("number of chars til colon is %d\n", charstilcolon); } u -> hostname = (char *) malloc( (charstilcolon+1) * sizeof(char)); strncpy(u -> hostname, result, charstilcolon); (u -> hostname)[charstilcolon] = '\0'; if (UTILS_DEBUG) printf("Allocated hostname ok, it is %s\n", u -> hostname); strcpy(port_as_string, result+charstilcolon+1); u -> port = atoi(port_as_string); if (UTILS_DEBUG) printf("port number is %d\n", u -> port); } // And everything else should be the path. if (UTILS_DEBUG) printf("1\n"); if (u -> path != NULL) { if (UTILS_DEBUG) printf("2\n"); // free(u -> path); } if (UTILS_DEBUG) printf("3\n"); result = strtok(NULL, ""); if (UTILS_DEBUG) printf("4\n"); if (result == NULL || strlen(u -> path) == 0) { if (UTILS_DEBUG) printf("5\n"); u -> path = (char *) malloc (4 * sizeof(char)); u -> path = "/"; } else { if (UTILS_DEBUG) printf("6\n"); char * foobar = strdup("/"); strcat(foobar, result); int fbl = strlen(foobar); if (UTILS_DEBUG) printf("foobar = %s, length is %d\n", foobar, fbl); u -> path = (char *) malloc ((fbl+3) * sizeof(char)); if (UTILS_DEBUG) printf("Allocated u -> path ok...\n"); strcpy(u -> path, foobar); } if (UTILS_DEBUG) printf("7\n"); return 0; }