Example #1
0
/* send_request() - Contains the principal code the for the program.
 * The function creates a HTTP GET string to send to the server
 * and returns everything on a file descriptor to pass it to main().
 */
int send_request(char *dns, char *port){

	int sockfd, bindfd;					 // socket and bind file descriptors
        char *ptr, *host, path[100];
	char getrequest[1024];
        struct addrinfo hints, *res;

	memset(&hints, 0, sizeof hints);
	hints.ai_family = AF_UNSPEC;
	hints.ai_socktype = SOCK_STREAM;
		printf("1%s\n",dns );
	// conditions to differentiate between an IP address and a hostname
        if ( isvalidip(dns) ) 
        { // when an IP is found then no paths are specified
        	printf("2%s\n",dns );
		sprintf(getrequest, "GET / HTTP/1.0\nHOST: %s\n\n", dns); // create a get request only for the IP
        } else { // if it's not an IP, then it should be a DNS name
		if ( (ptr = strstr(dns, "/")) == NULL) { // if the DNS name does not contain a slash at the end
			printf("3%s\n",dns );
			 sprintf(getrequest, "GET / HTTP/1.0\nHOST: %s\n\n", dns); // we send a request only for the dns name
			}
		else {  // it there is a slash right after the DNS name then there is path to a filename
			strcpy(path, ptr);
        		host = strtok(dns, "/"); //truncate the argument to a PATH and DNS name
			sprintf(getrequest, "GET %s HTTP/1.0\nHOST: %s\n\n", path, dns);
			printf("4%s %s\n",dns,path );
		}
	}
	// gets linked list of results of a specified hostname
	if ( getaddrinfo(dns, port, &hints, &res) != 0 ){
		fprintf(stderr, "Host or IP not valid\n"); //quits program if the hostname was not found
		exit(0);
		}


	// creates a socket from hostname results and passes everything to a file descriptor
	sockfd = socket(res->ai_family, res->ai_socktype, res->ai_protocol);

	// bind the socket to the port passed to getaddrinfo()
        bindfd = bind(sockfd, res->ai_addr, res->ai_addrlen);

	// establish a connection and quits if there is a connection error
	if ( connect(sockfd, res->ai_addr, res->ai_addrlen) != 0 ){
		fprintf(stderr, "Connection error\n");
		exit(0);
		}

        int optval = 1; //is 
        setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof optval);

	// writes the HTTP request to the socked file descriptor
	write(sockfd, getrequest, strlen(getrequest));

return sockfd; //returns the results to main
}
Example #2
0
void
configip(int bargc, char **bargv, int needfs)
{
	Waitmsg *w;
	int argc, pid;
	char **arg, **argv, buf[32], *p;

	fmtinstall('I', eipfmt);
	fmtinstall('M', eipfmt);
	fmtinstall('E', eipfmt);

	arg = malloc((bargc+1) * sizeof(char*));
	if(arg == nil)
		fatal("malloc");
	memmove(arg, bargv, bargc * sizeof(char*));
	arg[bargc] = 0;

	argc = bargc;
	argv = arg;
	strcpy(mpoint, "/net");
	ARGBEGIN {
	case 'x':
		p = ARGF();
		if(p != nil)
			snprint(mpoint, sizeof(mpoint), "/net%s", p);
		break;
	case 'g':
	case 'b':
	case 'h':
	case 'm':
		p = ARGF();
		USED(p);
		break;
	} ARGEND;

	/* bind in an ip interface */
	if(bind("#I", mpoint, MAFTER) < 0)
		fatal("bind #I");
	if(access("#l0", 0) == 0 && bind("#l0", mpoint, MAFTER) < 0)
		warning("bind #l0");
	if(access("#l1", 0) == 0 && bind("#l1", mpoint, MAFTER) < 0)
		warning("bind #l1");
	if(access("#l2", 0) == 0 && bind("#l2", mpoint, MAFTER) < 0)
		warning("bind #l2");
	if(access("#l3", 0) == 0 && bind("#l3", mpoint, MAFTER) < 0)
		warning("bind #l3");
	werrstr("");

	/* let ipconfig configure the ip interface */
	switch(pid = fork()){
	case -1:
		fatal("fork configuring ip: %r");
	case 0:
		exec("/boot/ipconfig", arg);
		fatal("execing /boot/ipconfig: %r");
	default:
		break;
	}

	/* wait for ipconfig to finish */
	if(debugboot)
		fprint(2, "waiting for dhcp...");
	for(;;){
		w = wait();
		if(w != nil && w->pid == pid){
			if(w->msg[0] != 0)
				fatal(w->msg);
			free(w);
			break;
		} else if(w == nil)
			fatal("configuring ip");
		free(w);
	}
	if(debugboot)
		fprint(2, "\n");

	if(!needfs)
		return;

	/* if we didn't get a file and auth server, query user */
	netndb("fs", fsip);
	if(!isvalidip(fsip))
		netenv("fs", fsip);
	while(!isvalidip(fsip)){
		buf[0] = 0;
		outin("filesystem IP address", buf, sizeof(buf));
		if (parseip(fsip, buf) == -1)
			fprint(2, "configip: can't parse fs ip %s\n", buf);
	}

	netndb("auth", auip);
	if(!isvalidip(auip))
		netenv("auth", auip);
	while(!isvalidip(auip)){
		buf[0] = 0;
		outin("authentication server IP address", buf, sizeof(buf));
		if (parseip(auip, buf) == -1)
			fprint(2, "configip: can't parse auth ip %s\n", buf);
	}
}
Example #3
0
File: cs.c Project: brho/akaros
/*
 *  get the system name
 */
static void ipid(void)
{
	uint8_t addr[6];
	struct ndbtuple *t, *tt;
	char *p, *attr;
	struct ndbs s;
	int f;
	char buf[Maxpath];

	/* use environment, ether addr, or ipaddr to get system name */
	if (mysysname == 0) {
		/*
		 *  environment has priority.
		 *
		 *  on the sgi power the default system name
		 *  is the ip address.  ignore that.
		 *
		 */
		p = getenv("sysname");
		if (p && *p) {
			attr = ipattr(p);
			if (strcmp(attr, "ip") != 0)
				mysysname = strdup(p);
		}

		/*
		 *  the /net/ndb contains what the network
		 *  figured out from DHCP.  use that name if
		 *  there is one.
		 */
		if (mysysname == 0 && netdb != NULL) {
			ndbreopen(netdb);
			for (tt = t = ndbparse(netdb); t != NULL; t = t->entry)
			{
				if (strcmp(t->attr, "sys") == 0) {
					mysysname = strdup(t->val);
					break;
				}
			}
			ndbfree(tt);
		}

		/* next network database, ip address, and ether address to find
		 * a name
		 */
		if (mysysname == 0) {
			t = NULL;
			if (isvalidip(ipa))
				free(ndbgetvalue(db, &s, "ip", ipaddr, "sys",
						 &t));
			if (t == NULL) {
				for (f = 0; f < 3; f++) {
					snprintf(buf, sizeof(buf), "%s/ether%d",
						 mntpt, f);
					if (myetheraddr(addr, buf) < 0)
						continue;
					snprintf(eaddr, sizeof(eaddr), "%E",
						 addr);
					free(ndbgetvalue(db, &s, "ether", eaddr,
							 "sys", &t));
					if (t != NULL)
						break;
				}
			}
			for (tt = t; tt != NULL; tt = tt->entry) {
				if (strcmp(tt->attr, "sys") == 0) {
					mysysname = strdup(tt->val);
					break;
				}
			}
			ndbfree(t);
		}

		/* nothing else worked, use the ip address */
		if (mysysname == 0 && isvalidip(ipa))
			mysysname = strdup(ipaddr);

		/* set /dev/sysname if we now know it */
		if (mysysname) {
			f = open("/dev/sysname", O_RDWR);
			if (f >= 0) {
				write(f, mysysname, strlen(mysysname));
				close(f);
			}
		}
	}
}