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