main.cpp 68 KB

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