nsock_event.c
来自「Ubuntu packages of security software。 相」· C语言 代码 · 共 474 行 · 第 1/2 页
C
474 行
#if HAVE_PCAP#if PCAP_BSD_SELECT_HACK if(nse->type==NSE_TYPE_PCAP_READ){ if (nsp->tracelevel > 8) nsock_trace(nsp, "PCAP NSE #%lu: CANCELL TEST el.pcap=%x el.read=%x el.curr=%x sd=%i", nse->id, &nsp->evl.pcap_read_events, &nsp->evl.read_events, event_list,((mspcap *) nse->iod->pcap)->pcap_desc ); /* If event occured, and we're in BSD_HACK mode, than this event was added * to two queues. evl.read_event and evl.pcap_read_event * Of coure we should destroy it only once. * I assume we're now in evl.read_event, co just unlink this event from * evl.pcap_read_event */ if(((mspcap *) nse->iod->pcap)->pcap_desc >= 0 && event_list == &nsp->evl.read_events){ /* event is done, list is read_events and we're in BSD_HACK mode. * So unlink event from pcap_read_events */ gh_list_remove(&nsp->evl.pcap_read_events, nse); if (nsp->tracelevel > 8) nsock_trace(nsp, "PCAP NSE #%lu: Removing event from PCAP_READ_EVENTS", nse->id); } if(((mspcap *) nse->iod->pcap)->pcap_desc >= 0 && event_list == &nsp->evl.pcap_read_events){ /* event is done, list is read_events and we're in BSD_HACK mode. * So unlink event from pcap_read_events */ gh_list_remove(&nsp->evl.read_events, nse); if (nsp->tracelevel > 8) nsock_trace(nsp, "PCAP NSE #%lu: Removing event from READ_EVENTS", nse->id); } }#endif#endif msevent_dispatch_and_delete(nsp, nse, notify); return 1;}/* Adjust various statistics, dispatches the event handler (if notify is nonzero) and then deletes the event. This function does NOT delete the event from any lists it might be on (eg nsp->evl.read_list etc.) nse->event_done MUST be true when you call this */void msevent_dispatch_and_delete(mspool *nsp, msevent *nse, int notify) { assert(nsp); assert(nse); assert(nse->event_done); nsp->evl.events_pending--; assert(nsp->evl.events_pending >= 0); if (nse->iod) { nse->iod->events_pending--; assert(nse->iod->events_pending >= 0); } if (notify) { nsock_trace_handler_callback(nsp, nse); nse->handler(nsp, nse, nse->userdata); }/* FIXME: We should be updating stats here ... */ /* Now we clobber the event ... */ msevent_delete(nsp, nse);}/* OK -- the idea is that we want the type included in the rightmost two bits and the serial number in the leftmost 30 or 62. But we also want to insure a correct wrap-around in the case of an obscene number of event. One definition of a "correct" wraparound is that it goes from the highest number back to one (not zero) because we don't want event numbers to ever be zero. */nsock_event_id get_new_event_id(mspool *ms, enum nse_type type) { int type_code = (int) type; unsigned long serial = ms->next_event_serial++; unsigned long max_serial_allowed; int shiftbits; assert(type < NSE_TYPE_MAX); shiftbits = sizeof(nsock_event_id) * 8 - TYPE_CODE_NUM_BITS; max_serial_allowed = ( 1 << shiftbits ) - 1; if (serial == max_serial_allowed ) { /* then the next serial will be one because 0 is forbidden */ ms->next_event_serial = 1; } return (serial << TYPE_CODE_NUM_BITS) | type_code;}/* Take an event ID and return the type (NSE_TYPE_CONNECT, etc */enum nse_type get_event_id_type(nsock_event_id event_id) { return (enum nse_type) ((event_id & ((1 << TYPE_CODE_NUM_BITS) - 1)));}/* Create a new event structure -- must be deleted later with msevent_delete, unless it returns NULL (failure). NULL can be passed in for the msiod and the userdata if not available */msevent *msevent_new(mspool *nsp, enum nse_type type, msiod *msiod, int timeout_msecs, nsock_ev_handler handler, void *userdata) { msevent *nse; if (msiod) { msiod->events_pending++; assert(msiod->state != NSIOD_STATE_DELETED); } /* First we check if one is available from the free list ... */ nse = (msevent *) gh_list_pop(&nsp->evl.free_events); if (!nse) nse = (msevent *) safe_malloc(sizeof(msevent)); memset(nse, 0, sizeof(*nse)); nse->id = get_new_event_id(nsp, type); nse->type = type; nse->status = NSE_STATUS_NONE;#if HAVE_OPENSSL nse->sslinfo.ssl_desire = SSL_ERROR_NONE;#endif if (type == NSE_TYPE_READ || type == NSE_TYPE_WRITE) { filespace_init(&(nse->iobuf), 1024); }#if HAVE_SSL if (type == NSE_TYPE_PCAP_READ) { mspcap *mp = (mspcap *) nsi->pcap; assert(mp); int sz = mp->snaplen+1 + sizeof(nsock_pcap); filespace_init(&(nse->iobuf), sz); }#endif if (timeout_msecs != -1) { assert(timeout_msecs >= 0); TIMEVAL_MSEC_ADD(nse->timeout, nsock_tod, timeout_msecs); } nse->iod = msiod; nse->handler = handler; nse->userdata = userdata; nse->time_created = nsock_tod; if (nsp->tracelevel > 3) { if(nse->iod == NULL) { nsock_trace(nsp, "msevent_new (IOD #NULL) (EID #%li)", nse->id); } else { nsock_trace(nsp, "msevent_new (IOD #%li) (EID #%li)", nse->iod->id, nse->id); } } return nse;}/* Free an msevent which was allocated with msevent_new, including all internal resources. Note -- we assume that nse->iod->events_pending (if it exists) has ALREADY been decremented (done during msevent_dispatch_and_delete) -- so remember to do this if you call msevent_delete() directly */void msevent_delete(mspool *nsp, msevent *nse) { if (nsp->tracelevel > 3) { if(nse->iod == NULL) { nsock_trace(nsp, "msevent_delete (IOD #NULL) (EID #%li)", nse->id); } else { nsock_trace(nsp, "msevent_delete (IOD #%li) (EID #%li)", nse->iod->id, nse->id); } } /* First free the IOBuf inside it if neccessary */ if (nse->type == NSE_TYPE_READ || nse->type == NSE_TYPE_WRITE) { fs_free(&nse->iobuf); } #if HAVE_PCAP if (nse->type == NSE_TYPE_PCAP_READ) { fs_free(&nse->iobuf); if (nsp->tracelevel > 5) nsock_trace(nsp, "PCAP removed %lu\n",nse->id); } #endif /* Now we add the event back into the free pool */ gh_list_prepend(&nsp->evl.free_events, nse);}/* Takes an nse_type (as returned by nse_type() and returns a static string name that you can use for printing, etc. */const char *nse_type2str(enum nse_type type) { switch(type) { case NSE_TYPE_CONNECT: return "CONNECT"; case NSE_TYPE_CONNECT_SSL: return "SSL-CONNECT"; case NSE_TYPE_READ: return "READ"; case NSE_TYPE_WRITE: return "WRITE"; case NSE_TYPE_TIMER: return "TIMER"; case NSE_TYPE_PCAP_READ: return "READ-PCAP"; default: return "UNKNOWN!"; } return "WTF????"; /* Unreached */}/* Takes an nse_status (as returned by nse_status() and returns a static string name that you can use for printing, etc. */const char *nse_status2str(enum nse_status status) { switch(status) { case NSE_STATUS_NONE: return "NONE"; case NSE_STATUS_SUCCESS: return "SUCCESS"; case NSE_STATUS_ERROR: return "ERROR"; case NSE_STATUS_TIMEOUT: return "TIMEOUT"; case NSE_STATUS_CANCELLED: return "CANCELLED"; case NSE_STATUS_KILL: return "KILL"; case NSE_STATUS_EOF: return "EOF"; default: return "UNKNOWN!"; } return "WTF????"; /* Unreached */}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?