6 const unsigned short helloRequest[]={
24 const struct ether_addr nullMac={.ether_addr_octet={0, 0, 0, 0, 0, 0}};
28 // -------------------
29 int min (int a, int b) {
35 // -----------------------------------------------------------------------------------------------------------------------------------------------
36 void initNgHeader (struct ng_header *nh, char code, const struct ether_addr *client_mac, const struct ether_addr *switch_mac, unsigned int seqnum) {
39 memset(nh, 0, sizeof(struct ng_header));
43 memcpy(nh->client_mac, client_mac, ETH_ALEN);
45 if ( switch_mac!=NULL ) {
46 memcpy(nh->switch_mac, switch_mac, ETH_ALEN);
49 nh->seqnum=htonl(seqnum);
50 strcpy(nh->proto_id, "NSDP");
57 // ----------------------------------------------------------------------------------------------------------------------------------------------
58 bool validateNgHeader (const struct ng_header *nh, char code, const struct ether_addr *client_mac, const struct ether_addr *switch_mac, unsigned int seqnum) {
64 //printf("unk1 not 1\n");
68 if ( code>0 && nh->code!=code ) {
73 if ( nh->unk2[i]!=0 ) {
74 //printf("unk2[%i] not 0\n", i);
79 if ( client_mac!=NULL && memcmp(nh->client_mac, client_mac, ETH_ALEN)!=0 ) {
80 //printf("client_mac err\n");
84 if ( switch_mac!=NULL && memcmp(nh->switch_mac, switch_mac, ETH_ALEN)!=0 ) {
85 //printf("switch_mac err\n");
89 if ( seqnum>0 && ntohl(nh->seqnum)!=seqnum ) {
90 //printf("incorrect seqnum\n");
94 if ( *(unsigned int*)nh->unk3!=0 ) {
95 //printf("unk3 not 0\n");
106 // -------------------------------------
107 void initNgPacket (struct ng_packet *np) {
109 np->ah=(struct attr_header*)np->nh->data;
115 // --------------------------------------------------------------------------------------------
116 void addPacketAttr (struct ng_packet *np, unsigned short attr, unsigned short size, void* data) {
118 struct attr_header *ah=np->ah;
121 if ( (int)(getPacketTotalSize(np)+sizeof(struct attr_header)+size)>(np->maxlen) ) {
125 ah->attr=htons(attr);
126 ah->size=htons(size);
128 if ( size>0 && data!=NULL ) {
129 memcpy(ah->data, data, size);
132 np->ah=(struct attr_header*)(ah->data+size);
138 // ----------------------------------------------------------------
139 void addPacketEmptyAttr (struct ng_packet *np, unsigned short attr) {
140 addPacketAttr(np, attr, 0, NULL);
145 // -------------------------------------------------------------------------
146 void addPacketByteAttr (struct ng_packet *np, unsigned short attr, char val) {
147 addPacketAttr(np, attr, 1, &val);
152 // ---------------------------------------------------------------------------
153 void addPacketShortAttr (struct ng_packet *np, unsigned short attr, short val) {
158 addPacketAttr(np, attr, 2, &s);
164 // ------------------------------------------------
165 int getPacketTotalSize (const struct ng_packet *np) {
166 return ((char*)np->ah)-np->buffer;
170 // --------------------------------------------
171 struct attr* newEmptyAttr (unsigned short attr) {
176 at=malloc(sizeof(struct attr));
187 // ----------------------------
188 void freeAttr (struct attr *at) {
199 // -------------------------------------------------
200 List* extractPacketAttributes (struct ng_packet *np) {
208 while ( getPacketTotalSize(np)<np->maxlen ) {
210 at=malloc(sizeof(struct attr));
211 at->attr=ntohs(np->ah->attr);
212 at->size=ntohs(np->ah->size);
214 if ( getPacketTotalSize(np)+at->size>np->maxlen ) {
222 at->data=malloc(at->size*sizeof(char));
223 memcpy(at->data, np->ah->data, at->size);
228 if ( at->attr==ATTR_END ) {
232 np->ah=(struct attr_header*)(np->ah->data+at->size);