]> git.sur5r.net Git - ngadmin/blob - lib/src/protocol.c
d1be292b8d8d7f460171001b812522d50235e523
[ngadmin] / lib / src / protocol.c
1
2 #include "protocol.h"
3
4
5
6
7
8 const struct ether_addr nullMac={.ether_addr_octet={0, 0, 0, 0, 0, 0}};
9
10
11
12 // -------------------
13 int min (int a, int b) {
14  return a<b ? a : b ;
15 }
16
17
18
19 // -----------------------------------------------------------------------------------------------------------------------------------------------
20 void initNgHeader (struct ng_header *nh, char code, const struct ether_addr *client_mac, const struct ether_addr *switch_mac, unsigned int seqnum) {
21  
22  
23  memset(nh, 0, sizeof(struct ng_header));
24  nh->unk1=1;
25  nh->code=code;
26  
27  memcpy(nh->client_mac, client_mac, ETH_ALEN);
28  
29  if ( switch_mac!=NULL ) {
30   memcpy(nh->switch_mac, switch_mac, ETH_ALEN);
31  }
32  
33  nh->seqnum=htonl(seqnum);
34  strcpy(nh->proto_id, "NSDP");
35  
36  
37 }
38
39
40
41 // ---------------------------------------------------------------------------------------------------------------------------------------------------------
42 bool validateNgHeader (const struct ng_header *nh, char code, const struct ether_addr *client_mac, const struct ether_addr *switch_mac, unsigned int seqnum) {
43  
44  
45  if ( nh->unk1!=1 ) {
46   return false;
47  }
48  
49  if ( code>0 && nh->code!=code ) {
50   return false;
51  }
52  
53  if ( *(unsigned short*)nh->unk2!=0 ) {
54   return false;
55  }
56  
57  if ( client_mac!=NULL && memcmp(nh->client_mac, client_mac, ETH_ALEN)!=0 ) {
58   return false;
59  }
60  
61  if ( switch_mac!=NULL && memcmp(nh->switch_mac, switch_mac, ETH_ALEN)!=0 ) {
62   return false;
63  }
64  
65  if ( seqnum>0 && ntohl(nh->seqnum)!=seqnum ) {
66   return false;
67  }
68  
69  if ( memcmp(nh->proto_id, "NSDP", 4)!=0 ) {
70   return false;
71  }
72  
73  if ( *(unsigned int*)nh->unk3!=0 ) {
74   return false;
75  }
76  
77  
78  return true;
79  
80 }
81
82
83
84 // -------------------------------------
85 void initNgPacket (struct ng_packet *np) {
86  
87  np->ah=(struct attr_header*)np->nh->data;
88  
89 }
90
91
92
93 // --------------------------------------------------------------------------------------------
94 void addPacketAttr (struct ng_packet *np, unsigned short attr, unsigned short size, void* data) {
95  
96  struct attr_header *ah=np->ah;
97  
98  
99  if ( (int)(getPacketTotalSize(np)+sizeof(struct attr_header)+size)>(np->maxlen) ) {
100   return;
101  }
102  
103  ah->attr=htons(attr);
104  ah->size=htons(size);
105  
106  if ( size>0 && data!=NULL ) {
107   memcpy(ah->data, data, size);
108  }
109  
110  np->ah=(struct attr_header*)(ah->data+size);
111  
112 }
113
114
115
116 // ----------------------------------------------------------------
117 void addPacketEmptyAttr (struct ng_packet *np, unsigned short attr) {
118  addPacketAttr(np, attr, 0, NULL);
119 }
120
121
122
123 // -------------------------------------------------------------------------
124 void addPacketByteAttr (struct ng_packet *np, unsigned short attr, char val) {
125  addPacketAttr(np, attr, 1, &val);
126 }
127
128
129
130 // ---------------------------------------------------------------------------
131 void addPacketShortAttr (struct ng_packet *np, unsigned short attr, short val) {
132  
133  short s=htons(val);
134  
135  
136  addPacketAttr(np, attr, 2, &s);
137  
138 }
139
140
141
142 // ------------------------------------------------
143 int getPacketTotalSize (const struct ng_packet *np) {
144  return ((char*)np->ah)-np->buffer;
145 }
146
147
148
149 // --------------------------------------------
150 struct attr* newEmptyAttr (unsigned short attr) {
151  return newAttr(attr, 0, NULL);
152 }
153
154
155
156 // ------------------------------------------------------------------------
157 struct attr* newAttr (unsigned short attr, unsigned short size, void *data) {
158  
159  struct attr *at;
160  
161  
162  at=malloc(sizeof(struct attr));
163  at->attr=attr;
164  at->size=size;
165  at->data=data;
166  
167  
168  return at;
169  
170 }
171
172
173
174 // ----------------------------------------------------------------
175 struct attr* newByteAttr (unsigned short attr, unsigned char value) {
176  
177  char *v=malloc(sizeof(char));
178  
179  *v=value;
180  
181  return newAttr(attr, sizeof(char), v);
182  
183 }
184
185
186
187 // ---------------------------------------------------------
188 struct attr* newShortAttr (unsigned short attr, short value) {
189  
190  short *v=malloc(sizeof(short));
191  
192  *v=htons(value);
193  
194  return newAttr(attr, sizeof(short), v);
195  
196 }
197
198
199
200 // -----------------------------------------------------
201 struct attr* newIntAttr (unsigned short attr, int value) {
202  
203  int *v=malloc(sizeof(int));
204  
205  *v=htonl(value);
206  
207  return newAttr(attr, sizeof(int), v);
208  
209 }
210
211
212
213 // -----------------------------------------------------------------
214 struct attr* newAddrAttr (unsigned short attr, struct in_addr value) {
215  
216  struct in_addr *v=malloc(sizeof(struct in_addr));
217  
218  *v=value;
219  
220  return newAttr(attr, sizeof(struct in_addr), v);
221  
222 }
223
224
225
226 // ----------------------------
227 void freeAttr (struct attr *at) {
228  
229  if ( at!=NULL ) {
230   free(at->data);
231   free(at);
232  }
233  
234 }
235
236
237
238 // ---------------------------------------------------------------------------------------------------------------
239 void extractPacketAttributes (struct ng_packet *np, unsigned short *error, unsigned short *attr_error, List *attr) {
240  
241  struct attr *at;
242  
243  
244  if ( error!=NULL ) *error=ntohs(np->nh->error);
245  if ( attr_error!=NULL ) *attr_error=ntohs(np->nh->attr);
246  
247  while ( getPacketTotalSize(np)<np->maxlen ) {
248   
249   at=malloc(sizeof(struct attr));
250   at->attr=ntohs(np->ah->attr);
251   at->size=ntohs(np->ah->size);
252   
253   if ( getPacketTotalSize(np)+at->size>np->maxlen ) {
254    free(at);
255    break;
256   }
257   
258   if ( at->size==0 ) {
259    at->data=NULL;
260   } else {
261    at->data=malloc(at->size*sizeof(char));
262    memcpy(at->data, np->ah->data, at->size);
263   }
264   
265   pushBackList(attr, at);
266   
267   if ( at->attr==ATTR_END ) {
268    break;
269   }
270   
271   np->ah=(struct attr_header*)(np->ah->data+at->size);
272   
273  }
274  
275  
276 }
277
278
279
280 // --------------------------------------------------------------
281 void extractSwitchAttributes (struct swi_attr *sa, const List *l) {
282  
283  const ListNode *ln;
284  const struct attr *at;
285  int len;
286  
287  
288  memset(sa, 0, sizeof(struct swi_attr));
289  
290  // FIXME: mutex lock ?
291  
292  for (ln=l->first; ln!=NULL; ln=ln->next) {
293   at=ln->data;
294   
295   switch ( at->attr ) {
296    
297    case ATTR_PRODUCT:
298     len=min(at->size, PRODUCT_SIZE);
299     memcpy(sa->product, at->data, len);
300     //trim(sa->product, len); // FIXME
301    break;
302    
303    case ATTR_NAME:
304     len=min(at->size, NAME_SIZE);
305     memcpy(sa->name, at->data, len);
306     //trim(sa->name, len); // FIXME
307    break;
308    
309    case ATTR_MAC:
310     memcpy(&sa->mac, at->data, ETH_ALEN);
311    break;
312    
313    case ATTR_IP:
314     sa->nc.ip=*(struct in_addr*)at->data;
315    break;
316    
317    case ATTR_NETMASK:
318     sa->nc.netmask=*(struct in_addr*)at->data;
319    break;
320    
321    case ATTR_GATEWAY:
322     sa->nc.gw=*(struct in_addr*)at->data;
323    break;
324    
325    case ATTR_DHCP:
326     sa->nc.dhcp=( ntohs(*(unsigned short*)at->data)==1 );
327    break;
328    
329    case ATTR_FIRM_VER:
330     len=min(at->size, FIRMWARE_SIZE-1);
331     memcpy(sa->firmware, at->data, len);
332     sa->firmware[len]=0;
333    break;
334    
335    case ATTR_PORTS_COUNT:
336     sa->ports=*(unsigned char*)at->data;
337    break;
338    
339    case ATTR_END:
340     return;
341    
342   }
343   
344  }
345  
346  
347 }
348
349
350