- 论坛徽章:
- 0
|
我想用下面这func扫描指定主机的端口.
在外层使用for调用它.
现在出了个问题是,扫描有的ip没问题,有的ip会非常慢,甚至没结果(都是内网IP,无firewall),而用nmap扫描就没事.- #include "include/fus.h"
- int scan_connect(char *ip, int port)
- {
- int sockfd, ifok, i, retconn, retval, len, flags,error;
- fd_set wfds;
- struct timeval timeout;
- struct sockaddr_in servaddr;
- timeout.tv_sec = TIMEOUT;
- sockfd = socket(AF_INET,SOCK_STREAM,0);
- if (sockfd < 0)
- {
- FINFO("Can 't creat a socket \n");
- return CONNECT_FAIL;
- }
- /* SET METHOD OF NON-BLOCKED SOCKET*/
- if ((flags=fcntl(sockfd, F_GETFL, 0)) < 0)
- {
- FINFO("nfcntl error \n");
- return CONNECT_FAIL;
- }
- if (fcntl(sockfd, F_SETFL, flags | O_NONBLOCK) < 0)
- {
- FINFO("nfcntl error \n");
- return CONNECT_FAIL;
- }
- bzero(&servaddr,sizeof(servaddr));
- servaddr.sin_family=AF_INET;
- servaddr.sin_port=htons(port);
- inet_pton(AF_INET, ip, &servaddr.sin_addr);
- if ((ifok = connect(sockfd, (struct sockaddr*)&servaddr, sizeof(servaddr))) < 0)
- if (EINPROGRESS != errno)
- {
- return CONNECT_FAIL;
- }
- FD_ZERO(&wfds);
- FD_SET(sockfd, &wfds);
- retval = select(sockfd+1, NULL, &wfds, NULL, &timeout);
- if (0 == retval || -1 == retval) /* ....select.. */
- {
- close(sockfd);
- return CONNECT_FAIL;
- }
- if (FD_ISSET(sockfd, &wfds)) /* sockfd.. */
- {
- len = sizeof(error);
- if (getsockopt(sockfd, SOL_SOCKET, SO_ERROR, &error, &len) < 0)
- {
- return CONNECT_FAIL;
- }
- }
- else
- {
- FINFO("\nFD_ISSET:");
- return CONNECT_FAIL;
- }
- if (error) /* ........ */
- {
- close(sockfd);
- return CONNECT_FAIL;
- }
- else /* ......0 */
- {
- printf("port %d is opened!\n", port);
- return CONNECT_OK;
- }
- }
复制代码 |
|