Exemple #1
0
/*
This will send hello message to the beacon node and intiated the connection.
*/
int sendHelloBeacon(Node* nei){	
	struct sockaddr_in serv_addr;
	memset(&serv_addr, 0, sizeof(serv_addr));
	serv_addr.sin_family = AF_INET;
	serv_addr.sin_addr.s_addr = htonl(inet_addr(getIPAddr(nei->hostname)));
	serv_addr.sin_port = htons(nei->port);
	memset(&(serv_addr.sin_zero), 0, 8);
	Message* hello = (Message*)malloc(sizeof(Message));
	prepareHello(hello);
	//this is to make sure that port is available for reuse after server shutdown
	for(;;){
		if(nei->terminate || isShutdown || softshutdown){
			return -1;
		}
		Node * other = findNode(nei, conhead);
		if((other != NULL) && (other->isHelloDone == 1)){			
			return -1;
		}		
		nei->sock_id = socket(AF_INET, SOCK_STREAM, 0);
		if(connect(nei->sock_id, (struct sockaddr*) &serv_addr, sizeof(serv_addr)) >= 0){
			if(sendMessage(nei->sock_id, hello, nei->nodeId) == 0){
				nei->isHelloDone = 1;
				break;
			}
		}
		//printf("sleeping %s\n", nei->nodeId);
		sleep(retry);		
	}	
	return 0;
}
Exemple #2
0
/**
 * Handles changes in the IP-address field.
 */
void MainWindow::E_IPAddr_textChanged(){
    if(getIPAddr()){
        ui_->PB_ON->setEnabled(1);
    }else{
        ui_->PB_ON->setEnabled(0);
    }
}
Exemple #3
0
/**
 * Sets availability of controls when TCP protocol is chosen
 */
void MainWindow::RB_TCP_clicked(){
    ui_->E_IPAddr->setEnabled(1);
    ui_->CB_CWR->setEnabled(1);
    ui_->CB_ECN->setEnabled(1);
    ui_->CB_URG->setEnabled(1);
    ui_->CB_ACK->setEnabled(1);
    ui_->CB_PSH->setEnabled(1);
    ui_->CB_RST->setEnabled(1);
    ui_->CB_SYN->setEnabled(1);
    ui_->CB_FIN->setEnabled(1);
    if(getIPAddr()){
        ui_->PB_ON->setEnabled(1);
    }else{
        ui_->PB_ON->setEnabled(0);
    }
}
/* This function searches for the resource(sid:uri) in the ResourceList.
 * If the Resource is found in the list then it returns 0 else insert
 * the resource to front of the list and returns 1.
 */
int insertResource(const char * sid, char const * uri,
            const OCClientResponse * clientResponse)
{
    ResourceNode * iter = resourceList;

    //Checking if the resource(sid:uri) is new
    while(iter)
    {
        if((strcmp(iter->sid, sid) == 0) && (strcmp(iter->uri, uri) == 0))
        {
            return 0;
        }
        else
        {
            iter = iter->next;
        }
    }

    //Creating new ResourceNode
    if((iter = (ResourceNode *) OICMalloc(sizeof(ResourceNode))))
    {
        iter->sid = sid;
        iter->uri = uri;
        iter->ip = getIPAddr(clientResponse);
        iter->port = getPort(clientResponse);
        iter->connType = clientResponse->connType;
        iter->next = NULL;
    }
    else
    {
        OC_LOG(ERROR, TAG, "Memory not allocated to ResourceNode");
        return -1;
    }

    //Adding new ResourceNode to front of the ResourceList
    if(!resourceList)
    {
        resourceList = iter;
    }
    else
    {
        iter->next = resourceList;
        resourceList = iter;
    }
    return 1;
}
Exemple #5
0
/*
This will send hello message to the non beacon node and intiated the connection.
*/
int sendHelloNonBeacon(Node* nei){	
	struct sockaddr_in serv_addr;
	memset(&serv_addr, 0, sizeof(serv_addr));
	serv_addr.sin_family = AF_INET;
	serv_addr.sin_addr.s_addr = htonl(inet_addr(getIPAddr(nei->hostname)));
	serv_addr.sin_port = htons(nei->port);
	memset(&(serv_addr.sin_zero), 0, 8);
	Message* hello = (Message*)malloc(sizeof(Message));
	prepareHello(hello);
	//sending hello	
	nei->sock_id = socket(AF_INET, SOCK_STREAM, 0);
	if(connect(nei->sock_id, (struct sockaddr*) &serv_addr, sizeof(serv_addr)) >= 0){					
		if(sendMessage(nei->sock_id, hello, nei->nodeId) == 0){
			nei->isHelloDone = 1;			
			return 0;
		}
	}				
	return -1;
}
int main(){
	int opt;						//for setsockopt
	struct sockaddr_in server_addr, client_addr;		//declare structure sockaddr_in of server, client 	
	int socket_fd, client_fd;				//declare socket descripter
	int len;						//message length 
	char req_buf[BUFFSIZE], res_buf[BUFFSIZE];		//message buffer				
	int portnum=80;						//port number

	char* IPAddr;						//IPaddress
	char host[100];
	char URL[500];						//URL

	pid_t pid;						//pid varialbe for fork		
	int hash_fd;						//file descriptor of hashed file
	int semid;
	
	semid=initialize_sem();					//set semaphore initializial state
	
	if((socket_fd = socket(PF_INET, SOCK_STREAM, 0)) <0){	//create socket about server part
		printf("Server : Can't open stream socket.");	//if can't create, print error message
		return 0;
	}		  
	
	bzero((char *)&server_addr, sizeof(server_addr));	//initialize s_server_addr
	server_addr.sin_family=AF_INET;				//set variables of s_server_addr
	
	
	

	///////SET SERVER ADDRESS!!
	server_addr.sin_addr.s_addr=inet_addr("128.134.52.60");	
	//set address of proxy_server




	server_addr.sin_port=htons(PORTNO);			//set portnumber of my process
	
	opt=1;
	setsockopt(socket_fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));//set socket option
	
	//bind s_server_addr and s_socket
	if(bind(socket_fd, (struct sockaddr *)&server_addr, sizeof(server_addr))<0){
		printf("Server : Can't bind local address.\n");		//if bind error , print message
		return 0;
	}
	
	listen(socket_fd, 5);						//change proxy_server state to wait connection
		
	if(signal(SIGCHLD, sig_handler)==SIG_ERR)					//**signal for SIGCHLD	
		printf("SIGNAL ERROR\n");
	if(signal(SIGALRM, sig_handler)==SIG_ERR)					//**signal for SIGALRM
		printf("SIGNAL ERROR\n");

	while(1){	
		
		//length of client_addr
		len=sizeof(client_addr);	
		
		//accept connection request of client(WEB-BROWSER)
		client_fd = accept(socket_fd, (struct sockaddr*)&client_addr, &len);	
		if(client_fd<0) {					//if connection fails, print error meesage
			printf("Server : accept failed.\n");
		}
		
		//**create child process
		pid=fork();		
	
		if(pid<0)		//if fork error
			fprintf(stderr, "fork error\n");
	
		else if(pid==0)		//Child process
		{	
			bzero(res_buf, BUFFSIZE);		//initialize buffer
			bzero(req_buf, BUFFSIZE);
			printf("===========================================\n");
			printf("PID : %d\n",getpid());			//print child process id 
			read(client_fd, req_buf, BUFFSIZE);		//read request message of client
			
			getURL(req_buf, URL);
			portnum=getHostname_AND_portnum(req_buf, host);
			IPAddr=getIPAddr(host);				//hostname swap into IPaddress
			
			printf("* request :\n%s\n",req_buf); 		//print request message 
			printf("* host name(%d) : %s\n",getpid(),host);	//print host name
			printf("* IP address : %s\n", IPAddr);		//print IPaddress

			//hash_path is hashed file of URL
			proxy_cache(URL, hash_path, semid);		//check whether HIT or MISS AND get hash_path

			
			hash_fd=open(hash_path, O_RDWR);		//open file of hash_path
			
			if(hash_fd==-1)					//if file descriptor of hash_path is -1, that file doesn't exist
				printf("* ProxyCache(%d) : MISS\n",getpid());		//Thus MISS
			else{						//else that file exist	
				printf("* ProxyCache(%d) : HIT\n",getpid());		//Thus HIT
				close(hash_fd);				//close file descriptor of hash_path
			}


			if(hash_fd==-1)					//MISS
				connect_webserver(IPAddr, portnum, req_buf, URL, semid);
				//proxy server as clinet is connected to web-server

			hash_fd=open(hash_path, O_RDONLY);		//hasehd file open in proxy_cache

                        while((len=read(hash_fd, res_buf, BUFFSIZE))>0)	//read Response message from file
                        {
                                write(client_fd, res_buf, len);		//write response message to client
                        	bzero(res_buf, BUFFSIZE);		//initialize
                        }  
			close(client_fd);				//close descriptor
			close(hash_fd);
			return 0;
		}	
	
	
	}
					

	return 0;
}