#include #include #include #include #include "functions.h" typedef char PACKET[26]; int packetCreate(int dest, int from, char *data, int pkt, int totalpackets, char *output) { int y = 0; output[0] = '{'; // Open Packet output[1] = 1; // Destination address output[2] = 0; // Sender Address output[3] = 'D'; // Packet Type for(y = 4; y <= 13; y++) { output[y] = 0; } output[14] = 0; // Packet chksum (value added when 14th byte = 0 output[15] = '}'; // Total packets for message! output[16] = '['; output[17] = pkt; output[18] = totalpackets; output[19] = ']'; stringcopy(data, output, 4, 10); //printf("data = %s, txPkt = %s \n", data, output); //getchar(); return pkt; } int packetSplit(char *message, char (*pktStore)[20], int dest, int sender) { char txPkt[20]; /** Somewhere to make our 20 byte packet **/ char pktData[10]; /** Somewhere to place selected packet data **/ int i,k; /** Basic incrementing integers **/ int pktCount = 0; /** Counts the number of packets created, during the process **/ int pktRem = 0; /** The number of packets remaining for the current message **/ int totalPkts = 0; /** Number of packets used for the message **/ int dataSize = 0; /** Used to calculate the length of the message **/ /********* Count size of the message! & Assess packet requirements ********/ dataSize = count(message); /** Counts the length of the message **/ pktRem = dataSize / 10; /** Divide data into segments of 10 **/ //printf("datasize = %d remainder / 10 = %d", dataSize, (dataSize % 10)); //getchar(); if((dataSize % 10) > 0) /** if a remainder exists add an extra packet requirement **/ { pktRem++; } totalPkts = pktRem - 1; /** -1 because packet count includes 0! Separate store reqd **/ while(pktRem > 0) /** Process entire packet requirement until no more are needed **/ { printf("PktCount = %d\n", pktCount); getchar(); packetPrep(message, &pktData, (pktCount * 10), 10); /** Prepare the packet! **/ packetCreate(dest,sender,&pktData,pktCount,totalPkts,&txPkt); /** Create the final packet! **/ checksum(&txPkt); /** Add checksum data to final packet, stored in txPkt **/ printf("Packet = %s\n", pktData); getchar(); /** Copy the newly created packet into packet storage for current message **/ for(k = 0; k < 20 ; k++) { pktStore[pktCount][k] = txPkt[k]; } /** Packet created so increase pktCount **/ pktCount++; /** And therefore decrease the pktRem requirement **/ pktRem--; printf("Message = %s",message); } return 1; } int main(void) { char txPkt[20]; //17 bytes because of buffer overflow problems (this will be traced later) char input[255] = "Hello world! This message is longer than 10 bytes!"; // Our data is 20 bytes long, must be split into 2 packets! char newpkt[10]; char packets[26][20]; int i,k; int pktcount = 0; int pkts = 0; int totalpkts = 0; int datasize; //printf("Please Enter a message (max 255 chars long) & press ENTER:\n\n"); //scanf(input, "%s"); /**** Count Input data length and decide how many packets we need! ****/ datasize = count(input); //count input data pkts = datasize / 10; //divide by 10 printf("datasize = %d remainder / 10 = %d", datasize, (datasize % 10)); getchar(); if((datasize % 10) > 0) //if a remainder add an extra packet requirement { pkts++; } totalpkts = pkts - 1; // -1 because packet count includes 0! //int packetSplit(char *message, char *pktStore, int dest, int sender) packetSplit(&input, &packets, 1, 0); printf("packet 1: %x\n\n", packets[0]); //display packets //displayPkt(&packets); printf("Easy read pKts!:\n"); for(k = 0; k < pktcount; k++) { printf("Packet %d: %c ",k,packets[k][0]); for(i = 1; i < 3; i++) { printf(" %d ",packets[k][i]); } printf(" %c ",packets[k][3]); for(i = 4; i < 14; i++) { printf("%c",packets[k][i]); } for(i = 14; i < 15; i++) { printf(" %5d ",packets[k][i]); } printf("%c ",packets[k][15]); printf("%c",packets[k][16]); printf("%d|",packets[k][17]); printf("%d",packets[k][18]); printf("%c\n",packets[k][19]); } printf("\nPacket data stream!:\n"); for(k = 0; k < pktcount; k++) { printf("%c",k,packets[k][0]); for(i = 1; i < 3; i++) { printf("%d",packets[k][i]); } printf("%c",packets[k][3]); for(i = 4; i < 14; i++) { printf("%c",packets[k][i]); } for(i = 14; i < 15; i++) { printf("%d",packets[k][i]); } printf("%c",packets[k][15]); printf("%c",packets[k][16]); printf("%d",packets[k][17]); printf("%d",packets[k][18]); printf("%c",packets[k][19]); } getchar(); /* //printf("strlen(input) = %d pkts required = %d", datasize, pkts); //getchar(); while(pkts > 0) { packetPrep(&input, &newpkt, (pktcount * 10), 10); // extract 10 or fewer chars of message for a packet packetCreate(1,0,&newpkt,pktcount,totalpkts,&txPkt); // create a packet with that data checksum(&txPkt); //Copy Characters of packet into packet array! for(k = 0; k < 20 ; k++) { packets[pktcount][k] = txPkt[k]; } //printf("pktcount[%d] = %s\n",pktcount,packets[pktcount]); if(datasize >= 10) { datasize = datasize - 10; } //printf("Pkt %d Data:%s Data remaining = %d\n", pktcount, newpkt, datasize); //getchar(); pktcount++; pkts--; }*/ }