-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathencapsulation.c
404 lines (351 loc) · 11.1 KB
/
encapsulation.c
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
/* demo of Encapsulation and Decapsulation of datas */
#include "stdio.h"
#include "string.h"
typedef unsigned char uint8_t;
typedef unsigned short int uint16_t;
typedef unsigned int uint32_t;
typedef unsigned long uint64_t;
typedef char int8_t;
typedef short int int16_t;
typedef int int32_t;
typedef long int64_t;
#define TRACE_ERROR (0)
#define NULL_DATA_PTR ((void *)0)
#define NULL_CHAR ('\0')
#define STR_MAX_CHARS (7)
#define IP_MIN_VAL (1)
#define IP_MAX_VAL (1000)
typedef enum { SUCCESS, FAILURE } system_status_t;
uint16_t Get_Validate_Input_Number(void *const int32_input_num_ptr, char *const input_str_ptr, const unsigned int input_str_max_chars, const int32_t valid_min_value, const int32_t valid_max_value);
uint16_t Get_Input_Str(char *const input_str_ptr, const unsigned int input_str_max_chars);
uint16_t Str_to_Num_Conv(void *const num_conv_from_str, const char *const num_in_str);
uint32_t Power_Of(const uint8_t base, const uint8_t power);
typedef struct
{
int mac;
int msgtype;
unsigned char dataptr[1];
} mainencap;
typedef struct
{
int ip;
int namelen;
char name[20];
int msgtype;
unsigned char dataptr[1];
} subencap1;
typedef struct
{
int port;
unsigned char dataptr[1];
} lastencap;
unsigned char tempdata[50];
char trace_flag = 0;
int Main_Encap(void **const ptr);
int Sub_Encap1(void *const ptr);
int LastEncap(void *const ptr);
char input_str[STR_MAX_CHARS];
int main()
{
mainencap *mainptr = NULL;
subencap1 *subptr;
lastencap *lastptr;
Main_Encap((void **)&mainptr);
if(trace_flag)
printf("\n inside main: mainptr: %#X", mainptr);
printf("\n mainencap: mac: %d",mainptr->mac);
if(mainptr->msgtype == 1)
{
printf("\n mainencap: msgtype: %d", mainptr->msgtype);
/* subptr points to garbage address */
/* subptr = mainptr->dataptr; */ /* warning: suspicous ptr conv */
subptr = (subencap1 *)mainptr->dataptr;
if(trace_flag)
printf("\n subptr: %#X ",subptr);
printf("\n sub_encap1: num: %d, name: %s, namelen: %d",subptr->ip, subptr->name, subptr->namelen);
if(subptr->msgtype == 2)
{
lastptr = (lastencap *)subptr->dataptr;
if(trace_flag)
printf("\n lastptr: %#X",lastptr);
printf("\n last_encap: msg type: %d, port: %d, uchar: %c ",subptr->msgtype, lastptr->port, lastptr->dataptr[0]);
}
}
return 1;
}
int Main_Encap(void **const ptr)
{
mainencap *mainptr = NULL;
*ptr = mainptr = (mainencap *)tempdata;
if(trace_flag)
printf("\n mainptr: %#X", mainptr);
printf("\n In mainencap's msgtype = 1 for sub_encap1");
// scanf("%d %d", &mainptr->mac, &mainptr->msgtype);
printf("\n Enter: mac - ");
if((Get_Validate_Input_Number(&mainptr->mac, input_str, STR_MAX_CHARS, IP_MIN_VAL, IP_MAX_VAL)) != SUCCESS)
{
mainptr->mac = 50;
printf("\n ERR: invalid MAC - default MAC = %d", mainptr->mac);
}
mainptr->msgtype = 1;
if((mainptr)->msgtype == 1)
Sub_Encap1(mainptr);
return 1;
}
int Sub_Encap1(void *const ptr)
{
subencap1 *subptr;
/* subptr = ptr->dataptr; */ /* error: ptr req at left side of -> */
/* subptr points to NULL, if assigned below */
/* subptr = ((mainencap *)ptr)->dataptr; */ /* warning: suspicous ptr conv */
/* we are using address allocated dataptr, */
subptr = (subencap1 *)((mainencap *)ptr)->dataptr;
if(trace_flag)
printf("\n subptr: %#X ",subptr);
// printf("Inside sub_encap1: Enter: num, name: ");
// scanf("%d %s", &subptr->ip, subptr->name);
printf("\n For message - %u, inside sub_encap1", ((mainencap *)ptr)->msgtype);
printf("\n Enter: num - ");
if((Get_Validate_Input_Number(&subptr->ip, input_str, STR_MAX_CHARS, IP_MIN_VAL, IP_MAX_VAL)) != SUCCESS)
{
subptr->ip = 100;
printf("\n ERR: invalid IP - default IP = %d", subptr->ip);
}
printf("Enter: name - ");
if((Get_Input_Str(subptr->name, 20)) != SUCCESS)
{
strcpy(subptr->name, "TEMP");
printf("\n ERR: invalid name - default IP = %d", subptr->ip);
}
subptr->namelen = strlen(subptr->name) + 1;
if(trace_flag)
printf("\n ((mainencap *)ptr)->dataptr: %#X,&subptr->ip: %#X",((mainencap *)ptr)->dataptr, &subptr->ip);
//printf("\n msgtype = 2 for last encap: Enter msgtype : ");
//scanf("%d", &subptr->msgtype);
subptr->msgtype = 2;
if(subptr->msgtype == 2)
LastEncap(ptr);
return 1;
}
int LastEncap(void *const ptr)
{
subencap1 *subptr;
lastencap *lastptr;
/* lastptr = (lastencap *)((subencap1 *)ptr->dataptr)->dataptr; */
/* error: ptr req on left side of -> */
subptr = (subencap1 *)((mainencap *)ptr)->dataptr;
/* lastptr = subptr->dataptr; */ /* warning: suspicous ptr conv */
/* lastptr, is garbage */
/* lastptr = (lastencap *)((subencap1 *)((mainencap *)ptr)->dataptr)->dataptr; */
lastptr = (lastencap *)((subencap1 *)&((mainencap *)ptr)->dataptr)->dataptr;
if(trace_flag)
{
printf("\n lastptr: %#X", lastptr);
}
printf("\n For sub_encap1 message - %u, inside last_encap ", subptr->msgtype);
printf("\n Enter: port - " );
if((Get_Validate_Input_Number(&lastptr->port, input_str, STR_MAX_CHARS, IP_MIN_VAL, IP_MAX_VAL)) != SUCCESS)
{
lastptr->port = 80;
printf("\n ERR: invalid PORT - default port = %d", lastptr->port);
}
printf("\n Enter: a char - " );
if((Get_Input_Str(input_str, 2)) != SUCCESS)
{
lastptr->dataptr[0] = 'X';
printf("\n ERR: invalid CHAR - default char = %d", lastptr->dataptr[0]);
}
else
{
lastptr->dataptr[0] = input_str[0];
}
return 1;
}
/*------------------------------------------------------------*
FUNCTION NAME : Str_to_Num_Conv
DESCRIPTION :
INPUT :
OUTPUT :
NOTE : digits are extracted from left to right format from digit in num_in_str
Func ID : 02.04
BUGS :
-*------------------------------------------------------------*/
uint16_t Str_to_Num_Conv( void *const num_conv_from_str_ptr, const char *const num_in_str)
{
int32_t num = 0, *num_conv_from_str;
uint32_t place;
int16_t cur_unit;
uint8_t num_chars = 0, base = 10, pos = 0, start_num_pos = 0 ;
if(num_conv_from_str_ptr == NULL_DATA_PTR || num_in_str == NULL_DATA_PTR )
{
#ifdef TRACE_ERROR
printf("ERR: data are null ptr \n");
#endif
return FAILURE;
}
num_conv_from_str = (int32_t *)num_conv_from_str_ptr;
*num_conv_from_str = 0;
if(num_in_str[0] >= '0' && num_in_str[0] <= '9')
{
start_num_pos = 0;
}
else if(num_in_str[0] == '-')
{
start_num_pos = 1;
}
else
{
#ifdef TRACE_ERROR
printf("ERR: invalid char: %c \n", num_in_str[0]);
#endif
return FAILURE;
}
num_chars = strlen(num_in_str + start_num_pos);
if(num_chars == 0)
{
#ifdef TRACE_ERROR
printf("ERR: data empty \n");
#endif
return FAILURE;
}
pos = start_num_pos;
for( place = Power_Of(base, num_chars - 1); place >= 1; place /= base, ++pos )
{
cur_unit = num_in_str[pos] - '0';
if(cur_unit < 0 || cur_unit > 9 )
{
#ifdef TRACE_ERROR
printf("ERR: invalid char at data[%d] = %c \n", pos, num_in_str[pos]);
#endif
return FAILURE;
}
num += (cur_unit * place);
}
if(num_in_str[0] == '-')
{
*num_conv_from_str = -num;
}
else
{
*num_conv_from_str = num;
}
return SUCCESS;
}
/*------------------------------------------------------------*
FUNCTION NAME : Power_Of
DESCRIPTION :
INPUT :
OUTPUT :
NOTE :
Func ID : 02.10
Bugs :
-*------------------------------------------------------------*/
uint32_t Power_Of(const uint8_t base, const uint8_t power )
{
uint32_t power_val = 1;
uint8_t i = 0;
if(power == 0)
{
return power_val;
}
for(i = 1; i <= power; ++i)
{
power_val *= base;
}
return power_val;
}
/*------------------------------------------------------------*
FUNCTION NAME : Validate_Number_Input
DESCRIPTION :
INPUT :
OUTPUT :
NOTE :
Func ID : 01.02
BUGS :
-*------------------------------------------------------------*/
uint16_t Get_Validate_Input_Number(void *const input_num_ptr, char *const input_str_ptr, const unsigned int input_str_max_chars, const int32_t valid_min_value, const int32_t valid_max_value)
{
int32_t temp_int, *int32_input_num_ptr;
if(int32_input_num_ptr == NULL_DATA_PTR)
{
return FAILURE;
}
if(valid_min_value > valid_max_value)
{
return FAILURE;
}
int32_input_num_ptr = (int32_t *)input_num_ptr;
*int32_input_num_ptr = 0;
if((Get_Input_Str(input_str_ptr, input_str_max_chars)) != SUCCESS)
return FAILURE;
if((Str_to_Num_Conv(&temp_int, input_str_ptr)) != SUCCESS)
{
memset(input_str_ptr, NULL_CHAR, input_str_max_chars);
return FAILURE;
}
memset(input_str_ptr, NULL_CHAR, input_str_max_chars);
if(temp_int < valid_min_value || temp_int > valid_max_value)
{
#ifdef TRACE_ERROR
printf("ERR: input data - %d, out of range [%d,%d] \n", temp_int, valid_min_value, valid_max_value);
#endif
return FAILURE;
}
*int32_input_num_ptr = temp_int;
return SUCCESS;
}
/*------------------------------------------------------------*
FUNCTION NAME : Get_Input_Str
DESCRIPTION :
INPUT :
OUTPUT :
NOTE :
Func ID : 01.02
BUGS :
-*------------------------------------------------------------*/
uint16_t Get_Input_Str(char *const input_str_ptr, const unsigned int input_str_max_chars)
{
unsigned int input_str_num_chars = 0;
char rcvd_char;
if(input_str_ptr == NULL_DATA_PTR || input_str_max_chars <= 1)
{
return FAILURE;
}
memset(input_str_ptr, NULL_CHAR, input_str_max_chars);
while (1)
{
rcvd_char = (char) getchar();
//scanf("%c", &rcvd_char);
switch(rcvd_char)
{
case '\b':
if(input_str_num_chars > 0)
{
input_str_ptr[input_str_num_chars] = NULL_CHAR;
--input_str_num_chars;
}
break;
case '\n':
if(input_str_num_chars != 0)
{
input_str_ptr[input_str_num_chars] = NULL_CHAR;
return SUCCESS;
}
break;
default:
if(input_str_num_chars + 1 < input_str_max_chars )
{
input_str_ptr[input_str_num_chars] = rcvd_char;
++input_str_num_chars;
}
else
{
printf("ERR: Input data num chars exceeds max chars : %u \n", input_str_max_chars - 1);
memset(input_str_ptr, NULL_CHAR, input_str_max_chars);
fflush(stdin);
return FAILURE;
}
}
}
return SUCCESS;
}