main.cpp 68 KB

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