main.cpp 64 KB

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