skt_ip_t skt_my_ip(void) { char hostname[1000]; skt_ip_t ip = _skt_invalid_ip; int ifcount = 0; #if CMK_HAS_GETIFADDRS /* Code snippet from Jens Alfke * http://lists.apple.com/archives/macnetworkprog/2008/May/msg00013.html */ struct ifaddrs *ifaces=0; if( getifaddrs(&ifaces) == 0 ) { struct ifaddrs *iface; for( iface=ifaces; iface; iface=iface->ifa_next ) { if( (iface->ifa_flags & IFF_UP) && ! (iface->ifa_flags & IFF_LOOPBACK) ) { const struct sockaddr_in *addr = (const struct sockaddr_in*)iface->ifa_addr; if( addr && addr->sin_family==AF_INET ) { ifcount ++; if ( ifcount==1 ) memcpy(&ip, &addr->sin_addr, sizeof(ip)); } } } freeifaddrs(ifaces); } /* fprintf(stderr, "My IP is %d.%d.%d.%d\n", ip.data[0],ip.data[1],ip.data[2],ip.data[3]); */ if (ifcount==1) return ip; #endif if (gethostname(hostname, 999)==0) { skt_ip_t ip2 = skt_lookup_ip(hostname); if ( ip2.data[0] != 127 ) return ip2; else if ( ifcount != 0 ) return ip; } return _skt_invalid_ip; }
/** Initiate an HTTP connection */ osl::http_connection::http_connection(std::string host_,network_progress &p_,int port,int timeout) :host(host_), p(p_), s(0) { p.status(1,"Looking up IP address for "+host); skt_ip_t hostIP=skt_lookup_ip(host.c_str()); p.status(1,"Connecting to "+host); s=skt_connect(hostIP,port,timeout); }
skt_ip_t skt_my_ip(void) { char hostname[1000]; if (!skt_inited) skt_init(); if (gethostname(hostname, 999)==0) return skt_lookup_ip(hostname); return _skt_invalid_ip; }
skt_ip_t skt_innode_lookup_ip(const char *name) { #if CMK_BPROC struct sockaddr_in addr; int len = sizeof(struct sockaddr_in); if (-1 == bproc_nodeaddr(atoi(name), &addr, &len)) { return _skt_invalid_ip; } else { skt_ip_t ret; memcpy(&ret,&addr.sin_addr.s_addr,sizeof(ret)); return ret; } #else return skt_lookup_ip(name); #endif }
unsigned char *download_url(const char *url_in, int verbose, int *length) { char *url=STRDUP(url_in); // parse the url char *protocol, *host, *path; int port; parse_url(url, &protocol, &host, &port, &path); if (verbose) printf("Connecting to URL: %s://%s:%d%s\n", protocol, host, port, path); int timeout = 60; if (verbose) printf("Looking up IP Address for: %s\n", host); skt_ip_t hostIP = skt_lookup_ip(host); if (verbose) printf("Connecting to %s\n", host); SOCKET s = skt_connect(hostIP, port, timeout); char *send_get = MALLOC(strlen(url)+128); sprintf(send_get, "GET %s HTTP/1.1\r\n" "Host: %s\r\n" "User-Agent: Mozilla/5.0\r\n" "\r\n", path, host); // send our get request skt_sendN(s, send_get, strlen(send_get)); // wait... if (verbose) printf("Waiting for a response from %s\n", host); char *status = skt_recv_line(s); if (verbose) printf("Received status: %s\n", status); //char *status_trim = trim_spaces(status); free(status); // now can get the response code //int code = atoi(status_trim); //free(status_trim); // a zero-length line indicates the end of the HTTP headers... we ignore int len=-1; while (1) { char *line = skt_recv_line(s); if (strlen(line)==0) break; if (strstr(line, "Content-Length") != 0) { char *l = line + strlen("Content-Length") + 2; len=atoi(l); } } if (verbose) printf("Content Length: %d\n", len); if (len==-1) { asfPrintWarning("No Content-Length specified in the HTTP headers.\n"); return NULL; } // receiving data... unsigned char *data = CALLOC(len+12, sizeof(char)); int curr=0, chunkSize=1024; while (1) { int amt = chunkSize < len-curr ? chunkSize : len-curr; skt_recvN(s,data+curr,amt); curr += amt; if (verbose) printf("Retrieved %d Kb so far.\n", curr); if (curr==len) break; else if (curr>len) asfPrintError("Invalid Content-Length?\n"); } if(verbose) printf("Done.\n"); //if (verbose) // printf("Received data:\n" // "-------------------------------------------------------------\n" // "%s\n" // "-------------------------------------------------------------\n", // data); free(protocol); free(host); free(path); free(url); free(send_get); skt_close(s); *length = len; return data; }