4-08 1,367PVs
别问我为什么要用原始套接字做服务器。说多了都是眼泪。8G内存的机器,最大CPU利用率可以到42左右。最高发包速度可以到200M+。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 |
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <stdarg.h> #include <sys/types.h> #include <sys/socket.h> #include <sys/errno.h> #include <sys/time.h> #include <sys/ioctl.h> #include <sys/stat.h> #include <net/if.h> #include <net/ethernet.h> #include <netinet/in.h> #include <netinet/ip.h> #include <netinet/if_ether.h> #include <netpacket/packet.h> #include <netdb.h> #include <arpa/inet.h> #include <unistd.h> #include <pthread.h> #include <time.h> #include <errno.h> #include <fcntl.h> #include <dirent.h> #include <resolv.h> #include <signal.h> #include <getopt.h> #define Rec_Port "eth0" #define Send_Port "eth2" #define PHYSICALPORTlength 30 char Physicalport[30]; #define Send_Bufsize 10240 #define Rec_Bufsize 10240 int thread_num; struct argc { int s_f; //socket uint32_t flow_packets; uint32_t flow_size; uint32_t flow_time; uint32_t pid; struct data_header *data; }; struct get_header { uint8_t version_type; uint8_t ttl; uint16_t total_len; uint16_t port_src; uint16_t port_dst; uint16_t minimal_PID_CP; uint8_t PIDs; uint8_t offsets_RES; uint32_t offset; uint32_t length; uint16_t content_len; uint16_t mtu; uint16_t publickey_len; uint32_t pid; }; struct data_header { uint8_t version_type; uint8_t ttl; uint16_t total_len; uint16_t port_src; uint16_t port_dst; uint16_t minimal_pid_p; uint8_t PIDs; uint8_t RES; uint32_t offset; uint32_t length; uint16_t next_header; uint16_t check_sum; uint32_t reserved; uint8_t nid_eid[16]; uint8_t l_eid[20]; uint8_t NID[16]; uint32_t pid; }; static void * send_data_packets(void *argv) { struct argc B; // &B = (struct argc *)argv; memcpy(&B,argv,sizeof(struct argc)); int sockfd = B.s_f; uint8_t sbuf[Send_Bufsize]; memset(sbuf,0,sizeof(sbuf)); int lastpsize = B.flow_size % B.flow_packets; int psize = B.flow_size / B.flow_packets; int duration = B.flow_time / B.flow_packets; int len = 14 + sizeof(struct data_header) + psize; int len2= 14 + sizeof(struct data_header) + lastpsize; int j=0; for(;len>1514 || len2>1514 || psize>1400 || lastpsize>1400;) { j++; psize = B.flow_size / (B.flow_packets+j); lastpsize = B.flow_size % (B.flow_packets + j); len = 14 + sizeof(struct data_header) + psize; len2 = 14 + sizeof(struct data_header) + lastpsize; } if(j!=0) B.flow_packets += j; uint8_t mac[14]={255,255,255,255,255,255,255,255,255,255,255,255,8,0}; uint8_t *sbuf2=sbuf+14+sizeof(struct data_header); memcpy(sbuf,mac,14); memcpy(sbuf+14,B.data,sizeof(struct data_header)); memset(sbuf2,1,psize); int i,pklen; struct sockaddr to; memset(&to,0,sizeof(to)); memcpy(Physicalport,Send_Port,PHYSICALPORTlength); strcpy(to.sa_data,Physicalport); if(duration == 0) { if(B.flow_packets>1) { duration = 1; B.flow_time=1; } } if(B.flow_time==0 && B.flow_packets>10) {printf("B2 %d %d %d\n",B.flow_packets,B.flow_size,B.flow_time);exit(1);} if(lastpsize == 0) { for(i=0;i<B.flow_packets;i++) { pklen = sendto(sockfd,sbuf,len,0,&to,sizeof(to)); if(pklen==-1) { printf("Sendto Error!\n"); printf("B %d %d %d\n",B.flow_packets,B.flow_size,B.flow_time); printf("psize %d lastpsize %d duration %d\n",psize,lastpsize,duration); close(sockfd); exit(1); } if((i!=B.flow_packets-1) && (duration!=0)) usleep(duration); } } else { for(i=0;i<B.flow_packets-1;i++) { pklen = sendto(sockfd,sbuf,len,0,&to,sizeof(to)); if(pklen==-1) { printf("Sendto Error2 pklen=%d len=%d\n",pklen,len); printf("B %d %d %d\n",B.flow_packets,B.flow_size,B.flow_time); printf("psize %d lastpsize %d duration %d\n",psize,lastpsize,duration); close(sockfd); exit(1); } if((i!=B.flow_packets-1)&&(duration!=0)) usleep(duration); } if(pklen=sendto(sockfd,sbuf,len2,0,&to,sizeof(to))==-1) { printf("Sendto error3\n"); printf("B %d %d %d\n",B.flow_packets,B.flow_size,B.flow_time); printf("psize %d lastpsize %d duration %d\n",psize,lastpsize,duration); close(sockfd); exit(1); } } pthread_detach(pthread_self()); thread_num-=1; } void main() { thread_num = 0; struct argc A; int s_s; char rbuf[Rec_Bufsize]; memset(rbuf,0,Rec_Bufsize); pthread_t thread_do; struct data_header dh; dh.version_type=0xa1; dh.ttl=255; dh.total_len=0; dh.port_src=0; dh.port_dst=0; dh.minimal_pid_p=0; dh.PIDs=1; dh.RES=0; dh.offset=0; dh.length=0; dh.next_header=0; dh.check_sum = 0; dh.reserved =0; char nid[16]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}; memcpy(dh.nid_eid,nid,16); char sid[20]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,100}; memcpy(dh.l_eid,sid,20); memcpy(dh.NID,nid,16); dh.pid=1; struct sockaddr from; memset(&from,0,sizeof(from)); memcpy(Physicalport,Rec_Port,PHYSICALPORTlength); strcpy(from.sa_data,Physicalport); if((s_s=socket(PF_PACKET,SOCK_PACKET,htons(ETH_P_ALL)))==-1) { printf("socket error!\n"); exit(0); } while(1){ int lenr = recvfrom(s_s,rbuf,sizeof(rbuf),0,NULL,NULL); if(lenr<0) { printf("recv error!\n"); close(s_s); exit(1); } while(1){ int lenr = recvfrom(s_s,rbuf,sizeof(rbuf),0,NULL,NULL); if(lenr<0) { printf("recv error!\n"); close(s_s); exit(1); } struct get_header *get_h = (struct get_header *)(rbuf+14+20); if(get_h->version_type==0xa0) { A.s_f=s_s; A.flow_packets=get_h->cc_flow_packets; A.flow_size=get_h->cc_flow_size; A.flow_time=get_h->cc_flow_time; A.pid = get_h->pid; // printf("%d %d %d\n",A.flow_packets,A.flow_size,A.flow_time); if((get_h->cc_flow_time==0) && (get_h->cc_flow_packets>10)) {printf("data error\n");exit(1);} A.data = &dh; int err = pthread_create(&thread_do,NULL,send_data_packets,(void *)&A); if(err !=0) { printf("create thread udp failed err=%d/n",err); exit(1); } thread_num+=1; } else continue; printf("thread_number %d\n",thread_num); } } |