/* * Cisco router simulation platform. * Copyright (c) 2005,2006 Christophe Fillot. All rights reserved. * * Utility functions. */ #include "dynamips_common.h" #define _GNU_SOURCE #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #ifdef __CYGWIN__ #include #endif #include "utils.h" extern FILE *log_file; /* Add an element to a list */ m_list_t *m_list_add(m_list_t **head,void *data) { m_list_t *item; if ((item = malloc(sizeof(*item))) != NULL) { item->data = data; item->next = *head; *head = item; } return item; } /* Dynamic sprintf */ char *dyn_sprintf(const char *fmt,...) { int n,size = 512; va_list ap; char *p,*p2; if ((p = malloc(size)) == NULL) { perror("dyn_sprintf: malloc"); return NULL; } for(;;) { /* Try to print in the allocated space */ va_start(ap,fmt); n = vsnprintf(p,size,fmt,ap); va_end(ap); /* If that worked, return the string */ if ((n > -1) && (n < size)) return p; /* Else try again with more space. */ if (n > -1) size = n + 1; else size *= 2; if ((p2 = realloc(p,size)) == NULL) { perror("dyn_sprintf: realloc"); free(p); return NULL; } p = p2; } } /* Split a string */ int m_strsplit(char *str,char delim,char **array,int max_count) { int i,pos = 0; size_t len; char *ptr; for(i=0;i sizeof(hexval) || hexval[*in] == BAD) break; if (empty) { *out = (unsigned char)hexval[*in] << 4; } else { *out |= (unsigned char)hexval[*in]; ++out; ++len; } ++in; empty = !empty; } return(len); } /* Ugly function that dumps a structure in hexa and ascii. */ void mem_dump(FILE *f_output,u_char *pkt,u_int len) { u_int x,i = 0, tmp; while (i < len) { if ((len - i) > 16) x = 16; else x = len - i; fprintf(f_output,"%4.4x: ",i); for (tmp=0;tmp= 'A') && (c <= 'Z')) || ((c >= 'a') && (c <= 'z')) || ((c >= '0') && (c <= '9'))) fprintf(f_output,"%c",c); else fputs(".",f_output); } i += x; fprintf(f_output,"\n"); } fprintf(f_output,"\n"); fflush(f_output); } /* Logging function */ void m_flog(FILE *fd,char *module,char *fmt,va_list ap) { struct timespec spec; struct tm tmn; char buf[256]; if (fd != NULL) { clock_gettime(CLOCK_REALTIME, &spec); gmtime_r(&spec.tv_sec, &tmn); strftime(buf,sizeof(buf),"%b %d %H:%M:%S",&tmn); fprintf(fd,"%s.%03ld %s: ",buf,(long)spec.tv_nsec/1000000,module); vfprintf(fd,fmt,ap); fflush(fd); } } /* Logging function */ void m_log(char *module,char *fmt,...) { va_list ap; va_start(ap,fmt); m_flog(log_file,module,fmt,ap); va_end(ap); } /* Write an array of string to a logfile */ void m_flog_str_array(FILE *fd,int count,char *str[]) { int i; for(i=0;i 1) { sum = sum + ntohs(*ptr); ptr++; count -= sizeof(m_uint16_t); } if (count > 0) sum = sum + ((ntohs(*ptr) & 0xFF) << 8); while(sum>>16) sum = (sum & 0xffff) + (sum >> 16); return(~sum); } /* Byte-swap a memory block */ void mem_bswap32(void *ptr,size_t len) { m_uint32_t *p _not_aligned = ptr; size_t count = len >> 2; int i; for(i=0;inext; for(i=0;ifd[i] != -1) { shutdown(p->fd[i],2); close(p->fd[i]); } } if (pool != p) free(p); } } /* Initialize an empty pool */ void fd_pool_init(fd_pool_t *pool) { int i; for(i=0;ifd[i] = -1; pool->next = NULL; } /* Get a free slot for a FD in a pool */ int fd_pool_get_free_slot(fd_pool_t *pool,int **slot) { fd_pool_t *p; int i; for(p=pool;p;p=p->next) { for(i=0;ifd[i] == -1) { *slot = &p->fd[i]; return(0); } } } /* No free slot, allocate a new pool */ if (!(p = malloc(sizeof(*p)))) return(-1); fd_pool_init(p); *slot = &p->fd[0]; p->next = pool->next; pool->next = p; return(0); } /* Fill a FD set and get the maximum FD in order to use with select */ int fd_pool_set_fds(fd_pool_t *pool,fd_set *fds) { fd_pool_t *p; int i,max_fd = -1; for(p=pool;p;p=p->next) for(i=0;ifd[i] != -1) { FD_SET(p->fd[i],fds); if (p->fd[i] > max_fd) max_fd = p->fd[i]; } } return(max_fd); } /* Send a buffer to all FDs of a pool */ int fd_pool_send(fd_pool_t *pool,void *buffer,size_t len,int flags) { fd_pool_t *p; ssize_t res; int i,err; for(p=pool,err=0;p;p=p->next) for(i=0;ifd[i] == -1) continue; res = send(p->fd[i],buffer,len,flags); if (res != len) { shutdown(p->fd[i],2); close(p->fd[i]); p->fd[i] = -1; err++; } } return(err); } /* Call a function for each FD having incoming data */ int fd_pool_check_input(fd_pool_t *pool,fd_set *fds, void (*cbk)(int *fd_slot,void *opt),void *opt) { fd_pool_t *p; int i,count; for(p=pool,count=0;p;p=p->next) for(i=0;ifd[i] != -1) && FD_ISSET(p->fd[i],fds)) { cbk(&p->fd[i],opt); count++; } } return(count); } /* Equivalent to fprintf, but for a posix fd */ ssize_t fd_printf(int fd,int flags,char *fmt,...) { char buffer[2048]; va_list ap; va_start(ap,fmt); vsnprintf(buffer,sizeof(buffer),fmt,ap); va_end(ap); return(send(fd,buffer,strlen(buffer),flags)); }