//主动连接发起连接操作 void on_connecting(Socket *socket) { if(!check_in(socket)) { _sockets.insert(socket); _online_queue.push(socket); } }
int do_select(fd_set in, fd_set out, fd_set ex, fd_set *inp, fd_set *outp, fd_set *exp) { int count; select_table wait_table; int i; fd_set mask; mask = in | out | ex; for (i = 0 ; i < NR_OPEN ; i++,mask >>= 1) { if (!(mask & 1)) continue; if (!current->filp[i]) return -EBADF; if (!current->filp[i]->f_inode) return -EBADF; if (current->filp[i]->f_inode->i_pipe) continue; if (S_ISCHR(current->filp[i]->f_inode->i_mode)) continue; if (S_ISFIFO(current->filp[i]->f_inode->i_mode)) continue; return -EBADF; } repeat: wait_table.nr = 0; *inp = *outp = *exp = 0; count = 0; mask = 1; for (i = 0 ; i < NR_OPEN ; i++, mask += mask) { if (mask & in) if (check_in(&wait_table,current->filp[i]->f_inode)) { *inp |= mask; count++; } if (mask & out) if (check_out(&wait_table,current->filp[i]->f_inode)) { *outp |= mask; count++; } if (mask & ex) if (check_ex(&wait_table,current->filp[i]->f_inode)) { *exp |= mask; count++; } } if (!(current->signal & ~current->blocked) && (wait_table.nr || current->timeout) && !count) { current->state = TASK_INTERRUPTIBLE; schedule(); free_wait(&wait_table); goto repeat; } free_wait(&wait_table); return count; }
void get_in_b(int n) { for (int i = 1; i < n; i++) { //이전에 들어간 적 없는 분수만 들어가야 하므로 //분자, 분모의 최대공약수가 1이어야 함 if (check_in(i, n)) { b[cnt].ja = i; b[cnt].mo = n; b[cnt].v = (double)i / n; cnt++; } } }
//有发送或是接收网络的成功事件,证明连接是正常的。 void on_active(Socket *socket) { if(check_in(socket)) { //将socket放到_online_queue队列的队尾 socket->_last_active_time = time(0); _online_queue.erase(socket); _online_queue.push(socket); } dlog2("WARN: EventBaseSockMgr::on_active"); }
//socket断开 void on_closed(Socket *socket) { if (check_in(socket)) { _sockets.erase(socket); _online_queue.erase(socket); _waiting_queue.push(socket); } dlog2("WARN: EventBaseSockMgr::on_closed"); }
//tcpserver接收到新的连接 void on_new_connection(Socket *socket) { socket->_last_active_time = time(0); if(!check_in(socket)) { _sockets.insert(socket); _online_queue.push(socket); } dlog2("WARN: EventBaseSockMgr::on_new_connection"); }
void create_tcp_packet(struct tcphdr2 *tcp,struct ip* ip, int size_payload,const u_char* packet){ char errbuf[LIBNET_ERRBUF_SIZE]; libnet_ptag_t tag=0,tag_tcp=0,ipv=0; /* libnet protocol block */ libnet_t * libnet_context; int set=0; int port_out,port_in; char* payload ; if(!strcmp("192.168.1.3",inet_ntoa(ip->ip_src)))set=1; else set=2; if(set==1){ libnet_context = libnet_init(LIBNET_RAW4,"eth0", errbuf); port_out=check_out(ntohs(tcp->source)); } else{ libnet_context = libnet_init(LIBNET_RAW4,"eth1", errbuf); //if(ntohs(tcp->dest)!=9999){exit(0);} port_in=check_in(ntohs(tcp->dest)); if(!port_in)return;//exit(0);return;} } if (libnet_context == NULL) { fprintf(stderr, "libnet_init() failed: %s", errbuf); exit(1); } fprintf(stderr, "libnet_init() seccess\n"); payload=(char*) (packet+54); char* flags=(char*)(tcp+13); if((tcp->doff)*4 > 20) { fprintf(stderr, "(tcp->doff)*4 > 20\n"); if(libnet_build_tcp_options(/*(u_int8_t *)(tcp+20)*/(u_int8_t *)packet+54,(tcp->doff)*4 -20,libnet_context,0)==-1) { printf("tcp_option error\n\n"); } //payload=(char*)( packet+(tcp->doff)*4-20+54); printf("tcp_option\n"); } fprintf(stderr, "libnet_init() seccess2\n"); if (size_payload == 0) payload = NULL; if(set==1){ // port_test=ntohs(tcp->source); tag_tcp = libnet_build_tcp ( //ntohs(tcp->source), /* src port */ //9999, port_out, ntohs(tcp->dest), /* destination port */ ntohl(tcp->seq), /* sequence number */ ntohl(tcp->ack_seq), /* acknowledgement */ tcp->th_flags, //(*flags), /* control flags */ ntohs(tcp->window), /* window */ 0 , /* checksum - 0 = autofill */ ntohs(tcp->urg_ptr), /* urgent */ (tcp->doff)*4+size_payload, /* total tcp size */ payload, /* payload */ size_payload, /* payload length */ libnet_context, /* libnet context */ tag); /* protocol tag */ } else{ tag_tcp = libnet_build_tcp ( ntohs(tcp->source), /* src port */ //ntohs(tcp->dest), /* destination port */ port_in, //port_test, ntohl(tcp->seq), /* sequence number */ ntohl(tcp->ack_seq), /* acknowledgement */ tcp->th_flags, /* control flags */ ntohs(tcp->window), /* window */ 0 , /* checksum - 0 = autofill */ ntohs(tcp->urg_ptr), /* urgent */ (tcp->doff)*4+size_payload, /* total tcp size */ payload, /* payload */ size_payload, /* payload length */ libnet_context, /* libnet context */ tag); } if (tag_tcp == -1) { fprintf (stderr, "Unable to build TCP header: %s\n", libnet_geterror (libnet_context)); exit (1); } //libnet_do_checksum(&libnet_context,tcp,IPPROTO_TCP,(tcp->doff)); fprintf (stderr,"tcp_build OK\n"); struct in_addr* tmp_src = &ip->ip_src; struct in_addr* tmp_dst = &ip->ip_dst; if(set==1){ ipv = libnet_build_ipv4( ntohs(ip->ip_len), /* length */ (ip->ip_tos), /* TOS */ ntohs(ip->ip_id), /* IP ID */ ntohs(ip->ip_off), /* IP Frag */ (ip->ip_ttl), /* TTL */ (ip->ip_p), /* protocol */ //IPPROTO_TCP, 0, /* checksum */ libnet_name2addr4(libnet_context,"140.114.195.31",LIBNET_DONT_RESOLVE), *((u_int32_t*)(tmp_dst)), /* destination IP */ //libnet_name2addr4(libnet_context,"192.168.1.3",LIBNET_DONT_RESOLVE), // *((u_int32_t*)(tmp_src)), /*src IP*/ NULL, /* payload */ 0, /* payload size */ libnet_context, /* libnet context */ tag); /* ptag */ }else if(set==2){ ipv = libnet_build_ipv4( ntohs(ip->ip_len), /* length */ (ip->ip_tos), /* TOS */ ntohs(ip->ip_id), /* IP ID */ ntohs(ip->ip_off), /* IP Frag */ (ip->ip_ttl), /* TTL */ (ip->ip_p), /* protocol */ //IPPROTO_TCP, 0, /* checksum */ // libnet_name2addr4(libnet_context,"192.168.1.0",LIBNET_DONT_RESOLVE), *((u_int32_t*)(tmp_src)), /*src IP*/ libnet_name2addr4(libnet_context,"192.168.1.3",LIBNET_DONT_RESOLVE), //*((u_int32_t*)(tmp_dst)), /* destination IP */ NULL, /* payload */ 0, /* payload size */ libnet_context, /* libnet context */ tag); } else { printf("error iph\n");exit(0); } if (ipv == -1) { fprintf(stderr, "Can't build IP header: %s\n", libnet_geterror(libnet_context)); } if (set==1) { //flag为1,带表从内网到外网,目的MAC:gateway 源MAC:localMACw libnet_build_ethernet( gatewayMAC, /* ethernet destination */ LOCAL_MAC_OUTTER, /* ethernet source */ ETHERTYPE_IP, /* protocol type */ NULL, /* payload */ 0, /* payload size */ libnet_context, /* libnet handle */ 0); /* libnet id */ } else { //flag为0,代表从外网到内网,目的MAC:targetMAC 源MAC:localMACn libnet_build_ethernet( targetMAC, /* ethernet destination */ LOCAL_MAC_INNER, /* ethernet source */ ETHERTYPE_IP, /* protocol type */ NULL, /* payload */ 0, /* payload size */ libnet_context, /* libnet handle */ 0); /* libnet id */ } if (libnet_write(libnet_context) == -1) fprintf(stderr, "Write error: %s\n", libnet_geterror(libnet_context)); fprintf(stderr, "Send %u\n",(u_int16_t)tcp->doff); libnet_clear_packet(libnet_context); libnet_destroy(libnet_context); }