int dxcat(dbuf_t * target, dbuf_t * source, int start, int len) { dchecksig(target); dchecksig(source); if(len == -1) { len = source->dsize - start; } if(len < 0) { debug(DBG_GLOBAL, 0, "negative length to dxcat!"); print_backtrace(); } if(start + len > source->dsize) { debug(DBG_MEMORY, 10, "dxcat: Trying to copy % bytes from %d offset, but the total len is %d"); return 0; } debug(DBG_MEMORY, 10, "dxcat of %d bytes, taken from pos %d\n", len, start); if(dgrow(target, len)) { memcpy(&target->buf[target->dsize], &(source->buf[start]), len); debug(DBG_MEMORY, 10, "dsize before grow: %d, delta: %d", target->dsize, source->dsize); target->dsize = target->dsize + len; debug(DBG_MEMORY, 10, "new dsize after grow: %d", target->dsize); return 1; } else { return 0; } }
int dstrcat(dbuf_t * d, char *str, int total) { dchecksig(d); dmemcat(d, str, total); if(d->dsize >= d->size) { dgrow(d, 10); } d->buf[d->dsize] = 0; // null-terminate to keep the zero "ahead" return 1; }
dbuf_t *dtry_reasm_timed(void *pile, uint32_t xid, char *data, uint16_t len, uint16_t offs, int more, time_t now) { reasm_pile_struct_t *rp = (void *)(((dbuf_t *)pile)->buf); reasm_chunk_t *chk; if (now > 0) { check_timeouts(rp, now); } if(offs + len > rp->mtu) { debug(DBG_REASM, 10, "Offset + length (%d + %d) of fragment > MTU (%d), discard", offs, len, rp->mtu); return NULL; } if ((offs > 0) && (offs < HOLE_MIN_LENGTH)) { debug(DBG_REASM, 10, "Offset %d less than min hole length %d\n", offs, HOLE_MIN_LENGTH); return NULL; } chk = hfind(rp->chs, &xid, sizeof(xid)); if (!chk) { debug(DBG_REASM, 10, "Reasm chunk %lu not found, creating", xid); chk = malloc(sizeof(reasm_chunk_t)); chk->xid = xid; chk->maxfrags = rp->maxfrags; chk->hole = 0; chk->esize = rp->ftu; chk->d = dalloc(chk->esize); chk->deadline = now + rp->reasm_timeout; memset(chk->d->buf, 0xaa, chk->d->size); hole_set(chk, 0, chk->esize, 0); hinsert(rp->chs, &xid, sizeof(xid), chk, chk_destructor, NULL, NULL, NULL); TAILQ_INSERT_TAIL(&rp->lru, chk, entries); } else { debug(DBG_REASM, 10, "Reasm chunk %lu found", xid); } debug(DBG_REASM, 100, "Chunk data (hole: %d, esize: %d):", chk->hole, chk->esize); debug_dump(DBG_REASM, 100, chk->d->buf, chk->d->size); if(offs + len > chk->d->size) { debug(DBG_REASM, 10, "Reasm chunk %lu overflow - %d + %d > %d", xid, offs, len, chk->d->size); /* We already checked the MTU overflow above, so we can safely reallocate here */ int oldsize = chk->d->size; dgrow(chk->d, rp->mtu - chk->d->size); hole_grow(chk, oldsize-1, chk->d->size); chk->esize = chk->d->size; debug(DBG_REASM, 100, "Fresh chunk data after growth to MTU:"); debug_dump(DBG_REASM, 100, chk->d->buf, chk->d->size); } chk->atime = now; return dperform_reasm(rp, chk, xid, data, len, offs, more); }
int dappendfile(dbuf_t * d, char *fname) { int fd; struct stat filestat; int nread; int result = 1; fd = open(fname, O_RDONLY); if(fd != -1) { if(fstat(fd, &filestat) == 0) { debug(DBG_GLOBAL, 5, "file size: %d", filestat.st_size); if(dgrow(d, filestat.st_size)) { nread = read(fd, &d->buf[d->dsize], filestat.st_size); if(nread != filestat.st_size) { debug(DBG_GLOBAL, 1, " dappendfile %s: tried to load %d bytes, loaded %d bytes", fname, filestat.st_size, nread); result = 0; } else { d->dsize += nread; } } else { debug(DBG_GLOBAL, 1, "Could not dgrow by %d bytes to load file %s", filestat.st_size, fname); result = 0; } } else { debug(DBG_GLOBAL, 1, "Could not stat file %s, error: %s", fname, strerror(errno)); result = 0; } close(fd); } else { debug(DBG_GLOBAL, 1, "Could not open the file %s, error: %s", fname, strerror(errno)); result = 0; } return result; }
int expand_timer_list(void) { int oldsize = timer_list_length(); int newsize; int i; if(timer_first_inactive != timer_last_inactive) { debug(DBG_TIMERS, 0, "Inappropriate call to expand_timer_list: first %d, last %d", timer_first_inactive, timer_last_inactive); return 0; } if(dgrow(timers_list, sizeof(timerwheel_entry_t) * TIMER_DBUF_INCREMENT)) { timers_list->dsize = timers_list->size; timerwheel_entry_t *timers = (timerwheel_entry_t *) timers_list->buf; newsize = timer_list_length(); for(i = oldsize; i < newsize; i++) { timers[i].state = TIMER_INACTIVE; timers[i].generation = 0; timers[i].next_inactive_index = i + 1; debug(DBG_TIMERS, 10, "Set next inactive index for %d to %d", i, i + 1); } timers[newsize - 1].next_inactive_index = 0; timers[timer_last_inactive].next_inactive_index = oldsize; debug(DBG_TIMERS, 10, "Set next inactive index for %d to %d", timer_last_inactive, oldsize); timer_last_inactive = newsize - 1; debug(DBG_TIMERS, 10, "Grown the timer list, new first: %d, last: %d", timer_first_inactive, timer_last_inactive); return 1; } else { return 0; } }
int dappendfill(dbuf_t * target, char c, int count) { void *sav; if(count == 0) { return 0; } debug(DBG_GLOBAL, 5, "dappendfill of %d, %d times\n", c, count); sav = target->buf; if(dgrow(target, count)) { memset(&target->buf[target->dsize], c, count); debug(DBG_MEMORY, 10, "dsize before grow: %d, delta: %d", target->dsize, count); target->dsize = target->dsize + count; debug(DBG_MEMORY, 10, "new dsize after grow: %d", target->dsize); debug(DBG_MEMORY, 10, "old buf before grow: %x, new buf: %x", sav, target->buf); return count; } else { return 0; } }
int dmemcat(dbuf_t * d, void *str, int total) { dchecksig(d); if(total == -1) { total = strlen(str); } if((d->size - d->dsize) < total + 1) { int delta = (1 + (1 + total) / d->growsize) * d->growsize; debug(DBG_GLOBAL, 250, "size: %d, dsize: %d, total: %d - need to grow", d->size, d->dsize, total); if(!dgrow(d, delta)) { debug(DBG_GLOBAL, 2, "failed to grow dbuf by %d bytes", delta); return 0; // failed to grow } else { debug(DBG_GLOBAL, 250, "grown the dbuf by %d bytes", delta); } } memcpy(&d->buf[d->dsize], str, total); d->dsize += total; return 1; }