summaryrefslogtreecommitdiff
path: root/staging/mops_ext.c
blob: 0a32a05c7a3d38a24815d4bd1b4b067436c09148 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
/*
 * Mausezahn - A fast versatile traffic generator
 * Copyright (C) 2008-2010 Herbert Haas
 * 
 * This program is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License version 2 as published by the 
 * Free Software Foundation.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT 
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more 
 * details.
 * 
 * You should have received a copy of the GNU General Public License along with 
 * this program; if not, see http://www.gnu.org/licenses/gpl-2.0.html
 * 
*/



#include "mz.h"
#include "mops.h"



// Add protocol descriptor of type ptype
// 
// Smart behaviour: 
// 
//    - If the desired p_desc has been assigned already, we leave everything
//      as it is and return to the calling function (return 0).
//      
//    - If a p_desc of another type has been already assigned, this function 
//      clears and frees everything before assigning another p_desc structure.
// 
int mops_ext_add_pdesc (struct mops *mp, int ptype)
{
   
	// 1. check if desired p_desc is already assigned
	if ( (mp->p_desc != NULL) && (mp->p_desc_type == ptype) ) {
		return 0;  
	}

	// 2. remove older p_desc
	if (mp->p_desc_type != MOPS_NO_PDESC) {
		if (mops_ext_del_pdesc (mp)) return 1;
	}

	// 3. allocate and assign a p_desp
	switch (ptype) {
	 case MOPS_ARP:
		mp->p_desc = ( MOPS_EXT_ARP ) malloc ( sizeof (struct mops_ext_arp ) );
		mp->p_desc_type = MOPS_ARP;
		mops_init_pdesc_arp(mp);
		break;
	 case MOPS_BPDU:
		mp->p_desc = ( MOPS_EXT_BPDU ) malloc ( sizeof (struct mops_ext_bpdu ) );
		mp->p_desc_type = MOPS_BPDU;
		mops_init_pdesc_bpdu(mp);
		break;
	 case MOPS_CDP:
		mp->p_desc = ( MOPS_EXT_CDP ) malloc ( sizeof (struct mops_ext_cdp ) );
		mp->p_desc_type = MOPS_CDP;
		mops_init_pdesc_cdp(mp);
		break;
	 case MOPS_DNS:
		mp->p_desc = ( MOPS_EXT_DNS ) malloc ( sizeof (struct mops_ext_dns ) );
		mp->p_desc_type = MOPS_DNS;
		mops_init_pdesc_dns(mp);
		break;
	 case MOPS_ICMP:
		mp->p_desc = ( MOPS_EXT_ICMP ) malloc ( sizeof (struct mops_ext_icmp ) );
		mp->p_desc_type = MOPS_ICMP;
		mops_init_pdesc_icmp(mp);
		break;
	 case MOPS_IGMP:
		mp->p_desc = ( MOPS_EXT_IGMP ) malloc ( sizeof (struct mops_ext_igmp ) );
		mp->p_desc_type = MOPS_IGMP;
		mops_init_pdesc_igmp(mp);
		break;
	 case MOPS_RTP:
		mp->p_desc = ( MOPS_EXT_RTP ) malloc ( sizeof (struct mops_ext_rtp ) );
		mp->p_desc_type = MOPS_RTP;
		mops_init_pdesc_rtp(mp);
		break;
	 case MOPS_LLDP:
		mp->p_desc = ( MOPS_EXT_LLDP ) malloc ( sizeof (struct mops_ext_lldp ) );
		((struct mops_ext_lldp *)mp->p_desc)->chassis_id = NULL;
		((struct mops_ext_lldp *)mp->p_desc)->port_id = NULL;
		((struct mops_ext_lldp *)mp->p_desc)->optional_tlvs = NULL;
		mp->p_desc_type = MOPS_LLDP;
		mops_init_pdesc_lldp(mp);
		break;
	 case MOPS_SYSLOG:
		mp->p_desc = ( MOPS_EXT_SYSLOG ) malloc ( sizeof (struct mops_ext_syslog ) );
		mp->p_desc_type = MOPS_SYSLOG;
		mops_init_pdesc_syslog(mp);
		break;
	 default:
		return 1; // unknown protocol
	}
   
	if (mp->p_desc == NULL) {
		fprintf (stderr, "mz/mops: could not allocate memory for mops element!\n");
		mp->p_desc_type = MOPS_NO_PDESC;
		return 1;
	}

   return 0;
}


// Delete any protocol descriptor
// 1) Free memory
// 2) Reset p_desc and p_desc_type
// 
int mops_ext_del_pdesc (struct mops *mp)
{

	mp->p_desc_type = MOPS_NO_PDESC;
	if (mp->p_desc==NULL) return 1; // already NULL pointer, nothing to free()
	
	switch (mp->p_desc_type) {
	 case MOPS_ARP:
		free ( (MOPS_EXT_ARP) mp->p_desc );
		break;
	 case MOPS_BPDU:
		free ( (MOPS_EXT_BPDU) mp->p_desc );
		break;
	 case MOPS_CDP:
		free ( (MOPS_EXT_CDP) mp->p_desc );
		break;
	 case MOPS_DNS:
		free ( (MOPS_EXT_DNS) mp->p_desc );
		break;
	 case MOPS_ICMP:
		free ( (MOPS_EXT_ICMP) mp->p_desc );
		break;
	 case MOPS_IGMP:
		free ( (MOPS_EXT_IGMP) mp->p_desc );
		break;
	 case MOPS_RTP:
		free ( (MOPS_EXT_RTP) mp->p_desc );
		break;
	 case MOPS_LLDP:
		if ( ((struct mops_ext_lldp *) mp->p_desc)->chassis_id != NULL)    
			free ( ((struct mops_ext_lldp *) mp->p_desc)->chassis_id);
		if ( ((struct mops_ext_lldp *) mp->p_desc)->port_id != NULL)
			free ( ((struct mops_ext_lldp *) mp->p_desc)->port_id);
		if ( ((struct mops_ext_lldp *) mp->p_desc)->optional_tlvs != NULL)  
			free ( ((struct mops_ext_lldp *) mp->p_desc)->optional_tlvs);
		free ( (MOPS_EXT_LLDP) mp->p_desc );
		break;
	 case MOPS_SYSLOG:
		free ( (MOPS_EXT_SYSLOG) mp->p_desc );
		break;
	 case MOPS_NO_PDESC: // already cleared?
		break;
		
		/* nothing */
	}

	mp->p_desc = NULL;
	return 0;
}


// Create msg based on p_desc data.
// After that call mops_update and the frame is complete.
int mops_ext_update (struct mops *mp)
{

	switch (mp->p_desc_type) {
	 case MOPS_ARP:
		mops_update_arp(mp);
		break;
	 case MOPS_BPDU:
		mops_update_bpdu(mp);
		break;
	 case MOPS_CDP:
		break;
	 case MOPS_DNS:
		break;
	 case MOPS_ICMP:
		break;
	 case MOPS_IGMP:
		mops_update_igmp(mp);
		break;
	 case MOPS_RTP:
		mops_update_rtp(mp);
		break;
	 case MOPS_LLDP:
		mops_update_lldp(mp);
		break;
	 case MOPS_SYSLOG:
		break;
	 case MOPS_NO_PDESC:
		return 0;  // OK!
		break;
	 default:
		return 1;  // Unknown value!?
	}
	
	return 0;
}


//////// General parameter update functions - modify a single parameter of p_desc structure
//
// 'Standardized' return values:
//
//   MOPS_PDESC_LOW          Value smaller than lower bound - but will set
//   MOPS_PDESC_HIGH         Value larger than upper bound  - but will set
//
//   MOPS_PDESC_OVERFLOW     Value exceeded possible range
//  
//   MOPS_PDESC_NO_MAC       Invalid MAC address
//   MOPS_PDESC_NO_IP        Invalid IP address
//
//   MOPS_PDESC_FAILURE      Unspecified problem
//   MOPS_PDESC_SUCCESS = 0  Value assigned properly
//
// 'Standardized' format:
// 
//  mops_pdesc_function ( *PDESC_VAR , USER_STRING , LIMITS )


   

// Assign one or more strings to a single string
// Practical example: Concatenate multiple tokens from the CLI
// Will never copy more than 'max' bytes to 'dst'
// 
// EXAMPLE:
// 
// mops_pdesc_mstrings (clipkt->description, argv, argc, 20);
// 
int mops_pdesc_mstrings (char *dst, char* argv[], int argc, int max)
{
   int i;
   char tmp[10000]; // should be sufficient for all purposes here
   
   dst[0]=0x00;
   tmp[0]=0x00;
   
   for (i=0; i<argc; i++)
     {  // check if next word would exceed tmp:
	if ((1+strlen(argv[i]))>(10000-strlen(tmp)))  // The '1+' counts for the additional space
	  return MOPS_PDESC_OVERFLOW;
	else
	  {
	     strncat(tmp, argv[i], 80); // Enforcing a maximum word length
	     strcat(tmp, " "); // We get only the tokens, not the spaces inbetween
	  }
     }
   
   strncpy(dst, tmp, max);
   if (strlen(tmp)>max) return MOPS_PDESC_OVERFLOW;
   
   return MOPS_PDESC_SUCCESS;
}
   




// Assign decimal or hexadecimal u_int8_t value, depending on spec
// spec can be 0=dec or 1=hex
int mops_pdesc_1byte (u_int8_t *dst, char* usr, int spec, int min, int max)
{
   u_int32_t i;
   int retval = MOPS_PDESC_SUCCESS;
   
   if ((max>255)||(min>255)) return MOPS_PDESC_FAILURE;
   
   if (spec==0)
     {
	i = (u_int32_t) str2int (usr);
     }
   else
     {
	i = (u_int32_t) xstr2int (usr);
     }
   
   if (i>255) return MOPS_PDESC_OVERFLOW;
   if (i<min) 
     retval = MOPS_PDESC_LOW;
   else if (i>max)
     retval = MOPS_PDESC_HIGH;

   *dst = (u_int8_t) i;
   
   return retval; 
}



// Assign decimal or hexadecimal u_int16_t value, depending on spec
// spec can be 0=dec or 1=hex
int mops_pdesc_2byte (u_int16_t *dst, char* usr, int spec, int min, int max)
{
   u_int32_t i;
   int retval = MOPS_PDESC_SUCCESS;
   
   if ((max>0xffff)||(min>0xffff)) return MOPS_PDESC_FAILURE;

   if (spec==0)
     {
	i = (u_int32_t) str2int (usr);
     }
   else
     {
	i = (u_int32_t) xstr2int (usr);
     }
   
   if (i>0xffff) return MOPS_PDESC_OVERFLOW;
   if (i<min) 
     retval = MOPS_PDESC_LOW;
   else if (i>max)
     retval = MOPS_PDESC_HIGH;

   *dst = (u_int16_t) i;
   
   return retval; 
}


// Assign decimal or hexadecimal u_int32_t value, depending on spec
// spec can be 0=dec or 1=hex
int mops_pdesc_4byte (u_int32_t *dst, char* usr, int spec, unsigned long int min, unsigned long int max)
{
   unsigned long int i;
   int retval = MOPS_PDESC_SUCCESS;
   
   if ((max>0xffffffff)||(min>0xffffffff)) return MOPS_PDESC_FAILURE;
   
   if (spec==0)
     {
	i = str2int (usr);
     }
   else
     {
	i = xstr2int (usr);
     }
   
   if (i>0xffffffff) return MOPS_PDESC_OVERFLOW;
   if (i<min) 
     retval = MOPS_PDESC_LOW;
   else if (i>max)
     retval = MOPS_PDESC_HIGH;

   *dst = (u_int32_t) i;
   
   return retval; 
}



// Maps MAC address given in 'usr' (e. g. 00:11:22:aa:bb:cc) into 'dst'
// which is an u_int8_t array.
// 
// Returns MOPS_PDESC_FAILURE (=1) upon invalid MAC address
// 
int mops_pdesc_mac (u_int8_t *dst, char* usr)
{
   u_int8_t tmp[6];
   
   // temporarily backup current value
   memcpy ((void*) tmp, (void*) dst, 6);
   
   if (str2hex_mac (usr, dst))
     {  
	// restore original value
	memcpy ((void*) dst, (void*) tmp, 6);
	return MOPS_PDESC_FAILURE;
     };
   
   return MOPS_PDESC_SUCCESS;
}


// Maps an IP address string into an byte-array u_int8_t ip[4] 
// Note: the destination is NOT an u_int32_t !!!
int mops_pdesc_ip (u_int8_t *dst, char* usr)
{
   u_int8_t tmp[4];
   int i, len, j=0;
   
   // Check if format is correct IPv4:
   len = strlen(usr);
   for (i=0; i<len; i++)
     {
	if (usr[i]=='.') 
	  j++;
	else if (!isdigit(usr[i]))
	  return MOPS_PDESC_FAILURE;
     }
   if (j!=3) return MOPS_PDESC_FAILURE;
   
   // temporarily backup current value
   memcpy ((void*) tmp, (void*) dst, 4);
   
   if (num2hex (usr, dst)!=4)
     {  
	// restore original value
	memcpy ((void*) dst, (void*) tmp, 4);
	return MOPS_PDESC_FAILURE;
     };
   
   return MOPS_PDESC_SUCCESS;
}






//////// Initialization functions for each protocol descriptor ///////////                    
//// Each function expects that an appropriate p_desc is already assigned
//// Also the p_desc_type should be set already.
	





int mops_init_pdesc_cdp(struct mops *mp)
{
   if (mp->p_desc == NULL) return 1;  // p_desc not properly assigned 

   
   return 0;
}


int mops_init_pdesc_dns(struct mops *mp)
{
   if (mp->p_desc == NULL) return 1;  // p_desc not properly assigned 

   
   return 0;
}


int mops_init_pdesc_icmp(struct mops *mp)
{
   if (mp->p_desc == NULL) return 1;  // p_desc not properly assigned 

   
   return 0;
}



int mops_init_pdesc_syslog(struct mops *mp)
{
   if (mp->p_desc == NULL) return 1;  // p_desc not properly assigned 
   
   return 0;
}