/* Remember that pcap descriptor is in nonblocking state. */ int do_actual_pcap_read(struct nevent *nse) { mspcap *mp = (mspcap *)nse->iod->pcap; nsock_pcap npp; nsock_pcap *n; struct pcap_pkthdr *pkt_header; const unsigned char *pkt_data = NULL; int rc; memset(&npp, 0, sizeof(nsock_pcap)); nsock_log_debug_all(nse->iod->nsp, "PCAP %s TEST (IOD #%li) (EID #%li)", __func__, nse->iod->id, nse->id); assert(fs_length(&(nse->iobuf)) == 0); rc = pcap_next_ex(mp->pt, &pkt_header, &pkt_data); switch (rc) { case 1: /* read good packet */ #ifdef PCAP_RECV_TIMEVAL_VALID npp.ts = pkt_header->ts; #else /* On these platforms time received from pcap is invalid. * It's better to set current time */ memcpy(&npp.ts, nsock_gettimeofday(), sizeof(struct timeval)); #endif npp.len = pkt_header->len; npp.caplen = pkt_header->caplen; npp.packet = pkt_data; 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); rc = 1; break; case 0: /* timeout */ rc = 0; break; case -1: /* error */ fatal("pcap_next_ex() fatal error while reading from pcap: %s\n", pcap_geterr(mp->pt)); break; case -2: /* no more packets in savefile (if reading from one) */ default: fatal("Unexpected return code from pcap_next_ex! (%d)\n", rc); } return rc; }
/* Write some data to the socket. If the write is not COMPLETED within * timeout_msecs , NSE_STATUS_TIMEOUT will be returned. If you are supplying * NUL-terminated data, you can optionally pass -1 for datalen and nsock_write * will figure out the length itself */ nsock_event_id nsock_write(nsock_pool ms_pool, nsock_iod ms_iod, nsock_ev_handler handler, int timeout_msecs, void *userdata, const char *data, int datalen) { mspool *nsp = (mspool *)ms_pool; msiod *nsi = (msiod *)ms_iod; msevent *nse; char displaystr[256]; nse = msevent_new(nsp, NSE_TYPE_WRITE, nsi, timeout_msecs, handler, userdata); assert(nse); nse->writeinfo.dest.ss_family = AF_UNSPEC; if (datalen < 0) datalen = (int)strlen(data); if (nsp->loglevel == NSOCK_LOG_DBG_ALL && datalen < 80) { memcpy(displaystr, ": ", 2); memcpy(displaystr + 2, data, datalen); displaystr[2 + datalen] = '\0'; replacenonprintable(displaystr + 2, datalen, '.'); } else { displaystr[0] = '\0'; } nsock_log_debug(nsp, "Write request for %d bytes to IOD #%li EID %li [%s]%s", datalen, nsi->id, nse->id, get_peeraddr_string(nsi), displaystr); fs_cat(&nse->iobuf, data, datalen); nsp_add_event(nsp, nse); return nse->id; }
nsock_event_id nsock_sendto(nsock_pool ms_pool, nsock_iod ms_iod, nsock_ev_handler handler, int timeout_msecs, void *userdata, struct sockaddr *saddr, size_t sslen, unsigned short port, const char *data, int datalen) { mspool *nsp = (mspool *)ms_pool; msiod *nsi = (msiod *)ms_iod; msevent *nse; char displaystr[256]; struct sockaddr_in *sin = (struct sockaddr_in *)saddr; #if HAVE_IPV6 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)saddr; #endif nse = msevent_new(nsp, NSE_TYPE_WRITE, nsi, timeout_msecs, handler, userdata); assert(nse); if (saddr->sa_family == AF_INET) { sin->sin_port = htons(port); #if HAVE_SYS_UN_H } else if (saddr->sa_family == AF_INET6) { #else } else { #endif assert(saddr->sa_family == AF_INET6); #if HAVE_IPV6 sin6->sin6_port = htons(port); #else fatal("IPv6 address passed to %s call, but nsock was not compiled w/IPv6 support", __func__); #endif } assert(sslen <= sizeof(nse->writeinfo.dest)); memcpy(&nse->writeinfo.dest, saddr, sslen); nse->writeinfo.destlen = sslen; assert(sslen <= sizeof(nse->iod->peer)); memcpy(&nse->iod->peer, saddr, sslen); nse->iod->peerlen = sslen; if (datalen < 0) datalen = (int) strlen(data); if (nsp->loglevel == NSOCK_LOG_DBG_ALL && datalen < 80) { memcpy(displaystr, ": ", 2); memcpy(displaystr + 2, data, datalen); displaystr[2 + datalen] = '\0'; replacenonprintable(displaystr + 2, datalen, '.'); } else { displaystr[0] = '\0'; } 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); fs_cat(&nse->iobuf, data, datalen); nsp_add_event(nsp, nse); return nse->id; }
/* Returns -1 if an error, otherwise the number of newly written bytes */ static int do_actual_read(struct npool *ms, struct nevent *nse) { char buf[8192]; int buflen = 0; struct niod *iod = nse->iod; int err = 0; int max_chunk = NSOCK_READ_CHUNK_SIZE; int startlen = fs_length(&nse->iobuf); if (nse->readinfo.read_type == NSOCK_READBYTES) max_chunk = nse->readinfo.num; if (!iod->ssl) { do { struct sockaddr_storage peer; socklen_t peerlen; peerlen = sizeof(peer); buflen = recvfrom(iod->sd, buf, sizeof(buf), 0, (struct sockaddr *)&peer, &peerlen); /* Using recv() was failing, at least on UNIX, for non-network sockets * (i.e. stdin) in this case, a read() is done - as on ENOTSOCK we may * have a non-network socket */ if (buflen == -1) { if (socket_errno() == ENOTSOCK) { peer.ss_family = AF_UNSPEC; peerlen = 0; buflen = read(iod->sd, buf, sizeof(buf)); } } if (buflen == -1) { err = socket_errno(); break; } if (peerlen > 0) { assert(peerlen <= sizeof(iod->peer)); memcpy(&iod->peer, &peer, peerlen); iod->peerlen = peerlen; } if (buflen > 0) { if (fs_cat(&nse->iobuf, buf, buflen) == -1) { nse->event_done = 1; nse->status = NSE_STATUS_ERROR; nse->errnum = ENOMEM; return -1; } /* 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 (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 * return only one datagram at a time. The consistency of the above * assignment of iod->peer depends on not consolidating more than one * UDP read buffer. */ if (buflen > 0 && buflen < sizeof(buf)) return fs_length(&nse->iobuf) - startlen; } } while (buflen > 0 || (buflen == -1 && err == EINTR)); if (buflen == -1) { if (err != EINTR && err != EAGAIN) { nse->event_done = 1; nse->status = NSE_STATUS_ERROR; nse->errnum = err; return -1; } } } else { #if HAVE_OPENSSL /* OpenSSL read */ while ((buflen = SSL_read(iod->ssl, buf, sizeof(buf))) > 0) { if (fs_cat(&nse->iobuf, buf, buflen) == -1) { nse->event_done = 1; nse->status = NSE_STATUS_ERROR; nse->errnum = ENOMEM; return -1; } /* 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 (fs_length(&nse->iobuf) > max_chunk) return fs_length(&nse->iobuf) - startlen; } if (buflen == -1) { err = SSL_get_error(iod->ssl, buflen); if (err == SSL_ERROR_WANT_READ) { int evclr; evclr = socket_count_dec_ssl_desire(nse); socket_count_read_inc(iod); update_events(iod, ms, EV_READ, evclr); nse->sslinfo.ssl_desire = err; } else if (err == SSL_ERROR_WANT_WRITE) { int evclr; evclr = socket_count_dec_ssl_desire(nse); socket_count_write_inc(iod); update_events(iod, ms, EV_WRITE, evclr); nse->sslinfo.ssl_desire = err; } else { /* Unexpected error */ nse->event_done = 1; nse->status = NSE_STATUS_ERROR; nse->errnum = EIO; nsock_log_info("SSL_read() failed for reason %s on NSI %li", ERR_error_string(err, NULL), iod->id); return -1; } } #endif /* HAVE_OPENSSL */ } if (buflen == 0) { nse->event_done = 1; nse->eof = 1; if (fs_length(&nse->iobuf) > 0) { nse->status = NSE_STATUS_SUCCESS; return fs_length(&nse->iobuf) - startlen; } else { nse->status = NSE_STATUS_EOF; return 0; } } return fs_length(&nse->iobuf) - startlen; }
/* Same as nsock_write except you can use a printf-style format and you can only use this for ASCII strings */ nsock_event_id nsock_printf(nsock_pool ms_pool, nsock_iod ms_iod, nsock_ev_handler handler, int timeout_msecs, void *userdata, char *format, ...) { mspool *nsp = (mspool *)ms_pool; msiod *nsi = (msiod *)ms_iod; msevent *nse; char buf[4096]; char *buf2 = NULL; int res, res2; int strlength = 0; char displaystr[256]; va_list ap; va_start(ap,format); nse = msevent_new(nsp, NSE_TYPE_WRITE, nsi, timeout_msecs, handler, userdata); assert(nse); res = Vsnprintf(buf, sizeof(buf), format, ap); va_end(ap); if (res != -1) { if (res > sizeof(buf)) { buf2 = (char * )safe_malloc(res + 16); res2 = Vsnprintf(buf2, sizeof(buf), format, ap); if (res2 == -1 || res2 > res) { free(buf2); buf2 = NULL; } else strlength = res2; } else { buf2 = buf; strlength = res; } } if (!buf2) { nse->event_done = 1; nse->status = NSE_STATUS_ERROR; nse->errnum = EMSGSIZE; } else { if (strlength == 0) { nse->event_done = 1; nse->status = NSE_STATUS_SUCCESS; } else { fs_cat(&nse->iobuf, buf2, strlength); } } if (nsp->loglevel == NSOCK_LOG_DBG_ALL && nse->status != NSE_STATUS_ERROR && strlength < 80) { memcpy(displaystr, ": ", 2); memcpy(displaystr + 2, buf2, strlength); displaystr[2 + strlength] = '\0'; replacenonprintable(displaystr + 2, strlength, '.'); } else { displaystr[0] = '\0'; } nsock_log_debug(nsp, "Write request for %d bytes to IOD #%li EID %li [%s]%s", strlength, nsi->id, nse->id, get_peeraddr_string(nsi), displaystr); if (buf2 != buf) free(buf2); nsp_add_event(nsp, nse); return nse->id; }
int process_request(const char *line, FILE *fp, FileSystem *fs) { char command[4096]; #ifdef DEBUG fprintf(stderr, "process request `%s`\n", line); #endif sscanf(line, "%s", command); #ifdef DEBUG fprintf(stderr, "command is `%s`\n", command); #endif if (0 == strcmp("f", command)) { fs_format(fs); return RESULT_DONE; } else if (0 == strcmp("mk", command)) { char f[4096]; char parent_path[4096]; sscanf(line + 2, "%s", f); #ifdef DEBUG fprintf(stderr, "> mk `%s`\n", f); #endif if (fs_exists(fs, f)) { #ifdef DEBUG fprintf(stderr, "> `%s` already exists\n", f); #endif return RESULT_NO; } else { #ifdef DEBUG fprintf(stderr, "> going to create `%s`\n", f); #endif } fs_split_path(f, parent_path, NULL); if (fs_isdir(fs, parent_path)) { if (fs_create(fs, f)) { #ifdef DEBUG fprintf(stderr, "> failed to create `%s`\n", f); #endif return RESULT_NO; } return RESULT_YES; } #ifdef DEBUG fprintf(stderr, "> parent path `%s` is not a directory\n", parent_path); #endif return RESULT_NO; } else if (0 == strcmp("mkdir", command)) { char d[4096]; char parent_path[4096]; sscanf(line + 5, "%s", d); if (fs_exists(fs, d)) { return RESULT_NO; } fs_split_path(d, parent_path, NULL); if (fs_isdir(fs, parent_path)) { if (fs_mkdir(fs, d)) { return RESULT_NO; } return RESULT_YES; } return RESULT_NO; } else if (0 == strcmp("rm", command)) { char f[4096]; sscanf(line + 2, "%s", f); if (fs_isfile(fs, f)) { if (fs_unlink(fs, f)) { return RESULT_NO; } return RESULT_YES; } return RESULT_NO; } else if (0 == strcmp("cd", command)) { char path[4096]; sscanf(line + 2, "%s", path); if (fs_isdir(fs, path)) { if (fs_chdir(fs, path)) { return RESULT_NO; } return RESULT_YES; } #ifdef DEBUG fprintf(stderr, "`%s` is not a directory\n", path); #endif return RESULT_NO; } else if (0 == strcmp("rmdir", command)) { char d[4096]; sscanf(line + 5, "%s", d); if (fs_isdir(fs, d)) { if (fs_rmdir(fs, d)) { return RESULT_NO; } return RESULT_YES; } return RESULT_NO; } else if (0 == strcmp("ls", command)) { fs_ls(fs, fp); return RESULT_ELSE; } else if (0 == strcmp("cat", command)) { char f[4096]; sscanf(line + 3, "%s", f); if (fs_isfile(fs, f)) { fs_cat(fs, f, fp); return RESULT_ELSE; } return RESULT_NO; } else if (0 == strcmp("w", command)) { char f[4096]; int l; char data[4096]; sscanf(line + 1, "%s %d %[^\n]", f, &l, data); if (fs_isfile(fs, f)) { if (fs_write(fs, f, l, data)) { return RESULT_NO; } return RESULT_YES; } return RESULT_NO; } else if (0 == strcmp("i", command)) { char f[4096]; int pos; int l; char data[4096]; sscanf(line + 1, "%s %d %d %[^\n]", f, &pos, &l, data); if (fs_isfile(fs, f)) { if (fs_insert(fs, f, pos, l, data)) { return RESULT_NO; } return RESULT_YES; } return RESULT_NO; } else if (0 == strcmp("d", command)) { char f[4096]; int pos; int l; sscanf(line + 1, "%s %d %d", f, &pos, &l); if (fs_isfile(fs, f)) { if (fs_delete(fs, f, pos, l)) { return RESULT_NO; } return RESULT_YES; } return RESULT_NO; } else if (0 == strcmp("e", command)) { return RESULT_EXIT; } return RESULT_ELSE; }
void fs_test(int tid) { fun_string f[25]; unsigned char d1[] = "kungfu\0kaleidescopes", d2[] = "funintheSun"; size_t count; printf("[%d] START: FUN STRING test!\n", tid); assert(f[1] = fs_new(d1, 20, 0)); assert(f[2] = fs_new(d1, 20, 0)); assert(f[3] = fs_new(d2, 11, 0)); assert(!fs_cmp(f[1], f[2])); assert(fs_cmp(f[1], f[3])); assert(f[4] = fs_resize(f[1], 10)); assert(f[5] = fs_resize(f[2], 15)); assert(f[6] = fs_resize(f[3], 10)); assert(fs_cmp(f[4], f[5])); assert(!fs_ncmp(f[4], f[5], 10)); assert(!fs_ncmp(f[3], f[6], 10)); assert(f[7] = fs_access(f[1], 3, 10)); assert(f[8] = fs_access(f[4], 3, 5)); assert(f[9] = fs_access(f[5], 5, 10)); assert(!fs_ncmp(f[7], f[8], 3)); assert(fs_cmp(f[7], f[8])); assert(fs_ncmp(f[7], f[9], 5)); assert(10 == fs_len(f[7])); assert(5 == fs_len(f[8])); assert(10 == fs_len(f[7])); assert(20 == fs_len(f[1])); assert(10 == fs_len(f[4])); assert(11 == fs_len(f[3])); assert(f[10] = fs_set(f[1], '9', 40)); assert(f[11] = fs_set(f[1], '9', 50)); assert(f[12] = fs_set(f[1], 'A', 40)); assert(!fs_ncmp(f[10], f[11], 30)); assert(fs_cmp(f[10], f[11])); assert(fs_cmp(f[10], f[12])); assert(fs_cmp(f[1], f[10])); assert(f[13] = fs_dup(f[1])); assert(f[14] = fs_dup(f[2])); assert(f[15] = fs_dup(f[4])); assert(!fs_cmp(f[13], f[14])); assert(!fs_ncmp(f[13], f[15], 5)); assert(fs_cmp(f[13], f[6])); assert(f[16] = fs_chr(f[1], 'g')); assert(f[17] = fs_chr(f[4], 'g')); assert(f[18] = fs_rchr(f[1], 'g')); assert(!fs_ncmp(f[16], f[17], 3)); assert(!fs_cmp(f[16], f[18])); assert(fs_cmp(f[16], f[7])); assert(f[19] = fs_cat(f[3], f[16])); assert(f[20] = fs_cat(f[3], f[18])); assert(f[21] = fs_ncat(f[3], f[17], 2)); assert(!fs_cmp(f[19], f[20])); assert(fs_cmp(f[19], f[21])); assert(!fs_ncmp(f[19], f[21], 13)); // To test // cat // ncat fs_free(&f[1]); fs_free(&f[2]); fs_free(&f[3]); fs_free(&f[4]); fs_free(&f[5]); fs_free(&f[6]); fs_free(&f[7]); fs_free(&f[8]); fs_free(&f[9]); fs_free(&f[10]); fs_free(&f[11]); fs_free(&f[12]); fs_free(&f[13]); fs_free(&f[14]); fs_free(&f[15]); fs_free(&f[16]); fs_free(&f[17]); fs_free(&f[18]); fs_free(&f[19]); fs_free(&f[20]); fs_free(&f[21]); printf("[%d] Strings destroyed!\n", tid); printf("[%d] END: FUN STRING Test!\n\n", tid); return; }