Example #1
0
int main(int argc, char *argv[])
{
    if (argc<2) {
      printf(" Use: %s <network interface>\ne.g.: %s eth0\n",argv[0],argv[0]
      );
      exit(-1);
    }
    
    char host[NI_MAXHOST]; strcpy(host,"<<<NOT FOUND>>>");
    getIPaddress(argv[1], host);
    printf("%s\n",host);
    
    exit(EXIT_SUCCESS);
}
Example #2
0
//{{{ liberation
void liberation(void* arg) {
	pthread_detach(pthread_self());
	msg_t msg;

	if (this_problem.w1 < this_problem.w2) {
		int tmp = this_problem.w1;
		this_problem.w1 = this_problem.w2;
		this_problem.w2 = tmp;
	}
	// no link with msg attribute names
	msg._typ_mes = 15;
	strncpy(msg._sender,getIPaddress(),16);
	msg._nb_access = this_problem.w1;
	msg._has_next = this_problem.w2;
	strncpy(msg._ip,this_site.resource,16);

	sendMessageWithAdd(msg);

	this_problem.processed = 0;
	this_problem.sent = 1;
	this_problem.w1 = 0;
	this_problem.w2 = 0;

	state = IDLE;
	if(next != -1) {
		if (_verbose) fprintf (stdout, "Sending TOKEN.\n");
		msg_t mes;
		type(mes) = TOKEN;
		if(sendMessage(next, mes) == -1) 
			if (_verbose) fprintf(stderr, "======> Error while sending message <====== \n");
		next = -1;
		tokenPresent = 0;
		position = -1;
	}
	else
		last = -1;
	ch_pid = 0;
	if (_verbose) fprintf(stdout, "CS released : %d access\n", ++acces);
}
int main(int argc , char *argv[])
{
	if(argc < 3)
	{
		fprintf(stdout,"Must have 3 command arguments\n");
		return FAILURE;
	}
	
	sockaddr_in server,host_server;
	int serverfd,hostfd,clientfd,c,reuse;
	
	
	//Define host_server attributes
	host_server.sin_family = AF_INET;
	host_server.sin_port = htons(80);
	
	char request[SIZE];
	
	//create socket
	serverfd = socket(AF_INET,SOCK_STREAM,IPPROTO_IP);
	
	//initialise and define server attributes
	server.sin_family = AF_INET;
	server.sin_port = htons(atoi(argv[2]));
	server.sin_addr.s_addr = inet_addr(argv[1]);

	
	c = sizeof(struct sockaddr_in);
	
	if(serverfd == -1)
	{
		puts("Error creating socket");
		return -1;
	}
	
	puts("Socket created");
	
	setsockopt(serverfd,SOL_SOCKET,SO_REUSEADDR,(char*)&reuse,sizeof(int));
		
	if(bind(serverfd,(struct sockaddr *)&server,sizeof(server)) < 0)
	{
		puts("Error binding socket");
		return -1;
	}
	
	puts("Socket bound");
	
	
	while(true)
	{	
		listen(serverfd,20);
		clientfd = accept(serverfd,(struct sockaddr*)&server,(socklen_t*)&c);
		
		
		if(clientfd != -1)
			puts("Attending to client.");
		
		recv(clientfd,&request[0],SIZE,0);
		//if(strstr(&request[0],"https://"))
		
		//Decide what request it is...
		
		puts("Done");
		
		
		char web_url[510];
		memset(&web_url[0],0,510);
		puts(&request[0]);
		char *pos = strstr(&request[0],"Host");
		sscanf(pos,"Host:%s\r\n",&web_url[0]);
		
		char *line = strdup(&web_url[0]);
		char *token;
		char temp[255];
		if(token = strstr(line,":443"))
		{
			token = strtok(line,":");
		}
		
		if(!strstr(token,"www."))
		{
			
			strcpy(&temp[0],"www.");
			strcat(&temp[0],token);
			token = &temp[0];
		}
		printf("line ------------------> %s\n",token);
		
		
		char *ip = getIPaddress(token);
		puts(ip);
		
		host_server.sin_addr.s_addr = inet_addr(ip);
		
		hostfd = socket(AF_INET,SOCK_STREAM,IPPROTO_IP);
		
		if(connect(hostfd,(struct sockaddr*)&host_server,sizeof(host_server))< 0)
		{
			puts("Failed to create host connection");
			return -1;
		}
		pid_t pid = fork();
		if(pid == 0){
			char t1[3],t2[510],t3[20];
			
			sscanf(&request[0],"%s %s %s\r\n",&t1[0],&t2[0],&t3[0]);
			if(strcmp("GET",&t1[0]) && strcmp("http://",&t2[0]) && (strcmp("HTTP/1.0",&t3[0])||strcmp("HTTP/1.1",&t3[0])))
			{
				char mod_request[510];
				sprintf(&mod_request[0],"%s %s %s\r\nHost: %s\r\nConnection: close\r\n\r\n",&t1[0],&t2[0],&t3[0],token);
				printf("MOD-Request ---------->  %s\n",&mod_request[0]);
				send(hostfd,&mod_request[0],SIZE,0);
				char reply[SIZE];
				memset(&reply[0],0,SIZE);
				int n ;
				while(recv(hostfd,&reply[0],SIZE,0) > 0)
				{
					puts(&reply[0]);
					send(clientfd,&reply[0],SIZE),0);
					memset(&reply[0],0,SIZE);
				}
			}
		close(hostfd);
		close(clientfd);
		_exit(0);
		}