原始套接字使用epoll而不接收ack数据包

时间:2019-01-25 05:28:30

标签: c sockets tcp epoll syn

我需要使用 tcp半开扫描来检查大量服务器的端口状态。

我用const storageRoot = 'https://storage.googleapis.com/'; const bucketName = 'myapp.appspot.com/'; // CHANGE TO YOUR BUCKET NAME const downloadUrl = storageRoot + bucketName + encodeURIComponent(dest); sendto()一个数据包来终止发送,并且应该syn=1一个数据包,如果recvfrom()syn=1的端口是开放的,否则ack=1,端口关闭。

我使用 epoll ,套接字发送数据包后,套接字的状态不会变为rst=1

ack数据包不是EPOLLIN吗?

我设置了EPOLLIN,所以我可以用socket IP_HDRINCL构建一个伪头 而且我尝试了nonblock socket和block socket,两者都不起作用。

这是我的代码的一部分:

syn=1

1。int main() { char localIp[20] = {0}; GetSelfServerIP(localIp);//to get local IP int epollfd = epoll_create(1); if (epollfd == -1) { error(1, errno, "Error epoll creating"); return 0; } SOCKET sock = socket (AF_INET, SOCK_RAW, IPPROTO_TCP); if(!IS_VALID_SOCK(sock)) error(1, 0, "Error socket initialization"); if(SetNonBlock(sock) < 0) error(1, errno, "Error switching socket to non-block mode."); if(SetReusable(sock) < 0) error(1, errno, "Error making socket reusable"); if(SetHdrincl(sock) < 0) error(1, errno, "Error making socket Hdrincl"); struct epoll_event ev; //only one ev for test ev.events = EPOLLIN | EPOLLPRI; ev.data.fd = sock; if (epoll_ctl(epollfd, EPOLL_CTL_ADD, ev.data.fd, &ev) == -1) error(1, errno, "Error adding event m to epoll"); char * targetip = "xxx.xxx.xxx.xxx";//test target ip struct epoll_event events[1]; memset(events, 0, sizeof(struct epoll_event)); while(1) { Sendpacket(ev.data.fd,localIp,get_random_sport(),targetIp);//to send syn=1 packet to targetIp port 1 to 1000; size_t nfds = epoll_wait(epollfd, events, 2, 1000); size_t i = 0; if (nfds == -1) error(1, errno, "Error calling epoll"); for (i = 0; i < nfds; ++i) { if ((events[i].events & EPOLLIN) == EPOLLIN || (events[i].events & EPOLLPRI) == EPOLLPRI) { int sock_raw; int saddr_size, data_size; struct sockaddr saddr; unsigned char *buffer = (unsigned char *)malloc(65536); //Its Big! saddr_size = sizeof saddr; data_size = recvfrom(sock_raw, buffer, 65536, 0, &saddr, &saddr_size); if(data_size <0 ) { printf("Recvfrom error , failed to get packets\n"); fflush(stdout); return 1; } //Now process the packet } } fflush(stdout); } return 1; } 始终== events[i].event

2。EPOLLOUT data_size始终<0。

0 个答案:

没有答案