Example #1
0
void hostSetName(hostState * hstate, char hname[], char replymsg[])
{
   /* Message to the manager */
   strcpy(replymsg, "Attempting to register name on DNS");
   /* Packet to DNS */
   packetBuffer temp;
   strcpy(temp.payload, hname);
   temp.type = 2; /* Should be 2 */
   temp.valid = 1;
   temp.srcaddr = hstate->physid;    
   temp.dstaddr = 100;/* Address of DNS */
   temp.length = strlen(hname);
   temp.payload[temp.length] = '\0'; 
   linkSend(&(hstate->linkout), &temp);
   hostReplySend(&(hstate->manLink), "DISPLAY",replymsg);
   
   strcpy(replymsg, ""); //Clear reply buffer
   packetBuffer rcv;
   hostInitRcvPacketBuff(&rcv);
   int size = 0;
   size = linkReceive(&(hstate->linkin), &rcv);
   while(rcv.type != 3 || rcv.valid != 1 || rcv.dstaddr != hstate->netaddr)   {
      size = linkReceive(&(hstate->linkin), &rcv);
   }

   if (rcv.flag == 1) {
    strcpy(replymsg, "Host name succesfully registered.");
   } else {
    strcpy(replymsg, "Host name failed to register.");
   }
   hostReplySend(&(hstate->manLink), "DISPLAY",replymsg);
}
Example #2
0
void hostReqAddr(hostState * hstate, char hname[], char replymsg[])
{
   /* Packet to DNS */
   packetBuffer temp;
   strcpy(temp.payload, hname);
   temp.type = 4; /* Should be 4 */
   temp.valid = 1;
   temp.srcaddr = hstate->physid;    
   temp.dstaddr = 100;/* Address of DNS */
   temp.length = strlen(hname);
   temp.payload[temp.length] = '\0'; 
   linkSend(&(hstate->linkout), &temp);
   
   strcpy(replymsg, ""); //Clear reply buffer
   packetBuffer rcv;
   hostInitRcvPacketBuff(&rcv);
   int size = 0;
   size = linkReceive(&(hstate->linkin), &rcv);
   while(rcv.type != 5 || rcv.valid != 1 || rcv.dstaddr != hstate->netaddr)   {
      size = linkReceive(&(hstate->linkin), &rcv);
   }

   char buff[20];
   buff[0] = '\0'; //init to blank
   int2Ascii(buff, rcv.dnsaddr);

   if (rcv.dnsaddr != 255) {
    strcpy(replymsg, "Host address is ");
    strcat(replymsg, " ");
    strcat(replymsg, buff);
   } else {
    strcpy(replymsg, "Name did not match any host registered on DNS Server \n");
   }
   hostReplySend(&(hstate->manLink), "DISPLAY",replymsg);
}
// main loop for the switch
void switchMain(struct switchState * sstate){
int i,j;
packetBuffer tempbuff;
	while(1){
		for(i = 0; i < NUMHOSTS; i++){
			linkReceive(&(sstate->linkin[i]),&tempbuff);
			if(tempbuff.valid != 0){
				for(j = 0; j < NUMHOSTS; j++){
					if(j != i) linkSend(&(sstate->linkout[j]), &tempbuff);
				}
			}
		}	
	}
}
Example #4
0
File: host.c Project: raegand/Lab8
/* 
 * Main loop of the host node
 *
 * It polls the manager connection for any requests from
 * the manager, and replies
 *
 * Then it polls any incoming links and downloads any
 * incoming packets to its receive packet buffer
 *
 * Then it sleeps for 10 milliseconds
 *
 * Then back to the top of the loop
 *
 */
void hostMain(hostState * hstate)
{
char buffer[1000]; /* The message from the manager */
char word[1000];
int  value;
char replymsg[1000];   /* Reply message to be displayed at the manager */
packetBuffer tmpbuff;
int length = 0; /* Size of string in pipe */

while(1) {
   /* Check if there is a command message from the manager */
   length = hostCommandReceive(&(hstate->manLink),buffer);

   if (length > 1) { /* Execute the manager's command */
      findWord(word, buffer, 1);
      if (strcmp(word, "SetNetAddr")==0) {
         findWord(word, buffer, 2); /* Find net address */
         value = ascii2Int(word);   /* Convert it to integer */
         hostSetNetAddr(hstate, value, replymsg);
         hostReplySend(&(hstate->manLink),"DISPLAY",replymsg);
      }
      else if (strcmp(word, "SetMainDir")==0) {
         findWord(word, buffer, 2); /* Find directory name */
         hostSetMainDir(hstate, word, replymsg);
         hostReplySend(&(hstate->manLink),"DISPLAY",replymsg);
      }
      else if (strcmp(word, "ClearRcvFlg")==0) {
         hostClearRcvFlg(hstate, replymsg);
         hostReplySend(&(hstate->manLink),"DISPLAY",replymsg);
      }
      else if (strcmp(word, "UploadPacket")==0) {
         findWord(word, buffer, 2); /* Find file name */
         hostUploadPacket(hstate, word, replymsg); 
         hostReplySend(&(hstate->manLink), "DISPLAY",replymsg);
      }
      else if (strcmp(word, "DownloadPacket")==0) {
         findWord(word, buffer, 2); /* Find file name */
         hostDownloadPacket(hstate, word, replymsg);
         hostReplySend(&(hstate->manLink), "DISPLAY",replymsg);
      }
      else if (strcmp(word, "GetHostState")==0) {
         hostGetHostState(hstate, &(hstate->manLink), replymsg);
         hostReplySend(&(hstate->manLink), "GetHostStateAck",replymsg);
      }
      else if (strcmp(word, "TransmitPacket")==0) {
		 hostInitTransmit(hstate, buffer, replymsg);
		 hostReplySend(&(hstate->manLink), "DISPLAY",replymsg);
      }
   } /* end of if */

   if (hstate->sendBuffer.busy == 1) {
	   hostTransmitPacket(hstate, replymsg);
   }


   /* Check if there is an incoming packet */
   length = linkReceive(&(hstate->linkin), &tmpbuff);

   /* 
    * If there is a packet and if the packet's destination address 
    * is the host's network address then store the packet in the
    * receive packet buffer
    */
   if (tmpbuff.dstaddr == hstate->netaddr && tmpbuff.valid == 1) {
	  /* if there is already something in the buffer; clear it */
	  if (tmpbuff.start == 1) {
		  memset(hstate->rcvBuffer.data, 0, sizeof(hstate->rcvBuffer.data));
		  hstate->rcvBuffer.length = 0;
		  hstate->rcvflag = 0;
	  }
	  strcat(hstate->rcvBuffer.data, tmpbuff.payload);
	  hstate->rcvBuffer.length += tmpbuff.length;
	  hstate->rcvBuffer.srcaddr = tmpbuff.srcaddr;
	  hstate->rcvBuffer.dstaddr = tmpbuff.dstaddr;
	  if (tmpbuff.end == 1) {
		  hstate->rcvflag = 1;
		  hstate->rcvBuffer.valid = 1;
	  }
   }

   /* The host goes to sleep for 10 ms */
   usleep(TENMILLISEC);

} /* End of while loop */

}