| 1 | #include <stdio.h> |
| 2 | #include "vera_directc.h" |
| 3 | #include "include/pg.h" |
| 4 | // CRC GEN Function |
| 5 | // |
| 6 | // |
| 7 | // |
| 8 | |
| 9 | extern void setpgSeed( unsigned int seed ); |
| 10 | extern unsigned int pgrand(); |
| 11 | |
| 12 | |
| 13 | #if 0 |
| 14 | // global name crc32 collides with one in the denali library |
| 15 | unsigned int crc32(unsigned char *pktbuf,unsigned int pktlen ) |
| 16 | { |
| 17 | |
| 18 | unsigned int crc; |
| 19 | unsigned int c; |
| 20 | unsigned int byte; |
| 21 | int bit; |
| 22 | |
| 23 | crc = (unsigned int) 0xffffffff; |
| 24 | for (byte = 0; byte < pktlen; byte++) { |
| 25 | c = (unsigned int) pktbuf[byte]; |
| 26 | printf("index - %d CRC Byte - %x CRC So far - %x \n",byte,c,crc); |
| 27 | for (bit = 0; bit < 8; bit++) { |
| 28 | if ((c & 0x1) ^ (crc & 0x1)) |
| 29 | crc = (crc >> 1)^0xedb88320; |
| 30 | else |
| 31 | crc = (crc >> 1); |
| 32 | c >>= 1; |
| 33 | } |
| 34 | } |
| 35 | printf("CRC - %x \n",~crc); |
| 36 | return ((~crc) ); |
| 37 | |
| 38 | } |
| 39 | #endif |
| 40 | |
| 41 | unsigned char getNextByte(unsigned char in, int data_type) { |
| 42 | |
| 43 | // data_type = DATA_SEQ; |
| 44 | if(data_type== DATA_SEQ) { |
| 45 | return(in +1); |
| 46 | } else if(data_type == DATA_FIXED) { |
| 47 | return(in); |
| 48 | } else if(data_type== DATA_RAND) { |
| 49 | return(pgrand()&0xff); |
| 50 | } else if(data_type == DATA_ALL0) { |
| 51 | return(0); |
| 52 | } else if(data_type == DATA_ALL1) { |
| 53 | return(0xff); |
| 54 | } else { |
| 55 | // default to DATA_SEQ |
| 56 | return(in +1); |
| 57 | } |
| 58 | |
| 59 | } |
| 60 | |
| 61 | unsigned char * genPayload ( int data_type, unsigned int seed, int data_length) { |
| 62 | /* defined data types are |
| 63 | * DATA_RANDOM - Random data |
| 64 | * DATA_SEQ - In Sequence |
| 65 | * More to be defined later |
| 66 | */ |
| 67 | |
| 68 | unsigned char *payload; |
| 69 | int index,i; |
| 70 | if(data_length==0) { |
| 71 | return(NULL); |
| 72 | } |
| 73 | payload = (unsigned char *) malloc(data_length* sizeof(unsigned char)); |
| 74 | index =0; |
| 75 | |
| 76 | if(data_type== DATA_RAND) { |
| 77 | setpgSeed(seed ) ; |
| 78 | } else if(data_type == DATA_ALL0) { |
| 79 | seed =0; |
| 80 | } else if(data_type== DATA_ALL1) { |
| 81 | seed =0xffffffff; |
| 82 | } else if(data_type == DATA_FC_PAUSE) { |
| 83 | // maintain seed for first 4 bytes and then all0s |
| 84 | data_type = DATA_ALL0; |
| 85 | } |
| 86 | |
| 87 | for(i=0;i<data_length;i++) { |
| 88 | if(i<4) { |
| 89 | // Start with Seed |
| 90 | payload[index]= ((seed & (0xff << (8*i)))>>(8*i)); |
| 91 | } else { |
| 92 | payload[index] = getNextByte(payload[index-1],data_type); |
| 93 | // printf(" Index - %d data -0 %x \n",index, payload[index] ); |
| 94 | } |
| 95 | index++; |
| 96 | } |
| 97 | return(payload); |
| 98 | |
| 99 | } |
| 100 | unsigned char * addL2Pad(unsigned char * pktbuf,int pktlen,int l2_pad_length) { |
| 101 | unsigned char *payload; |
| 102 | int i; |
| 103 | if(l2_pad_length==0) { |
| 104 | return(pktbuf); |
| 105 | } else { |
| 106 | payload = (unsigned char *) malloc((pktlen + l2_pad_length)* sizeof(unsigned char)); |
| 107 | for(i=0;i<pktlen;i++){ |
| 108 | payload[i] = pktbuf[i]; |
| 109 | } |
| 110 | for(i=pktlen;i<pktlen + l2_pad_length ;i++){ |
| 111 | payload[i] = (i%2) ? 0xa5:0x5a ; |
| 112 | } |
| 113 | return(payload); |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | unsigned char * addCrc ( unsigned char * p1, int l1, unsigned int crc) { |
| 118 | unsigned char *payload; |
| 119 | int i; |
| 120 | payload = (unsigned char *) malloc((l1+4)* sizeof(unsigned char)); |
| 121 | for(i=0;i<l1;i++){ |
| 122 | payload[i] = p1[i]; |
| 123 | } |
| 124 | for(i=0;i<4;i++) { |
| 125 | payload[l1+ i]= ((crc & (0xff << (8*i)))>>(8*i)); |
| 126 | } |
| 127 | return(payload); |
| 128 | } |
| 129 | unsigned char * addPayload ( unsigned char * p1, int l1, unsigned char * p2, int l2) { |
| 130 | unsigned char *payload; |
| 131 | int i; |
| 132 | if( (l1+l2) == 0) return(NULL); |
| 133 | payload = (unsigned char *) malloc((l1+l2)* sizeof(unsigned char)); |
| 134 | for(i=0;i<l1;i++) { |
| 135 | payload[i] = p1[i]; |
| 136 | } |
| 137 | for(i=l1;i<l1 + l2;i++) { |
| 138 | payload[i] = p2[i]; |
| 139 | } |
| 140 | return(payload); |
| 141 | } |
| 142 | |