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); }
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); }
/* * hostTransmitPacket will transmit a packet in the send packet buffer */ void hostTransmitPacket(hostState * hstate, char replymsg[]) { int i = 0; int length = hstate->sendBuffer.length - hstate->sendBuffer.pos; int error = 0; if (length > PAYLOAD_LENGTH) { length = PAYLOAD_LENGTH; } /* * Set up the send packet buffer's source and destination addresses */ hstate->sendPacketBuff.dstaddr = hstate->sendBuffer.dstaddr; hstate->sendPacketBuff.srcaddr = hstate->netaddr; hstate->sendPacketBuff.length = length; hstate->sendPacketBuff.type = 0; hstate->sendPacketBuff.end = 0; hstate->sendPacketBuff.start = 0; hstate->sendPacketBuff.root = 0; hstate->sendPacketBuff.distance = 0; if (hstate->sendBuffer.pos == 0) { hstate->sendPacketBuff.start = 1; } if (hstate->sendBuffer.valid) { hstate->sendPacketBuff.valid = 1; } for (i = 0; i < length; i++) { hstate->sendPacketBuff.payload[i] = hstate->sendBuffer.data[i+hstate->sendBuffer.pos]; } hstate->sendBuffer.pos += length; if (hstate->sendBuffer.pos >= hstate->sendBuffer.length) { hstate->sendBuffer.pos = 0; hstate->sendBuffer.busy = 0; hstate->sendPacketBuff.end = 1; } /* Transmit the packet on the link */ error = linkSend(&(hstate->linkout), &(hstate->sendPacketBuff)); if (error == -1) { strcpy(replymsg,"Error: Could not send packet, aborting transmit"); hostInitDataBuffer(&(hstate->sendBuffer)); return; } if (hstate->sendPacketBuff.end == 1) { strcpy(replymsg,"Final Packet Sent"); } else { /* Message to be sent back to the manager */ strcpy(replymsg,"Packet sent"); } }
// 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); } } } } }
void hostRequestFile(hostState * hstate, char filename[], int addr) { packetBuffer temp; strcpy(temp.payload, filename); temp.type = 6; temp.valid = 1; temp.srcaddr = hstate->physid; temp.dstaddr = addr; temp.length = strlen(filename); temp.payload[temp.length] = '\0'; linkSend(&(hstate->linkout), &temp); char reply[1000]; strcpy(reply, "File request sent..\n"); hostReplySend(&(hstate->manLink), "DISPLAY", reply); }
void hostSilentTransmitPacket(hostState * hstate) { int i = 0; int length = hstate->sendBuffer.length - hstate->sendBuffer.pos; int error = 0; if (length > PAYLOAD_LENGTH) { length = PAYLOAD_LENGTH; } hstate->sendPacketBuff.dstaddr = hstate->sendBuffer.dstaddr; hstate->sendPacketBuff.srcaddr = hstate->netaddr; hstate->sendPacketBuff.length = length; hstate->sendPacketBuff.type = 0; hstate->sendPacketBuff.end = 0; hstate->sendPacketBuff.start = 0; hstate->sendPacketBuff.root = 0; hstate->sendPacketBuff.distance = 0; if (hstate->sendBuffer.pos == 0) { hstate->sendPacketBuff.start = 1; } if (hstate->sendBuffer.valid) { hstate->sendPacketBuff.valid = 1; } for (i = 0; i < length; i++) { hstate->sendPacketBuff.payload[i] = hstate->sendBuffer.data[i+hstate->sendBuffer.pos]; } hstate->sendBuffer.pos += length; if (hstate->sendBuffer.pos >= hstate->sendBuffer.length) { hstate->sendBuffer.pos = 0; hstate->sendBuffer.busy = 0; hstate->sendPacketBuff.end = 1; } error = linkSend(&(hstate->linkout), &(hstate->sendPacketBuff)); if (error == -1) { hostInitDataBuffer(&(hstate->sendBuffer)); return; } }