znsoft
管理员
管理员
  • 注册日期2001-03-23
  • 最后登录2023-10-25
  • 粉丝300
  • 关注6
  • 积分910分
  • 威望14796点
  • 贡献值7点
  • 好评度2410点
  • 原创分5分
  • 专家分100分
  • 社区居民
  • 最爱沙发
  • 社区明星
阅读:979回复:8

test speed!!

楼主#
更多 发布于:2002-07-30 16:48
rt!
http://www.zndev.com 免费源码交换网 ----------------------------- 软件创造价值,驱动提供力量! 淡泊以明志,宁静以致远。 ---------------------------------- 勤用搜索,多查资料,先搜再问。
znsoft
管理员
管理员
  • 注册日期2001-03-23
  • 最后登录2023-10-25
  • 粉丝300
  • 关注6
  • 积分910分
  • 威望14796点
  • 贡献值7点
  • 好评度2410点
  • 原创分5分
  • 专家分100分
  • 社区居民
  • 最爱沙发
  • 社区明星
沙发#
发布于:2002-07-30 16:48
test 345656
http://www.zndev.com 免费源码交换网 ----------------------------- 软件创造价值,驱动提供力量! 淡泊以明志,宁静以致远。 ---------------------------------- 勤用搜索,多查资料,先搜再问。
moqingsong
论坛版主
论坛版主
  • 注册日期2002-04-07
  • 最后登录2011-02-03
  • 粉丝0
  • 关注0
  • 积分74分
  • 威望71点
  • 贡献值0点
  • 好评度10点
  • 原创分0分
  • 专家分0分
板凳#
发布于:2002-07-30 16:52
/*
 * Copyright (c) 2001 Jean-Baptiste Marchand, Herv・Schauer Consultants.  
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *      This product includes software developed by Jean-Baptiste Marchand
 * at Herv・Schauer Consultants.
 * 4. The name of the author may not be used to endorse or promote products
 *    derived from this software without specific prior written permission
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS\'\' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

/*
 * Control program for the pktfltsrv Win32 packet filtering service
 */

#include <windows.h>
#include <stdio.h>
#include <iphlpapi.h>

#define THE_PIPE \"\\\\\\\\.\\\\pipe\\\\PktFltPipe\"
#define MAX_RULE_LENGTH 256

#define SYN_ERROR_MSG \"Syntax error: \"
#define SYN_ERROR_MSG_LEN 14

#define FILTER_FAILURE 0 /* filter was correctly added */
#define FILTER_SUCCESS 1 /* filter had a syntax error */
#define FILTER_MESSAGE 2 /* informative message returned to the client */


void usage(char **argv)
{
char *progname = \"pktctl\";

fprintf(stderr, \"usage:\\n\", progname);
fprintf(stderr, \"       %s -a filtering_rule: add a filtering rule\\n\", progname);
fprintf(stderr, \"       %s -d rule interface: delete a rule on specificied interface\\n\", progname);
fprintf(stderr, \"       %s -f filters_file: source filters file\\n\", progname);
fprintf(stderr, \"       %s -i : interactive mode\\n\", progname);
fprintf(stderr, \"       %s -F filters_file: flush all interfaces and load filters file\\n\", progname);
fprintf(stderr, \"       %s -l interface: list rules on specified interface\\n\", progname);
fprintf(stderr, \"       %s -s interface: get statistics on specified interface\\n\", progname);
fprintf(stderr, \"       %s -Fa interface: flush all rules on specified interface\\n\", progname);
fprintf(stderr, \"       %s -I: list all interfaces\\n\", progname);
}

void display_help(void)
{
fprintf(stderr, \"Command Action\\n\");
fprintf(stderr, \"-------------------------------------------------------------------------------\\n\");
fprintf(stderr, \"rule add \'rule\' to the filtering engine\\n\");
fprintf(stderr, \"delete rule on ifname   delete rule numbered \'rule\' on \'ifname\'\\n\");
fprintf(stderr, \"flush on ifname flush rules on interface \'ifname\'\\n\");
fprintf(stderr, \"list on ifname list current rules on interface \'ifname\'\\n\");
fprintf(stderr, \"stats on ifname give filtering stats for interface \'ifname\'\\n\");
fprintf(stderr, \"source filename load rules from file \'filename\'\\n\");
fprintf(stderr, \"reload filename flush all interfaces and load rules from file \'filename\'\");
fprintf(stderr, \"help | h display this help text\\n\");
fprintf(stderr, \"quit | q | exit | x exit\\n\");
}

/* get the width of the output terminal */
short get_output_width(void)
{
HANDLE stdout_handle;
CONSOLE_SCREEN_BUFFER_INFO screen_infos;
BOOL status;
short width = 80; /* default value */

stdout_handle = GetStdHandle(STD_OUTPUT_HANDLE);
if (stdout_handle == INVALID_HANDLE_VALUE)
return width;

status = GetConsoleScreenBufferInfo(stdout_handle, &screen_infos);
if (status == 0)
return width;

return screen_infos.dwSize.X;
}

/* display the position of a syntax error */
void display_syn_error(char *filter, unsigned char pos)
{
unsigned char pad_width = pos;
unsigned short msg_width;
unsigned short output_width;
unsigned short chars_before;
char *one_line_format_str = \"%%s%%s%\\n%%%ds\\n\";
char *multi_lines_format_str = \"%%s%%.%ds\\n%%%ds\\n%%s\\n\";
char format_str_expanded[42];

output_width = get_output_width();
msg_width = SYN_ERROR_MSG_LEN + strlen(filter);
if (output_width > msg_width) {
/* everything on one line */
_snprintf(format_str_expanded, 42,
one_line_format_str,
pad_width +
SYN_ERROR_MSG_LEN + 1);
printf(format_str_expanded,
SYN_ERROR_MSG, filter,
\"^\");
}
else {
/* multi-lines */
chars_before = pad_width +
(output_width -
((SYN_ERROR_MSG_LEN +
  pad_width) % output_width));
_snprintf(format_str_expanded, 42,
multi_lines_format_str,
chars_before,
output_width -
(chars_before -
pad_width) +1);
if (chars_before < strlen(filter))
printf(format_str_expanded,
SYN_ERROR_MSG,
filter, \"^\", filter +
chars_before);
else
printf(format_str_expanded,
SYN_ERROR_MSG,
filter, \"^\", \"\");
}
}

/* list (Ethernet) interfaces */
int list_ip_interfaces(void)
{
IP_ADAPTER_INFO *adapt_info;
DWORD status;
ULONG size;
UCHAR count = 0;

/* trying with only one interface */
size = sizeof(IP_ADAPTER_INFO);
adapt_info = malloc(size);
status = GetAdaptersInfo(adapt_info, &size);
if (status != ERROR_SUCCESS) {
/* not enough space */
free(adapt_info);
adapt_info = malloc(size);
status = GetAdaptersInfo(adapt_info, &size);
}

while (adapt_info) {
/* Assuming only Ethernet adapters */
printf(\"eth%d (%s): %s \\n\", count++, adapt_info->Description, adapt_info->IpAddressList.IpAddress.String);
adapt_info = adapt_info->Next;
}
return 1;
}

/* write the filter and print an error message if necessary */
char write_filter(char *filter)
{
DWORD read;
DWORD written;
BOOL bool;
HANDLE my_pipe;
char status_code;
char syntax_error;
unsigned char *msg_buf = NULL;
unsigned int msg_buf_size;

bool = WaitNamedPipe(THE_PIPE, NMPWAIT_USE_DEFAULT_WAIT);
if (!bool) {
fprintf(stderr, \"error: unable to connect to %s\\n\", THE_PIPE);
fprintf(stderr, \"Stateless Packet Filtering service is probably not running\\n\");
exit(1);
}

/* open the pipe */
my_pipe = CreateFile(THE_PIPE, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
if (my_pipe == INVALID_HANDLE_VALUE) {
fprintf(stderr, \"error: unable to read from %s\\n\", THE_PIPE);
exit(1);
}

/* write the filter in the pipe */
bool = WriteFile(my_pipe, filter, strlen(filter), &written, NULL);
if (!bool) {
fprintf(stderr, \"error: unable to write to %s\\n\", THE_PIPE);
fprintf(stderr, \"Stateless Packet Filtering service is probably not running\\n\");
exit(1);
}

/* read the result */
bool = ReadFile(my_pipe, &status_code, 1, &read, NULL);
if (!bool || read != 1) {
fprintf(stderr, \"error: unable to read from %s\\n\", THE_PIPE);
exit(1);
}

if (status_code == FILTER_FAILURE) {
/* syntax error */
bool = ReadFile(my_pipe, &syntax_error, 1, &read, NULL);
if (!bool || read != 1) {
fprintf(stderr, \"error: unable to read from %s\\n\", THE_PIPE);
exit(1);
}
display_syn_error(filter, syntax_error);
return FILTER_FAILURE;
}
if (status_code == FILTER_MESSAGE) {
/* informative message */

/* read length of the message */
bool = ReadFile(my_pipe, &msg_buf_size, sizeof(msg_buf_size),
&read, NULL);
if (!bool || read != sizeof(msg_buf_size)) {
fprintf(stderr, \"error: unable to read from %s\\n\", THE_PIPE);
exit(1);
}

/* read message */
msg_buf = malloc((msg_buf_size + 1)* sizeof(char));
memset(msg_buf, 0, (msg_buf_size + 1));
bool = ReadFile(my_pipe, msg_buf, msg_buf_size, &read, NULL);
if (!bool || read != msg_buf_size) {
fprintf(stderr, \"error: unable to read from %s\\n\", THE_PIPE);
exit(1);
}
printf(\"%s\", msg_buf);
return FILTER_MESSAGE;
}

if (status_code != FILTER_SUCCESS)
fprintf(stderr, \"warning: unknown status code\\n\");
return FILTER_SUCCESS;
}


/* get filtering statistics on given interface */
void statistics_on_interface(char *iface)
{
char stats_msg[32] = \"stats on \";

strncat(stats_msg, iface, 32 - strlen(stats_msg));

write_filter(stats_msg);
}


/* delete the rules on given interface */
void flush_interface(char *iface)
{
char flush_msg[32] = \"flush on \";

strncat(flush_msg, iface, 32 - strlen(flush_msg));

write_filter(flush_msg);
}

/* list the rules on given interface */
void list_rules_on_interface(char *iface)
{
char list_msg[32] = \"list on \";

strncat(list_msg, iface, 32 - strlen(list_msg));

write_filter(list_msg);
}


/* read filters from a file */
char read_filters_from_file(char *filename)
{
FILE *filters;
char line[MAX_RULE_LENGTH]; /* XXX one rule can\'t exceed 512 bytes XXX */
char success;

filters = fopen(filename, \"r\");
if (filters == NULL) {
fprintf(stderr,\"error: could not open filters file %s\\n\",
filename);
return -1;
}
while (fgets(line, MAX_RULE_LENGTH, filters) != NULL) {
if (*line != \'#\')
success = write_filter(line);
else /* ignore comments */
continue;
if (success == FILTER_FAILURE)
/* stop if a line contains a syntax error */
break;
}
fclose(filters);
return success;
}

/* launch \'pktctl>\' shell */
char start_pktctl_shell(void)
{
char rule_buf[MAX_RULE_LENGTH];
char *word;
char *p;
char *ret;

while (1) {
printf(\"pktctl> \", 8);
memset(rule_buf, 0, MAX_RULE_LENGTH);
ret = fgets(rule_buf, MAX_RULE_LENGTH, stdin);
if (ret == NULL) {
fprintf(stderr, \"error: rule too long\\n\");
continue;
}
/* test \'help\' command */
if ((strncmp(rule_buf, \"help\", 4) == 0)
|| (strncmp(rule_buf, \"h\\n\", 2) == 0)) {
display_help();
continue;
}

/* test \'source\' command */
if (strncmp(rule_buf, \"source\", 6) == 0) {
word = rule_buf + 6;
while ((word < (rule_buf + MAX_RULE_LENGTH)) && (*word == \' \'))
word++;
p = word;
while ((word < (rule_buf + MAX_RULE_LENGTH)) && (*word != \'\\n\'))
word++;
*word = 0;
read_filters_from_file(p);
continue;
}

/* test \'reload\' command */
if (strncmp(rule_buf, \"reload\", 6) == 0) {

flush_interface(\"all\");

word = rule_buf + 6;
while ((word < (rule_buf + MAX_RULE_LENGTH)) && (*word == \' \'))
word++;
p = word;
while ((word < (rule_buf + MAX_RULE_LENGTH)) && (*word != \'\\n\'))
word++;
*word = 0;
read_filters_from_file(p);
continue;
}

/* test exit conditions : q, exit or ^D */
if (strncmp(rule_buf, \"q\\n\", 2) == 0)
return 1;
if (strncmp(rule_buf, \"quit\", 4) == 0)
      return 1;
if (strncmp(rule_buf, \"exit\", 4) == 0)
      return 1;
if (strncmp(rule_buf, \"\\004\\n\", 2) == 0)
return 1;
if (strncmp(rule_buf, \"x\\n\", 2) == 0)
return 1;
/* test \"\" condition */
if (strcmp(rule_buf, \"\\n\") == 0)
continue;
write_filter(rule_buf);
}
}

/* control program for the packet filtering service */
int main(int argc, char **argv)
{
DWORD status;
unsigned char arg;
char list_iface = 0;
char flush_all = 0;
char rule_to_delete = 0;
char interactive = 0;
char *iface = NULL;
char *rule = NULL;
char *filters_file = NULL;
char *stats_iface = NULL;
char *list_rules_iface = NULL;
char *flush_iface = NULL;

if (argc < 2) {
usage(argv);
return 1;
}

arg = 1;
while (arg < argc) {
if (!strcmp(argv[arg], \"-Fa\")) {
if (++arg == argc)
/* assume \'flush on all\' */
flush_iface = \"all\";
else {
flush_iface = strdup(argv[arg]);
if (++arg != argc) {
fprintf(stderr, \"error: option \'-Fa\' accepts only one parameter\\n\");
usage(argv);
return 1;
}
}
continue;
}
if (!strcmp(argv[arg], \"-i\")) {
interactive = 1;
if (++arg != argc) {
fprintf(stderr, \"error: option \'-i\' accepts no parameter\\n\");
usage(argv);
return 1;
}
continue;
}
if (!strcmp(argv[arg], \"-I\")) {
list_iface = 1;
if (++arg != argc) {
fprintf(stderr, \"error: option \'-I\' accepts no parameter\\n\");
usage(argv);
return 1;
}
continue;
}
if (!strcmp(argv[arg], \"-a\")) {
if (++arg == argc) {
fprintf(stderr, \"error: option \'-a\' needs one parameter\\n\");
usage(argv);
return 1;
}
rule = strdup(argv[arg]);
if (++arg != argc) {
fprintf(stderr, \"error: option \'-a\' accepts only one parameter\\n\");
usage(argv);
return 1;
}
continue;
}
if (!strcmp(argv[arg], \"-f\")) {
if (++arg == argc) {
fprintf(stderr, \"error: option \'-f\' needs one parameter\\n\");
usage(argv);
return 1;
}
filters_file = strdup(argv[arg]);
if (++arg != argc) {
fprintf(stderr, \"error: option \'-f\' accepts only one parameter\\n\");
usage(argv);
return 1;
}
continue;
}
if (!strcmp(argv[arg], \"-F\")) {
flush_all = 1;
if (++arg == argc) {
fprintf(stderr, \"error: option \'-F\' needs one parameter\\n\");
usage(argv);
return 1;
}
filters_file = strdup(argv[arg]);
if (++arg != argc) {
fprintf(stderr, \"error: option \'-F\' accepts only one parameter\\n\");
usage(argv);
return 1;
}
continue;
}
if (!strcmp(argv[arg], \"-d\")) {
if (++arg == argc) {
fprintf(stderr, \"error: option \'-d\' needs one parameter\\n\");
usage(argv);
return 1;
}
rule_to_delete = atoi(argv[arg]);
if (++arg != argc) {
fprintf(stderr, \"error: option \'-d\' accepts only one parameter\\n\");
usage(argv);
return 1;
}
continue;
}
if (!strcmp(argv[arg], \"-s\")) {
if (++arg == argc) {
fprintf(stderr, \"error: option \'-s\' needs one parameter\\n\");
usage(argv);
return 1;
}
stats_iface = strdup(argv[arg]);
if (++arg != argc) {
fprintf(stderr, \"error: option \'-s\' accepts only one parameter\\n\");
usage(argv);
return 1;
}
continue;
}
if (!strcmp(argv[arg], \"-l\")) {
if (++arg == argc) {
fprintf(stderr, \"error: option \'-l\' needs one parameter\\n\");
usage(argv);
return 1;
}
list_rules_iface = strdup(argv[arg]);
if (++arg != argc) {
fprintf(stderr, \"error: option \'-l\' accepts only one parameter\\n\");
usage(argv);
return 1;
}
continue;
}
fprintf(stderr, \"error: unknown option \'%s\'\\n\", argv[arg]);
usage(argv);
return 1;
}

/* -i option : interactive */
if (interactive)
status = start_pktctl_shell();

/* -I option */
if (list_iface) {
list_ip_interfaces();
return 0;
}

/* -a option */
if (rule) {
if (strlen(rule) > MAX_RULE_LENGTH) {
fprintf(stderr, \"error: rule too long\\n\");
return 1;
}
write_filter(rule);
}

/* -f | -F option */
if (filters_file) {
if (flush_all) {
FILE *filters;

/* test if file exist before flushing */
filters = fopen(filters_file, \"r\");
if (filters) {
flush_interface(\"all\");
fclose(filters);
}
else {
fprintf(stderr, \"error: could not open filters file %s\\n\", filters_file);
return 1;
}
}
status = read_filters_from_file(filters_file);
}

/* -l option */
if (list_rules_iface)
list_rules_on_interface(list_rules_iface);

/* -s option */
if (stats_iface)
statistics_on_interface(stats_iface);

/* -Fa option */
if (flush_iface)
flush_interface(flush_iface);

return 0;
}
按第一贴的“给分”键,给分。
moqingsong
论坛版主
论坛版主
  • 注册日期2002-04-07
  • 最后登录2011-02-03
  • 粉丝0
  • 关注0
  • 积分74分
  • 威望71点
  • 贡献值0点
  • 好评度10点
  • 原创分0分
  • 专家分0分
地板#
发布于:2002-07-30 16:53
上面一贴,耗时40秒。
按第一贴的“给分”键,给分。
znsoft
管理员
管理员
  • 注册日期2001-03-23
  • 最后登录2023-10-25
  • 粉丝300
  • 关注6
  • 积分910分
  • 威望14796点
  • 贡献值7点
  • 好评度2410点
  • 原创分5分
  • 专家分100分
  • 社区居民
  • 最爱沙发
  • 社区明星
地下室#
发布于:2002-07-30 16:55
感觉应该快点了吧?
http://www.zndev.com 免费源码交换网 ----------------------------- 软件创造价值,驱动提供力量! 淡泊以明志,宁静以致远。 ---------------------------------- 勤用搜索,多查资料,先搜再问。
moqingsong
论坛版主
论坛版主
  • 注册日期2002-04-07
  • 最后登录2011-02-03
  • 粉丝0
  • 关注0
  • 积分74分
  • 威望71点
  • 贡献值0点
  • 好评度10点
  • 原创分0分
  • 专家分0分
5楼#
发布于:2002-07-30 16:59
好一点了。
按第一贴的“给分”键,给分。
xdjm
驱动中牛
驱动中牛
  • 注册日期2001-04-02
  • 最后登录2024-01-25
  • 粉丝0
  • 关注0
  • 积分34分
  • 威望25点
  • 贡献值0点
  • 好评度3点
  • 原创分0分
  • 专家分0分
  • 社区居民
6楼#
发布于:2002-07-30 17:27
今天好象快一点儿了~~~
denizen
驱动大牛
驱动大牛
  • 注册日期2001-12-30
  • 最后登录2012-05-15
  • 粉丝0
  • 关注0
  • 积分0分
  • 威望0点
  • 贡献值0点
  • 好评度0点
  • 原创分0分
  • 专家分0分
7楼#
发布于:2002-07-30 17:30
是快了点。
Where there is a will, there is a road.
denizen
驱动大牛
驱动大牛
  • 注册日期2001-12-30
  • 最后登录2012-05-15
  • 粉丝0
  • 关注0
  • 积分0分
  • 威望0点
  • 贡献值0点
  • 好评度0点
  • 原创分0分
  • 专家分0分
8楼#
发布于:2002-07-30 17:31
不过没什么根本性的提高。
Where there is a will, there is a road.
游客

返回顶部