1
0

main.cpp 73 KB

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