免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1253 | 回复: 2
打印 上一主题 下一主题

tcp port scanner 意外停止的问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2004-05-11 09:48 |只看该作者 |倒序浏览
#include <stdio.h>;
#include <stdlib.h>;
#include <string.h>;
#include <unistd.h>;
#include <netdb.h>;
#include <sys/types.h>;
#include <sys/socket.h>;
#include <netinet/in.h>;
#include <arpa/inet.h>;

enum
{ CMD_NAME, DST_IP, START_PORT, LAST_PORT };
enum
{ CONNECT, NOCONNECT };

int tcpportscan (u_int dst_ip, int dst_port);

int
main (int argc, char *argv[])
{
        u_long dst_ip;
        int dst_port;
        int start;
        int end;

        if (argc != 4)
        {
                fprintf (stderr, "usage:%s dst_ip start_port last_port\n",
                         argv[CMD_NAME]);
                exit (EXIT_FAILURE);
        }

        if ((dst_ip = inet_addr (argv[DST_IP])) == INADDR_NONE)
        {
                struct hostent *he;
                if ((he = gethostbyname (argv[DST_IP])) == NULL)
                {
                        fprintf(stderr,"gethostbyname error\n";
                        exit (EXIT_FAILURE);
                }
                memcpy((char *)&dst_ip,(char *)he->;h_addr,he->;h_length);
                printf("scan %s(%s) tcp port\n",argv[DST_IP],inet_ntoa(*(struct in_addr *)&dst_ip));

        }
        start = atoi (argv[START_PORT]);
        end = atoi (argv[LAST_PORT]);

论坛徽章:
0
2 [报告]
发表于 2004-05-11 09:49 |只看该作者

tcp port scanner 意外停止的问题

//************processing unit****************
        for (dst_port = start; dst_port <= end; dst_port++)
        {
                //usleep(1000);
                printf ("Scan Port %d\r", dst_port);
                fflush (stdout);
                if (tcpportscan (dst_ip, dst_port) == CONNECT)
                {
                        struct servent *sp;
                        sp = getservbyport (htons (dst_port), "tcp";        //get server name
                        printf ("%5d %-20s\n", dst_port,
                                (sp == NULL) ? "unknown" : sp->;s_name);
                }
        }
        return EXIT_SUCCESS;
}

//******************tcpportscan***********************
int
tcpportscan (u_int dst_ip, int port)
{
        struct sockaddr_in dest;
        int s;
        int ret;

        memset ((char *) &dest, 0, sizeof (dest));
        dest.sin_family = AF_INET;
        dest.sin_port = htons (port);
        dest.sin_addr.s_addr = dst_ip;

        if ((s = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
        {
                perror ("socket";
                exit (EXIT_FAILURE);
        }

        if (connect (s, (struct sockaddr *) &dest, sizeof (dest)) < 0)
                ret = NOCONNECT;
        else
                ret = CONNECT;

        close (s);
        return ret;
}

论坛徽章:
0
3 [报告]
发表于 2004-05-11 09:57 |只看该作者

tcp port scanner 意外停止的问题

不好意思哈,贴上去后格式就变了,我的问题是扫描普通机子没有问题,但是扫描服务器时,可能是防火墙起作用了,导致扫了几十个port后就停止响应了,死在那边了,是因为connect系统调用在等待吗?connect没有超时错误吗?或者怎么把超时做进去?
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP