diff --git a/nsock/src/filespace.c b/nsock/src/filespace.c index f725701e8..2502ca848 100644 --- a/nsock/src/filespace.c +++ b/nsock/src/filespace.c @@ -78,47 +78,6 @@ int filespace_init(struct filespace *fs, int initial_size) { return 0; } -/* Prepend an n-char string to a filespace */ -int fs_prepend(char *str, int len, struct filespace *fs){ - char *tmpstr; - - if (len < 0) - return -1; - - if (len == 0) - return 0; - - if (fs->current_alloc - fs->current_size < len + 2) { - fs->current_alloc = (int)(fs->current_alloc * 1.4 + 1); - fs->current_alloc += 100 + len; - - tmpstr = (char *)safe_malloc(fs->current_alloc); - memcpy(tmpstr, fs->str, fs->current_size); - - fs->pos = (fs->pos - fs->str) + tmpstr; - if (fs->str) - free(fs->str); - - fs->str = tmpstr; - } - if (fs->current_size > 0) - memmove(fs->str + len, fs->str, fs->current_size); - memcpy(fs->str, str, len); - - fs->current_size += len; - fs->str[fs->current_size] = '\0'; - return 0; -} - -/* Used when you want to start over with a filespace you have been using (it - * sets the length to zero and the pointers to the beginning of memory , etc */ -int fs_clear(struct filespace *fs) { - fs->current_size = 0; - fs->pos = fs->str; - fs->str[0] = '\0'; /* Not necessary, possible help with debugging */ - return 0; -} - int fs_free(struct filespace *fs) { if (fs->str) free(fs->str); @@ -128,3 +87,39 @@ int fs_free(struct filespace *fs) { return 0; } +/* Concatenate a string to the end of a filespace */ +int fs_cat(struct filespace *fs, const char *str, int len) { + if (len < 0) + return -1; + + if (len == 0) + return 0; + + /* + printf("fscat: current_alloc=%d; current_size=%d; len=%d\n", + fs->current_alloc, fs->current_size, len); + */ + + if (fs->current_alloc - fs->current_size < len + 2) { + char *tmpstr; + + fs->current_alloc = (int)(fs->current_alloc * 1.4 + 1); + fs->current_alloc += 100 + len; + + tmpstr = (char *)safe_malloc(fs->current_alloc); + memcpy(tmpstr, fs->str, fs->current_size); + + fs->pos = (fs->pos - fs->str) + tmpstr; + + if (fs->str) + free(fs->str); + + fs->str = tmpstr; + } + memcpy(fs->str + fs->current_size, str, len); + + fs->current_size += len; + fs->str[fs->current_size] = '\0'; + return 0; +} + diff --git a/nsock/src/filespace.h b/nsock/src/filespace.h index 0b4f4de9d..9744ea29d 100644 --- a/nsock/src/filespace.h +++ b/nsock/src/filespace.h @@ -77,8 +77,6 @@ #include #endif -#define FILESPACE_LENGTH(fs) ((fs)->current_size) -#define FILESPACE_STR(fs) ((fs)->str) struct filespace { int current_size; @@ -89,107 +87,21 @@ struct filespace { char *str; }; -/* If you want to express a length, use fscat() */ -static inline int fs_rputs(const char *str, struct filespace *fs) { - char *new_str; - int len; - len = (int)strlen(str); - - if (len + fs->current_size + 1 > fs->current_alloc) { - fs->current_alloc = MAX(fs->current_size * 2, fs->current_size + len + 1000); - - new_str = (char *)safe_malloc(fs->current_alloc); - memcpy(new_str, fs->str, fs->current_size); - - fs->pos = (fs->pos - fs->str) + new_str; - if (fs->str) - free(fs->str); - fs->str = new_str; - } - memcpy(fs->str + fs->current_size, str, len); - fs->current_size += len; - fs->str[fs->current_size] = '\0'; - - return 0; +static inline int fs_length(const struct filespace *fs) { + return fs->current_size; } - -static inline int fs_rvputs(struct filespace *fs,...) { - va_list args; - const char *x; - - va_start(args, fs); - for (;;) { - x = va_arg(args, const char *); - if (x == NULL) - break; - - if (fs_rputs(x,fs) == -1) { - va_end(args); - return -1; - } - } - va_end(args); - return 1; -} - -/* Concatenate a string to the end of a filespace */ -static inline int fscat(struct filespace *fs, const char *str, int len) { - char *tmpstr; - - if (len < 0) - return -1; - if (len == 0) - return 0; - - /* - printf("fscat: current_alloc=%d; current_size=%d; len=%d\n", fs->current_alloc, fs->current_size, len); - */ - - if (fs->current_alloc - fs->current_size < len + 2) { - fs->current_alloc = (int) (fs->current_alloc * 1.4 + 1 ); - fs->current_alloc += 100 + len; - - tmpstr = (char *)safe_malloc(fs->current_alloc); - memcpy(tmpstr, fs->str, fs->current_size); - - fs->pos = (fs->pos - fs->str) + tmpstr; - if (fs->str) free(fs->str); - fs->str = tmpstr; - } - memcpy(fs->str + fs->current_size, str, len); - - fs->current_size += len; - fs->str[fs->current_size] = '\0'; - return 0; -} - -static inline int fs_rputc(int ch, struct filespace *fs) { - char s[2]; - - if (fs->current_size + 2 <= fs->current_alloc) { - fs->str[fs->current_size] = ch; - fs->current_size++; - fs->str[fs->current_size] = '\0'; - } else { - /* otherwise we use the ueber-technique of letting fscat handle it ... umm - * actually I don't know why we don't do this in all cases ... */ - s[0] = ch; - s[1] = '\0'; - fscat(fs, s, 1); - } - return 0; +static inline char * fs_str(const struct filespace *fs) { + return fs->str; } int filespace_init(struct filespace *fs, int initial_size); -int fs_prepend(char *str, int len, struct filespace *fs); - -int fs_clear(struct filespace *fs); - int fs_free(struct filespace *fs); +int fs_cat(struct filespace *fs, const char *str, int len); + #endif /* FILESPACE_H */ diff --git a/nsock/src/nsock_core.c b/nsock/src/nsock_core.c index a8bf09af1..ef84d8379 100644 --- a/nsock/src/nsock_core.c +++ b/nsock/src/nsock_core.c @@ -512,8 +512,8 @@ void handle_write_result(mspool *ms, msevent *nse, enum nse_status status) { nse->event_done = 1; nse->status = status; } else if (status == NSE_STATUS_SUCCESS) { - str = FILESPACE_STR(&nse->iobuf) + nse->writeinfo.written_so_far; - bytesleft = FILESPACE_LENGTH(&nse->iobuf) - nse->writeinfo.written_so_far; + str = fs_str(&nse->iobuf) + nse->writeinfo.written_so_far; + bytesleft = fs_length(&nse->iobuf) - nse->writeinfo.written_so_far; if (nse->writeinfo.written_so_far > 0) assert(bytesleft > 0); #if HAVE_OPENSSL @@ -601,7 +601,7 @@ static int do_actual_read(mspool *ms, msevent *nse) { msiod *iod = nse->iod; int err = 0; int max_chunk = NSOCK_READ_CHUNK_SIZE; - int startlen = FILESPACE_LENGTH(&nse->iobuf); + int startlen = fs_length(&nse->iobuf); if (nse->readinfo.read_type == NSOCK_READBYTES) max_chunk = nse->readinfo.num; @@ -634,7 +634,7 @@ static int do_actual_read(mspool *ms, msevent *nse) { iod->peerlen = peerlen; } if (buflen > 0) { - if (fscat(&nse->iobuf, buf, buflen) == -1) { + if (fs_cat(&nse->iobuf, buf, buflen) == -1) { nse->event_done = 1; nse->status = NSE_STATUS_ERROR; nse->errnum = ENOMEM; @@ -644,8 +644,8 @@ static int do_actual_read(mspool *ms, msevent *nse) { /* Sometimes a service just spews and spews data. So we return after a * somewhat large amount to avoid monopolizing resources and avoid DOS * attacks. */ - if (FILESPACE_LENGTH(&nse->iobuf) > max_chunk) - return FILESPACE_LENGTH(&nse->iobuf) - startlen; + if (fs_length(&nse->iobuf) > max_chunk) + return fs_length(&nse->iobuf) - startlen; /* No good reason to read again if we we were successful in the read but * didn't fill up the buffer. Especially for UDP, where we want to @@ -653,7 +653,7 @@ static int do_actual_read(mspool *ms, msevent *nse) { * assignment of iod->peer depends on not consolidating more than one * UDP read buffer. */ if (buflen > 0 && buflen < sizeof(buf)) - return FILESPACE_LENGTH(&nse->iobuf) - startlen; + return fs_length(&nse->iobuf) - startlen; } } while (buflen > 0 || (buflen == -1 && err == EINTR)); @@ -670,7 +670,7 @@ static int do_actual_read(mspool *ms, msevent *nse) { /* OpenSSL read */ while ((buflen = SSL_read(iod->ssl, buf, sizeof(buf))) > 0) { - if (fscat(&nse->iobuf, buf, buflen) == -1) { + if (fs_cat(&nse->iobuf, buf, buflen) == -1) { nse->event_done = 1; nse->status = NSE_STATUS_ERROR; nse->errnum = ENOMEM; @@ -680,8 +680,8 @@ static int do_actual_read(mspool *ms, msevent *nse) { /* Sometimes a service just spews and spews data. So we return * after a somewhat large amount to avoid monopolizing resources * and avoid DOS attacks. */ - if (FILESPACE_LENGTH(&nse->iobuf) > max_chunk) - return FILESPACE_LENGTH(&nse->iobuf) - startlen; + if (fs_length(&nse->iobuf) > max_chunk) + return fs_length(&nse->iobuf) - startlen; } if (buflen == -1) { @@ -716,16 +716,16 @@ static int do_actual_read(mspool *ms, msevent *nse) { if (buflen == 0) { nse->event_done = 1; nse->eof = 1; - if (FILESPACE_LENGTH(&nse->iobuf) > 0) { + if (fs_length(&nse->iobuf) > 0) { nse->status = NSE_STATUS_SUCCESS; - return FILESPACE_LENGTH(&nse->iobuf) - startlen; + return fs_length(&nse->iobuf) - startlen; } else { nse->status = NSE_STATUS_EOF; return 0; } } - return FILESPACE_LENGTH(&nse->iobuf) - startlen; + return fs_length(&nse->iobuf) - startlen; } @@ -737,7 +737,7 @@ void handle_read_result(mspool *ms, msevent *nse, enum nse_status status) { if (status == NSE_STATUS_TIMEOUT) { nse->event_done = 1; - if (FILESPACE_LENGTH(&nse->iobuf) > 0) + if (fs_length(&nse->iobuf) > 0) nse->status = NSE_STATUS_SUCCESS; else nse->status = NSE_STATUS_TIMEOUT; @@ -756,7 +756,7 @@ void handle_read_result(mspool *ms, msevent *nse, enum nse_status status) { nse->event_done = 1; break; case NSOCK_READBYTES: - if (FILESPACE_LENGTH(&nse->iobuf) >= nse->readinfo.num) { + if (fs_length(&nse->iobuf) >= nse->readinfo.num) { nse->status = NSE_STATUS_SUCCESS; nse->event_done = 1; } @@ -765,8 +765,8 @@ void handle_read_result(mspool *ms, msevent *nse, enum nse_status status) { case NSOCK_READLINES: /* Lets count the number of lines we have ... */ count = 0; - len = FILESPACE_LENGTH(&nse->iobuf) -1; - str = FILESPACE_STR(&nse->iobuf); + len = fs_length(&nse->iobuf) -1; + str = fs_str(&nse->iobuf); for (count=0; len >= 0; len--) { if (str[len] == '\n') { count++; @@ -817,7 +817,7 @@ void handle_pcap_read_result(mspool *ms, msevent *nse, enum nse_status status) { nse->event_done = 1; } else if (status == NSE_STATUS_SUCCESS) { /* check if we already have something read */ - if (FILESPACE_LENGTH(&(nse->iobuf)) == 0) { + if (fs_length(&(nse->iobuf)) == 0) { nse->status = NSE_STATUS_TIMEOUT; nse->event_done = 0; } else { @@ -988,12 +988,12 @@ void process_event(mspool *nsp, gh_list *evlist, msevent *nse, int ev) { if (ev & EV_READ) { /* buffer empty? check it! */ - if (FILESPACE_LENGTH(&(nse->iobuf)) == 0) + if (fs_length(&(nse->iobuf)) == 0) do_actual_pcap_read(nse); } /* if already received smth */ - if (FILESPACE_LENGTH(&(nse->iobuf)) > 0) + if (fs_length(&(nse->iobuf)) > 0) handle_pcap_read_result(nsp, nse, NSE_STATUS_SUCCESS); if (!nse->event_done && nse->timeout.tv_sec && !TIMEVAL_AFTER(nse->timeout, nsock_tod)) diff --git a/nsock/src/nsock_event.c b/nsock/src/nsock_event.c index e6d44fa33..cfa455699 100644 --- a/nsock/src/nsock_event.c +++ b/nsock/src/nsock_event.c @@ -115,8 +115,8 @@ char *nse_readbuf(nsock_event nse, int *nbytes) { msevent *me = (msevent *)nse; if (nbytes) - *nbytes = FILESPACE_LENGTH(&(me->iobuf)); - return FILESPACE_STR(&(me->iobuf)); + *nbytes = fs_length(&(me->iobuf)); + return fs_str(&(me->iobuf)); } static void first_ev_next(msevent *nse, gh_list_elem **first) { diff --git a/nsock/src/nsock_pcap.c b/nsock/src/nsock_pcap.c index 5163f85e0..252646a4c 100644 --- a/nsock/src/nsock_pcap.c +++ b/nsock/src/nsock_pcap.c @@ -370,7 +370,7 @@ int do_actual_pcap_read(msevent *nse) { nsock_log_debug_all(nse->iod->nsp, "PCAP %s TEST (IOD #%li) (EID #%li)", __func__, nse->iod->id, nse->id); - assert( FILESPACE_LENGTH(&(nse->iobuf)) == 0 ); + assert(fs_length(&(nse->iobuf)) == 0); rc = pcap_next_ex(mp->pt, &pkt_header, &pkt_data); switch(rc) { @@ -385,10 +385,10 @@ int do_actual_pcap_read(msevent *nse) { npp.caplen = pkt_header->caplen; npp.packet = pkt_data; - fscat(&(nse->iobuf), (char *)&npp, sizeof(npp)); - fscat(&(nse->iobuf), (char *)pkt_data, npp.caplen); - n = (nsock_pcap *)FILESPACE_STR(&(nse->iobuf)); - n->packet = (unsigned char *)FILESPACE_STR(&(nse->iobuf)) + sizeof(npp); + fs_cat(&(nse->iobuf), (char *)&npp, sizeof(npp)); + fs_cat(&(nse->iobuf), (char *)pkt_data, npp.caplen); + n = (nsock_pcap *)fs_str(&(nse->iobuf)); + n->packet = (unsigned char *)fs_str(&(nse->iobuf)) + sizeof(npp); nsock_log_debug_all(nse->iod->nsp, "PCAP %s READ (IOD #%li) (EID #%li) size=%i", __func__, nse->iod->id, nse->id, pkt_header->caplen); @@ -416,8 +416,8 @@ void nse_readpcap(nsock_event nsee, const unsigned char **l2_data, size_t *l2_le size_t l2l; size_t l3l; - nsock_pcap *n = (nsock_pcap *)FILESPACE_STR(&(nse->iobuf)); - if (FILESPACE_LENGTH(&(nse->iobuf)) < sizeof(nsock_pcap)) { + nsock_pcap *n = (nsock_pcap *)fs_str(&(nse->iobuf)); + if (fs_length(&(nse->iobuf)) < sizeof(nsock_pcap)) { if (l2_data) *l2_data = NULL; if (l2_len) diff --git a/nsock/src/nsock_write.c b/nsock/src/nsock_write.c index 32dd1a121..590b0326d 100644 --- a/nsock/src/nsock_write.c +++ b/nsock/src/nsock_write.c @@ -116,7 +116,7 @@ nsock_event_id nsock_sendto(nsock_pool ms_pool, nsock_iod ms_iod, nsock_ev_handl nsock_log_debug(nsp, "Sendto request for %d bytes to IOD #%li EID %li [%s]%s", datalen, nsi->id, nse->id, get_peeraddr_string(nse->iod), displaystr); - fscat(&nse->iobuf, data, datalen); + fs_cat(&nse->iobuf, data, datalen); nsp_add_event(nsp, nse); @@ -157,7 +157,7 @@ nsock_event_id nsock_write(nsock_pool ms_pool, nsock_iod ms_iod, nsock_log_debug(nsp, "Write request for %d bytes to IOD #%li EID %li (peer unspecified)%s", datalen, nsi->id, nse->id, displaystr); - fscat(&nse->iobuf, data, datalen); + fs_cat(&nse->iobuf, data, datalen); nsp_add_event(nsp, nse); @@ -209,7 +209,7 @@ nsock_event_id nsock_printf(nsock_pool ms_pool, nsock_iod ms_iod, nse->event_done = 1; nse->status = NSE_STATUS_SUCCESS; } else { - fscat(&nse->iobuf, buf2, strlength); + fs_cat(&nse->iobuf, buf2, strlength); } }