int main(int argc, char **argv) { pcap_if_t *alldevs; pcap_if_t *d; char *s; bpf_u_int32 net, mask; int exit_status = 0; char errbuf[PCAP_ERRBUF_SIZE+1]; if (pcap_findalldevs(&alldevs, errbuf) == -1) { fprintf(stderr,"Error in pcap_findalldevs: %s\n",errbuf); exit(1); } for(d=alldevs;d;d=d->next) { if (!ifprint(d)) exit_status = 2; } if ( (s = pcap_lookupdev(errbuf)) == NULL) { fprintf(stderr,"Error in pcap_lookupdev: %s\n",errbuf); exit_status = 2; } else { printf("Preferred device name: %s\n",s); } if (pcap_lookupnet(s, &net, &mask, errbuf) < 0) { fprintf(stderr,"Error in pcap_lookupnet: %s\n",errbuf); exit_status = 2; } else { printf("Preferred device is on network: %s/%s\n",iptos(net), iptos(mask)); } exit(exit_status); }
int main() { pcap_if_t *alldevs; pcap_if_t *d; char errbuf[PCAP_ERRBUF_SIZE+1]; char source[PCAP_ERRBUF_SIZE+1]; #ifdef WIN32 /* Load Npcap and its functions. */ if (!LoadNpcapDlls()) { fprintf(stderr, "Couldn't load Npcap\n"); exit(1); } #endif printf("Enter the device you want to list:\n" "rpcap:// ==> lists interfaces in the local machine\n" "rpcap://hostname:port ==> lists interfaces in a remote machine\n" " (rpcapd daemon must be up and running\n" " and it must accept 'null' authentication)\n" "file://foldername ==> lists all pcap files in the give folder\n\n" "Enter your choice: "); fgets(source, PCAP_ERRBUF_SIZE, stdin); source[PCAP_ERRBUF_SIZE] = '\0'; /* Retrieve the interfaces list */ if (pcap_findalldevs_ex(source, NULL, &alldevs, errbuf) == -1) { fprintf(stderr,"Error in pcap_findalldevs: %s\n",errbuf); exit(1); } /* Scan the list printing every entry */ for(d=alldevs;d;d=d->next) { ifprint(d); } pcap_freealldevs(alldevs); return 1; }
int main() { pcap_if_t *alldevs; pcap_if_t *d; char errbuf[PCAP_ERRBUF_SIZE+1]; /* Retrieve the device list */ if(pcap_findalldevs(&alldevs, errbuf) == -1) { fprintf(stderr,"Error in pcap_findalldevs: %s\n", errbuf); exit(1); } /* Scan the list printing every entry */ for(d=alldevs;d;d=d->next) { ifprint(d); } /* Free the device list */ pcap_freealldevs(alldevs); return 1; }
int iflist() { pcap_if_t *alldevs; pcap_if_t *d; char errbuf[PCAP_ERRBUF_SIZE+1]; int i; printf("WinPCap version: %s\n", PacketGetVersion()); // Retrieve the interfaces list if (pcap_findalldevs(&alldevs, errbuf) == -1) { fprintf(stderr, "Error in pcap_findalldevs: %s\n", errbuf); return 0; } // Scan the list printing every entry for (i=0,d=alldevs; d; i++,d=d->next) { ifprint(i, d); } return i; }
int main() { pcap_if_t *alldevs; pcap_if_t *d; int inum; int i=0; pcap_t *adhandle; char errbuf[PCAP_ERRBUF_SIZE]; u_int netmask; char packet_filter[20]; struct bpf_program fcode; // 获取网卡列表 if(pcap_findalldevs(&alldevs, errbuf) == -1) { fprintf(stderr,"pcap_findalldevs发生错误: %s\n", errbuf); exit(1); } // 输出网卡信息 for(i=0,d=alldevs; d; d=d->next,i++) { ifprint(d,i+1); } if(i==0) { printf("\n没有找到任何网卡,请确认Winpcap已经安装.\n"); return -1; } printf("\n\n请输入网卡编号 (1-%d):",i); scanf("%d", &inum); // 检测用户是否指定了有效网卡 if(inum < 1 || inum > i) { printf("\n网卡编号超出范围.\n"); // 释放设备列表 pcap_freealldevs(alldevs); return -1; } // 跳转到选择的网卡 for(d=alldevs, i=0; i< inum-1 ;d=d->next, i++); // 打开网卡设备 if ((adhandle= pcap_open_live(d->name, // 设备名称 65536, // 捕获全部的数据包 1, // 设置网卡为混杂模式 1000, // 读超时为1秒 errbuf // 错误缓存 )) == NULL) { fprintf(stderr,"\n不能打开网卡. %s 不被Winpcap支持\n"); // 释放设备列表 pcap_freealldevs(alldevs); return -1; } // 检测链接层,只支持以太网模式 if(pcap_datalink(adhandle) != DLT_EN10MB) { fprintf(stderr,"\n此程序只能运行在以太网上.\n"); // 释放设备列表 pcap_freealldevs(alldevs); return -1; } if(d->addresses != NULL) { // 返回接口的第一个地址的掩码 netmask=((struct sockaddr_in *)(d->addresses->netmask))->sin_addr.S_un.S_addr; } else { // 如果没有掩码,则默认设置为C类 netmask=0xffffff; } // 选择过滤包类型 int protocol_type; // 0->ip 1->tcp 2->udp 3->icmp printf("\n请选择监听的数据包协议类型(0->ip 1->tcp 2->udp 3->icmp) : "); scanf("%d",&protocol_type); switch(protocol_type) { case 0: strcpy(packet_filter,"ip"); break; case 1: strcpy(packet_filter,"ip and tcp"); break; case 2: strcpy(packet_filter,"ip and udp"); break; case 3: strcpy(packet_filter,"ip and icmp"); break; default: break; } // 编译过滤器 if (pcap_compile(adhandle, &fcode, packet_filter, 1, netmask) <0 ) { fprintf(stderr,"\n不能编译过滤器. 请检测语法.\n"); // 释放设备列表 pcap_freealldevs(alldevs); return -1; } //设置过滤器 if (pcap_setfilter(adhandle, &fcode)<0) { fprintf(stderr,"\n设置过滤器出错.\n"); pcap_freealldevs(alldevs); return -1; } printf("\n在网卡 %s 开始监听...\n", d->description); // 不再需要任何设备列表,进行释放 pcap_freealldevs(alldevs); // 开始嗅探 pcap_loop(adhandle, 0, packet_handler, NULL); return 0; }
int main(int argc, char **argv) { FILE *stdlog = NULL; bool borderless = false; char *input_pathname = NULL; char *output_pathname = NULL; int debug = DBG_ALL; int option; int option_index; int render_flags = 0; int printable_width = -1; int printable_height = -1; int rotation = -1; int inkjet = 1; int resolution = 300; int page_backside = 0; int stripe_height = STRIPE_HEIGHT; int concurrent_stripes = (BUFFERED_ROWS / STRIPE_HEIGHT); DF_duplex_t duplex = DF_DUPLEX_MODE_NONE; DF_media_size_t media_size = DF_MEDIA_SIZE_UNKNOWN; output_format_t output_format = OUTPUT_PPM; int testResult = ERROR; double extra_margin_left, extra_margin_right, extra_margin_top, extra_margin_bottom; int padding_options = PAD_NONE; extra_margin_left = extra_margin_right = extra_margin_top = extra_margin_bottom = 0.0f; const char *logfilename = NULL; FILE *imgfile = NULL; FILE *outputfile = NULL; bool monochrome = false; #ifndef EXCLUDE_PCLM bool pclm_test_mode = false; uint8 *pclm_output_buffer = NULL; void *pclmgen_obj= NULL; int outBuffSize = 0; PCLmPageSetup mypage; PCLmPageSetup *page_info = &mypage; memset(page_info, 0, sizeof(PCLmPageSetup)); #endif /* EXCLUDE_PCLM */ //Add pwg definitions #ifndef NO_CUPS cups_raster_t *ras_out = NULL; /* Raster stream */ cups_page_header2_t header_pwg; /* Page header */ #endif /* NO_CUPS */ while ( (option = getopt_long(argc, argv, "i:o:d:l:w:h:f:s:c:m:r:2:p:x:e:buz?v", long_options, &option_index)) != EOF ) { switch(option) { case 'i': input_pathname = optarg; break; case 'o': output_pathname = optarg; break; case 'd': debug = atoi(optarg); break; case 'l': logfilename = optarg; break; case 'b': borderless = true; break; case 'f': render_flags = atoi(optarg); break; case 'm': media_size = atoi(optarg); break; case 'w': printable_width = atoi(optarg); break; case 'h': printable_height = atoi(optarg); break; case 'r': rotation = atoi(optarg); break; case '2': duplex = atoi(optarg); break; case 'z': inkjet = 0; break; case 'x': resolution = atoi(optarg); break; case 'u': page_backside = 1; break; case 'p': output_format = atoi(optarg); break; case 's': stripe_height = atoi(optarg); break; case 'c': concurrent_stripes = atoi(optarg); break; case 'e': padding_options = atoi(optarg) & PAD_ALL; break; case OPTION_EXTRA_MARGIN_LEFT: extra_margin_left = atof(optarg); break; case OPTION_EXTRA_MARGIN_RIGHT: extra_margin_right = atof(optarg); break; case OPTION_EXTRA_MARGIN_TOP: extra_margin_top = atof(optarg); break; case OPTION_EXTRA_MARGIN_BOTTOM: extra_margin_bottom = atof(optarg); break; case OPTION_MONOCHROME: monochrome = true; break; case 'v': #ifndef EXCLUDE_PCLM pclm_test_mode = true; break; #endif /* EXCLUDE_PCLM */ case '?': default: _print_usage(argv[0]); return(0); } /* switch(option) */ } /* while */ // logging to a file? if (logfilename != NULL) { // open the logfile stdlog = fopen(logfilename, "w"); } // set the logging level and output wprint_set_debug_level(debug); wprint_set_stdlog(((stdlog != NULL) ? stdlog : stderr)); { char buffer[4096]; int param; snprintf(buffer, sizeof(buffer), "JOB PARAMETERS:"); for(param = 1; param < argc; param++) { strncat(buffer, " ", sizeof(buffer)); strncat(buffer, argv[param], sizeof(buffer)); } ifprint((DBG_FORCE, "%s", buffer)); } switch(output_format) { #ifndef EXCLUDE_PCLM case OUTPUT_PDF: if (!pclm_test_mode) { padding_options = PAD_ALL; } break; #endif /* EXCLUDE_PCLM */ #ifndef NO_CUPS case OUTPUT_PWG: break; #endif /* NO_CUPS */ case OUTPUT_PPM: break; default: ifprint((DBG_FORCE, "ERROR: output format not supported, switching to PPM")); output_format = OUTPUT_PPM; break; } do { /* if input file is specified at the end of the command line * without the '-f' option, take it */ if (!input_pathname && optind < argc && argv[optind] != NULL) { // coverity[var_assign_var] input_pathname = argv[optind++]; } if (!input_pathname) { ifprint((DBG_FORCE, "ERROR: invalid arguments")); _print_usage(argv[0]); continue; } /* if output file is specified at the end of the command line * without the '-f' option, take it */ if (!output_pathname && optind < argc && argv[optind] != NULL) { // coverity[var_assign_var] output_pathname = argv[optind++]; } if ((media_size != DF_MEDIA_SIZE_UNKNOWN) && (printable_width <= 0) && (printable_height <= 0)) { float margin_top, margin_bottom, margin_left, margin_right; wprint_job_params_t job_params; printer_capabilities_t printer_caps; memset(&job_params, 0, sizeof(wprint_job_params_t)); memset(&printer_caps, 0, sizeof(printer_capabilities_t)); printer_caps.canDuplex = 1; printer_caps.canPrintBorderless = 1; printer_caps.inkjet = inkjet; job_params.media_size = media_size; job_params.pixel_units = resolution; job_params.duplex = duplex, job_params.borderless = borderless; switch(output_format) { #ifndef EXCLUDE_PCLM case OUTPUT_PDF: job_params.pcl_type = PCLm; break; #endif /* EXCLUDE_PCLM */ #ifndef NO_CUPS case OUTPUT_PWG: job_params.pcl_type = PCLPWG; break; #endif /* NO_CUPS */ default: job_params.pcl_type = PCLNONE; break; } printable_area_get_default_margins(&job_params, &printer_caps, &margin_top, &margin_left, &margin_right, &margin_bottom); printable_area_get(&job_params, margin_top, margin_left, margin_right, margin_bottom); printable_width = job_params.printable_area_width; printable_height = job_params.printable_area_height; // mypage.mediaWidthInPixels = printable_width; //mypage.mediaHeightInPixels = printable_height; #ifndef EXCLUDE_PCLM if(margin_left < 0.0f || margin_top < 0.0f) { mypage.mediaWidthOffset=0.0f; mypage.mediaHeightOffset=0.0f; } else { mypage.mediaWidthOffset=margin_left; mypage.mediaHeightOffset=margin_top; } mypage.pageOrigin=top_left; // REVISIT mypage.dstColorSpaceSpefication=deviceRGB; #endif /* EXCLUDE_PCLM */ } #ifndef EXCLUDE_PCLM mypage.stripHeight=stripe_height; if(resolution==300) { mypage.destinationResolution=res300; } else if(resolution==600) { mypage.destinationResolution=res600; } else if(resolution==1200) { mypage.destinationResolution=res1200; } mypage.duplexDisposition=simplex; mypage.mirrorBackside=false; #endif /* EXCLUDE_PCLM */ if ((printable_width <= 0) || (printable_height <= 0)) { ifprint((DBG_FORCE, "ERROR: missing argumetns for dimensions")); _print_usage(argv[0]); continue; } imgfile = fopen(input_pathname, "r"); if (imgfile == NULL) { ifprint((DBG_FORCE, "unable to open input file")); continue; } testResult = OK; outputfile = NULL; if (output_pathname != NULL) { outputfile = fopen(output_pathname, "w"); } else { ifprint((DBG_FORCE, "output file not provided")); } wprint_image_info_t image_info; wprint_image_setup(&image_info, MIME_TYPE_HPIMAGE, &_wprint_ifc); wprint_image_init(&image_info); /* get the image_info of the input file of specified MIME type */ if ( wprint_image_get_info(imgfile, &image_info) == OK ) { if (rotation < 0) { rotation = ROT_0; if ((render_flags & RENDER_FLAG_PORTRAIT_MODE) != 0) { ifprint((DBG_LOG, "_print_page(): portrait mode")); rotation = ROT_0; } else if ((render_flags & RENDER_FLAG_LANDSCAPE_MODE) != 0) { ifprint((DBG_LOG, "_print_page(): landscape mode")); rotation = ROT_90; } else if (wprint_image_is_landscape(&image_info) && ((render_flags & RENDER_FLAG_AUTO_ROTATE) != 0)) { ifprint((DBG_LOG, "_print_page(): auto mode")); rotation = ROT_90; } if ((duplex == DF_DUPLEX_MODE_BOOK) && page_backside && ((render_flags & RENDER_FLAG_ROTATE_BACK_PAGE) != 0) && ((render_flags & RENDER_FLAG_BACK_PAGE_PREROTATED) == 0)) rotation = ((rotation == ROT_0) ? ROT_180 : ROT_270); } } else { ifprint((DBG_FORCE, "unable to process image")); } wprint_image_set_output_properties(&image_info, rotation, printable_width, printable_height, floor(resolution * extra_margin_top), floor(resolution * extra_margin_left), floor(resolution * extra_margin_right), floor(resolution * extra_margin_bottom), render_flags, stripe_height, concurrent_stripes, padding_options); int buff_size = wprint_image_get_output_buff_size(&image_info); unsigned char * buff = (unsigned char *)malloc(buff_size); memset(buff, 0xff, buff_size); int rows_left, num_rows; int output_width = wprint_image_get_width(&image_info); num_rows = rows_left = wprint_image_get_height(&image_info); int bytes_per_row = BYTES_PER_PIXEL(output_width); // process job start switch(output_format) { #ifndef EXCLUDE_PCLM case OUTPUT_PDF: { pclmgen_obj= (void*)CreatePCLmGen(); outBuffSize = 0; PCLmStartJob(pclmgen_obj, (void**)&pclm_output_buffer, &outBuffSize, pclm_test_mode); if (outputfile != NULL) { fwrite((char *)pclm_output_buffer, 1, outBuffSize, outputfile); } break; } #endif /* EXCLUDE_PCLM */ #ifndef NO_CUPS case OUTPUT_PWG: { ras_out = cupsRasterOpen(fileno(outputfile), CUPS_RASTER_WRITE_PWG); break; } #endif /* NO_CUPS */ default: { break; } } // write start page information switch(output_format) { #ifndef EXCLUDE_PCLM case OUTPUT_PDF: { mypage.SourceWidthPixels = output_width; mypage.SourceHeightPixels = num_rows; if (media_size != DF_MEDIA_SIZE_UNKNOWN) { _get_pclm_media_size_name(media_size, (char*)mypage.mediaSizeName); PCLmGetMediaDimensions(pclmgen_obj, mypage.mediaSizeName, &mypage ); } else { strcpy((char*)mypage.mediaSizeName, "CUSTOM"); mypage.mediaWidth = (((float)printable_width * STANDARD_SCALE_FOR_PDF)/(float)resolution); mypage.mediaHeight = (((float)printable_height * STANDARD_SCALE_FOR_PDF)/(float)resolution); mypage.mediaWidthInPixels = printable_width; mypage.mediaHeightInPixels = printable_height; } float standard_scale =(float)resolution/(float)72; page_info->sourceHeight = (float)num_rows/standard_scale; page_info->sourceWidth = (float)output_width/standard_scale; page_info->colorContent=color_content; page_info->srcColorSpaceSpefication=deviceRGB; page_info->compTypeRequested=compressDCT; outBuffSize = 0; PCLmStartPage(pclmgen_obj, page_info, (void**)&pclm_output_buffer, &outBuffSize); if (outputfile != NULL) { fwrite((char *)pclm_output_buffer, 1, outBuffSize, outputfile); } break; } #endif /* EXCLUDE_PCLM */ #ifndef NO_CUPS case OUTPUT_PWG: { _write_header_pwg(ras_out, resolution, image_info, &header_pwg, monochrome); /* * Output the pages... */ ifprint((DBG_LOG, "cupsWidth = %d", header_pwg.cupsWidth)); ifprint((DBG_LOG, "cupsHeight = %d", header_pwg.cupsHeight)); ifprint((DBG_LOG, "cupsBitsPerColor = %d", header_pwg.cupsBitsPerColor)); ifprint((DBG_LOG, "cupsBitsPerPixel = %d", header_pwg.cupsBitsPerPixel)); ifprint((DBG_LOG, "cupsBytesPerLine = %d", header_pwg.cupsBytesPerLine)); ifprint((DBG_LOG, "cupsColorOrder = %d", header_pwg.cupsColorOrder)); ifprint((DBG_LOG, "cupsColorSpace = %d", header_pwg.cupsColorSpace)); break; } #endif /* NO_CUPS */ default: { _write_header(outputfile, output_width, num_rows, monochrome); break; } } int height, nbytes; int image_row = 0; int actual_rows = 0; while(rows_left > 0) { height = MIN(rows_left, stripe_height); nbytes = wprint_image_decode_stripe(&image_info, image_row, &height, buff); if (nbytes > 0) { int rows_returned = (nbytes / bytes_per_row); actual_rows += rows_returned; if (height != rows_returned) { ifprint((DBG_LOG, "LOG: mismatch in reported bytes & height: %d vs %d", height, rows_returned)); } if (monochrome) { int readIndex, writeIndex; for(readIndex = writeIndex = 0; readIndex < nbytes; readIndex += BYTES_PER_PIXEL(1)) { unsigned char gray = SP_GRAY(buff[readIndex + 0], buff[readIndex + 1], buff[readIndex + 2]); buff[writeIndex++] = gray; if (output_format == OUTPUT_PDF) { buff[writeIndex++] = gray; buff[writeIndex++] = gray; } } nbytes = writeIndex; } // write the data switch(output_format) { #ifndef EXCLUDE_PCLM case OUTPUT_PDF: { outBuffSize= 0; PCLmEncapsulate(pclmgen_obj, buff, bytes_per_row, rows_returned, (void**)&pclm_output_buffer, &outBuffSize); if (outputfile != NULL) { fwrite((char *)pclm_output_buffer, 1, outBuffSize, outputfile); } break; } #endif /* EXCLUDE_PCLM */ #ifndef NO_CUPS case OUTPUT_PWG: { if (ras_out != NULL) { cupsRasterWritePixels(ras_out, buff, nbytes); } break; } #endif /* NO_CUPS */ default: { if (outputfile != NULL) { fwrite(buff, 1, nbytes, outputfile); } } } image_row += height; rows_left -= height; } else { if (nbytes < 0) { ifprint((DBG_ERROR, "ERROR: file appears to be corrupted")); } else { ifprint((DBG_ERROR, "LOG: data end with request image_row: %d for %d rows", image_row, MIN(rows_left, stripe_height))); } break; } } if (num_rows != actual_rows) { ifprint((DBG_ERROR, "ERROR: actual image rows: %d", actual_rows)); } // end of page processing switch(output_format) { #ifndef EXCLUDE_PCLM case OUTPUT_PDF: { outBuffSize = 0; PCLmEndPage(pclmgen_obj, (void**)&pclm_output_buffer, &outBuffSize); if (outputfile != NULL) { fwrite((char *)pclm_output_buffer, 1, outBuffSize, outputfile); } break; } #endif /* EXCLUDE_PCLM */ #ifndef NO_CUPS case OUTPUT_PWG: { break; } #endif /* NO_CUPS */ default: { if (num_rows != actual_rows) { _write_header(outputfile, wprint_image_get_width(&image_info), actual_rows, monochrome); break; } break; } } // end of job processing switch(output_format) { #ifndef EXCLUDE_PCLM case OUTPUT_PDF: { outBuffSize = 0; PCLmEndJob(pclmgen_obj, (void**)&pclm_output_buffer, &outBuffSize); if (outputfile != NULL) { fwrite((char *)pclm_output_buffer, 1, outBuffSize, outputfile); } PCLmFreeBuffer(pclmgen_obj, pclm_output_buffer); DestroyPCLmGen(pclmgen_obj); break; } #endif /* EXCLUDE_PCLM */ #ifndef NO_CUPS case OUTPUT_PWG: { break; } #endif /* NO_CUPS */ default: { break; } } wprint_image_cleanup(&image_info); if (buff != NULL) { free(buff); } } while(0); // close the imagefile if (imgfile != NULL) { fclose(imgfile); } // close the output file if (outputfile != NULL) { fclose(outputfile); } //if we use a res stream close it #ifndef NO_CUPS if(ras_out != NULL){ cupsRasterClose(ras_out); } #endif /* NO_CUPS */ // close the logfile if (stdlog != NULL) { fclose(stdlog); } return(testResult); } /* main */
int plugin_add(wprint_plugin_t *plugin) { char const **mt, **pf; int i, j, index; const char *name; if (plugin == NULL) { return ERROR; } index = _index; mt= plugin->get_mime_types(); pf = plugin->get_print_formats(); int version = plugin->version; if ((mt == NULL) || (pf == NULL)) { return ERROR; } memset(&_plugin[index], 0, sizeof(_plugin_db_t)); _plugin[index].version = version; _plugin[index].plugin = plugin; ifprint((DBG_LOG, " MIME types:")); // save a pointer to the name for comparison i = j = 0; while(mt[i]) { if (strlen(mt[i]) < MAX_MIME_LENGTH) { ifprint((DBG_LOG, "\t\t %s", mt[i])); strncpy(_plugin[index].mime_types[j++], mt[i], MAX_MIME_LENGTH); } i++; } if (j < _MAX_MIME_TYPES) _plugin[index].mime_types[j][0] = 0; ifprint((DBG_LOG, " print formats:")); i = j = 0; while(pf[i]) { if (strlen(pf[i]) < MAX_MIME_LENGTH) { ifprint((DBG_LOG, "\t\t %s", pf[i])); strncpy(_plugin[index].print_formats[j++], pf[i], MAX_MIME_LENGTH); } i++; } if (j < _MAX_PRINT_FORMATS) _plugin[index].print_formats[j][0] = 0; ifprint((DBG_LOG, "\r")); _index++; return(OK); } /* plugin_add */