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> |
339 |
|
|
340 |
return(fcntl(fd,F_SETFL, flags | O_NONBLOCK)); |
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 |
|
} |