PersonalCorpus 版 (精华区)
发信人: cliff (夺命小辣椒), 信区: Unix
标 题: UDP端口访问器
发信站: 紫 丁 香 (Mon Dec 6 11:00:25 1999) WWW-POST
/* UDP端口访问器, udp 2.0, author:cpu */
/* 用法: udp 主机 端口 -n -b - */
/* lf: 送出字符串尾自动加\r\n, -lf则不加 */
/* bin: 送回字符串同时以字节码方式显示 */
/* ... */
# include
# include
# include
# include
# include
# include
# include
# include
# include
# include
# include
# include
# include
# define TRUE 1
# define FALSE 0
# define MAXSIZE 1024
extern int errno;
extern char * optarg;
extern int optind;
char host[64];
u_short s_port = 3000;
u_short d_port;
u_long lHostIp;
int sock;
struct sockaddr_in me, it;
char sLine[MAXSIZE];
int linefeed = TRUE;
int bin = FALSE;
int WriteLine(int fd, char * buffer) /* write a line to socket */
{
int length;
length = strlen( buffer );
if (linefeed) {
buffer[length++] = '\r';
buffer[length++] = '\n';
}
buffer[length] = '\0';
if ( sendto( fd, buffer, length, 0, &it, sizeof(it)) > 0 ) {
return TRUE;
}
return FALSE;
}
int WriteAll(int fd, char * buffer)
{
unsigned char c;
char temp[MAXSIZE], * s;
strcpy(temp, buffer);
s = strtok(temp, " \n");
if (strcmp( s, "bin" ) != 0) {
if (!WriteLine( fd, buffer )) return FALSE;
} else {
while (s = strtok( NULL, " \n" )) {
if (isalpha(s[0])) {
if (sendto(fd, s, strlen(s), 0,
&it, sizeof(it)) < 0) {
close( fd );
return FALSE;
}
} else {
c = (unsigned char)atoi(s);
if (sendto(fd, &c, 1, 0, &it, sizeof(it)) < 0) {
close( fd );
return FALSE;
}
}
}
fprintf(stderr, "\n");
}
return TRUE;
}
int ReadLine(int fd, char * buffer) /* recv some char from socket */
{
int n;
int len;
if ((n = recvfrom( fd, buffer, MAXSIZE, 0, &it, &len)) > 0) {
buffer[n] = '\0';
return TRUE;
}
return FALSE;
}
int Explain(char *command) /* explain the command u input */
{
if (strlen(command) == 0) {
return TRUE;
}
if (strcmp(command, "exit") == 0) {
fprintf(stderr,
"udp>thx for using this tool ;)\n\n");
close(sock);
exit(0);
} else if (strcmp(command, "asc") == 0) {
fprintf(stderr,
"udp>string sent back by server will be shown only by asc mode\n\n");
bin = FALSE;
return TRUE;
} else if (strcmp(command, "bin") == 0) {
fprintf(stderr,
"udp>string sent back by server will be shown both by asc and byte-code
mode\n\n");
bin = TRUE;
return TRUE;
} else if (strcmp(command, "help") == 0) {
fprintf(stderr, "udp>Valid command is:\n");
fprintf(stderr, "asc: set string show mode as asc mode only.\n");
fprintf(stderr, "bin: set string show mode as both asc and byte mode.\n");
fprintf(stderr, "exit: exit udp session.\n");
fprintf(stderr, "help: show these help informations.\n");
fprintf(stderr, "Other string will be directly sent to server and wait for
server's reply.\n");
fprintf(stderr, "U can input string begin with \'bin\' to send bytes and
string tokens.\n");
fprintf(stderr, "Just Like \'bin 255 240 cpu\' will cause udp send char(255),
char(244)\n");
fprintf(stderr, "and string token \'cpu\'. Enjoy it...\n");
} else
return FALSE;
}
main(int argc, char** argv)
{
struct hostent* h;
int i, namelen;
fd_set fdR;
if (argc < 3) {
printf("\nudp is a simple tool to visit udp port of internet host\n");
printf("this tool is developed by cpu of bbs.gznet.edu.cn\n");
ERROR:
printf("\n");
printf("usage: %s host|IP port [s source_port] [nb]\n", argv[0]);
printf("s: indicate the source port bind to me is source_port\n");
printf("n: each command will be sent without ,default is each line end
\n\twith \n");
printf("b: also display characters received as byte-code mode\n");
printf("the command prompt will be \'udp<\'\n");
printf("the characters received will be lead by prompt \'udp>\'\n\n");
printf("valid command is asc, bin, exit, help and bin...\n");
printf("input command begin with \'bin\' and \' \' to send byte-code and
strings\n");
printf("for example: \'bin 23 token 255 10 13\' will cause this tool send
byte-code 23,\n");
printf("string \'token\' and byte-code 255, 10(lf), 13(cr)\n");
printf("input \'exit\' to terminate udp\n\n");
exit(0);
}
strcpy(host, argv[1]);
d_port = atoi(argv[2]);
while ((i = getopt(argc-2, argv+2, "nbs:")) != EOF) {
switch (i) {
case 's': /* source port */
s_port = (u_short)atoi(optarg);
break;
case 'n': /* if each line end with '\r\n' */
linefeed = FALSE;
break;
case 'b': /* if display with byte-code style */
bin = TRUE;
break;
default :
goto ERROR;
break; /* NOTREACHED */
}
}
bzero(&it, sizeof(it));
it.sin_family = AF_INET;
if ((h = gethostbyname(host)) == NULL) {
if ((it.sin_addr.s_addr = inet_addr(host)) == -1) {
fprintf(stderr, "%s: unknown host\n", argv[1]);
exit(1);
}
} else {
bcopy(h->h_addr_list[0], &it.sin_addr, h->h_length);
}
it.sin_port = htons(d_port);
if ((sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
perror("socket");
exit(3);
}
bzero(&me, sizeof(me));
me.sin_family = AF_INET;
me.sin_addr.s_addr = htons(INADDR_ANY);
me.sin_port = htons(s_port);
if (bind(sock, &me, sizeof(me)) < 0) {
perror("bind");
exit(4);
}
fprintf(stderr, "bind localhost port %d, input \'help\' to see help
information.\n", s_port);
fprintf(stderr, "\nudp<");
for(;;) {
FD_ZERO( &fdR );
FD_SET(sock, &fdR);
FD_SET(0, &fdR);
sLine[0] = '\0';
switch(select(32, &fdR, NULL, NULL, NULL)) {
case -1:
perror("select error\n");
exit(-1);
case 0: /* select timeout */
continue;
default:
/* stdin readable, send command to server */
if (FD_ISSET(0, &fdR)) {
gets(sLine);
if (Explain(sLine) == FALSE) {
WriteAll(sock, sLine);
fprintf(stderr, "udp<");
} else {
fprintf(stderr, "udp<");
continue;
}
}
if (FD_ISSET(sock, &fdR)) {
if(!ReadLine(sock, sLine)) {
printf("\rudp>peer closed, abort reading\n\n");
close(sock);
return;
}
fprintf(stderr, "\rudp>%s\n",
sLine);
if (bin) {
fprintf(stderr, "byte mode is: ");
for (i = 0;i < strlen(sLine);i++) {
fprintf(stderr, "%d ", (unsigned char)sLine[i]);
}
fprintf(stderr, "\n");
}
fprintf(stderr, "udp<");
}
}
}
}
--
________________
/________________/|
| | |
| 网 | |
| 络 | |
※ 来源:·紫 丁 香 bbs.hit.edu.cn·[FROM: 202.118.239.10]
Powered by KBS BBS 2.0 (http://dev.kcn.cn)
页面执行时间:5.511毫秒