static void pcap_retrieve(unsigned char *args, const struct pcap_pkthdr *pkt_hdr, unsigned char *packet) { int l2_len, ip_pack_len; pcap_t *pcap; unsigned char *ip_data; struct ethernet_hdr *ether; if (pkt_hdr->len < ETHERNET_HDR_LEN) { tc_log_info(LOG_ERR, 0, "recv len is less than:%d", ETHERNET_HDR_LEN); return; } ether = (struct ethernet_hdr *) packet; if (ntohs(ether->ether_type) != ETH_P_IP) { return; } pcap = (pcap_t *)args; ip_data = get_ip_data(pcap, packet, pkt_hdr->len, &l2_len); ip_pack_len = pkt_hdr->len - l2_len; dispose_packet((char *) ip_data, ip_pack_len, NULL); }
int tc_offline_parse(char *pcap_file) { int l2_len, ip_pack_len = 0; bool stop = false; char ebuf[PCAP_ERRBUF_SIZE]; pcap_t *pcap; unsigned char *pkt_data, *ip_data; struct pcap_pkthdr pkt_hdr; struct timeval last_pack_time; if (pcap_file == NULL) { return TC_ERROR; } if ((settings.pcap = pcap_open_offline(pcap_file, ebuf)) == NULL) { tc_log_info(LOG_ERR, 0, "open %s" , ebuf); fprintf(stderr, "open %s\n", ebuf); return TC_ERROR; } pcap = settings.pcap; tc_log_info(LOG_NOTICE, 0, "open pcap success:%s", pcap_file); while (!stop) { pkt_data = (u_char *) pcap_next(pcap, &pkt_hdr); if (pkt_data != NULL) { if (pkt_hdr.caplen < pkt_hdr.len) { tc_log_debug0(LOG_DEBUG, 0, "truncated packets,drop"); } ip_data = get_ip_data(pkt_data, pkt_hdr.caplen, &l2_len); last_pack_time = pkt_hdr.ts; if (ip_data != NULL) { settings.pcap_time = last_pack_time.tv_sec * 1000 + last_pack_time.tv_usec/1000; ip_pack_len = pkt_hdr.len - l2_len; if (is_packet_needed((const char *) ip_data)) { process((char *)ip_data); } else { tc_log_debug0(LOG_DEBUG, 0, "invalid flag"); } } } else { tc_log_info(LOG_WARN, 0, "stop, null from pcap_next"); stop = true; } } return TC_OK; }
static void send_packets_from_pcap(int first) { int l2_len, ip_pack_len, p_valid_flag = 0; bool stop; pcap_t *pcap; unsigned char *pkt_data, *ip_data; struct pcap_pkthdr pkt_hdr; pcap = clt_settings.pcap; if (pcap == NULL || read_pcap_over) { return; } gettimeofday(&cur_time, NULL); stop = check_read_stop(); while (!stop) { pkt_data = (u_char *) pcap_next(pcap, &pkt_hdr); if (pkt_data != NULL) { if (pkt_hdr.caplen < pkt_hdr.len) { tc_log_info(LOG_WARN, 0, "truncated packets,drop"); } else { ip_data = get_ip_data(pkt_data, pkt_hdr.len, &l2_len); if (ip_data != NULL) { ip_pack_len = pkt_hdr.len - l2_len; dispose_packet((char*)ip_data, ip_pack_len, &p_valid_flag); if (p_valid_flag) { tc_log_debug0(LOG_DEBUG, 0, "valid flag for packet"); if (first) { first_pack_time = pkt_hdr.ts; first = 0; } last_pack_time = pkt_hdr.ts; } else { stop = false; tc_log_debug0(LOG_DEBUG, 0, "stop,invalid flag"); } } } stop = check_read_stop(); } else { tc_log_info(LOG_WARN, 0, "stop, null from pcap_next"); stop = true; read_pcap_over = true; } } }
static void pcap_tunnel_retrieve(pcap_t *pcap, const struct pcap_pkthdr *pkt_hdr, unsigned char *frame) { int l2_len = 0, ip_pack_len, frame_len; unsigned char *ip_data, tunnel_frame[ETHERNET_HDR_LEN + IP_RECV_BUF_SIZE]; ip_data = get_ip_data(pcap, frame, pkt_hdr->len, &l2_len); ip_pack_len = pkt_hdr->len - l2_len; memcpy(tunnel_frame + ETHERNET_HDR_LEN, ip_data, ip_pack_len); frame_len = ip_pack_len + ETHERNET_HDR_LEN; dispose_packet(tunnel_frame, frame_len, ip_pack_len, NULL); }
static void pcap_pack_callback(unsigned char *args, const struct pcap_pkthdr *pkt_hdr, unsigned char *frame) { int l2_len; pcap_t *pcap; unsigned char *ip_data; if (pkt_hdr->len >= ETHERNET_HDR_LEN) { pcap = (pcap_t *) args; ip_data = get_ip_data(pcap, frame, pkt_hdr->len, &l2_len); resp_dispose((tc_iph_t *) ip_data); } else { tc_log_info(LOG_ERR, 0, "recv len is less than:%d", ETHERNET_HDR_LEN); } }
static void pcap_packet_callback(unsigned char *args, const struct pcap_pkthdr *pkt_hdr, unsigned char *packet) { pcap_t *pcap; unsigned char *ip_data; int l2_len; if (pkt_hdr->len < ETHERNET_HDR_LEN) { tc_log_info(LOG_ERR, 0, "recv len is less than:%d", ETHERNET_HDR_LEN); return; } pcap = (pcap_t *)args; ip_data = get_ip_data(pcap, packet, pkt_hdr->len, &l2_len); resp_dispose((tc_ip_header_t *) ip_data); }
SANE_Status soapht_read(SANE_Handle handle, SANE_Byte *data, SANE_Int maxLength, SANE_Int *length) { struct soap_session *ps = (struct soap_session *)handle; int ret, stat=SANE_STATUS_IO_ERROR; DBG8("sane_hpaio_read() handle=%p data=%p maxLength=%d\n", (void *)handle, data, maxLength); if(ps->user_cancel) { DBG8("soapht_read() EVENT_SCAN_CANCEL****uri=%s\n", ps->uri); SendScanEvent(ps->uri, EVENT_SCAN_CANCEL); return SANE_STATUS_CANCELLED; } ret = get_ip_data(ps, data, maxLength, length); if(ret & (IP_INPUT_ERROR | IP_FATAL_ERROR)) { BUG("ipConvert error=%x\n", ret); goto bugout; } if (ret & IP_DONE) { stat = SANE_STATUS_EOF; SendScanEvent(ps->uri, EVENT_END_SCAN_JOB); } else stat = SANE_STATUS_GOOD; bugout: if (stat != SANE_STATUS_GOOD) { if (ps->ip_handle) { /* Note always call ipClose when SANE_STATUS_EOF, do not depend on sane_cancel because sane_cancel is only called at the end of a batch job. */ ipClose(ps->ip_handle); ps->ip_handle = 0; } ps->bb_end_page(ps, 0); } DBG8("-sane_hpaio_read() output=%p bytes_read=%d maxLength=%d status=%d\n", data, *length, maxLength, stat); return stat; } /* soapht_read */
static void pcap_retrieve(unsigned char *args, const struct pcap_pkthdr *pkt_hdr, unsigned char *frame) { int l2_len, ip_pack_len, frame_len; pcap_t *pcap; unsigned char *ip_data; struct ethernet_hdr *ether; if (pkt_hdr->len < ETHERNET_HDR_LEN) { tc_log_info(LOG_ERR, 0, "recv len is less than:%d", ETHERNET_HDR_LEN); return; } pcap = (pcap_t *) args; frame_len = pkt_hdr->len; l2_len = get_l2_len(frame, frame_len, pcap_datalink(pcap)); if (l2_len != ETHERNET_HDR_LEN) { if (l2_len > ETHERNET_HDR_LEN) { ip_data = get_ip_data(pcap, frame, pkt_hdr->len, &l2_len); frame = ip_data - ETHERNET_HDR_LEN; frame_len = frame_len - l2_len + ETHERNET_HDR_LEN; } else if (l2_len == 0) { /* tunnel frames without ethernet header */ pcap_tunnel_retrieve(pcap, pkt_hdr, frame); return; } else { tc_log_info(LOG_WARN, 0, "l2 len is %d", l2_len); return; } } else { ether = (struct ethernet_hdr *) frame; if (ntohs(ether->ether_type) != ETH_P_IP) { return; } } ip_pack_len = pkt_hdr->len - l2_len; dispose_packet(frame, frame_len, ip_pack_len, NULL); }
SANE_Status soapht_start(SANE_Handle handle) { struct soap_session *ps = (struct soap_session *)handle; SANE_Parameters pp; IP_IMAGE_TRAITS traits; IP_XFORM_SPEC xforms[IP_MAX_XFORMS], *pXform=xforms; int stat, ret; DBG8("sane_hpaio_start()\n"); ps -> user_cancel = 0; ps -> cnt = 0; ps -> index = 0; if (set_extents(ps)) { BUG("invalid extents: tlx=%d brx=%d tly=%d bry=%d minwidth=%d minheight%d maxwidth=%d maxheight=%d\n", ps->currentTlx, ps->currentTly, ps->currentBrx, ps->currentBry, ps->min_width, ps->min_height, ps->tlxRange.max, ps->tlyRange.max); stat = SANE_STATUS_INVAL; goto bugout; } /* If input is ADF and ADF is empty, return SANE_STATUS_NO_DOCS. */ if (ps->currentInputSource==IS_ADF || ps->currentInputSource==IS_ADF_DUPLEX) { ret = ps->bb_is_paper_in_adf(ps); /* 0 = no paper in adf, 1 = paper in adf, -1 = error */ if (ret == 0) { stat = SANE_STATUS_NO_DOCS; /* done scanning */ SendScanEvent (ps->uri, EVENT_SCAN_ADF_NO_DOCS); goto bugout; } else if (ret < 0) { stat = SANE_STATUS_IO_ERROR; goto bugout; } } /* Start scan and get actual image traits. */ if (ps->bb_start_scan(ps)) { stat = SANE_STATUS_IO_ERROR; goto bugout; } SendScanEvent(ps->uri, EVENT_START_SCAN_JOB); memset(xforms, 0, sizeof(xforms)); /* Setup image-processing pipeline for xform. */ if (ps->currentScanMode == CE_RGB24 || ps->currentScanMode == CE_GRAY8) { switch(ps->currentCompression) { case SF_JFIF: pXform->aXformInfo[IP_JPG_DECODE_FROM_DENALI].dword = 0; /* 0=no */ ADD_XFORM(X_JPG_DECODE); pXform->aXformInfo[IP_CNV_COLOR_SPACE_WHICH_CNV].dword = IP_CNV_YCC_TO_SRGB; pXform->aXformInfo[IP_CNV_COLOR_SPACE_GAMMA].dword = 0x00010000; ADD_XFORM(X_CNV_COLOR_SPACE); break; case SF_HPRAW: default: break; } } else { /* Must be BLACK_AND_WHITE1 (Lineart). */ switch(ps->currentCompression) { case SF_JFIF: pXform->aXformInfo[IP_JPG_DECODE_FROM_DENALI].dword = 0; /* 0=no */ ADD_XFORM(X_JPG_DECODE); pXform->aXformInfo[IP_GRAY_2_BI_THRESHOLD].dword = 127; ADD_XFORM(X_GRAY_2_BI); break; case SF_HPRAW: pXform->aXformInfo[IP_GRAY_2_BI_THRESHOLD].dword = 127; ADD_XFORM(X_GRAY_2_BI); default: break; } } /* Setup x/y cropping for xform. (Actually we let cm1017 do it's own cropping) */ pXform->aXformInfo[IP_CROP_LEFT].dword = 0; pXform->aXformInfo[IP_CROP_RIGHT].dword = 0; pXform->aXformInfo[IP_CROP_TOP].dword = 0; pXform->aXformInfo[IP_CROP_MAXOUTROWS].dword = 0; ADD_XFORM(X_CROP); /* Setup x/y padding for xform. (Actually we let cm1017 do it's own padding) */ pXform->aXformInfo[IP_PAD_LEFT].dword = 0; /* # of pixels to add to left side */ pXform->aXformInfo[IP_PAD_RIGHT].dword = 0; /* # of pixels to add to right side */ pXform->aXformInfo[IP_PAD_TOP].dword = 0; /* # of rows to add to top */ pXform->aXformInfo[IP_PAD_BOTTOM].dword = 0; /* # of rows to add to bottom */ pXform->aXformInfo[IP_PAD_VALUE].dword = ps->currentScanMode == CE_BLACK_AND_WHITE1 ? 0 : -1; /* lineart white = 0, rgb white = -1 */ pXform->aXformInfo[IP_PAD_MIN_HEIGHT].dword = 0; ADD_XFORM(X_PAD); /* Open image processor. */ if ((ret = ipOpen(pXform-xforms, xforms, 0, &ps->ip_handle)) != IP_DONE) { BUG("unable open image processor: err=%d\n", ret); stat = SANE_STATUS_INVAL; goto bugout; } /* Get scan parameters for image processor. */ if (ps->currentCompression == SF_HPRAW) ps->bb_get_parameters(ps, &pp, SPO_STARTED_JR); /* hpraw, use actual parameters */ else ps->bb_get_parameters(ps, &pp, SPO_BEST_GUESS); /* jpeg, use best guess */ traits.iPixelsPerRow = pp.pixels_per_line; switch(ps->currentScanMode) { case CE_BLACK_AND_WHITE1: /* lineart (let IP create Mono from Gray8) */ case CE_GRAY8: traits.iBitsPerPixel = 8; /* grayscale */ break; case CE_RGB24: default: traits.iBitsPerPixel = 24; /* color */ break; } traits.lHorizDPI = ps->currentResolution << 16; traits.lVertDPI = ps->currentResolution << 16; traits.lNumRows = pp.lines; traits.iNumPages = 1; traits.iPageNum = 1; traits.iComponentsPerPixel = ((traits.iBitsPerPixel % 3) ? 1 : 3); ipSetDefaultInputTraits(ps->ip_handle, &traits); /* If jpeg get output image attributes from the image processor. */ if (ps->currentCompression == SF_JFIF) { /* Enable parsed header flag. */ ipResultMask(ps->ip_handle, IP_PARSED_HEADER); /* Wait for image processor to process header so we know the exact size of the image for sane_get_params. */ while (1) { ret = get_ip_data(ps, NULL, 0, NULL); if (ret & (IP_INPUT_ERROR | IP_FATAL_ERROR | IP_DONE)) { BUG("ipConvert error=%x\n", ret); stat = SANE_STATUS_IO_ERROR; goto bugout; } if (ret & IP_PARSED_HEADER) { ipGetImageTraits(ps->ip_handle, NULL, &ps->image_traits); /* get valid image traits */ ipResultMask(ps->ip_handle, 0); /* disable parsed header flag */ break; } } } else ipGetImageTraits(ps->ip_handle, NULL, &ps->image_traits); /* get valid image traits */ stat = SANE_STATUS_GOOD; bugout: if (stat != SANE_STATUS_GOOD) { if (ps->ip_handle) { ipClose(ps->ip_handle); ps->ip_handle = 0; } ps->bb_end_scan(ps, stat == SANE_STATUS_IO_ERROR ? 1: 0); } return stat; } /* soapht_start */
static void send_packets_from_pcap(int first) { int l2_len, ip_pack_len, p_valid_flag = 0; bool stop; pcap_t *pcap; unsigned char *pkt_data, *frame, *ip_data; struct pcap_pkthdr pkt_hdr; pcap = clt_settings.pcap; if (pcap == NULL || read_pcap_over) { return; } gettimeofday(&cur_time, NULL); stop = check_read_stop(); while (!stop) { pkt_data = (u_char *) pcap_next(pcap, &pkt_hdr); if (pkt_data != NULL) { if (pkt_hdr.caplen < pkt_hdr.len) { tc_log_info(LOG_WARN, 0, "truncated packets,drop"); } else { ip_data = get_ip_data(clt_settings.pcap, pkt_data, pkt_hdr.len, &l2_len); if (l2_len < ETHERNET_HDR_LEN) { tc_log_info(LOG_WARN, 0, "l2 len is %d", l2_len); continue; } last_pack_time = pkt_hdr.ts; if (ip_data != NULL) { clt_settings.pcap_time = last_pack_time.tv_sec * 1000 + last_pack_time.tv_usec / 1000; ip_pack_len = pkt_hdr.len - l2_len; tc_log_debug2(LOG_DEBUG, 0, "frame len:%d, ip len:%d", pkt_hdr.len, ip_pack_len); frame = ip_data - ETHERNET_HDR_LEN; dispose_packet(frame, ip_pack_len + ETHERNET_HDR_LEN, ip_pack_len, &p_valid_flag); if (p_valid_flag) { tc_log_debug0(LOG_DEBUG, 0, "valid flag for packet"); if (first) { first_pack_time = pkt_hdr.ts; first = 0; } else { adj_v_pack_diff = timeval_diff(&last_v_pack_time, &last_pack_time); } /* set last valid packet time in pcap file */ last_v_pack_time = last_pack_time; stop = check_read_stop(); } else { tc_log_debug0(LOG_DEBUG, 0, "invalid flag"); } } } } else { tc_log_info(LOG_WARN, 0, "stop, null from pcap_next"); stop = true; read_pcap_over = true; } } }