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> |
299 |
#ifdef __linux__ |
#ifdef __linux__ |
300 |
if (posix_memalign((void *)&p,boundary,size)) |
if (posix_memalign((void *)&p,boundary,size)) |
301 |
#else |
#else |
302 |
#ifdef __CYGWIN__ |
#if defined(__CYGWIN__) || defined(SUNOS) |
303 |
if (!(p = memalign(boundary,size))) |
if (!(p = memalign(boundary,size))) |
304 |
#else |
#else |
305 |
if (!(p = malloc(size))) |
if (!(p = malloc(size))) |
311 |
return p; |
return p; |
312 |
} |
} |
313 |
|
|
314 |
|
/* Block specified signal for calling thread */ |
315 |
|
int m_signal_block(int sig) |
316 |
|
{ |
317 |
|
sigset_t sig_mask; |
318 |
|
sigemptyset(&sig_mask); |
319 |
|
sigaddset(&sig_mask,sig); |
320 |
|
return(pthread_sigmask(SIG_BLOCK,&sig_mask,NULL)); |
321 |
|
} |
322 |
|
|
323 |
|
/* Unblock specified signal for calling thread */ |
324 |
|
int m_signal_unblock(int sig) |
325 |
|
{ |
326 |
|
sigset_t sig_mask; |
327 |
|
sigemptyset(&sig_mask); |
328 |
|
sigaddset(&sig_mask,sig); |
329 |
|
return(pthread_sigmask(SIG_UNBLOCK,&sig_mask,NULL)); |
330 |
|
} |
331 |
|
|
332 |
|
/* Set non-blocking mode on a file descriptor */ |
333 |
|
int m_fd_set_non_block(int fd) |
334 |
|
{ |
335 |
|
int flags; |
336 |
|
|
337 |
|
if ((flags = fcntl(fd,F_GETFL,0)) < 1) |
338 |
|
return(-1); |
339 |
|
|
340 |
|
return(fcntl(fd,F_SETFL, flags | O_NONBLOCK)); |
341 |
|
} |
342 |
|
|
343 |
|
/* Map a memory zone from a file */ |
344 |
|
u_char *memzone_map_file(int fd,size_t len) |
345 |
|
{ |
346 |
|
return(mmap(NULL,len,PROT_READ|PROT_WRITE,MAP_SHARED,fd,(off_t)0)); |
347 |
|
} |
348 |
|
|
349 |
|
/* Map a memory zone from a file, with copy-on-write (COW) */ |
350 |
|
u_char *memzone_map_cow_file(int fd,size_t len) |
351 |
|
{ |
352 |
|
return(mmap(NULL,len,PROT_READ|PROT_WRITE,MAP_PRIVATE,fd,(off_t)0)); |
353 |
|
} |
354 |
|
|
355 |
|
/* Create a file to serve as a memory zone */ |
356 |
|
int memzone_create_file(char *filename,size_t len,u_char **ptr) |
357 |
|
{ |
358 |
|
int fd; |
359 |
|
|
360 |
|
if ((fd = open(filename,O_CREAT|O_RDWR,S_IRWXU)) == -1) { |
361 |
|
perror("memzone_create_file: open"); |
362 |
|
return(-1); |
363 |
|
} |
364 |
|
|
365 |
|
if (ftruncate(fd,len) == -1) { |
366 |
|
perror("memzone_create_file: ftruncate"); |
367 |
|
close(fd); |
368 |
|
return(-1); |
369 |
|
} |
370 |
|
|
371 |
|
*ptr = memzone_map_file(fd,len); |
372 |
|
|
373 |
|
if (!*ptr) { |
374 |
|
close(fd); |
375 |
|
fd = -1; |
376 |
|
} |
377 |
|
|
378 |
|
return(fd); |
379 |
|
} |
380 |
|
|
381 |
|
/* Open a file to serve as a COW memory zone */ |
382 |
|
int memzone_open_cow_file(char *filename,size_t len,u_char **ptr) |
383 |
|
{ |
384 |
|
int fd; |
385 |
|
|
386 |
|
if ((fd = open(filename,O_RDWR,S_IRWXU)) == -1) { |
387 |
|
perror("memzone_open_file: open"); |
388 |
|
return(-1); |
389 |
|
} |
390 |
|
|
391 |
|
*ptr = memzone_map_cow_file(fd,len); |
392 |
|
|
393 |
|
if (!*ptr) { |
394 |
|
close(fd); |
395 |
|
fd = -1; |
396 |
|
} |
397 |
|
|
398 |
|
return(fd); |
399 |
|
} |
400 |
|
|
401 |
|
/* Open a file and map it in memory */ |
402 |
|
int memzone_open_file(char *filename,u_char **ptr,off_t *fsize) |
403 |
|
{ |
404 |
|
struct stat fprop; |
405 |
|
int fd; |
406 |
|
|
407 |
|
if ((fd = open(filename,O_RDWR,S_IRWXU)) == -1) |
408 |
|
return(-1); |
409 |
|
|
410 |
|
if (fstat(fd,&fprop) == -1) |
411 |
|
goto err_fstat; |
412 |
|
|
413 |
|
*fsize = fprop.st_size; |
414 |
|
if (!(*ptr = memzone_map_file(fd,*fsize))) |
415 |
|
goto err_mmap; |
416 |
|
|
417 |
|
return(fd); |
418 |
|
|
419 |
|
err_mmap: |
420 |
|
err_fstat: |
421 |
|
close(fd); |
422 |
|
return(-1); |
423 |
|
} |
424 |
|
|
425 |
|
/* Compute NVRAM checksum */ |
426 |
|
m_uint16_t nvram_cksum(m_uint16_t *ptr,size_t count) |
427 |
|
{ |
428 |
|
m_uint32_t sum = 0; |
429 |
|
|
430 |
|
while(count > 1) { |
431 |
|
sum = sum + ntohs(*ptr); |
432 |
|
ptr++; |
433 |
|
count -= sizeof(m_uint16_t); |
434 |
|
} |
435 |
|
|
436 |
|
if (count > 0) |
437 |
|
sum = sum + ((ntohs(*ptr) & 0xFF) << 8); |
438 |
|
|
439 |
|
while(sum>>16) |
440 |
|
sum = (sum & 0xffff) + (sum >> 16); |
441 |
|
|
442 |
|
return(~sum); |
443 |
|
} |
444 |
|
|
445 |
|
/* Byte-swap a memory block */ |
446 |
|
void mem_bswap32(void *ptr,size_t len) |
447 |
|
{ |
448 |
|
m_uint32_t *p = ptr; |
449 |
|
size_t count = len >> 2; |
450 |
|
int i; |
451 |
|
|
452 |
|
for(i=0;i<count;i++,p++) |
453 |
|
*p = swap32(*p); |
454 |
|
} |