PersonalCorpus 版 (精华区)
发信人: cliff (夺命小辣椒), 信区: Unix
标 题: TCP端口访问器
发信站: 紫 丁 香 (Mon Dec 6 10:58:57 1999) WWW-POST
/* TCP端口访问器,visitor 2.0, author:cpu */
/* 用法: visitor 主机 端口 [lf|-lf] bin */
/* lf: 送出字符串尾自动加\r\n, -lf则不加 */
/* bin: 送回字符串同时以子节码方式显示 */
/* visitor host 110 lf 就可以帮你简单pop3取删信, 相当于telnet host 110 */
/* visitor host 23 lf bin 可以帮你了解telnet通信规程 */
/* ... */
# include
# include
# include
# include
# include
# include
# include
# include
# include
# include
# include
# include
# define TRUE 1
# define FALSE 0
# define MAXSIZE 1024
extern int errno;
char host[64];
short port;
u_long lHostIp;
int sock;
struct sockaddr_in me, it;
char sLine[MAXSIZE];
int linefeed = FALSE;
int bin = FALSE;
int WriteLine(int fd, char * buffer) /* write a line to socket */
{
int length;
int flag;
length = strlen( buffer );
if (buffer[length-1] == '~') {
flag = MSG_OOB;
--length;
} else {
flag = 0;
}
if (linefeed) {
buffer[length++] = '\r';
buffer[length++] = '\n';
}
buffer[length] = '\0';
if ( send( fd, buffer, length, flag) > 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 (send(fd, s, strlen(s), 0) < 0) {
close( fd );
return FALSE;
}
} else {
c = (unsigned char)atoi(s);
if (send(fd, &c, 1, 0) < 0) {
close( fd );
return FALSE;
}
}
}
fprintf(stderr, "\n");
}
return TRUE;
}
int ReadLine(int fd, char * buffer) /* recv some char from socket */
{
int n;
if ((n = recv( fd, buffer, MAXSIZE, 0)) > 0) {
buffer[n] = '\0';
return n;
}
return FALSE;
}
int Explain(char *command) /* explain the command u input */
{
if (strlen(command) == 0) {
return TRUE;
}
if (strcmp(command, "exit") == 0) {
fprintf(stderr,
"visitor->cpu will love u for ever;)\n\n");
close(sock);
exit(0);
} else if (strcmp(command, "asc") == 0) {
fprintf(stderr,
"visitor->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,
"visitor->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, "visitor->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 visitor 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 visitor 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, n, namelen;
fd_set fdR;
if (argc < 4) {
printf("\nvisitor is a simple tool to visit tcp 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 lf|-lf [bin]\n", argv[0]);
printf("lf|-lf: each command will be sent with|without \'\\r\\n\'\n");
printf("bin: also display characters received as byte-code mode\n");
printf("the command prompt will be \'visitor<-\'\n");
printf("the characters received will be lead by prompt \'visitor->\'\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 shit 255 10 13\' will cause this tool send
byte-code 23,\n");
printf("string \'shit\' and byte-code 255, 10(lf), 13(cr)\n");
printf("input command end with \'~\' to send OOB data,just like \'aaa~\'\n");
printf("input \'exit\' to terminate visitor\n\n");
exit(0);
}
if (strcmp(argv[3], "lf") == 0) {
linefeed = TRUE;
} else if (strcmp(argv[3], "-lf") == 0) {
linefeed = FALSE;
} else {
goto ERROR;
}
if (argv[4] && strcmp(argv[4], "bin") == 0) {
bin = TRUE;
}
strcpy(host, argv[1]);
port = atoi(argv[2]);
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(port);
if ((sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) {
perror("socket");
exit(3);
}
if (connect(sock, (struct sockaddr *)&it, sizeof(it)) < 0) {
perror("connect");
exit(4);
} else {
fprintf(stderr, "connected to %s port %d, input \'help\' to see help
information.\n",
host, port);
fprintf(stderr, "\nvisitor<-");
}
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, "visitor<-");
} else {
fprintf(stderr, "visitor<-");
continue;
}
}
if (FD_ISSET(sock, &fdR)) {
if(!(n = ReadLine(sock, sLine))) {
printf("\rvisitor->peer closed, abort reading\n\n");
close(sock);
return;
}
fprintf(stderr, "\rvisitor->%s\n",
sLine);
if (bin) {
fprintf(stderr, "binary mode is: ");
for (i = 0;i < n;i++) {
fprintf(stderr, "%d ", (unsigned char)sLine[i]);
}
fprintf(stderr, "\n");
}
fprintf(stderr, "visitor<-");
}
}
}
}
--
________________
/________________/|
| | |
| 网 | |
| 络 | |
※ 来源:·紫 丁 香 bbs.hit.edu.cn·[FROM: 202.118.239.10]
Powered by KBS BBS 2.0 (http://dev.kcn.cn)
页面执行时间:4.616毫秒