main.cpp 65 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883
  1. #include<stdio.h>
  2. #include<string.h>
  3. #include<sys/socket.h>
  4. #include<arpa/inet.h>
  5. #include<stdlib.h>
  6. #include<getopt.h>
  7. #include <unistd.h>
  8. #include<errno.h>
  9. #include <fcntl.h>
  10. //#include"aes.h"
  11. #include <sys/epoll.h>
  12. #include <sys/wait.h>
  13. #include<map>
  14. #include<string>
  15. #include<vector>
  16. #include <sys/socket.h> //for socket ofcourse
  17. #include <sys/types.h>
  18. #include <stdlib.h> //for exit(0);
  19. #include <errno.h> //For errno - the error number
  20. #include <netinet/tcp.h> //Provides declarations for tcp header
  21. #include <netinet/udp.h>
  22. #include <netinet/ip.h> //Provides declarations for ip header
  23. #include <netinet/if_ether.h>
  24. #include <arpa/inet.h>
  25. #include <fcntl.h>
  26. #include <byteswap.h>
  27. #include <sys/socket.h>
  28. #include <sys/types.h>
  29. #include <arpa/inet.h>
  30. #include <linux/if_ether.h>
  31. #include <linux/filter.h>
  32. #include <sys/time.h>
  33. #include <time.h>
  34. #include <sys/timerfd.h>
  35. #include <set>
  36. #include <encrypt.h>
  37. #include <inttypes.h>
  38. using namespace std;
  39. const int mode_tcp=0;
  40. const int mode_udp=1;
  41. const int mode_icmp=2;
  42. int raw_mode=mode_udp;
  43. char local_address[100], remote_address[100],source_address[100];
  44. int local_port = -1, remote_port = -1;
  45. int epollfd ;
  46. uint32_t const_id=0;
  47. uint32_t oppsite_const_id=0;
  48. uint32_t my_id=0;
  49. uint32_t oppsite_id=0;
  50. uint32_t conv_id=0;
  51. uint32_t link_level_header_len=0;
  52. const int handshake_timeout=2000;
  53. const int heartbeat_timeout=10000;
  54. const int udp_timeout=3000;
  55. const int heartbeat_interval=1000;
  56. const int timer_interval=500;
  57. const int RETRY_TIME=3;
  58. //const uint16_t tcp_window=50000;
  59. const int buf_len = 65535+100;
  60. const int server_mode=2;
  61. const int client_mode=1;
  62. int prog_mode=0; //0 unset; 1client 2server
  63. const int disable_encrypt=0;
  64. const int disable_anti_replay=0;
  65. const int disable_bpf_filter=1;
  66. const int debug_mode=0;
  67. int bind_fd;
  68. int first_data_packet=0;
  69. const int seq_mode=2; //0 dont increase /1 increase //increase randomly,about every 5 packet
  70. const uint64_t epoll_timer_fd_sn=1;
  71. const uint64_t epoll_raw_recv_fd_sn=2;
  72. const uint64_t epoll_udp_fd_sn_begin=256;
  73. uint64_t epoll_udp_fd_sn=epoll_udp_fd_sn_begin; //udp_fd_sn =256,512,768......the lower 8 bit is not used,to avoid confliction
  74. const int server_nothing=0;
  75. const int server_syn_ack_sent=1;
  76. const int server_heartbeat_sent=2;
  77. const int server_ready=3;
  78. int server_current_state=server_nothing;
  79. long long last_hb_recv_time;
  80. long long last_udp_recv_time=0;
  81. int socket_buf_size=1024*1024*4;
  82. int udp_fd=-1;
  83. int raw_recv_fd;
  84. int raw_send_fd;
  85. int filter_port=-1;
  86. const int client_nothing=0;
  87. const int client_syn_sent=1;
  88. const int client_ack_sent=2;
  89. const int client_heartbeat_sent=3;
  90. const int client_ready=4;
  91. int client_current_state=client_nothing;
  92. int retry_counter;
  93. long long last_state_time=0;
  94. long long last_hb_sent_time=0;
  95. char buf[buf_len];
  96. char buf2[buf_len];
  97. char raw_send_buf[buf_len];
  98. char raw_send_buf2[buf_len];
  99. char raw_recv_buf[buf_len];
  100. char raw_recv_buf2[buf_len];
  101. char raw_recv_buf3[buf_len];
  102. char replay_buf[buf_len];
  103. char send_data_buf[buf_len]; //buf for send data and send hb
  104. struct sock_filter code_tcp[] = {
  105. { 0x28, 0, 0, 0x0000000c },//0
  106. { 0x15, 0, 10, 0x00000800 },//1
  107. { 0x30, 0, 0, 0x00000017 },//2
  108. { 0x15, 0, 8, 0x00000006 },//3
  109. { 0x28, 0, 0, 0x00000014 },//4
  110. { 0x45, 6, 0, 0x00001fff },//5
  111. { 0xb1, 0, 0, 0x0000000e },//6
  112. { 0x48, 0, 0, 0x0000000e },//7
  113. { 0x15, 2, 0, 0x0000ef32 },//8
  114. { 0x48, 0, 0, 0x00000010 },//9
  115. { 0x15, 0, 1, 0x0000ef32 },//10
  116. { 0x6, 0, 0, 0x0000ffff },//11
  117. { 0x6, 0, 0, 0x00000000 },//12
  118. };
  119. sock_fprog bpf;
  120. uint16_t ip_id=1;
  121. //const int MTU=1440;
  122. struct sockaddr_in udp_old_addr_in;
  123. uint64_t anti_replay_seq=0;
  124. uint8_t key[]={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16, 0,0,0,0};
  125. uint8_t key_me[16];
  126. uint8_t key_oppsite[16];
  127. const int window_size=2000;
  128. int random_number_fd=-1;
  129. const int conv_timeout=60000; //60 second
  130. const int conv_clear_ratio=10;
  131. const int hb_length=1+3*sizeof(uint32_t);
  132. int OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO;
  133. ////////==============================variable/function divider=============================================================
  134. void init_random_number_fd()
  135. {
  136. random_number_fd=open("/dev/urandom",O_RDONLY);
  137. if(random_number_fd==-1)
  138. {
  139. printf("error open /dev/urandom");
  140. exit(-1);
  141. }
  142. }
  143. uint32_t get_true_random_number_0()
  144. {
  145. uint32_t ret;
  146. read(random_number_fd,&ret,sizeof(ret));
  147. return ret;
  148. }
  149. uint32_t get_true_random_number_nz() //nz for non-zero
  150. {
  151. uint32_t ret=0;
  152. while(ret==0)
  153. {
  154. ret=get_true_random_number_0();
  155. }
  156. return ret;
  157. }
  158. struct anti_replay_t
  159. {
  160. uint64_t max_packet_received;
  161. char window[window_size];
  162. char disabled;
  163. anti_replay_t()
  164. {
  165. disabled=0;
  166. max_packet_received=0;
  167. //memset(window,0,sizeof(window)); //not necessary
  168. }
  169. void re_init()
  170. {
  171. disabled=0;
  172. max_packet_received=0;
  173. //memset(window,0,sizeof(window));
  174. }
  175. void disable()
  176. {
  177. disabled=1;
  178. }
  179. void enable()
  180. {
  181. disabled=0;
  182. }
  183. int is_vaild(uint64_t seq)
  184. {
  185. //if(disabled) return 0;
  186. if(seq==max_packet_received) return 0||disabled;
  187. else if(seq>max_packet_received)
  188. {
  189. if(seq-max_packet_received>=window_size)
  190. {
  191. memset(window,0,sizeof(window));
  192. window[seq%window_size]=1;
  193. }
  194. else
  195. {
  196. for (int i=max_packet_received+1;i<seq;i++)
  197. window[i%window_size]=0;
  198. window[seq%window_size]=1;
  199. }
  200. max_packet_received=seq;
  201. return 1;
  202. }
  203. else if(seq<max_packet_received)
  204. {
  205. if(max_packet_received-seq>=window_size) return 0||disabled;
  206. else
  207. {
  208. if (window[seq%window_size]==1) return 0||disabled;
  209. else
  210. {
  211. window[seq%window_size]=1;
  212. return 1;
  213. }
  214. }
  215. }
  216. }
  217. }anti_replay;
  218. int pre_send(char * data, int &data_len)
  219. {
  220. //return 0;
  221. if(data_len<0) return -3;
  222. if(disable_encrypt&&disable_anti_replay) return 0;
  223. if(!disable_anti_replay)
  224. {
  225. anti_replay_seq++;
  226. uint32_t seq_high= htonl(anti_replay_seq>>32u);
  227. uint32_t seq_low= htonl((anti_replay_seq<<32u)>>32u);
  228. memcpy(replay_buf,&seq_high,sizeof(uint32_t));
  229. memcpy(replay_buf+sizeof(uint32_t),&seq_low,sizeof(uint32_t));
  230. memcpy(replay_buf+sizeof(uint32_t)*2,data,data_len);
  231. data_len+=sizeof(uint32_t)*2;
  232. }
  233. else
  234. {
  235. memcpy(replay_buf,data,data_len);
  236. }
  237. if(!disable_encrypt)
  238. {
  239. if(my_encrypt((unsigned char*)replay_buf,(unsigned char*)data,data_len,key_me) <0)
  240. {
  241. printf("encrypt fail\n");
  242. return -1;
  243. }
  244. }
  245. else
  246. {
  247. memcpy(data,replay_buf,data_len);
  248. }
  249. return 0;
  250. }
  251. int pre_recv(char * data, int &data_len)
  252. {
  253. //return 0;
  254. if(data_len<0) return -1;
  255. if(disable_encrypt&&disable_anti_replay) return 0;
  256. if(!disable_encrypt)
  257. {
  258. if(my_decrypt((uint8_t*)data,(uint8_t*)replay_buf,data_len,key_oppsite) <0)
  259. {
  260. printf("decrypt fail\n");
  261. return -1;
  262. }
  263. else
  264. {
  265. printf("decrypt succ\n");
  266. }
  267. }
  268. else
  269. {
  270. memcpy(replay_buf,data,data_len);
  271. }
  272. if(!disable_anti_replay)
  273. {
  274. data_len-=sizeof(uint32_t)*2;
  275. if(data_len<0)
  276. {
  277. printf("data_len<=0\n");
  278. return -2;
  279. }
  280. uint64_t seq_high= ntohl(*((uint32_t*)(replay_buf) ) );
  281. uint32_t seq_low= ntohl(*((uint32_t*)(replay_buf+sizeof(uint32_t)) ) );
  282. uint64_t recv_seq =(seq_high<<32u )+seq_low;
  283. if((prog_mode==client_mode&&client_current_state==client_ready)
  284. ||(prog_mode==server_mode&&server_current_state==server_ready ))
  285. {
  286. if(data_len<sizeof(uint32_t)*2+1)
  287. {
  288. printf("no room for session id and oppiste session_id");
  289. return -4;
  290. }
  291. uint32_t tmp_oppiste_session_id = ntohl(
  292. *((uint32_t*) (replay_buf + sizeof(uint32_t) * 2+1)));
  293. uint32_t tmp_session_id = ntohl(
  294. *((uint32_t*) (replay_buf + sizeof(uint32_t) * 3+1)));
  295. if (tmp_oppiste_session_id != oppsite_id
  296. || tmp_session_id != my_id) {
  297. printf("auth fail and pre send\n");
  298. return -5;
  299. }
  300. printf("seq=========%u\n", recv_seq);
  301. if (anti_replay.is_vaild(recv_seq) != 1) {
  302. printf("dropped replay packet\n");
  303. return -1;
  304. }
  305. }
  306. printf("<<<<<%ld,%d,%ld>>>>\n",seq_high,seq_low,recv_seq);
  307. memcpy(data,replay_buf+sizeof(uint32_t)*2,data_len);
  308. }
  309. else
  310. {
  311. memcpy(data,replay_buf,data_len);
  312. }
  313. return 0;
  314. }
  315. void handler(int num) {
  316. int status;
  317. int pid;
  318. while ((pid = waitpid(-1, &status, WNOHANG)) > 0) {
  319. if (WIFEXITED(status)) {
  320. //printf("The child exit with code %d",WEXITSTATUS(status));
  321. }
  322. }
  323. }
  324. void setnonblocking(int sock) {
  325. int opts;
  326. opts = fcntl(sock, F_GETFL);
  327. if (opts < 0) {
  328. perror("fcntl(sock,GETFL)");
  329. exit(1);
  330. }
  331. opts = opts | O_NONBLOCK;
  332. if (fcntl(sock, F_SETFL, opts) < 0) {
  333. perror("fcntl(sock,SETFL,opts)");
  334. exit(1);
  335. }
  336. }
  337. int set_buf_size(int fd)
  338. {
  339. if(setsockopt(fd, SOL_SOCKET, SO_SNDBUFFORCE, &socket_buf_size, sizeof(socket_buf_size))<0)
  340. {
  341. printf("SO_SNDBUFFORCE fail\n");
  342. exit(1);
  343. }
  344. if(setsockopt(fd, SOL_SOCKET, SO_RCVBUFFORCE, &socket_buf_size, sizeof(socket_buf_size))<0)
  345. {
  346. printf("SO_RCVBUFFORCE fail\n");
  347. exit(1);
  348. }
  349. return 0;
  350. }
  351. int init_raw_socket()
  352. {
  353. raw_send_fd = socket(AF_INET , SOCK_RAW , IPPROTO_TCP);
  354. if(raw_send_fd == -1) {
  355. perror("Failed to create raw_send_fd");
  356. exit(1);
  357. }
  358. if(setsockopt(raw_send_fd, SOL_SOCKET, SO_SNDBUFFORCE, &socket_buf_size, sizeof(socket_buf_size))<0)
  359. {
  360. printf("SO_SNDBUFFORCE fail\n");
  361. exit(1);
  362. }
  363. //raw_fd = socket(AF_PACKET, SOCK_DGRAM, htons(ETH_P_ALL));
  364. raw_recv_fd= socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_IP));
  365. if(setsockopt(raw_recv_fd, SOL_SOCKET, SO_RCVBUFFORCE, &socket_buf_size, sizeof(socket_buf_size))<0)
  366. {
  367. printf("SO_RCVBUFFORCE fail\n");
  368. exit(1);
  369. }
  370. //raw_fd=socket(AF_PACKET , SOCK_RAW , htons(ETH_P_IP));
  371. // packet_recv_sd = socket(AF_INET , SOCK_RAW , IPPROTO_TCP);
  372. if(raw_recv_fd == -1) {
  373. //socket creation failed, may be because of non-root privileges
  374. perror("Failed to create raw_recv_fd");
  375. exit(1);
  376. }
  377. //IP_HDRINCL to tell the kernel that headers are included in the packet
  378. int one = 1;
  379. const int *val = &one;
  380. if (setsockopt (raw_send_fd, IPPROTO_IP, IP_HDRINCL, val, sizeof (one)) < 0) {
  381. perror("Error setting IP_HDRINCL");
  382. exit(2);
  383. }
  384. setnonblocking(raw_send_fd); //not really necessary
  385. setnonblocking(raw_recv_fd);
  386. return 0;
  387. }
  388. void init_filter(int port)
  389. {
  390. filter_port=port;
  391. if(disable_bpf_filter) return;
  392. code_tcp[8].k=code_tcp[10].k=port;
  393. bpf.len = sizeof(code_tcp)/sizeof(code_tcp[0]);
  394. bpf.filter = code_tcp;
  395. //printf("<%d>\n",bpf.len);
  396. int dummy;
  397. int ret=setsockopt(raw_recv_fd, SOL_SOCKET, SO_DETACH_FILTER, &dummy, sizeof(dummy));
  398. if (ret != 0)
  399. {
  400. printf("error remove fiter\n");
  401. perror("filter");
  402. //exit(-1);
  403. }
  404. ret = setsockopt(raw_recv_fd, SOL_SOCKET, SO_ATTACH_FILTER, &bpf, sizeof(bpf));
  405. //memset(code,0,sizeof(code));
  406. if (ret != 0)
  407. {
  408. printf("error set fiter\n");
  409. perror("filter");
  410. exit(-1);
  411. }
  412. }
  413. long long get_current_time()
  414. {
  415. timespec tmp_time;
  416. clock_gettime(CLOCK_MONOTONIC, &tmp_time);
  417. return tmp_time.tv_sec*1000+tmp_time.tv_nsec/(1000*1000l);
  418. }
  419. void server_clear(uint64_t u64)
  420. {
  421. int fd=int((u64<<32u)>>32u);
  422. epoll_event ev;
  423. ev.events = EPOLLIN;
  424. ev.data.u64 = u64;
  425. int ret = epoll_ctl(epollfd, EPOLL_CTL_DEL, fd, &ev);
  426. if (ret!=0)
  427. {
  428. printf("fd:%d epoll delete failed!!!!\n",fd);
  429. }
  430. ret= close(fd);
  431. if (ret!=0)
  432. {
  433. printf("close fd %d failed !!!!\n",fd);
  434. }
  435. }
  436. struct conv_manager_t
  437. {
  438. map<uint64_t,uint32_t> u64_to_conv; //conv and u64 are both supposed to be uniq
  439. map<uint32_t,uint64_t> conv_to_u64;
  440. map<uint32_t,uint64_t> conv_last_active_time;
  441. map<uint32_t,uint64_t>::iterator clear_it;
  442. void (*clear_function)(uint64_t u64) ;
  443. conv_manager_t()
  444. {
  445. clear_it=conv_last_active_time.begin();
  446. clear_function=0;
  447. }
  448. void set_clear_function(void (*a)(uint64_t u64))
  449. {
  450. clear_function=a;
  451. }
  452. void clear()
  453. {
  454. if(clear_function!=0)
  455. {
  456. map<uint32_t,uint64_t>::iterator it;
  457. for(it=conv_last_active_time.begin();it!=conv_last_active_time.end();it++)
  458. {
  459. clear_function(it->second);
  460. }
  461. }
  462. u64_to_conv.clear();
  463. conv_to_u64.clear();
  464. conv_last_active_time.clear();
  465. clear_it=conv_last_active_time.begin();
  466. }
  467. uint32_t get_new_conv()
  468. {
  469. uint32_t conv=get_true_random_number_nz();
  470. while(conv!=0&&conv_to_u64.find(conv)!=conv_to_u64.end())
  471. {
  472. conv=get_true_random_number_nz();
  473. }
  474. return conv;
  475. }
  476. int is_conv_used(uint32_t conv)
  477. {
  478. return conv_to_u64.find(conv)!=conv_to_u64.end();
  479. }
  480. int is_u64_used(uint64_t u64)
  481. {
  482. return u64_to_conv.find(u64)!=u64_to_conv.end();
  483. }
  484. uint32_t find_conv_by_u64(uint64_t u64)
  485. {
  486. return u64_to_conv[u64];
  487. }
  488. uint64_t find_u64_by_conv(uint32_t conv)
  489. {
  490. return conv_to_u64[conv];
  491. }
  492. int update_active_time(uint32_t conv)
  493. {
  494. return conv_last_active_time[conv]=get_current_time();
  495. }
  496. int insert_conv(uint32_t conv,uint64_t u64)
  497. {
  498. u64_to_conv[u64]=conv;
  499. conv_to_u64[conv]=u64;
  500. conv_last_active_time[conv]=get_current_time();
  501. return 0;
  502. }
  503. int erase_conv(uint32_t conv)
  504. {
  505. uint64_t u64=conv_to_u64[conv];
  506. if(clear_function!=0)
  507. {
  508. clear_function(u64);
  509. }
  510. conv_to_u64.erase(conv);
  511. u64_to_conv.erase(u64);
  512. conv_last_active_time.erase(conv);
  513. return 0;
  514. }
  515. int clean_inactive( )
  516. {
  517. map<uint32_t,uint64_t>::iterator old_it;
  518. map<uint32_t,uint64_t>::iterator it;
  519. int cnt=0;
  520. it=clear_it;
  521. int size=conv_last_active_time.size();
  522. int num_to_clean=size/conv_clear_ratio; //clear 1/10 each time,to avoid latency glitch
  523. uint64_t current_time=get_current_time();
  524. for(;;)
  525. {
  526. if(cnt>=num_to_clean) break;
  527. if(conv_last_active_time.begin()==conv_last_active_time.end()) break;
  528. if(it==conv_last_active_time.end())
  529. {
  530. it=conv_last_active_time.begin();
  531. }
  532. if( current_time -it->second >conv_timeout )
  533. {
  534. printf("inactive conv %u cleared !!!!!!!!!!!!!!!!!!!!!!!!!\n",it->first);
  535. old_it=it;
  536. it++;
  537. erase_conv(old_it->first);
  538. }
  539. else
  540. {
  541. it++;
  542. }
  543. cnt++;
  544. }
  545. return 0;
  546. }
  547. }conv_manager;
  548. void process_arg(int argc, char *argv[])
  549. {
  550. int i,j,k,opt;
  551. static struct option long_options[] =
  552. {
  553. /* These options set a flag. */
  554. {"source-ip", required_argument, 0, 1},
  555. };
  556. int option_index = 0;
  557. printf("argc=%d ", argc);
  558. for (i = 0; i < argc; i++)
  559. printf("%s ", argv[i]);
  560. printf("\n");
  561. if (argc == 1)
  562. {
  563. printf(
  564. "proc -l [adress:]port -r [adress:]port [-a passwd] [-b passwd]\n");
  565. exit(-1);
  566. }
  567. int no_l = 1, no_r = 1;
  568. while ((opt = getopt_long(argc, argv, "l:r:sch",long_options,&option_index)) != -1) {
  569. //string opt_key;
  570. //opt_key+=opt;
  571. switch (opt) {
  572. case 'l':
  573. no_l = 0;
  574. if (strchr(optarg, ':') != 0) {
  575. sscanf(optarg, "%[^:]:%d", local_address, &local_port);
  576. } else {
  577. strcpy(local_address, "127.0.0.1");
  578. sscanf(optarg, "%d", &local_port);
  579. }
  580. break;
  581. case 'r':
  582. no_r = 0;
  583. if (strchr(optarg, ':') != 0) {
  584. sscanf(optarg, "%[^:]:%d", remote_address, &remote_port);
  585. } else {
  586. strcpy(remote_address, "127.0.0.1");
  587. sscanf(optarg, "%d", &remote_port);
  588. }
  589. break;
  590. case 's':
  591. if(prog_mode==0)
  592. {
  593. prog_mode=server_mode;
  594. }
  595. else
  596. {
  597. printf("-s /-c has already been set,-s option conflict");
  598. exit(-1);
  599. }
  600. break;
  601. case 'c':
  602. if(prog_mode==0)
  603. {
  604. prog_mode=client_mode;
  605. }
  606. else
  607. {
  608. printf("-s /-c has already been set,-c option conflict");
  609. exit(-1);
  610. }
  611. break;
  612. case 'h':
  613. break;
  614. case 1:
  615. //if (strchr(optarg, ':') != 0) {
  616. sscanf(optarg, "%s", source_address);
  617. printf("source: %s",source_address);
  618. //} else {
  619. //printf("format --source-ip :adress");
  620. //exit(-1);
  621. //}
  622. break;
  623. default:
  624. printf("ignore unknown <%s>", optopt);
  625. }
  626. }
  627. if (no_l)
  628. printf("error: -i not found\n");
  629. if (no_r)
  630. printf("error: -o not found\n");
  631. if(prog_mode==0)
  632. printf("error: -s /-r hasnt been set\n");
  633. if (no_l || no_r||prog_mode==0)
  634. {
  635. exit(-1);
  636. }
  637. }
  638. struct packet_info_t
  639. {
  640. uint8_t protocol;
  641. //ip_part:
  642. uint32_t src_ip;
  643. uint16_t src_port;
  644. uint32_t dst_ip;
  645. uint16_t dst_port;
  646. //tcp_part:
  647. bool syn,ack,psh,rst;
  648. uint32_t seq,ack_seq;
  649. uint32_t ts,ts_ack;
  650. uint16_t icmp_seq;
  651. bool has_ts;
  652. }g_packet_info_send,g_packet_info_recv;
  653. struct pseudo_header {
  654. u_int32_t source_address;
  655. u_int32_t dest_address;
  656. u_int8_t placeholder;
  657. u_int8_t protocol;
  658. u_int16_t tcp_length;
  659. };
  660. /*
  661. Generic checksum calculation function
  662. */
  663. unsigned short csum(unsigned short *ptr,int nbytes) {
  664. register long sum;
  665. unsigned short oddbyte;
  666. register short answer;
  667. sum=0;
  668. while(nbytes>1) {
  669. sum+=*ptr++;
  670. nbytes-=2;
  671. }
  672. if(nbytes==1) {
  673. oddbyte=0;
  674. *((u_char*)&oddbyte)=*(u_char*)ptr;
  675. sum+=oddbyte;
  676. }
  677. sum = (sum>>16)+(sum & 0xffff);
  678. sum = sum + (sum>>16);
  679. answer=(short)~sum;
  680. return(answer);
  681. }
  682. char send_raw_ip_buf[buf_len];
  683. int send_raw_ip(packet_info_t &info,char * payload,int payloadlen)
  684. {
  685. struct iphdr *iph = (struct iphdr *) send_raw_ip_buf;
  686. memset(iph,0,sizeof(iphdr));
  687. struct sockaddr_in sin;
  688. sin.sin_family = AF_INET;
  689. //sin.sin_port = htons(info.dst_port); //dont need this
  690. sin.sin_addr.s_addr = info.dst_ip;
  691. iph->ihl = sizeof(iphdr)/4; //we dont use ip options,so the length is just sizeof(iphdr)
  692. iph->version = 4;
  693. iph->tos = 0;
  694. iph->id = htonl (ip_id++); //Id of this packet
  695. iph->frag_off = htons(0x4000); //DF set,others are zero
  696. iph->ttl = 64;
  697. iph->protocol = info.protocol;
  698. iph->check = 0; //Set to 0 before calculating checksum
  699. iph->saddr = info.src_ip; //Spoof the source ip address
  700. iph->daddr = info.dst_ip;
  701. iph->tot_len = sizeof (struct iphdr)+payloadlen;
  702. memcpy(send_raw_ip_buf+sizeof(iphdr) , payload, payloadlen);
  703. iph->check = csum ((unsigned short *) send_raw_ip_buf, iph->tot_len);
  704. int ret = sendto(raw_send_fd, send_raw_ip_buf, iph->tot_len , 0, (struct sockaddr *) &sin, sizeof (sin));
  705. if(ret==-1)
  706. {
  707. printf("sendto failed\n");
  708. return -1;
  709. }
  710. return 0;
  711. }
  712. char recv_raw_ip_buf[buf_len];
  713. int recv_raw_ip(packet_info_t &info,char * &payload,int &payloadlen)
  714. {
  715. iphdr * iph;
  716. struct sockaddr saddr;
  717. socklen_t saddr_size;
  718. saddr_size = sizeof(saddr);
  719. int recv_len = recvfrom(raw_recv_fd, recv_raw_ip_buf, buf_len, 0 ,&saddr , &saddr_size);
  720. if(recv_len<0)
  721. {
  722. printf("recv_len %d",recv_len);
  723. return -1;
  724. }
  725. if(recv_len<link_level_header_len)
  726. {
  727. printf("length error");
  728. }
  729. if(link_level_header_len ==14&&(recv_raw_ip_buf[12]!=8||recv_raw_ip_buf[13]!=0))
  730. {
  731. printf("not an ipv4 packet!\n");
  732. return -1;
  733. }
  734. char *ip_begin=recv_raw_ip_buf+link_level_header_len; //14 is eth net header
  735. iph = (struct iphdr *) (ip_begin);
  736. info.src_ip=iph->saddr;
  737. info.dst_ip=iph->daddr;
  738. info.protocol=iph->protocol;
  739. if (!(iph->ihl > 0 && iph->ihl <=60)) {
  740. if(debug_mode) printf("iph ihl error");
  741. return -1;
  742. }
  743. int ip_len=ntohs(iph->tot_len);
  744. if(recv_len-link_level_header_len <ip_len)
  745. {
  746. printf("incomplete packet\n");
  747. return -1;
  748. }
  749. unsigned short iphdrlen =iph->ihl*4;
  750. uint32_t ip_chk=csum ((unsigned short *) ip_begin, iphdrlen);
  751. if(ip_chk!=0)
  752. {
  753. printf("ip header error %d\n",ip_chk);
  754. return -1;
  755. }
  756. payload=ip_begin+iphdrlen;
  757. payloadlen=ip_len-iphdrlen;
  758. if(payloadlen<0)
  759. {
  760. printf("error payload len");
  761. return -1;
  762. }
  763. return 0;
  764. }
  765. struct icmphdr
  766. {
  767. uint8_t type;
  768. uint8_t code;
  769. uint16_t check_sum;
  770. uint16_t id;
  771. uint16_t seq;
  772. };
  773. char send_raw_icmp_buf[buf_len];
  774. int send_raw_icmp(packet_info_t &info, char * payload, int payloadlen)
  775. {
  776. icmphdr *icmph=(struct icmphdr *) (send_raw_icmp_buf);
  777. memset(icmph,0,sizeof(icmphdr));
  778. if(prog_mode==client_mode)
  779. {
  780. icmph->type=8;
  781. }
  782. else
  783. {
  784. icmph->type=0;
  785. }
  786. icmph->code=0;
  787. icmph->id=htons(g_packet_info_send.src_port);
  788. icmph->seq=htons(g_packet_info_send.icmp_seq++);
  789. memcpy(send_raw_icmp_buf+sizeof(icmphdr),payload,payloadlen);
  790. icmph->check_sum = csum( (unsigned short*) send_raw_icmp_buf, sizeof(icmphdr)+payloadlen);
  791. if(send_raw_ip(info,send_raw_icmp_buf,sizeof(icmphdr)+payloadlen)!=0)
  792. {
  793. return -1;
  794. }
  795. return 0;
  796. }
  797. char send_raw_udp_buf[buf_len];
  798. int send_raw_udp(packet_info_t &info, char * payload, int payloadlen)
  799. {
  800. udphdr *udph=(struct udphdr *) (send_raw_udp_buf
  801. + sizeof(struct pseudo_header));
  802. memset(udph,0,sizeof(udphdr));
  803. struct pseudo_header *psh = (struct pseudo_header *) (send_raw_udp_buf);
  804. udph->source = htons(info.src_port);
  805. udph->dest = htons(info.dst_port);
  806. int udp_tot_len=payloadlen+sizeof(udph);
  807. if(udp_tot_len>65535)
  808. {
  809. printf("invalid len\n");
  810. return -1;
  811. }
  812. udph->len=htons(uint16_t(udp_tot_len));
  813. memcpy(send_raw_udp_buf+sizeof(struct pseudo_header)+sizeof(udph),payload,payloadlen);
  814. psh->source_address = info.src_ip;
  815. psh->dest_address = info.dst_ip;
  816. psh->placeholder = 0;
  817. psh->protocol = IPPROTO_UDP;
  818. psh->tcp_length = htons(uint16_t(udp_tot_len));
  819. int csum_size = sizeof(struct pseudo_header) +udp_tot_len ;
  820. udph->check = csum( (unsigned short*) send_raw_udp_buf, csum_size);
  821. if(send_raw_ip(info,send_raw_udp_buf+ sizeof(struct pseudo_header),udp_tot_len)!=0)
  822. {
  823. return -1;
  824. }
  825. return 0;
  826. }
  827. char send_raw_tcp_buf[buf_len];
  828. int send_raw_tcp(packet_info_t &info, char * payload, int payloadlen) { //TODO seq increase
  829. struct tcphdr *tcph = (struct tcphdr *) (send_raw_tcp_buf
  830. + sizeof(struct pseudo_header));
  831. memset(tcph,0,sizeof(tcphdr));
  832. struct pseudo_header *psh = (struct pseudo_header *) (send_raw_tcp_buf);
  833. //TCP Header
  834. tcph->source = htons(info.src_port);
  835. tcph->dest = htons(info.dst_port);
  836. tcph->seq = htonl(info.seq);
  837. tcph->ack_seq = htonl(info.ack_seq);
  838. tcph->fin = 0;
  839. tcph->syn = info.syn;
  840. tcph->rst = 0;
  841. tcph->psh = info.psh;
  842. tcph->ack = info.ack;
  843. if (tcph->syn == 1) {
  844. tcph->doff = 10; //tcp header size
  845. int i = sizeof(pseudo_header)+sizeof(tcphdr);
  846. send_raw_tcp_buf[i++] = 0x02; //mss
  847. send_raw_tcp_buf[i++] = 0x04;
  848. send_raw_tcp_buf[i++] = 0x05;
  849. send_raw_tcp_buf[i++] = 0xb4;
  850. //raw_send_buf[i++]=0x01;
  851. //raw_send_buf[i++]=0x01;
  852. send_raw_tcp_buf[i++] = 0x04; //sack ok
  853. send_raw_tcp_buf[i++] = 0x02; //sack ok
  854. send_raw_tcp_buf[i++] = 0x08; //i=6;
  855. send_raw_tcp_buf[i++] = 0x0a;
  856. *(uint32_t*) (&send_raw_tcp_buf[i]) = htonl(
  857. (uint32_t) get_current_time());
  858. i += 4;
  859. *(uint32_t*) (&send_raw_tcp_buf[i]) = htonl(info.ts_ack);
  860. i += 4;
  861. send_raw_tcp_buf[i++] = 0x01;
  862. send_raw_tcp_buf[i++] = 0x03;
  863. send_raw_tcp_buf[i++] = 0x03;
  864. send_raw_tcp_buf[i++] = 0x05;
  865. } else {
  866. tcph->doff = 8;
  867. int i = sizeof(pseudo_header)+sizeof(tcphdr);
  868. send_raw_tcp_buf[i++] = 0x01;
  869. send_raw_tcp_buf[i++] = 0x01;
  870. send_raw_tcp_buf[i++] = 0x08; //i=0;
  871. send_raw_tcp_buf[i++] = 0x0a;
  872. *(uint32_t*) (&send_raw_tcp_buf[i]) = htonl(
  873. (uint32_t) get_current_time());
  874. i += 4;
  875. *(uint32_t*) (&send_raw_tcp_buf[i]) = htonl(info.ts_ack);
  876. i += 4;
  877. }
  878. tcph->urg = 0;
  879. //tcph->window = htons((uint16_t)(1024));
  880. tcph->window = htons((uint16_t) (10240 + random() % 100));
  881. tcph->check = 0; //leave checksum 0 now, filled later by pseudo header
  882. tcph->urg_ptr = 0;
  883. char *tcp_data = send_raw_tcp_buf+sizeof(struct pseudo_header) + tcph->doff * 4;
  884. memcpy(tcp_data, payload, payloadlen);
  885. psh->source_address = info.src_ip;
  886. psh->dest_address = info.dst_ip;
  887. psh->placeholder = 0;
  888. psh->protocol = IPPROTO_TCP;
  889. psh->tcp_length = htons(tcph->doff * 4 + payloadlen);
  890. int csum_size = sizeof(struct pseudo_header) + tcph->doff*4 + payloadlen;
  891. tcph->check = csum( (unsigned short*) send_raw_tcp_buf, csum_size);
  892. int tcp_totlen=tcph->doff*4 + payloadlen;
  893. if(send_raw_ip(info,send_raw_tcp_buf+ sizeof(struct pseudo_header),tcp_totlen)!=0)
  894. {
  895. return -1;
  896. }
  897. return 0;
  898. }
  899. int send_raw_tcp_deprecated(packet_info_t &info,char * payload,int payloadlen)
  900. {
  901. if((prog_mode==client_mode&& payloadlen!=9) ||(prog_mode==server_mode&& payloadlen!=5 ) )
  902. printf("send raw from to %d %d %d %d\n",info.src_ip,info.src_port,info.dst_ip,info.dst_port);
  903. char *data;
  904. memset(raw_send_buf,0,payloadlen+100);
  905. struct iphdr *iph = (struct iphdr *) raw_send_buf;
  906. //TCP header
  907. struct tcphdr *tcph = (struct tcphdr *) (raw_send_buf + sizeof (struct ip));
  908. struct sockaddr_in sin;
  909. struct pseudo_header psh;
  910. //some address resolution
  911. sin.sin_family = AF_INET;
  912. sin.sin_port = htons(info.dst_port);
  913. sin.sin_addr.s_addr = info.dst_ip;
  914. //Fill in the IP Header
  915. iph->ihl = 5;
  916. iph->version = 4;
  917. iph->tos = 0;
  918. iph->id = htonl (ip_id++); //Id of this packet
  919. iph->frag_off = htons(0x4000); //DF set,others are zero
  920. iph->ttl = 64;
  921. iph->protocol = IPPROTO_TCP;
  922. iph->check = 0; //Set to 0 before calculating checksum
  923. iph->saddr = info.src_ip; //Spoof the source ip address
  924. iph->daddr = info.dst_ip;
  925. //TCP Header
  926. tcph->source = htons(info.src_port);
  927. tcph->dest = htons(info.dst_port);
  928. tcph->seq =htonl(info.seq);
  929. tcph->ack_seq = htonl(info.ack_seq);
  930. tcph->fin=0;
  931. tcph->syn=info.syn;
  932. tcph->rst=0;
  933. tcph->psh=info.psh;
  934. tcph->ack=info.ack;
  935. if(tcph->syn==1)
  936. {
  937. tcph->doff = 10; //tcp header size
  938. int i=sizeof (struct iphdr)+20;
  939. raw_send_buf[i++]=0x02;//mss
  940. raw_send_buf[i++]=0x04;
  941. raw_send_buf[i++]=0x05;
  942. raw_send_buf[i++]=0xb4;
  943. //raw_send_buf[i++]=0x01;
  944. //raw_send_buf[i++]=0x01;
  945. raw_send_buf[i++]=0x04; //sack ok
  946. raw_send_buf[i++]=0x02; //sack ok
  947. raw_send_buf[i++]=0x08; //i=6;
  948. raw_send_buf[i++]=0x0a;
  949. *(uint32_t*)(& raw_send_buf[i])=htonl((uint32_t)get_current_time());
  950. i+=4;
  951. *(uint32_t*)(& raw_send_buf[i])=htonl(info.ts_ack);
  952. i+=4;
  953. raw_send_buf[i++]=0x01;
  954. raw_send_buf[i++]=0x03;
  955. raw_send_buf[i++]=0x03;
  956. raw_send_buf[i++]=0x05;
  957. }
  958. else
  959. {
  960. tcph->doff=8;
  961. int i=sizeof (struct iphdr)+20;
  962. raw_send_buf[i++]=0x01;
  963. raw_send_buf[i++]=0x01;
  964. raw_send_buf[i++]=0x08; //i=0;
  965. raw_send_buf[i++]=0x0a;
  966. *(uint32_t*)(& raw_send_buf[i])=htonl((uint32_t)get_current_time());
  967. i+=4;
  968. *(uint32_t*)(& raw_send_buf[i])=htonl(info.ts_ack);
  969. i+=4;
  970. }
  971. tcph->urg=0;
  972. //tcph->window = htons((uint16_t)(1024));
  973. tcph->window = htons((uint16_t)(10240+random()%100));
  974. tcph->check = 0; //leave checksum 0 now, filled later by pseudo header
  975. tcph->urg_ptr = 0;
  976. //Data part
  977. data = raw_send_buf + sizeof(struct iphdr) + tcph->doff*4;
  978. iph->tot_len = sizeof (struct iphdr) + tcph->doff*4 + payloadlen;
  979. memcpy(data , payload, payloadlen);
  980. psh.source_address = info.src_ip;
  981. psh.dest_address = sin.sin_addr.s_addr;
  982. psh.placeholder = 0;
  983. psh.protocol = IPPROTO_TCP;
  984. psh.tcp_length = htons(tcph->doff*4 + payloadlen );
  985. int psize = sizeof(struct pseudo_header) + tcph->doff*4 + payloadlen;
  986. memcpy(raw_send_buf2 , (char*) &psh , sizeof (struct pseudo_header));
  987. memcpy(raw_send_buf2 + sizeof(struct pseudo_header) , tcph , tcph->doff*4 + payloadlen);
  988. tcph->check = csum( (unsigned short*) raw_send_buf2, psize);
  989. //Ip checksum
  990. iph->check = csum ((unsigned short *) raw_send_buf, iph->tot_len);
  991. if(prog_mode==client_mode&& payloadlen!=9 ||prog_mode==server_mode&& payloadlen!=5)
  992. printf("sent seq ack_seq len<%u %u %d>\n",g_packet_info_send.seq,g_packet_info_send.ack_seq,payloadlen);
  993. int ret = sendto(raw_send_fd, raw_send_buf, iph->tot_len , 0, (struct sockaddr *) &sin, sizeof (sin));
  994. if(g_packet_info_send.syn==0&&g_packet_info_send.ack==1&&payloadlen!=0)
  995. {
  996. if(seq_mode==0)
  997. {
  998. }
  999. else if(seq_mode==1)
  1000. {
  1001. g_packet_info_send.seq+=payloadlen;
  1002. }
  1003. else if(seq_mode==2)
  1004. {
  1005. if(random()% 5==3 )
  1006. g_packet_info_send.seq+=payloadlen;
  1007. }
  1008. }
  1009. if(debug_mode) printf("<ret:%d>\n",ret);
  1010. if(ret<0)
  1011. {
  1012. perror("raw send error");
  1013. //printf("send error\n");
  1014. }
  1015. return 0;
  1016. }
  1017. char recv_raw_icmp_buf[buf_len];
  1018. int recv_raw_icmp(packet_info_t &info, char *&payload, int &payloadlen)
  1019. {
  1020. char * ip_payload;
  1021. int ip_payloadlen;
  1022. if(recv_raw_ip(info,ip_payload,ip_payloadlen)!=0)
  1023. {
  1024. printf("recv_raw_ip error");
  1025. return -1;
  1026. }
  1027. if(info.protocol!=IPPROTO_ICMP)
  1028. {
  1029. //printf("not udp protocol\n");
  1030. return -1;
  1031. }
  1032. icmphdr *icmph=(struct icmphdr *) (ip_payload);
  1033. info.src_port=info.dst_port=ntohs(icmph->id);
  1034. if(prog_mode==client_mode)
  1035. {
  1036. if(icmph->type!=0)
  1037. return -1;
  1038. }
  1039. else
  1040. {
  1041. if(icmph->type!=8)
  1042. return -1;
  1043. }
  1044. if(icmph->code!=0)
  1045. return -1;
  1046. unsigned short check = csum( (unsigned short*) ip_payload, ip_payloadlen);
  1047. if(check!=0)
  1048. {
  1049. printf("icmp checksum fail %x\n",check);
  1050. return -1;
  1051. }
  1052. payload=ip_payload+sizeof(icmphdr);
  1053. payloadlen=ip_payloadlen-sizeof(icmphdr);
  1054. printf("get a packet len=%d\n",payloadlen);
  1055. return 0;
  1056. }
  1057. char recv_raw_udp_buf[buf_len];
  1058. int recv_raw_udp(packet_info_t &info, char *&payload, int &payloadlen)
  1059. {
  1060. char * ip_payload;
  1061. int ip_payloadlen;
  1062. if(recv_raw_ip(info,ip_payload,ip_payloadlen)!=0)
  1063. {
  1064. printf("recv_raw_ip error");
  1065. return -1;
  1066. }
  1067. if(info.protocol!=IPPROTO_UDP)
  1068. {
  1069. //printf("not udp protocol\n");
  1070. return -1;
  1071. }
  1072. if(ip_payloadlen<sizeof(udphdr))
  1073. {
  1074. printf("too short to hold udpheader\n");
  1075. return -1;
  1076. }
  1077. udphdr *udph=(struct udphdr*)ip_payload;
  1078. if(ntohs(udph->len)!=ip_payloadlen)
  1079. {
  1080. printf("udp length error %d %d \n",ntohs(udph->len),ip_payloadlen);
  1081. return -1;
  1082. }
  1083. if(udph->dest!=ntohs(uint16_t(filter_port)))
  1084. {
  1085. //printf("%x %x",tcph->dest,);
  1086. return -1;
  1087. }
  1088. memcpy(recv_raw_udp_buf+ sizeof(struct pseudo_header) , ip_payload , ip_payloadlen);
  1089. struct pseudo_header *psh=(pseudo_header *)recv_raw_udp_buf ;
  1090. psh->source_address = info.src_ip;
  1091. psh->dest_address = info.dst_ip;
  1092. psh->placeholder = 0;
  1093. psh->protocol = IPPROTO_UDP;
  1094. psh->tcp_length = htons(ip_payloadlen);
  1095. int csum_len=sizeof(struct pseudo_header)+ip_payloadlen;
  1096. uint16_t udp_chk = csum( (unsigned short*) recv_raw_udp_buf, csum_len);
  1097. if(udp_chk!=0)
  1098. {
  1099. printf("udp_chk:%x\n",udp_chk);
  1100. printf("udp header error\n");
  1101. return -1;
  1102. }
  1103. char *udp_begin=recv_raw_udp_buf+sizeof(struct pseudo_header);
  1104. info.src_port=ntohs(udph->source);
  1105. info.dst_port=ntohs(udph->dest);
  1106. payloadlen = ip_payloadlen-sizeof(udphdr);
  1107. payload=udp_begin+sizeof(udphdr);
  1108. return 0;
  1109. }
  1110. char recv_raw_tcp_buf[buf_len];
  1111. int recv_raw_tcp(packet_info_t &info,char * &payload,int &payloadlen)
  1112. {
  1113. char * ip_payload;
  1114. int ip_payloadlen;
  1115. if(recv_raw_ip(info,ip_payload,ip_payloadlen)!=0)
  1116. {
  1117. printf("recv_raw_ip error");
  1118. return -1;
  1119. }
  1120. if(info.protocol!=IPPROTO_TCP)
  1121. {
  1122. //printf("not tcp protocol\n");
  1123. return -1;
  1124. }
  1125. tcphdr * tcph=(struct tcphdr*)ip_payload;
  1126. unsigned short tcphdrlen = tcph->doff*4;
  1127. if (!(tcph->doff > 0 && tcph->doff <=60)) {
  1128. if(debug_mode) printf("tcph error");
  1129. return 0;
  1130. }
  1131. if(tcph->dest!=ntohs(uint16_t(filter_port)))
  1132. {
  1133. //printf("%x %x",tcph->dest,);
  1134. return -1;
  1135. }
  1136. memcpy(recv_raw_tcp_buf+ sizeof(struct pseudo_header) , ip_payload , ip_payloadlen);
  1137. struct pseudo_header *psh=(pseudo_header *)recv_raw_tcp_buf ;
  1138. psh->source_address = info.src_ip;
  1139. psh->dest_address = info.dst_ip;
  1140. psh->placeholder = 0;
  1141. psh->protocol = IPPROTO_TCP;
  1142. psh->tcp_length = htons(ip_payloadlen);
  1143. int csum_len=sizeof(struct pseudo_header)+ip_payloadlen;
  1144. uint16_t tcp_chk = csum( (unsigned short*) recv_raw_tcp_buf, csum_len);
  1145. if(tcp_chk!=0)
  1146. {
  1147. printf("tcp_chk:%x\n",tcp_chk);
  1148. printf("tcp header error\n");
  1149. return -1;
  1150. }
  1151. char *tcp_begin=recv_raw_tcp_buf+sizeof(struct pseudo_header); //ip packet's data part
  1152. char *tcp_option=recv_raw_tcp_buf+sizeof(struct pseudo_header)+sizeof(tcphdr);
  1153. info.has_ts=0;
  1154. if(tcph->doff==10)
  1155. {
  1156. if(tcp_option[6]==0x08 &&tcp_option[7]==0x0a)
  1157. {
  1158. info.has_ts=1;
  1159. info.ts=ntohl(*(uint32_t*)(&tcp_option[8]));
  1160. info.ts_ack=ntohl(*(uint32_t*)(&tcp_option[12]));
  1161. //g_packet_info_send.ts_ack= ntohl(*(uint32_t*)(&tcp_option[8]));
  1162. }
  1163. }
  1164. else if(tcph->doff==8)
  1165. {
  1166. if(tcp_option[3]==0x08 &&tcp_option[4]==0x0a)
  1167. {
  1168. info.has_ts=1;
  1169. info.ts=ntohl(*(uint32_t*)(&tcp_option[0]));
  1170. info.ts_ack=ntohl(*(uint32_t*)(&tcp_option[4]));
  1171. //g_packet_info_send.ts_ack= ntohl(*(uint32_t*)(&tcp_option[0]));
  1172. }
  1173. }
  1174. if(tcph->rst==1)
  1175. {
  1176. printf("%%%%%%%%%%rst==1%%%%%%%%%%%%%\n");
  1177. }
  1178. info.ack=tcph->ack;
  1179. info.syn=tcph->syn;
  1180. info.rst=tcph->rst;
  1181. info.src_port=ntohs(tcph->source);
  1182. info.dst_port=ntohs(tcph->dest);
  1183. info.seq=ntohl(tcph->seq);
  1184. info.ack_seq=ntohl(tcph->ack_seq);
  1185. info.psh=tcph->psh;
  1186. if(info.has_ts)
  1187. {
  1188. g_packet_info_send.ts_ack=info.ts;
  1189. }
  1190. payloadlen = ip_payloadlen-tcphdrlen;
  1191. payload=tcp_begin+tcphdrlen;
  1192. return 0;
  1193. }
  1194. int recv_raw_tcp_deprecated(packet_info_t &info,char * &payload,int &payloadlen)
  1195. {
  1196. iphdr * iph;
  1197. tcphdr * tcph;
  1198. int size;
  1199. struct sockaddr saddr;
  1200. socklen_t saddr_size;
  1201. saddr_size = sizeof(saddr);
  1202. if(debug_mode)printf("raw!\n");
  1203. size = recvfrom(raw_recv_fd, buf, buf_len, 0 ,&saddr , &saddr_size);
  1204. if(buf[12]!=8||buf[13]!=0)
  1205. {
  1206. printf("not an ipv4 packet!\n");
  1207. fflush(stdout);
  1208. return -1;
  1209. }
  1210. char *ip_begin=buf+14;
  1211. iph = (struct iphdr *) (ip_begin);
  1212. if (!(iph->ihl > 0 && iph->ihl <=60)) {
  1213. if(debug_mode) printf("iph ihl error");
  1214. return -1;
  1215. }
  1216. if (iph->protocol != IPPROTO_TCP) {
  1217. if(debug_mode)printf("iph protocal != tcp\n");
  1218. return -1;
  1219. }
  1220. int ip_len=ntohs(iph->tot_len);
  1221. unsigned short iphdrlen =iph->ihl*4;
  1222. tcph=(struct tcphdr*)(ip_begin+ iphdrlen);
  1223. unsigned short tcphdrlen = tcph->doff*4;
  1224. if (!(tcph->doff > 0 && tcph->doff <=60)) {
  1225. if(debug_mode) printf("tcph error");
  1226. return 0;
  1227. }
  1228. if(tcph->dest!=ntohs(uint16_t(filter_port)))
  1229. {
  1230. //printf("%x %x",tcph->dest,);
  1231. return -1;
  1232. }
  1233. /////ip
  1234. uint32_t ip_chk=csum ((unsigned short *) ip_begin, iphdrlen);
  1235. int psize = sizeof(struct pseudo_header) + ip_len-iphdrlen;
  1236. /////ip end
  1237. ///tcp
  1238. struct pseudo_header psh;
  1239. psh.source_address = iph->saddr;
  1240. psh.dest_address = iph->daddr;
  1241. psh.placeholder = 0;
  1242. psh.protocol = IPPROTO_TCP;
  1243. psh.tcp_length = htons(ip_len-iphdrlen);
  1244. memcpy(raw_recv_buf2 , (char*) &psh , sizeof (struct pseudo_header));
  1245. memcpy(raw_recv_buf2 + sizeof(struct pseudo_header) , ip_begin+ iphdrlen , ip_len-iphdrlen);
  1246. uint16_t tcp_chk = csum( (unsigned short*) raw_recv_buf2, psize);
  1247. if(ip_chk!=0)
  1248. {
  1249. printf("ip header error %d\n",ip_chk);
  1250. return -1;
  1251. }
  1252. if(tcp_chk!=0)
  1253. {
  1254. printf("tcp_chk:%x\n",tcp_chk);
  1255. printf("tcp header error\n");
  1256. return -1;
  1257. }
  1258. char *tcp_begin=raw_recv_buf2+sizeof(struct pseudo_header); //ip packet's data part
  1259. char *tcp_option=raw_recv_buf2+sizeof(struct pseudo_header)+sizeof(tcphdr);
  1260. info.has_ts=0;
  1261. if(tcph->doff==10)
  1262. {
  1263. if(tcp_option[6]==0x08 &&tcp_option[7]==0x0a)
  1264. {
  1265. info.has_ts=1;
  1266. info.ts=ntohl(*(uint32_t*)(&tcp_option[8]));
  1267. info.ts_ack=ntohl(*(uint32_t*)(&tcp_option[12]));
  1268. //g_packet_info_send.ts_ack= ntohl(*(uint32_t*)(&tcp_option[8]));
  1269. }
  1270. }
  1271. else if(tcph->doff==8)
  1272. {
  1273. if(tcp_option[3]==0x08 &&tcp_option[4]==0x0a)
  1274. {
  1275. info.has_ts=1;
  1276. info.ts=ntohl(*(uint32_t*)(&tcp_option[0]));
  1277. info.ts_ack=ntohl(*(uint32_t*)(&tcp_option[4]));
  1278. //g_packet_info_send.ts_ack= ntohl(*(uint32_t*)(&tcp_option[0]));
  1279. }
  1280. }
  1281. if(tcph->rst==1)
  1282. {
  1283. printf("%%%%%%%%%%rst==1%%%%%%%%%%%%%\n");
  1284. }
  1285. info.ack=tcph->ack;
  1286. info.syn=tcph->syn;
  1287. info.rst=tcph->rst;
  1288. info.src_port=ntohs(tcph->source);
  1289. info.src_ip=iph->saddr;
  1290. info.seq=ntohl(tcph->seq);
  1291. info.ack_seq=ntohl(tcph->ack_seq);
  1292. info.psh=tcph->psh;
  1293. if(info.has_ts)
  1294. {
  1295. g_packet_info_send.ts_ack=info.ts;
  1296. }
  1297. ////tcp end
  1298. payloadlen = ip_len-tcphdrlen-iphdrlen;
  1299. payload=ip_begin+tcphdrlen+iphdrlen;
  1300. if(payloadlen>0&&payload[0]=='h')
  1301. {
  1302. printf("recvd <%u %u %d>\n",ntohl(tcph->seq ),ntohl(tcph->ack_seq), payloadlen);
  1303. }
  1304. if(payloadlen>0&&tcph->syn==0&&tcph->ack==1)
  1305. {
  1306. //if(seq_increse)
  1307. g_packet_info_send.ack_seq=ntohl(tcph->seq)+(uint32_t)payloadlen;
  1308. }
  1309. //printf("%d\n",ip_len);
  1310. /*
  1311. for(int i=0;i<size;i++)
  1312. {
  1313. printf("<%x>",(unsigned char)buf[i]);
  1314. }
  1315. printf("\n");
  1316. */
  1317. /*
  1318. for(int i=0;i<data_len;i++)
  1319. {
  1320. printf("<%x>",(unsigned char)data[i]);
  1321. }*/
  1322. if(debug_mode)
  1323. {
  1324. printf("\n");
  1325. printf("<%u,%u,%u,%u,%d>\n",(unsigned int)iphdrlen,(unsigned int)tcphdrlen,(unsigned int)tcph->syn,(unsigned int)tcph->ack,payloadlen);
  1326. //fflush(stdout);
  1327. }
  1328. return 0;
  1329. }
  1330. int send_raw(packet_info_t &info,char * payload,int payloadlen)
  1331. {
  1332. if(raw_mode==mode_tcp) return send_raw_tcp(info,payload,payloadlen);
  1333. else if(raw_mode==mode_udp) return send_raw_udp(info,payload,payloadlen);
  1334. else if(raw_mode==mode_icmp) return send_raw_icmp(info,payload,payloadlen);
  1335. }
  1336. int recv_raw(packet_info_t &info,char * &payload,int &payloadlen)
  1337. {
  1338. if(raw_mode==mode_tcp) return recv_raw_tcp(info,payload,payloadlen);
  1339. else if(raw_mode==mode_udp) return recv_raw_udp(info,payload,payloadlen);
  1340. else if(raw_mode==mode_icmp) return recv_raw_icmp(info,payload,payloadlen);
  1341. }
  1342. int send_bare_data(packet_info_t &info,char* data,int len)
  1343. {
  1344. int new_len=len;
  1345. memcpy(send_data_buf,data,len);
  1346. if(pre_send(send_data_buf,new_len)<0)
  1347. {
  1348. return -1;
  1349. }
  1350. send_raw(info,send_data_buf,new_len);
  1351. return 0;
  1352. }
  1353. int send_data(packet_info_t &info,char* data,int len,uint32_t id1,uint32_t id2,uint32_t conv_id)
  1354. {
  1355. int new_len=1+sizeof(my_id)*3+len;
  1356. send_data_buf[0]='d';
  1357. uint32_t tmp;
  1358. tmp=htonl(id1);
  1359. memcpy(send_data_buf+1,&tmp,sizeof(my_id));
  1360. tmp=htonl(id2);
  1361. memcpy(send_data_buf+1+sizeof(my_id),&tmp,sizeof(my_id));
  1362. tmp=htonl(conv_id);
  1363. memcpy(send_data_buf+1+sizeof(my_id)*2,&tmp,sizeof(my_id));
  1364. memcpy(send_data_buf+1+sizeof(my_id)*3,data,len);
  1365. if(pre_send(send_data_buf,new_len)<0)
  1366. {
  1367. return -1;
  1368. }
  1369. send_raw(info,send_data_buf,new_len);
  1370. return 0;
  1371. }
  1372. int send_hb(packet_info_t &info,uint32_t id1,uint32_t id2 ,uint32_t id3)
  1373. {
  1374. int new_len=1+sizeof(my_id)*3;
  1375. send_data_buf[0]='h';
  1376. uint32_t tmp;
  1377. tmp=htonl(id1);
  1378. memcpy(send_data_buf+1,&tmp,sizeof(my_id));
  1379. tmp=htonl(id2);
  1380. memcpy(send_data_buf+1+sizeof(my_id),&tmp,sizeof(my_id));
  1381. tmp=htonl(id3);
  1382. memcpy(send_data_buf+1+sizeof(my_id)*2,&tmp,sizeof(my_id));
  1383. if(pre_send(send_data_buf,new_len)<0)
  1384. {
  1385. return -1;
  1386. }
  1387. send_raw(info,send_data_buf,new_len);
  1388. return 0;
  1389. }
  1390. /*int send_sync()
  1391. {
  1392. //g_packet_info.seq=3;
  1393. g_packet_info.ack=0;
  1394. g_packet_info.syn=1;
  1395. //g_packet_info.ack_seq=5;
  1396. g_packet_info.psh=0;
  1397. send_raw(g_packet_info,0,0);
  1398. return 0;
  1399. }*/
  1400. int try_to_list_and_bind(int port)
  1401. {
  1402. int old_bind_fd=bind_fd;
  1403. if(raw_mode==mode_tcp)
  1404. {
  1405. bind_fd=socket(AF_INET,SOCK_STREAM,0);
  1406. }
  1407. else if(raw_mode==mode_udp||raw_mode==mode_icmp)
  1408. {
  1409. bind_fd=socket(AF_INET,SOCK_DGRAM,0);
  1410. }
  1411. if(old_bind_fd!=-1)
  1412. {
  1413. close(old_bind_fd);
  1414. }
  1415. struct sockaddr_in temp_bind_addr;
  1416. bzero(&temp_bind_addr, sizeof(temp_bind_addr));
  1417. temp_bind_addr.sin_family = AF_INET;
  1418. temp_bind_addr.sin_port = htons(port);
  1419. temp_bind_addr.sin_addr.s_addr = inet_addr(local_address);
  1420. if (bind(bind_fd, (struct sockaddr*)&temp_bind_addr, sizeof(temp_bind_addr)) !=0)
  1421. {
  1422. printf("bind fail\n");
  1423. return -1;
  1424. }
  1425. if(raw_mode==mode_tcp)
  1426. {
  1427. if (listen(bind_fd, SOMAXCONN) != 0) {
  1428. printf("listen fail\n");
  1429. return -1;
  1430. }
  1431. }
  1432. return 0;
  1433. }
  1434. int client_bind_to_a_new_port()
  1435. {
  1436. int raw_send_port=10000+get_true_random_number_nz()%(65535-10000);
  1437. for(int i=0;i<1000;i++)//try 1000 times at max,this should be enough
  1438. {
  1439. if (try_to_list_and_bind(raw_send_port)==0)
  1440. {
  1441. return raw_send_port;
  1442. }
  1443. }
  1444. printf("bind port fail\n");
  1445. fflush(stdout);
  1446. exit(-1);
  1447. return -1;////for compiler check
  1448. }
  1449. int keep_connection_client() //for client
  1450. {
  1451. conv_manager.clean_inactive();
  1452. if(debug_mode)printf("timer!\n");
  1453. //fflush(stdout);
  1454. begin:
  1455. if(client_current_state==client_nothing)
  1456. {
  1457. anti_replay.re_init(); // this is not safe
  1458. g_packet_info_send.src_port = client_bind_to_a_new_port();
  1459. if(raw_mode==mode_icmp)
  1460. {
  1461. g_packet_info_send.dst_port =g_packet_info_send.src_port ;
  1462. }
  1463. printf("using port %d\n", g_packet_info_send.src_port);
  1464. g_packet_info_send.src_ip = inet_addr(source_address);
  1465. init_filter(g_packet_info_send.src_port);
  1466. if(raw_mode==mode_tcp)
  1467. {
  1468. client_current_state = client_syn_sent;
  1469. last_state_time = get_current_time();
  1470. printf("state changed from nothing to syn_sent\n");
  1471. retry_counter = RETRY_TIME;
  1472. g_packet_info_send.seq = get_true_random_number_nz();
  1473. g_packet_info_send.ack_seq = 0; //get_true_random_number();
  1474. g_packet_info_send.ts_ack = 0;
  1475. g_packet_info_send.ack = 0;
  1476. g_packet_info_send.syn = 1;
  1477. g_packet_info_send.psh = 0;
  1478. send_raw(g_packet_info_send, 0, 0);
  1479. }
  1480. else if(raw_mode==mode_udp||raw_mode==mode_icmp)
  1481. {
  1482. client_current_state = client_ack_sent;
  1483. last_state_time = get_current_time();
  1484. printf("state changed from nothing to ack_sent\n");
  1485. retry_counter = RETRY_TIME;
  1486. g_packet_info_send.icmp_seq=0;
  1487. send_bare_data(g_packet_info_send, (char*)"hello", strlen("hello"));
  1488. }
  1489. }
  1490. if(client_current_state==client_syn_sent &&get_current_time()-last_state_time>handshake_timeout)
  1491. {
  1492. if(retry_counter==0)
  1493. {
  1494. client_current_state=client_nothing;
  1495. printf("state back to nothing\n");
  1496. return 0;
  1497. //goto begin;
  1498. }
  1499. else
  1500. {
  1501. retry_counter--;
  1502. printf("retry send sync\n");
  1503. send_raw(g_packet_info_send,0,0);
  1504. last_state_time=get_current_time();
  1505. }
  1506. }
  1507. if(client_current_state==client_ack_sent &&get_current_time()-last_state_time>handshake_timeout)
  1508. {
  1509. if(retry_counter==0)
  1510. {
  1511. client_current_state=client_nothing;
  1512. printf("state back to nothing\n");
  1513. return 0;
  1514. //goto begin;
  1515. }
  1516. else
  1517. {
  1518. retry_counter--;
  1519. if(raw_mode==mode_tcp)
  1520. {
  1521. send_raw(g_packet_info_send,0,0);
  1522. }
  1523. else if(raw_mode==mode_udp||raw_mode==mode_icmp)
  1524. {
  1525. send_bare_data(g_packet_info_send, (char*)"hello", strlen("hello"));
  1526. }
  1527. last_state_time=get_current_time();
  1528. printf("retry send ack counter left:%d\n",retry_counter);
  1529. }
  1530. }
  1531. if(client_current_state==client_heartbeat_sent&&get_current_time()-last_state_time>handshake_timeout)
  1532. {
  1533. if(retry_counter==0)
  1534. {
  1535. client_current_state=client_nothing;
  1536. printf("state back to nothing\n");
  1537. return 0;
  1538. //goto begin;
  1539. }
  1540. else
  1541. {
  1542. retry_counter--;
  1543. send_hb(g_packet_info_send,my_id,oppsite_id,const_id);
  1544. last_state_time=get_current_time();
  1545. printf("retry send heart_beat counter left:%d\n",retry_counter);
  1546. printf("heartbeat sent <%x,%x>\n",oppsite_id,my_id);
  1547. }
  1548. }
  1549. if(client_current_state==client_ready)
  1550. {
  1551. if(debug_mode)printf("time %lld %lld\n",get_current_time(),last_state_time);
  1552. if(get_current_time()-last_hb_recv_time>heartbeat_timeout)
  1553. {
  1554. client_current_state=client_nothing;
  1555. my_id=get_true_random_number_nz();
  1556. printf("state back to nothing\n");
  1557. return 0;
  1558. }
  1559. if(get_current_time()-last_hb_sent_time<heartbeat_interval)
  1560. {
  1561. return 0;
  1562. }
  1563. if(debug_mode)printf("heartbeat sent <%x,%x>\n",oppsite_id,my_id);
  1564. send_hb(g_packet_info_send,my_id,oppsite_id,const_id);
  1565. last_hb_sent_time=get_current_time();
  1566. }
  1567. }
  1568. int keep_connection_server()
  1569. {
  1570. conv_manager.clean_inactive();
  1571. //begin:
  1572. if(debug_mode) printf("timer!\n");
  1573. if(server_current_state==server_nothing)
  1574. {
  1575. return 0;
  1576. }
  1577. if(server_current_state==server_syn_ack_sent &&get_current_time()-last_state_time>handshake_timeout )
  1578. {
  1579. if(retry_counter==0)
  1580. {
  1581. server_current_state=server_nothing;
  1582. printf("state back to nothing\n");
  1583. }
  1584. else
  1585. {
  1586. retry_counter--;
  1587. send_raw(g_packet_info_send,0,0);
  1588. last_state_time=get_current_time();
  1589. printf("resend syn ack\n");
  1590. }
  1591. }
  1592. if(server_current_state==server_heartbeat_sent &&get_current_time()-last_state_time>handshake_timeout)
  1593. {
  1594. if(retry_counter==0)
  1595. {
  1596. server_current_state=server_nothing;
  1597. printf("state back to nothing\n");
  1598. }
  1599. else
  1600. {
  1601. retry_counter--;
  1602. send_hb(g_packet_info_send,my_id,random(),const_id);
  1603. last_state_time=get_current_time();
  1604. printf("half heart beat sent<%x>\n",my_id);
  1605. }
  1606. }
  1607. if(server_current_state==server_ready)
  1608. {
  1609. if( get_current_time()-last_hb_recv_time>heartbeat_timeout )
  1610. {
  1611. printf("%lld %lld",get_current_time(),last_state_time);
  1612. server_current_state=server_nothing;
  1613. printf("changed session id\n");
  1614. my_id=get_true_random_number_nz();
  1615. printf("state back to nothing\n");
  1616. printf("changed state to server_nothing111\n");
  1617. return 0;
  1618. }
  1619. if(get_current_time()-last_hb_sent_time<heartbeat_interval)
  1620. {
  1621. return 0;
  1622. }
  1623. send_hb(g_packet_info_send,my_id,oppsite_id,const_id);
  1624. last_hb_sent_time=get_current_time();
  1625. if(debug_mode) printf("heart beat sent<%x>\n",my_id);
  1626. }
  1627. }
  1628. int set_timer(int epollfd,int &timer_fd)
  1629. {
  1630. int ret;
  1631. epoll_event ev;
  1632. itimerspec its;
  1633. memset(&its,0,sizeof(its));
  1634. if((timer_fd=timerfd_create(CLOCK_MONOTONIC,TFD_NONBLOCK)) < 0)
  1635. {
  1636. printf("timer_fd create error");
  1637. exit(1);
  1638. }
  1639. its.it_interval.tv_nsec=timer_interval*1000ll*1000ll;
  1640. its.it_value.tv_nsec=1; //imidiately
  1641. timerfd_settime(timer_fd,0,&its,0);
  1642. ev.events = EPOLLIN;
  1643. ev.data.u64 = epoll_timer_fd_sn;
  1644. epoll_ctl(epollfd, EPOLL_CTL_ADD, timer_fd, &ev);
  1645. if (ret < 0) {
  1646. printf("epoll_ctl return %d\n", ret);
  1647. exit(-1);
  1648. }
  1649. }
  1650. int client_on_raw_recv(packet_info_t &info,char * data,int data_len)
  1651. {
  1652. if(client_current_state==client_syn_sent )
  1653. {
  1654. if (raw_mode==mode_tcp&&!(info.syn==1&&info.ack==1&&data_len==0)) return 0;
  1655. if(info.src_ip!=g_packet_info_send.dst_ip||info.src_port!=g_packet_info_send.dst_port)
  1656. {
  1657. printf("unexpected adress\n");
  1658. return 0;
  1659. }
  1660. g_packet_info_send.ack_seq=info.seq+1;
  1661. g_packet_info_send.psh=0;
  1662. g_packet_info_send.syn=0;
  1663. g_packet_info_send.ack=1;
  1664. g_packet_info_send.seq+=1;
  1665. printf("sent ack back\n");
  1666. send_raw(g_packet_info_send,0,0);
  1667. client_current_state=client_ack_sent;
  1668. last_state_time=get_current_time();
  1669. retry_counter=RETRY_TIME;
  1670. printf("changed state to client_ack_sent\n");
  1671. }
  1672. if(client_current_state==client_ack_sent )
  1673. {
  1674. if(raw_mode==mode_tcp&& (info.syn==1||info.ack!=1 ||data_len==0))
  1675. {
  1676. printf("unexpected syn ack or other zero lenght packet\n");
  1677. return 0;
  1678. }
  1679. if(info.src_ip!=g_packet_info_send.dst_ip||info.src_port!=g_packet_info_send.dst_port)
  1680. {
  1681. printf("unexpected adress\n");
  1682. return 0;
  1683. }
  1684. if(data_len<hb_length||data[0]!='h')
  1685. {
  1686. printf("not a heartbeat\n");
  1687. return 0;
  1688. }
  1689. oppsite_id= ntohl(* ((uint32_t *)&data[1]));
  1690. printf("====first hb received %x\n==",oppsite_id);
  1691. printf("changed state to client_heartbeat_sent\n");
  1692. send_hb(g_packet_info_send,my_id,oppsite_id,const_id);
  1693. client_current_state=client_heartbeat_sent;
  1694. last_state_time=get_current_time();
  1695. retry_counter=RETRY_TIME;
  1696. }
  1697. if(client_current_state==client_heartbeat_sent)
  1698. {
  1699. if((raw_mode==mode_tcp&&( info.syn==1||info.ack!=1 ) )||data_len==0 )
  1700. {
  1701. printf("unexpected syn ack or other zero lenght packet\n");
  1702. return 0;
  1703. }
  1704. if(info.src_ip!=g_packet_info_send.dst_ip||info.src_port!=g_packet_info_send.dst_port)
  1705. {
  1706. printf("unexpected adress\n");
  1707. return 0;
  1708. }
  1709. if(data_len<hb_length||data[0]!='h')
  1710. {
  1711. printf("not a heartbeat\n");
  1712. return 0;
  1713. }
  1714. uint32_t tmp_my_id= ntohl(* ((uint32_t *)&data[1+sizeof(my_id)]));
  1715. if(tmp_my_id!=my_id)
  1716. {
  1717. printf("auth fail\n");
  1718. return 0;
  1719. }
  1720. uint32_t tmp_oppsite_session_id=ntohl(* ((uint32_t *)&data[1]));
  1721. if(tmp_oppsite_session_id!=oppsite_id)
  1722. {
  1723. printf("oppsite id mismatch%x %x,ignore\n",tmp_oppsite_session_id,my_id);
  1724. return 0;
  1725. }
  1726. printf("changed state to client_ready\n");
  1727. client_current_state=client_ready;
  1728. last_state_time=get_current_time();
  1729. last_hb_recv_time=get_current_time();
  1730. }
  1731. if(client_current_state==client_ready )
  1732. {
  1733. if((raw_mode==mode_tcp&&( info.syn==1||info.ack!=1) )||data_len==0)
  1734. {
  1735. printf("unexpected syn ack");
  1736. return 0;
  1737. }
  1738. if(info.src_ip!=g_packet_info_send.dst_ip||info.src_port!=g_packet_info_send.dst_port)
  1739. {
  1740. printf("unexpected adress\n");
  1741. return 0;
  1742. }
  1743. if(data_len>=hb_length&&data[0]=='h')
  1744. {
  1745. if(debug_mode)printf("heart beat received\n");
  1746. last_hb_recv_time=get_current_time();
  1747. return 0;
  1748. }
  1749. else if(data_len>=sizeof(my_id)*3+1&&data[0]=='d')
  1750. {
  1751. printf("received a data from fake tcp,len:%d\n",data_len);
  1752. uint32_t tmp_session_id= ntohl(* ((uint32_t *)&data[1+sizeof(my_id)]));
  1753. if(tmp_session_id!=my_id)
  1754. {
  1755. printf("client session id mismatch%x %x,ignore\n",tmp_session_id,my_id);
  1756. return 0;
  1757. }
  1758. uint32_t tmp_oppsite_session_id=ntohl(* ((uint32_t *)&data[1]));
  1759. if(tmp_oppsite_session_id!=oppsite_id)
  1760. {
  1761. printf("server session id mismatch%x %x,ignore\n",tmp_oppsite_session_id,my_id);
  1762. return 0;
  1763. }
  1764. last_hb_recv_time=get_current_time();
  1765. uint32_t tmp_conv_id= ntohl(* ((uint32_t *)&data[1+sizeof(my_id)*2]));
  1766. /*
  1767. if(tmp_conv_id!=conv_id)
  1768. {
  1769. printf("conv id mismatch%x %x,ignore\n",tmp_oppsite_session_id,my_id);
  1770. return 0;
  1771. }*/
  1772. if(!conv_manager.is_conv_used(tmp_conv_id))
  1773. {
  1774. printf("unknow conv %d,ignore\n",tmp_conv_id);
  1775. return 0;
  1776. }
  1777. conv_manager.update_active_time(tmp_conv_id);
  1778. uint64_t u64=conv_manager.find_u64_by_conv(tmp_conv_id);
  1779. sockaddr_in tmp_sockaddr;
  1780. memset(&tmp_sockaddr,0,sizeof(tmp_sockaddr));
  1781. tmp_sockaddr.sin_family = AF_INET;
  1782. tmp_sockaddr.sin_addr.s_addr=(u64>>32u);
  1783. tmp_sockaddr.sin_port= htons(uint16_t((u64<<32u)>>32u));
  1784. int ret=sendto(udp_fd,data+1+sizeof(my_id)*3,data_len -(1+sizeof(my_id)*3),0,(struct sockaddr *)&tmp_sockaddr,sizeof(tmp_sockaddr));
  1785. if(ret<0)perror("ret<0");
  1786. printf("%s :%d\n",inet_ntoa(tmp_sockaddr.sin_addr),ntohs(tmp_sockaddr.sin_port));
  1787. printf("%d byte sent!!!!!!!!!!!!!!!!!!\n",ret);
  1788. }
  1789. return 0;
  1790. }
  1791. }
  1792. int server_on_raw_recv(packet_info_t &info,char * data,int data_len)
  1793. {
  1794. if(server_current_state==server_nothing)
  1795. {
  1796. anti_replay.re_init();
  1797. if(raw_mode==mode_icmp)
  1798. {
  1799. g_packet_info_send.src_port = info.src_port;;
  1800. }
  1801. g_packet_info_send.dst_port = info.src_port;
  1802. g_packet_info_send.dst_ip = info.src_ip;
  1803. if(raw_mode==mode_tcp)
  1804. {
  1805. if (!(info.syn == 1 && info.ack == 0 && data_len == 0))
  1806. return 0;
  1807. g_packet_info_send.ack_seq = info.seq + 1;
  1808. g_packet_info_send.psh = 0;
  1809. g_packet_info_send.syn = 1;
  1810. g_packet_info_send.ack = 1;
  1811. g_packet_info_send.seq = get_true_random_number_nz(); //not necessary to set
  1812. printf("sent syn ack\n");
  1813. send_raw(g_packet_info_send, 0, 0);
  1814. printf("changed state to server_syn_ack_sent\n");
  1815. server_current_state = server_syn_ack_sent;
  1816. retry_counter = RETRY_TIME;
  1817. last_state_time = get_current_time();
  1818. }
  1819. else if(raw_mode==mode_udp||raw_mode==mode_icmp)
  1820. {
  1821. if(data_len==strlen("hello")&& memcmp((char *)"hello",data,strlen("hello"))!=0)
  1822. {
  1823. //data[6]=0;
  1824. printf("not a hello packet %d\n",data,data_len);
  1825. return 0;
  1826. }
  1827. else
  1828. {
  1829. printf("got a hello packet\n");
  1830. }
  1831. printf("sent half heart_beat\n");
  1832. //send_raw(g_packet_info_send, 0, 0);
  1833. send_hb(g_packet_info_send,my_id,random(),const_id);
  1834. printf("changed state to server_heartbeat_sent_sent\n");
  1835. server_current_state = server_heartbeat_sent;
  1836. retry_counter = RETRY_TIME;
  1837. last_state_time = get_current_time();
  1838. }
  1839. }
  1840. else if(server_current_state==server_syn_ack_sent)
  1841. {
  1842. if(raw_mode==mode_tcp&&!( info.syn==0&&info.ack==1 &&data_len==0)) return 0;
  1843. if(info.src_ip!=g_packet_info_send.dst_ip||info.src_port!=g_packet_info_send.dst_port)
  1844. {
  1845. printf("unexpected adress\n");
  1846. return 0;
  1847. }
  1848. g_packet_info_send.syn=0;
  1849. g_packet_info_send.ack=1;
  1850. g_packet_info_send.seq+=1;////////is this right?
  1851. send_hb(g_packet_info_send,my_id,0,const_id); // send a hb immidately
  1852. printf("changed state to server_heartbeat_sent\n");
  1853. server_current_state=server_heartbeat_sent;
  1854. last_state_time=get_current_time();
  1855. retry_counter=RETRY_TIME;
  1856. }
  1857. else if(server_current_state==server_heartbeat_sent)//heart beat received
  1858. {
  1859. if(( raw_mode==mode_tcp&& (info.syn==1||info.ack!=1)) ||data_len==0) return 0;
  1860. if(info.src_ip!=g_packet_info_send.dst_ip||info.src_port!=g_packet_info_send.dst_port)
  1861. {
  1862. printf("unexpected adress\n");
  1863. return 0;
  1864. }
  1865. if(data_len<hb_length||data[0]!='h')
  1866. {
  1867. return 0;
  1868. }
  1869. uint32_t tmp_session_id= ntohl(* ((uint32_t *)&data[1+sizeof(my_id)]));
  1870. uint32_t tmp_oppsite_const_id=ntohl(* ((uint32_t *)&data[1+sizeof(my_id)*2]));
  1871. if(oppsite_const_id!=0&&tmp_oppsite_const_id!=oppsite_const_id)
  1872. {
  1873. conv_manager.clear();
  1874. }
  1875. oppsite_const_id=tmp_oppsite_const_id;
  1876. printf("received hb %x %x\n",oppsite_id,tmp_session_id);
  1877. if(tmp_session_id!=my_id)
  1878. {
  1879. printf("auth fail!!\n");
  1880. return 0;
  1881. }
  1882. int tmp_oppsite_session_id= ntohl(* ((uint32_t *)&data[1]));
  1883. oppsite_id=tmp_oppsite_session_id;
  1884. send_hb(g_packet_info_send,my_id,oppsite_id,const_id);
  1885. server_current_state=server_ready;
  1886. last_state_time=get_current_time();
  1887. last_hb_recv_time=get_current_time();
  1888. //first_data_packet=1;
  1889. printf("changed state to server_ready\n");
  1890. }
  1891. else if(server_current_state==server_ready)
  1892. {
  1893. if( (raw_mode==mode_tcp&&(info.syn==1||info.ack!=1)) ||data_len==0) return 0;
  1894. if(info.src_ip!=g_packet_info_send.dst_ip||info.src_port!=g_packet_info_send.dst_port)
  1895. {
  1896. printf("unexpected adress\n");
  1897. return 0;
  1898. }
  1899. if(data[0]=='h'&&data_len>=hb_length)
  1900. {
  1901. uint32_t tmp= ntohl(* ((uint32_t *)&data[1+sizeof(uint32_t)]));
  1902. if(debug_mode)printf("received hb <%x,%x>\n",oppsite_id,tmp);
  1903. last_hb_recv_time=get_current_time();
  1904. return 0;
  1905. }
  1906. else if(data[0]=='d'&&data_len>=sizeof(my_id)*3+1)
  1907. {
  1908. uint32_t tmp_oppsite_session_id=ntohl(* ((uint32_t *)&data[1]));
  1909. uint32_t tmp_session_id=ntohl(* ((uint32_t *)&data[1+sizeof(my_id)]));
  1910. uint32_t tmp_conv_id=ntohl(* ((uint32_t *)&data[1+sizeof(my_id)*2]));
  1911. if(tmp_session_id!=my_id)
  1912. {
  1913. printf("my id mismatch,ignore\n");
  1914. return 0;
  1915. }
  1916. if(tmp_oppsite_session_id!=oppsite_id)
  1917. {
  1918. printf("oppsite id mismatch,ignore\n");
  1919. return 0;
  1920. }
  1921. last_hb_recv_time=get_current_time();
  1922. printf("<<<<conv:%u>>>>\n",tmp_conv_id);
  1923. if(!conv_manager.is_conv_used(tmp_conv_id))
  1924. {
  1925. struct sockaddr_in remote_addr_in;
  1926. socklen_t slen = sizeof(sockaddr_in);
  1927. memset(&remote_addr_in, 0, sizeof(remote_addr_in));
  1928. remote_addr_in.sin_family = AF_INET;
  1929. remote_addr_in.sin_port = htons(remote_port);
  1930. remote_addr_in.sin_addr.s_addr = inet_addr(remote_address);
  1931. int new_udp_fd=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
  1932. if(new_udp_fd<0)
  1933. {
  1934. printf("create udp_fd error");
  1935. return -1;
  1936. }
  1937. set_buf_size(new_udp_fd);
  1938. printf("created new udp_fd %d\n",new_udp_fd);
  1939. int ret = connect(new_udp_fd, (struct sockaddr *) &remote_addr_in, slen);
  1940. if(ret!=0)
  1941. {
  1942. printf("udp fd connect fail\n");
  1943. close(new_udp_fd);
  1944. return -1;
  1945. }
  1946. struct epoll_event ev;
  1947. uint64_t u64=((u_int64_t(tmp_conv_id))<<32u)+(uint32_t)new_udp_fd;
  1948. printf("u64: %ld\n",u64);
  1949. ev.events = EPOLLIN;
  1950. ev.data.u64 = u64;
  1951. ret = epoll_ctl(epollfd, EPOLL_CTL_ADD, new_udp_fd, &ev);
  1952. if (ret!= 0) {
  1953. printf("add udp_fd error\n");
  1954. close(new_udp_fd);
  1955. return -1;
  1956. }
  1957. conv_manager.insert_conv(tmp_conv_id,u64);
  1958. }
  1959. uint64_t u64=conv_manager.find_u64_by_conv(tmp_conv_id);
  1960. conv_manager.update_active_time(tmp_conv_id);
  1961. int fd=int((u64<<32u)>>32u);
  1962. printf("received a data from fake tcp,len:%d\n",data_len);
  1963. int ret=send(fd,data+1+sizeof(my_id)*3,data_len -(1+sizeof(my_id)*3),0);
  1964. printf("%d byte sent ,fd :%d\n ",ret,fd);
  1965. if(ret<0)
  1966. {
  1967. perror("what happened????");
  1968. }
  1969. /*
  1970. if(first_data_packet==0&& tmp_conv_id!=conv_id) //magic to find out which one is actually larger
  1971. //consider 0xffffffff+1= 0x0 ,in this case 0x0 is "actually" larger
  1972. {
  1973. uint32_t smaller,bigger;
  1974. smaller=min(conv_id,tmp_conv_id);//smaller in normal sense
  1975. bigger=max(conv_id,tmp_conv_id);
  1976. uint32_t distance=min(bigger-smaller,smaller+(0xffffffff-bigger+1));
  1977. if(distance==bigger-smaller)
  1978. {
  1979. if(bigger==conv_id) //received_session_id is acutally bigger
  1980. {
  1981. printf("old_session_id ,ingored1\n");
  1982. return 0;
  1983. }
  1984. }
  1985. else
  1986. {
  1987. if(smaller==conv_id) //received_session_id is acutally bigger
  1988. {
  1989. printf("old_session_id ,ingored2\n");
  1990. return 0;
  1991. }
  1992. }
  1993. }
  1994. first_data_packet=0;
  1995. if(udp_fd==-1||tmp_conv_id!=conv_id)// this is first send or client changed session
  1996. {
  1997. int old_fd=udp_fd;
  1998. struct sockaddr_in remote_addr_in;
  1999. socklen_t slen = sizeof(sockaddr_in);
  2000. memset(&remote_addr_in, 0, sizeof(remote_addr_in));
  2001. remote_addr_in.sin_family = AF_INET;
  2002. remote_addr_in.sin_port = htons(remote_port);
  2003. remote_addr_in.sin_addr.s_addr = inet_addr(remote_address);
  2004. udp_fd=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
  2005. set_udp_buf_size(udp_fd);
  2006. printf("created new udp_fd");
  2007. int ret = connect(udp_fd, (struct sockaddr *) &remote_addr_in, slen);
  2008. if(ret!=0)
  2009. {
  2010. printf("udp fd connect fail\n");
  2011. }
  2012. struct epoll_event ev;
  2013. ev.events = EPOLLIN;
  2014. epoll_udp_fd_sn+=256;
  2015. ev.data.u64 = epoll_udp_fd_sn;
  2016. ret = epoll_ctl(epollfd, EPOLL_CTL_ADD, udp_fd, &ev);
  2017. if (ret!= 0) {
  2018. printf("add udp_fd error\n");
  2019. exit(-1);
  2020. }
  2021. if(old_fd!=-1)
  2022. {
  2023. epoll_ctl(epollfd, EPOLL_CTL_DEL, old_fd, 0);
  2024. close(old_fd);
  2025. }
  2026. }
  2027. if(tmp_conv_id!=conv_id)
  2028. {
  2029. conv_id=tmp_conv_id;
  2030. }
  2031. */
  2032. }
  2033. }
  2034. }
  2035. int client_event_loop()
  2036. {
  2037. int i, j, k;int ret;
  2038. init_raw_socket();
  2039. my_id=get_true_random_number_nz();
  2040. conv_id=get_true_random_number_nz();
  2041. //init_filter(source_port);
  2042. g_packet_info_send.dst_ip=inet_addr(remote_address);
  2043. g_packet_info_send.dst_port=remote_port;
  2044. //g_packet_info.src_ip=inet_addr(source_address);
  2045. //g_packet_info.src_port=source_port;
  2046. udp_fd=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
  2047. set_buf_size(udp_fd);
  2048. int yes = 1;
  2049. //setsockopt(udp_fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes));
  2050. struct sockaddr_in local_me;
  2051. socklen_t slen = sizeof(sockaddr_in);
  2052. memset(&local_me, 0, sizeof(local_me));
  2053. local_me.sin_family = AF_INET;
  2054. local_me.sin_port = htons(local_port);
  2055. local_me.sin_addr.s_addr = inet_addr(local_address);
  2056. if (bind(udp_fd, (struct sockaddr*) &local_me, slen) == -1) {
  2057. perror("socket bind error");
  2058. exit(1);
  2059. }
  2060. setnonblocking(udp_fd);
  2061. int epollfd = epoll_create1(0);
  2062. const int max_events = 4096;
  2063. struct epoll_event ev, events[max_events];
  2064. if (epollfd < 0) {
  2065. printf("epoll return %d\n", epollfd);
  2066. exit(-1);
  2067. }
  2068. ev.events = EPOLLIN;
  2069. ev.data.u64 = epoll_udp_fd_sn;
  2070. ret = epoll_ctl(epollfd, EPOLL_CTL_ADD, udp_fd, &ev);
  2071. if (ret!=0) {
  2072. printf("add udp_listen_fd error\n");
  2073. exit(-1);
  2074. }
  2075. ev.events = EPOLLIN;
  2076. ev.data.u64 = epoll_raw_recv_fd_sn;
  2077. ret = epoll_ctl(epollfd, EPOLL_CTL_ADD, raw_recv_fd, &ev);
  2078. if (ret!= 0) {
  2079. printf("add raw_fd error\n");
  2080. exit(-1);
  2081. }
  2082. ////add_timer for fake_tcp_keep_connection_client
  2083. //sleep(10);
  2084. memset(&udp_old_addr_in,0,sizeof(sockaddr_in));
  2085. int unbind=1;
  2086. int timer_fd;
  2087. set_timer(epollfd,timer_fd);
  2088. while(1)////////////////////////
  2089. {
  2090. int nfds = epoll_wait(epollfd, events, max_events, 180 * 1000);
  2091. if (nfds < 0) { //allow zero
  2092. printf("epoll_wait return %d\n", nfds);
  2093. exit(-1);
  2094. }
  2095. int n;
  2096. for (n = 0; n < nfds; ++n) {
  2097. if (events[n].data.u64 == epoll_raw_recv_fd_sn)
  2098. {
  2099. iphdr *iph;tcphdr *tcph;char* data;int data_len;
  2100. if(recv_raw(g_packet_info_recv,data,data_len)!=0)
  2101. {
  2102. continue;
  2103. }
  2104. int new_len=data_len;
  2105. memcpy(raw_recv_buf3,data,new_len); //for safety,copy to a new buffer,will remove later
  2106. if(data_len!=0)
  2107. {
  2108. if(pre_recv(raw_recv_buf3,new_len)<0)
  2109. continue;
  2110. }
  2111. client_on_raw_recv(g_packet_info_recv,raw_recv_buf3,new_len);
  2112. }
  2113. if(events[n].data.u64 ==epoll_timer_fd_sn)
  2114. {
  2115. //printf("timer!\n");
  2116. //fflush(stdout);
  2117. uint64_t value;
  2118. read(timer_fd, &value, 8);
  2119. keep_connection_client();
  2120. }
  2121. if (events[n].data.u64 == epoll_udp_fd_sn)
  2122. {
  2123. socklen_t recv_len;
  2124. struct sockaddr_in udp_new_addr_in;
  2125. if ((recv_len = recvfrom(udp_fd, buf, buf_len, 0,
  2126. (struct sockaddr *) &udp_new_addr_in, &slen)) == -1) {
  2127. printf("recv_from error");
  2128. exit(1);
  2129. };
  2130. printf("Received packet from %s:%d,len: %d\n", inet_ntoa(udp_new_addr_in.sin_addr),
  2131. ntohs(udp_new_addr_in.sin_port),recv_len);
  2132. /*
  2133. if(udp_old_addr_in.sin_addr.s_addr==0&&udp_old_addr_in.sin_port==0)
  2134. {
  2135. memcpy(&udp_old_addr_in,&udp_new_addr_in,sizeof(udp_new_addr_in));
  2136. }
  2137. else if(udp_new_addr_in.sin_addr.s_addr!=udp_old_addr_in.sin_addr.s_addr
  2138. ||udp_new_addr_in.sin_port!=udp_old_addr_in.sin_port)
  2139. {
  2140. if(get_current_time()- last_udp_recv_time <udp_timeout)
  2141. {
  2142. printf("new <ip,port> connected in,ignored,bc last connection is still active\n");
  2143. continue;
  2144. }
  2145. else
  2146. {
  2147. printf("new <ip,port> connected in,accpeted\n");
  2148. memcpy(&udp_old_addr_in,&udp_new_addr_in,sizeof(udp_new_addr_in));
  2149. conv_id++;
  2150. }
  2151. }*/
  2152. //last_udp_recv_time=get_current_time();
  2153. uint64_t u64=((uint64_t(udp_new_addr_in.sin_addr.s_addr))<<32u)+ntohs(udp_new_addr_in.sin_port);
  2154. uint32_t conv;
  2155. if(!conv_manager.is_u64_used(u64))
  2156. {
  2157. printf("new connection!!!!!!!!!!!\n");
  2158. conv=conv_manager.get_new_conv();
  2159. conv_manager.insert_conv(conv,u64);
  2160. }
  2161. else
  2162. {
  2163. conv=conv_manager.find_conv_by_u64(u64);
  2164. }
  2165. conv_manager.update_active_time(conv);
  2166. if(client_current_state==client_ready)
  2167. {
  2168. send_data(g_packet_info_send,buf,recv_len,my_id,oppsite_id,conv);
  2169. }
  2170. }
  2171. }
  2172. }
  2173. return 0;
  2174. }
  2175. int server_event_loop()
  2176. {
  2177. conv_manager.set_clear_function(server_clear);
  2178. int i, j, k;int ret;
  2179. g_packet_info_send.src_ip=inet_addr(local_address);
  2180. g_packet_info_send.src_port=local_port;
  2181. if(raw_mode==mode_tcp)
  2182. {
  2183. bind_fd=socket(AF_INET,SOCK_STREAM,0);
  2184. }
  2185. else if(raw_mode==mode_udp||raw_mode==mode_icmp)
  2186. {
  2187. bind_fd=socket(AF_INET,SOCK_DGRAM,0);
  2188. }
  2189. struct sockaddr_in temp_bind_addr;
  2190. bzero(&temp_bind_addr, sizeof(temp_bind_addr));
  2191. temp_bind_addr.sin_family = AF_INET;
  2192. temp_bind_addr.sin_port = htons(local_port);
  2193. temp_bind_addr.sin_addr.s_addr = inet_addr(local_address);
  2194. if (bind(bind_fd, (struct sockaddr*)&temp_bind_addr, sizeof(temp_bind_addr)) !=0)
  2195. {
  2196. printf("bind fail\n");
  2197. exit(-1);
  2198. }
  2199. if(raw_mode==mode_tcp)
  2200. {
  2201. if(listen(bind_fd, SOMAXCONN) != 0 )
  2202. {
  2203. printf("listen fail\n");
  2204. exit(-1);
  2205. }
  2206. }
  2207. init_raw_socket();
  2208. init_filter(local_port);
  2209. epollfd = epoll_create1(0);
  2210. const int max_events = 4096;
  2211. struct epoll_event ev, events[max_events];
  2212. if (epollfd < 0) {
  2213. printf("epoll return %d\n", epollfd);
  2214. exit(-1);
  2215. }
  2216. ev.events = EPOLLIN;
  2217. ev.data.u64 = epoll_raw_recv_fd_sn;
  2218. ret = epoll_ctl(epollfd, EPOLL_CTL_ADD, raw_recv_fd, &ev);
  2219. if (ret!= 0) {
  2220. printf("add raw_fd error\n");
  2221. exit(-1);
  2222. }
  2223. int timer_fd;
  2224. set_timer(epollfd,timer_fd);
  2225. while(1)////////////////////////
  2226. {
  2227. int nfds = epoll_wait(epollfd, events, max_events, 180 * 1000);
  2228. if (nfds < 0) { //allow zero
  2229. printf("epoll_wait return %d\n", nfds);
  2230. exit(-1);
  2231. }
  2232. int n;
  2233. const int MTU=1440;
  2234. for (n = 0; n < nfds; ++n)
  2235. {
  2236. if ((events[n].data.u64 >>32u) > 0u)
  2237. {
  2238. uint32_t conv_id=events[n].data.u64>>32u;
  2239. if(!conv_manager.is_u64_used(events[n].data.u64))
  2240. {
  2241. printf("conv no longer exists");
  2242. continue;
  2243. }
  2244. int fd=int((events[n].data.u64<<32u)>>32u);
  2245. int recv_len=recv(fd,buf,buf_len,0);
  2246. printf("received a packet from udp_fd,len:%d\n",recv_len);
  2247. if(recv_len<0)
  2248. {
  2249. printf("continue\n");
  2250. perror("wtf?");
  2251. continue;
  2252. //return 0;
  2253. }
  2254. conv_manager.update_active_time(conv_id);
  2255. if(server_current_state==server_ready)
  2256. {
  2257. send_data(g_packet_info_send,buf,recv_len,my_id,oppsite_id,conv_id);
  2258. printf("send !!!!!!!!!!!!!!!!!!");
  2259. }
  2260. }
  2261. //printf("%d %d %d %d\n",timer_fd,raw_recv_fd,raw_send_fd,n);
  2262. if (events[n].data.u64 == epoll_timer_fd_sn)
  2263. {
  2264. uint64_t value;
  2265. read(timer_fd, &value, 8);
  2266. keep_connection_server();
  2267. }
  2268. if (events[n].data.u64 == epoll_raw_recv_fd_sn)
  2269. {
  2270. iphdr *iph;tcphdr *tcph;char* data;int data_len;
  2271. if(recv_raw(g_packet_info_recv,data,data_len)!=0)
  2272. {
  2273. continue;
  2274. }
  2275. int new_len=data_len;
  2276. memcpy(raw_recv_buf3,data,new_len);
  2277. if(data_len!=0)
  2278. {
  2279. //if(raw_mode==mode_tcp || ((raw_mode==mode_udp||raw_mode==mode_icmp) &&server_current_state!=server_nothing ))
  2280. //{
  2281. if(pre_recv(raw_recv_buf3,new_len)<0)
  2282. continue;
  2283. //}
  2284. }
  2285. server_on_raw_recv(g_packet_info_recv,raw_recv_buf3,new_len);
  2286. }
  2287. }
  2288. }
  2289. return 0;
  2290. }
  2291. int main(int argc, char *argv[])
  2292. {
  2293. srand(time(0));
  2294. if(raw_mode==mode_tcp)
  2295. {
  2296. g_packet_info_send.protocol=IPPROTO_TCP;
  2297. }
  2298. else if(raw_mode==mode_udp)
  2299. {
  2300. g_packet_info_send.protocol=IPPROTO_UDP;
  2301. }
  2302. else if(raw_mode==mode_icmp)
  2303. {
  2304. g_packet_info_send.protocol=IPPROTO_ICMP;
  2305. }
  2306. init_random_number_fd();
  2307. const_id=get_true_random_number_nz();
  2308. anti_replay_seq=get_true_random_number_nz();
  2309. g_packet_info_send.ack_seq=get_true_random_number_nz();
  2310. g_packet_info_send.seq=get_true_random_number_nz();
  2311. int i, j, k;
  2312. signal(SIGCHLD, handler);
  2313. process_arg(argc,argv);
  2314. if(prog_mode==client_mode)
  2315. {
  2316. for(int i=0;i<16;i++)
  2317. {
  2318. key_me[i]=key[i];
  2319. key_oppsite[i]=key[i]+1;
  2320. }
  2321. client_event_loop();
  2322. }
  2323. else
  2324. {
  2325. for(int i=0;i<16;i++)
  2326. {
  2327. key_me[i]=key[i]+1;
  2328. key_oppsite[i]=key[i];
  2329. }
  2330. server_event_loop();
  2331. }
  2332. return 0;
  2333. }