1 |
/* |
/* |
2 |
* Cisco C7200 (Predator) simulation platform. |
* Cisco router simulation platform. |
3 |
* Copyright (c) 2005,2006 Christophe Fillot. All rights reserved. |
* Copyright (c) 2005,2006 Christophe Fillot. All rights reserved. |
4 |
* |
* |
5 |
* Utility functions. |
* Utility functions. |
12 |
#include <stdarg.h> |
#include <stdarg.h> |
13 |
#include <unistd.h> |
#include <unistd.h> |
14 |
#include <time.h> |
#include <time.h> |
15 |
|
#include <signal.h> |
16 |
#include <sys/time.h> |
#include <sys/time.h> |
17 |
#include <sys/ioctl.h> |
#include <sys/ioctl.h> |
18 |
#include <sys/types.h> |
#include <sys/types.h> |
19 |
|
#include <sys/stat.h> |
20 |
#include <sys/socket.h> |
#include <sys/socket.h> |
21 |
#include <arpa/inet.h> |
#include <arpa/inet.h> |
22 |
#include <netdb.h> |
#include <netdb.h> |
208 |
} |
} |
209 |
|
|
210 |
fprintf(f_output,"\n"); |
fprintf(f_output,"\n"); |
211 |
|
fflush(f_output); |
212 |
} |
} |
213 |
|
|
214 |
/* Logging function */ |
/* Logging function */ |
257 |
} |
} |
258 |
|
|
259 |
/* Read a file and returns it in a buffer */ |
/* Read a file and returns it in a buffer */ |
260 |
ssize_t m_read_file(char *filename,char **buffer) |
ssize_t m_read_file(char *filename,u_char **buffer) |
261 |
{ |
{ |
262 |
char tmp[256],*ptr,*nptr; |
u_char tmp[256],*ptr,*nptr; |
263 |
size_t len,tot_len; |
size_t len,tot_len; |
264 |
FILE *fd; |
FILE *fd; |
265 |
|
|
297 |
{ |
{ |
298 |
void *p; |
void *p; |
299 |
|
|
300 |
#ifdef __linux__ |
#if defined(__linux__) || HAS_POSIX_MEMALIGN |
301 |
if (posix_memalign((void *)&p,boundary,size)) |
if (posix_memalign((void *)&p,boundary,size)) |
302 |
#else |
#else |
303 |
#ifdef __CYGWIN__ |
#if defined(__CYGWIN__) || defined(SUNOS) |
304 |
if (!(p = memalign(boundary,size))) |
if (!(p = memalign(boundary,size))) |
305 |
#else |
#else |
306 |
if (!(p = malloc(size))) |
if (!(p = malloc(size))) |
312 |
return p; |
return p; |
313 |
} |
} |
314 |
|
|
315 |
|
/* Block specified signal for calling thread */ |
316 |
|
int m_signal_block(int sig) |
317 |
|
{ |
318 |
|
sigset_t sig_mask; |
319 |
|
sigemptyset(&sig_mask); |
320 |
|
sigaddset(&sig_mask,sig); |
321 |
|
return(pthread_sigmask(SIG_BLOCK,&sig_mask,NULL)); |
322 |
|
} |
323 |
|
|
324 |
|
/* Unblock specified signal for calling thread */ |
325 |
|
int m_signal_unblock(int sig) |
326 |
|
{ |
327 |
|
sigset_t sig_mask; |
328 |
|
sigemptyset(&sig_mask); |
329 |
|
sigaddset(&sig_mask,sig); |
330 |
|
return(pthread_sigmask(SIG_UNBLOCK,&sig_mask,NULL)); |
331 |
|
} |
332 |
|
|
333 |
|
/* Set non-blocking mode on a file descriptor */ |
334 |
|
int m_fd_set_non_block(int fd) |
335 |
|
{ |
336 |
|
int flags; |
337 |
|
|
338 |
|
if ((flags = fcntl(fd,F_GETFL,0)) < 1) |
339 |
|
return(-1); |
340 |
|
|
341 |
|
return(fcntl(fd,F_SETFL, flags | O_NONBLOCK)); |
342 |
|
} |
343 |
|
|
344 |
|
/* Map a memory zone from a file */ |
345 |
|
u_char *memzone_map_file(int fd,size_t len) |
346 |
|
{ |
347 |
|
return(mmap(NULL,len,PROT_READ|PROT_WRITE,MAP_SHARED,fd,(off_t)0)); |
348 |
|
} |
349 |
|
|
350 |
|
/* Map a memory zone from a file, with copy-on-write (COW) */ |
351 |
|
u_char *memzone_map_cow_file(int fd,size_t len) |
352 |
|
{ |
353 |
|
return(mmap(NULL,len,PROT_READ|PROT_WRITE,MAP_PRIVATE,fd,(off_t)0)); |
354 |
|
} |
355 |
|
|
356 |
|
/* Create a file to serve as a memory zone */ |
357 |
|
int memzone_create_file(char *filename,size_t len,u_char **ptr) |
358 |
|
{ |
359 |
|
int fd; |
360 |
|
|
361 |
|
if ((fd = open(filename,O_CREAT|O_RDWR,S_IRWXU)) == -1) { |
362 |
|
perror("memzone_create_file: open"); |
363 |
|
return(-1); |
364 |
|
} |
365 |
|
|
366 |
|
if (ftruncate(fd,len) == -1) { |
367 |
|
perror("memzone_create_file: ftruncate"); |
368 |
|
close(fd); |
369 |
|
return(-1); |
370 |
|
} |
371 |
|
|
372 |
|
*ptr = memzone_map_file(fd,len); |
373 |
|
|
374 |
|
if (!*ptr) { |
375 |
|
close(fd); |
376 |
|
fd = -1; |
377 |
|
} |
378 |
|
|
379 |
|
return(fd); |
380 |
|
} |
381 |
|
|
382 |
|
/* Open a file to serve as a COW memory zone */ |
383 |
|
int memzone_open_cow_file(char *filename,size_t len,u_char **ptr) |
384 |
|
{ |
385 |
|
int fd; |
386 |
|
|
387 |
|
if ((fd = open(filename,O_RDWR,S_IRWXU)) == -1) { |
388 |
|
perror("memzone_open_file: open"); |
389 |
|
return(-1); |
390 |
|
} |
391 |
|
|
392 |
|
*ptr = memzone_map_cow_file(fd,len); |
393 |
|
|
394 |
|
if (!*ptr) { |
395 |
|
close(fd); |
396 |
|
fd = -1; |
397 |
|
} |
398 |
|
|
399 |
|
return(fd); |
400 |
|
} |
401 |
|
|
402 |
|
/* Open a file and map it in memory */ |
403 |
|
int memzone_open_file(char *filename,u_char **ptr,off_t *fsize) |
404 |
|
{ |
405 |
|
struct stat fprop; |
406 |
|
int fd; |
407 |
|
|
408 |
|
if ((fd = open(filename,O_RDWR,S_IRWXU)) == -1) |
409 |
|
return(-1); |
410 |
|
|
411 |
|
if (fstat(fd,&fprop) == -1) |
412 |
|
goto err_fstat; |
413 |
|
|
414 |
|
*fsize = fprop.st_size; |
415 |
|
if (!(*ptr = memzone_map_file(fd,*fsize))) |
416 |
|
goto err_mmap; |
417 |
|
|
418 |
|
return(fd); |
419 |
|
|
420 |
|
err_mmap: |
421 |
|
err_fstat: |
422 |
|
close(fd); |
423 |
|
return(-1); |
424 |
|
} |
425 |
|
|
426 |
|
/* Compute NVRAM checksum */ |
427 |
|
m_uint16_t nvram_cksum(m_uint16_t *ptr,size_t count) |
428 |
|
{ |
429 |
|
m_uint32_t sum = 0; |
430 |
|
|
431 |
|
while(count > 1) { |
432 |
|
sum = sum + ntohs(*ptr); |
433 |
|
ptr++; |
434 |
|
count -= sizeof(m_uint16_t); |
435 |
|
} |
436 |
|
|
437 |
|
if (count > 0) |
438 |
|
sum = sum + ((ntohs(*ptr) & 0xFF) << 8); |
439 |
|
|
440 |
|
while(sum>>16) |
441 |
|
sum = (sum & 0xffff) + (sum >> 16); |
442 |
|
|
443 |
|
return(~sum); |
444 |
|
} |
445 |
|
|
446 |
|
/* Byte-swap a memory block */ |
447 |
|
void mem_bswap32(void *ptr,size_t len) |
448 |
|
{ |
449 |
|
m_uint32_t *p = ptr; |
450 |
|
size_t count = len >> 2; |
451 |
|
int i; |
452 |
|
|
453 |
|
for(i=0;i<count;i++,p++) |
454 |
|
*p = swap32(*p); |
455 |
|
} |
456 |
|
|
457 |
|
/* Reverse a byte */ |
458 |
|
m_uint8_t m_reverse_u8(m_uint8_t val) |
459 |
|
{ |
460 |
|
m_uint8_t res = 0; |
461 |
|
int i; |
462 |
|
|
463 |
|
for(i=0;i<8;i++) |
464 |
|
if (val & (1 << i)) |
465 |
|
res |= 1 << (7 - i); |
466 |
|
|
467 |
|
return(res); |
468 |
|
} |