]> git.sur5r.net Git - ngadmin/blob - lib/src/protocol.c
a71df2eaa9740bf2b80c06a103a0025828e80484
[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 ( nh->unk2!=0 ) {
54   return false;
55  }
56  
57  if ( *(unsigned short*)nh->unk3!=0 ) {
58   return false;
59  }
60  
61  if ( client_mac!=NULL && memcmp(nh->client_mac, client_mac, ETH_ALEN)!=0 ) {
62   return false;
63  }
64  
65  if ( switch_mac!=NULL && memcmp(nh->switch_mac, switch_mac, ETH_ALEN)!=0 ) {
66   return false;
67  }
68  
69  if ( seqnum>0 && ntohl(nh->seqnum)!=seqnum ) {
70   return false;
71  }
72  
73  if ( *(unsigned int*)nh->unk4!=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* newIntAttr (unsigned short attr, int value) {
189  
190  int *v=malloc(sizeof(int));
191  
192  *v=htonl(value);
193  
194  return newAttr(attr, sizeof(int), v);
195  
196 }
197
198
199
200 // ----------------------------
201 void freeAttr (struct attr *at) {
202  
203  if ( at!=NULL ) {
204   free(at->data);
205   free(at);
206  }
207  
208 }
209
210
211
212 // -----------------------------------------------------------------------------------------------------
213 void extractPacketAttributes (struct ng_packet *np, char *error, unsigned short *attr_error, List *attr) {
214  
215  struct attr *at;
216  
217  
218  if ( error!=NULL ) {
219   *error=np->nh->error;
220  }
221  
222  if ( attr_error!=NULL ) {
223   *attr_error=ntohs(np->nh->attr);
224  }
225  
226  while ( getPacketTotalSize(np)<np->maxlen ) {
227   
228   at=malloc(sizeof(struct attr));
229   at->attr=ntohs(np->ah->attr);
230   at->size=ntohs(np->ah->size);
231   
232   if ( getPacketTotalSize(np)+at->size>np->maxlen ) {
233    free(at);
234    break;
235   }
236   
237   if ( at->size==0 ) {
238    at->data=NULL;
239   } else {
240    at->data=malloc(at->size*sizeof(char));
241    memcpy(at->data, np->ah->data, at->size);
242   }
243   
244   pushBackList(attr, at);
245   
246   if ( at->attr==ATTR_END ) {
247    break;
248   }
249   
250   np->ah=(struct attr_header*)(np->ah->data+at->size);
251   
252  }
253  
254  
255 }
256
257
258
259 // --------------------------------------------------------------
260 void extractSwitchAttributes (struct swi_attr *sa, const List *l) {
261  
262  const ListNode *ln;
263  const struct attr *at;
264  int len;
265  
266  
267  memset(sa, 0, sizeof(struct swi_attr));
268  
269  // FIXME: mutex lock ?
270  
271  for (ln=l->first; ln!=NULL; ln=ln->next) {
272   at=ln->data;
273   
274   switch ( at->attr ) {
275    
276    case ATTR_PRODUCT:
277     len=min(at->size, PRODUCT_SIZE);
278     memcpy(sa->product, at->data, len);
279     //trim(sa->product, len); // FIXME
280    break;
281    
282    case ATTR_NAME:
283     len=min(at->size, NAME_SIZE);
284     memcpy(sa->name, at->data, len);
285     //trim(sa->name, len); // FIXME
286    break;
287    
288    case ATTR_MAC:
289     memcpy(&sa->mac, at->data, ETH_ALEN);
290    break;
291    
292    case ATTR_IP:
293     sa->nc.ip=*(struct in_addr*)at->data;
294    break;
295    
296    case ATTR_NETMASK:
297     sa->nc.netmask=*(struct in_addr*)at->data;
298    break;
299    
300    case ATTR_GATEWAY:
301     sa->nc.gw=*(struct in_addr*)at->data;
302    break;
303    
304    case ATTR_DHCP:
305     sa->nc.dhcp=( ntohs(*(unsigned short*)at->data)==1 );
306    break;
307    
308    case ATTR_FIRM_VER:
309     len=min(at->size, FIRMWARE_SIZE-1);
310     memcpy(sa->firmware, at->data, len);
311     sa->firmware[len]=0;
312    break;
313    
314    case ATTR_PORTS_COUNT:
315     sa->ports=*(unsigned char*)at->data;
316    break;
317    
318    case ATTR_END:
319     return;
320    
321   }
322   
323  }
324  
325  
326 }
327
328
329