void test_cmsg_nexthdr() { struct msghdr mh; struct cmsghdr cmh; struct cmsghdr *cmhp, *cmhnextp; char ancbuf[10240]; char magic[] = "MAGIC"; set_funcname("test_cmsg_nexthdr", sizeof("test_cmsg_nexthdr")); /* * Test: More than one cmsghdr */ init_hdrs(&mh, &cmh, ancbuf, sizeof(ancbuf)); mh.msg_control = (caddr_t)ancbuf; mh.msg_controllen = CMSG_SPACE(0) * 2; /* 2 cmsghdr with no data */ cmh.cmsg_len = CMSG_LEN(0); /* * Copy the same instance of cmsghdr twice. Use a magic value * to id the second copy. */ bcopy((void *)&cmh, (void *)ancbuf, sizeof(cmh)); strlcpy((char *)&cmh, (const char *)&magic, sizeof(magic)); bcopy((void *)&cmh, (void *)((caddr_t)ancbuf + CMSG_SPACE(0)), sizeof(cmh)); cmhp = CMSG_FIRSTHDR(&mh); cmhnextp = CMSG_NXTHDR(&mh, cmhp); checkstr((const char *)&magic, (const char *)cmhnextp, sizeof(magic), "more than one cmsghdr\0"); /* * Test: only one cmsghdr */ init_hdrs(&mh, &cmh, ancbuf, sizeof(ancbuf)); mh.msg_control = (caddr_t)ancbuf; mh.msg_controllen = CMSG_SPACE(0); cmh.cmsg_len = CMSG_LEN(0); bcopy((void *)&cmh, (void *)ancbuf, sizeof(cmh)); cmhp = CMSG_FIRSTHDR(&mh); cmhnextp = CMSG_NXTHDR(&mh, cmhp); checkptr(NULL, (caddr_t)cmhnextp, "only one cmsghdr\0"); /* * Test: NULL cmsg pointer */ init_hdrs(&mh, &cmh, ancbuf, sizeof(ancbuf)); mh.msg_control = (caddr_t)ancbuf; mh.msg_controllen = sizeof(ancbuf); cmh.cmsg_len = sizeof(ancbuf); bcopy((void *)&cmh, (void *)ancbuf, sizeof(cmh)); cmhp = CMSG_FIRSTHDR(&mh); cmhnextp = CMSG_NXTHDR(&mh, NULL); checkptr((caddr_t)cmhp, (caddr_t)cmhnextp, "null second argument\0"); }
char *mygetline (FILE *fp) { if (buf1==NULL) { checkptr(buf1 = (char *) malloc (buf_size)); checkptr(buf2 = (char *) malloc (buf_size)); } buf1=fgets(buf1, buf_size, fp); if (buf1==NULL) return buf1; /* end of file */ while (buf1[strlen(buf1)-1] != '\n') { /* failed to get whole line */ buf_size *= 2; new_buffers(); if (fgets(buf1+strlen(buf1),buf_size-strlen(buf1),fp)==NULL) return buf1; /* end of file */ } return buf1; }
void test_cmsg_firsthdr() { struct msghdr mh; struct cmsghdr cmh; struct cmsghdr *cmhp; char ancbuf[1024]; char magic[] = "MAGIC"; set_funcname("test_cmsg_firsthdr", sizeof("test_cmsg_firsthdr")); /* CMSG_FIRSTHDR() where msg_control is NULL */ init_hdrs(&mh, NULL, NULL, 0); mh.msg_control = NULL; cmhp = CMSG_FIRSTHDR(&mh); checkptr(NULL, (caddr_t)cmhp, "msg_control is NULL\0"); /* - where msg_controllen < sizeof cmsghdr */ init_hdrs(&mh, NULL, NULL, 0); mh.msg_control = (caddr_t)&cmh; mh.msg_controllen = sizeof(cmh) - 1; cmhp = CMSG_FIRSTHDR(&mh); checkptr(NULL, (caddr_t)cmhp, "msg_controllen < sizeof cmsghdr\0"); /* - where msg_controllen == 0 */ init_hdrs(&mh, NULL, NULL, 0); mh.msg_control = (caddr_t)&cmh; mh.msg_controllen = 0; cmhp = CMSG_FIRSTHDR(&mh); checkptr(NULL, (caddr_t)cmhp, "msg_controllen == 0\0"); /* no errors */ init_hdrs(&mh, &cmh, ancbuf, sizeof(ancbuf)); memset((void *)ancbuf, 0, sizeof(ancbuf)); mh.msg_control = (caddr_t)ancbuf; mh.msg_controllen = sizeof(ancbuf); strlcpy((char *)&cmh, (const char *)&magic, sizeof(magic)); bcopy((void *)&cmh, (void *)ancbuf, sizeof(cmh)); cmhp = CMSG_FIRSTHDR(&mh); checkstr((const char *)&magic, (const char *)cmhp, sizeof(magic), "with payload\0"); }
void test_rth_init() { char buf[10240]; char *pbuf; set_funcname("test_rth_init", sizeof("test_rth_init\0")); pbuf = inet6_rth_init((void *)buf, 10, IPV6_RTHDR_TYPE_0, 100); checkptr(NULL, pbuf, "buffer too small\0"); pbuf = inet6_rth_init((void *)buf, 10240, IPV6_RTHDR_TYPE_0, 0); checkptr((caddr_t)&buf, pbuf, "0 segments\0"); pbuf = inet6_rth_init((void *)buf, 10240, IPV6_RTHDR_TYPE_0, 127); checkptr((caddr_t)&buf, pbuf, "127 segments\0"); pbuf = inet6_rth_init((void *)buf, 10240, IPV6_RTHDR_TYPE_0, -1); checkptr(NULL, pbuf, "negative number of segments\0"); pbuf = inet6_rth_init((void *)buf, 10240, IPV6_RTHDR_TYPE_0, 128); checkptr(NULL, pbuf, "128 segments\0"); }
void *makeBuf( const size_t length, const char *file, const size_t line) { BUFQUEUE *current; if ( length == 0 ) { printmsg(0,"makeBuf: Invalid request for zero byte buffer"); bugout( file, line ); } current = malloc( length + sizeof (BUFQUEUE) ); /*--------------------------------------------------------------------*/ /* Verify our buffer was properly allocated */ /*--------------------------------------------------------------------*/ if ( !current ) checkptr( file, line); /*--------------------------------------------------------------------*/ /* Chain new buffer to top of queue */ /*--------------------------------------------------------------------*/ #ifdef UDEBUG entries++; current->length = length; current->entry = entries; #endif current->signature = SIGNATURE; current->userBuffer = (void *) (sizeof (BUFQUEUE) + (char *) current); current->previous = top; top = current; return current->userBuffer; } /* makeBuf */
char *strpool( const char *input , const char UUFAR *file, size_t line) { unsigned len; int best_fit = SHRT_MAX; char *result; STR_QUEUE *current = anchor; STR_QUEUE *last = anchor; STR_QUEUE *save = NULL; #ifdef __DEBUG_ALLOC__ _heap_check(); #endif if ( input == NULL ) { printmsg(0,"strpool: NULL pointer passed to newstr()"); bugout( file, line ); /* Become Info Highway Roadkill */ } len = strlen( input ); /*--------------------------------------------------------------------*/ /* Handle over length strings */ /*--------------------------------------------------------------------*/ if ( len > UCHAR_MAX ) { result = strdup( input ); if ( !result) checkptr( file, line); return result; } /*--------------------------------------------------------------------*/ /* Perform best fit search */ /*--------------------------------------------------------------------*/ while(current != NULL ) { int available; /*--------------------------------------------------------------------*/ /* Scan current buffer for the string */ /*--------------------------------------------------------------------*/ if ( ! bflag[ F_SPEEDOVERMEMORY ] ) { char *target = current->pool; char *bufend = target + current->used; while( target < bufend ) { int target_len = (unsigned char) *target++; int diff = target_len - (int) len; if ((diff >= 0 ) && equal( target + diff, input)) { #ifdef UDEBUG duplicates ++; saved += (long) len + 2; #endif return target+diff; } target += target_len + 1; /* Step to start of next string */ } /* while( offset < current->used ) */ } /* if */ /*--------------------------------------------------------------------*/ /* No string in this buffer, look for best fit in case we need */ /* to allocate the string from scratch */ /*--------------------------------------------------------------------*/ available = (int) (pool_size - current->used); if (( available < (int) best_fit) && (available > (int) (len+1) )) { best_fit = available; save = current; } else last = current; /* Save last buffer in case we have to chain new buffer in */ current = current->next_link; } /* while */ /*--------------------------------------------------------------------*/ /* We have no matching string, we have to insert the new string */ /* into our pool */ /*--------------------------------------------------------------------*/ if ( save == NULL ) /* We find a buffer? */ { /* No --> Allocate a new one */ pools ++; save = malloc( sizeof *save ); if ( !save) checkptr( file, line); if ( anchor == NULL ) { #ifdef UDEBUG atexit( dump_pool ); #endif anchor = save; } else last->next_link = save; save->used = 0; save->next_link = NULL; } /*--------------------------------------------------------------------*/ /* Save the string, update memory available in current pool, */ /* and return to the caller with the new string */ /*--------------------------------------------------------------------*/ result = save->pool + save->used; *result = (char) ((unsigned char) len); strcpy( ++result, input ); save->used += len + 2; #ifdef UDEBUG strings ++; used += (long) len + 2; #endif return result; } /* strpool */
char *strsave (char *s) { char *t = malloc (strlen(s)+1); checkptr(t); strcpy(t,s); return t; }
void new_buffers(void) { checkptr(buf1 = (char *) realloc(buf1, buf_size)); checkptr(buf2 = (char *) realloc(buf2, buf_size)); }