| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067 |
- #include<stdio.h>
- #include<string.h>
- #include<sys/socket.h>
- #include<arpa/inet.h>
- #include<stdlib.h>
- #include<getopt.h>
- #include <unistd.h>
- #include<errno.h>
- #include <fcntl.h>
- //#include"aes.h"
- #include <sys/epoll.h>
- #include <sys/wait.h>
- #include<map>
- #include<string>
- #include<vector>
- #include <sys/socket.h> //for socket ofcourse
- #include <sys/types.h>
- #include <stdlib.h> //for exit(0);
- #include <errno.h> //For errno - the error number
- #include <netinet/tcp.h> //Provides declarations for tcp header
- #include <netinet/udp.h>
- #include <netinet/ip.h> //Provides declarations for ip header
- #include <netinet/if_ether.h>
- #include <arpa/inet.h>
- #include <fcntl.h>
- #include <byteswap.h>
- #include <sys/socket.h>
- #include <sys/types.h>
- #include <arpa/inet.h>
- #include <linux/if_ether.h>
- #include <linux/filter.h>
- #include <sys/time.h>
- #include <time.h>
- #include <sys/timerfd.h>
- #include <set>
- #include <encrypt.h>
- #include <inttypes.h>
- using namespace std;
- const int mode_tcp=0;
- const int mode_udp=1;
- const int mode_icmp=2;
- int raw_mode=mode_udp;
- char local_address[100], remote_address[100],source_address[100];
- int local_port = -1, remote_port = -1;
- int epollfd ;
- typedef uint32_t id_t;
- typedef uint64_t iv_t;
- typedef uint64_t anti_replay_seq_t;
- anti_replay_seq_t anti_replay_seq=0;
- id_t const_id=0;
- id_t oppsite_const_id=0;
- id_t my_id=0;
- id_t oppsite_id=0;
- uint32_t conv_num=0;
- uint32_t link_level_header_len=0;
- const int handshake_timeout=2000;
- const int heartbeat_timeout=10000;
- const int udp_timeout=3000;
- const int heartbeat_interval=1000;
- const int timer_interval=500;
- const int RETRY_TIME=3;
- //const uint16_t tcp_window=50000;
- const int buf_len = 65535+100;
- const int server_mode=2;
- const int client_mode=1;
- int prog_mode=0; //0 unset; 1client 2server
- const int disable_encrypt=0;
- const int disable_anti_replay=0;
- const int disable_bpf_filter=1;
- const int disable_conv_clear=0;
- const int debug_mode=0;
- int bind_fd;
- int first_data_packet=0;
- const int seq_mode=2; //0 dont increase /1 increase //increase randomly,about every 5 packet
- const uint64_t epoll_timer_fd_sn=1;
- const uint64_t epoll_raw_recv_fd_sn=2;
- const uint64_t epoll_udp_fd_sn_begin=256;
- 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
- const int server_nothing=0;
- const int server_syn_ack_sent=1;
- const int server_heartbeat_sent=2;
- const int server_ready=3;
- int server_current_state=server_nothing;
- long long last_hb_recv_time;
- long long last_udp_recv_time=0;
- int socket_buf_size=1024*1024*4;
- int udp_fd=-1;
- int raw_recv_fd;
- int raw_send_fd;
- int filter_port=-1;
- const int client_nothing=0;
- const int client_syn_sent=1;
- const int client_ack_sent=2;
- const int client_heartbeat_sent=3;
- const int client_ready=4;
- int client_current_state=client_nothing;
- int retry_counter;
- long long last_state_time=0;
- long long last_hb_sent_time=0;
- struct sock_filter code_tcp[] = {
- { 0x28, 0, 0, 0x0000000c },//0
- { 0x15, 0, 10, 0x00000800 },//1
- { 0x30, 0, 0, 0x00000017 },//2
- { 0x15, 0, 8, 0x00000006 },//3
- { 0x28, 0, 0, 0x00000014 },//4
- { 0x45, 6, 0, 0x00001fff },//5
- { 0xb1, 0, 0, 0x0000000e },//6
- { 0x48, 0, 0, 0x0000000e },//7
- { 0x15, 2, 0, 0x0000ef32 },//8
- { 0x48, 0, 0, 0x00000010 },//9
- { 0x15, 0, 1, 0x0000ef32 },//10
- { 0x6, 0, 0, 0x0000ffff },//11
- { 0x6, 0, 0, 0x00000000 },//12
- };
- sock_fprog bpf;
- uint16_t ip_id=1;
- //const int MTU=1440;
- struct sockaddr_in udp_old_addr_in;
- uint8_t key[]={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16, 0,0,0,0};
- uint8_t key_me[16];
- uint8_t key_oppsite[16];
- const int window_size=2000;
- int random_number_fd=-1;
- const int conv_timeout=60000; //60 second
- const int conv_clear_ratio=10;
- const int hb_length=1+3*sizeof(uint32_t);
- int VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV;
- ////////==============================variable divider=============================================================
- struct pseudo_header {
- u_int32_t source_address;
- u_int32_t dest_address;
- u_int8_t placeholder;
- u_int8_t protocol;
- u_int16_t tcp_length;
- };
- struct icmphdr
- {
- uint8_t type;
- uint8_t code;
- uint16_t check_sum;
- uint16_t id;
- uint16_t seq;
- };
- struct anti_replay_t
- {
- uint64_t max_packet_received;
- char window[window_size];
- char disabled;
- anti_replay_t()
- {
- disabled=0;
- max_packet_received=0;
- //memset(window,0,sizeof(window)); //not necessary
- }
- void re_init()
- {
- disabled=0;
- max_packet_received=0;
- //memset(window,0,sizeof(window));
- }
- void disable()
- {
- disabled=1;
- }
- void enable()
- {
- disabled=0;
- }
- int is_vaild(uint64_t seq)
- {
- //if(disabled) return 0;
- if(seq==max_packet_received) return 0||disabled;
- else if(seq>max_packet_received)
- {
- if(seq-max_packet_received>=window_size)
- {
- memset(window,0,sizeof(window));
- window[seq%window_size]=1;
- }
- else
- {
- for (int i=max_packet_received+1;i<seq;i++)
- window[i%window_size]=0;
- window[seq%window_size]=1;
- }
- max_packet_received=seq;
- return 1;
- }
- else if(seq<max_packet_received)
- {
- if(max_packet_received-seq>=window_size) return 0||disabled;
- else
- {
- if (window[seq%window_size]==1) return 0||disabled;
- else
- {
- window[seq%window_size]=1;
- return 1;
- }
- }
- }
- }
- }anti_replay;
- uint32_t get_true_random_number_nz();
- long long get_current_time();
- struct conv_manager_t
- {
- map<uint64_t,uint32_t> u64_to_conv; //conv and u64 are both supposed to be uniq
- map<uint32_t,uint64_t> conv_to_u64;
- map<uint32_t,uint64_t> conv_last_active_time;
- map<uint32_t,uint64_t>::iterator clear_it;
- void (*clear_function)(uint64_t u64) ;
- conv_manager_t()
- {
- clear_it=conv_last_active_time.begin();
- clear_function=0;
- }
- void set_clear_function(void (*a)(uint64_t u64))
- {
- clear_function=a;
- }
- void clear()
- {
- if(disable_conv_clear) return ;
- if(clear_function!=0)
- {
- map<uint32_t,uint64_t>::iterator it;
- for(it=conv_last_active_time.begin();it!=conv_last_active_time.end();it++)
- {
- clear_function(it->second);
- }
- }
- u64_to_conv.clear();
- conv_to_u64.clear();
- conv_last_active_time.clear();
- clear_it=conv_last_active_time.begin();
- }
- uint32_t get_new_conv()
- {
- uint32_t conv=get_true_random_number_nz();
- while(conv!=0&&conv_to_u64.find(conv)!=conv_to_u64.end())
- {
- conv=get_true_random_number_nz();
- }
- return conv;
- }
- int is_conv_used(uint32_t conv)
- {
- return conv_to_u64.find(conv)!=conv_to_u64.end();
- }
- int is_u64_used(uint64_t u64)
- {
- return u64_to_conv.find(u64)!=u64_to_conv.end();
- }
- uint32_t find_conv_by_u64(uint64_t u64)
- {
- return u64_to_conv[u64];
- }
- uint64_t find_u64_by_conv(uint32_t conv)
- {
- return conv_to_u64[conv];
- }
- int update_active_time(uint32_t conv)
- {
- return conv_last_active_time[conv]=get_current_time();
- }
- int insert_conv(uint32_t conv,uint64_t u64)
- {
- u64_to_conv[u64]=conv;
- conv_to_u64[conv]=u64;
- conv_last_active_time[conv]=get_current_time();
- return 0;
- }
- int erase_conv(uint32_t conv)
- {
- if(disable_conv_clear) return 0;
- uint64_t u64=conv_to_u64[conv];
- if(clear_function!=0)
- {
- clear_function(u64);
- }
- conv_to_u64.erase(conv);
- u64_to_conv.erase(u64);
- conv_last_active_time.erase(conv);
- return 0;
- }
- int clean_inactive( )
- {
- if(disable_conv_clear) return 0;
- map<uint32_t,uint64_t>::iterator old_it;
- map<uint32_t,uint64_t>::iterator it;
- int cnt=0;
- it=clear_it;
- int size=conv_last_active_time.size();
- int num_to_clean=size/conv_clear_ratio; //clear 1/10 each time,to avoid latency glitch
- uint64_t current_time=get_current_time();
- for(;;)
- {
- if(cnt>=num_to_clean) break;
- if(conv_last_active_time.begin()==conv_last_active_time.end()) break;
- if(it==conv_last_active_time.end())
- {
- it=conv_last_active_time.begin();
- }
- if( current_time -it->second >conv_timeout )
- {
- printf("inactive conv %u cleared !!!!!!!!!!!!!!!!!!!!!!!!!\n",it->first);
- old_it=it;
- it++;
- erase_conv(old_it->first);
- }
- else
- {
- it++;
- }
- cnt++;
- }
- return 0;
- }
- }conv_manager;
- struct packet_info_t
- {
- uint8_t protocol;
- //ip_part:
- uint32_t src_ip;
- uint16_t src_port;
- uint32_t dst_ip;
- uint16_t dst_port;
- //tcp_part:
- bool syn,ack,psh,rst;
- uint32_t seq,ack_seq;
- uint32_t ts,ts_ack;
- uint16_t icmp_seq;
- bool has_ts;
- }g_packet_info_send,g_packet_info_recv;
- int TTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT;
- ////////==========================type divider=======================================================
- void init_random_number_fd()
- {
- random_number_fd=open("/dev/urandom",O_RDONLY);
- if(random_number_fd==-1)
- {
- printf("error open /dev/urandom");
- exit(-1);
- }
- }
- uint64_t get_true_random_number_64()
- {
- uint64_t ret;
- read(random_number_fd,&ret,sizeof(ret));
- return ret;
- }
- uint32_t get_true_random_number_0()
- {
- uint32_t ret;
- read(random_number_fd,&ret,sizeof(ret));
- return ret;
- }
- uint32_t get_true_random_number_nz() //nz for non-zero
- {
- uint32_t ret=0;
- while(ret==0)
- {
- ret=get_true_random_number_0();
- }
- return ret;
- }
- uint64_t ntoh64(uint64_t a)
- {
- if(__BYTE_ORDER == __LITTLE_ENDIAN)
- {
- return __bswap_64( a);
- }
- else return a;
- }
- uint64_t hton64(uint64_t a)
- {
- if(__BYTE_ORDER == __LITTLE_ENDIAN)
- {
- return __bswap_64( a);
- }
- else return a;
- }
- int pre_send(char * data, int &data_len)
- {
- char replay_buf[buf_len];
- //return 0;
- if(data_len<0) return -3;
- if(disable_encrypt&&disable_anti_replay) return 0;
- if(!disable_anti_replay)
- {
- anti_replay_seq++;
- uint32_t seq_high= htonl(anti_replay_seq>>32u);
- uint32_t seq_low= htonl((anti_replay_seq<<32u)>>32u);
- memcpy(replay_buf,&seq_high,sizeof(uint32_t));
- memcpy(replay_buf+sizeof(uint32_t),&seq_low,sizeof(uint32_t));
- memcpy(replay_buf+sizeof(uint32_t)*2,data,data_len);
- data_len+=sizeof(uint32_t)*2;
- }
- else
- {
- memcpy(replay_buf,data,data_len);
- }
- if(!disable_encrypt)
- {
- if(my_encrypt((unsigned char*)replay_buf,(unsigned char*)data,data_len,key_me) <0)
- {
- printf("encrypt fail\n");
- return -1;
- }
- }
- else
- {
- memcpy(data,replay_buf,data_len);
- }
- return 0;
- }
- int pre_recv(char * data, int &data_len)
- {
- char replay_buf[buf_len];
- //return 0;
- if(data_len<0) return -1;
- if(disable_encrypt&&disable_anti_replay) return 0;
- if(!disable_encrypt)
- {
- if(my_decrypt((uint8_t*)data,(uint8_t*)replay_buf,data_len,key_oppsite) <0)
- {
- printf("decrypt fail\n");
- return -1;
- }
- else
- {
- printf("decrypt succ\n");
- }
- }
- else
- {
- memcpy(replay_buf,data,data_len);
- }
- if(!disable_anti_replay)
- {
- data_len-=sizeof(uint32_t)*2;
- if(data_len<0)
- {
- printf("data_len<=0\n");
- return -2;
- }
- uint64_t seq_high= ntohl(*((uint32_t*)(replay_buf) ) );
- uint32_t seq_low= ntohl(*((uint32_t*)(replay_buf+sizeof(uint32_t)) ) );
- uint64_t recv_seq =(seq_high<<32u )+seq_low;
- if((prog_mode==client_mode&&client_current_state==client_ready)
- ||(prog_mode==server_mode&&server_current_state==server_ready ))
- {
- if(data_len<sizeof(uint32_t)*2+1)
- {
- printf("no room for session id and oppiste session_id");
- return -4;
- }
- uint32_t tmp_oppiste_session_id = ntohl(
- *((uint32_t*) (replay_buf + sizeof(uint32_t) * 2+1)));
- uint32_t tmp_session_id = ntohl(
- *((uint32_t*) (replay_buf + sizeof(uint32_t) * 3+1)));
- if (tmp_oppiste_session_id != oppsite_id
- || tmp_session_id != my_id) {
- printf("auth fail and pre send\n");
- return -5;
- }
- printf("seq=========%u\n", recv_seq);
- if (anti_replay.is_vaild(recv_seq) != 1) {
- printf("dropped replay packet\n");
- return -1;
- }
- }
- printf("<<<<<%ld,%d,%ld>>>>\n",seq_high,seq_low,recv_seq);
- memcpy(data,replay_buf+sizeof(uint32_t)*2,data_len);
- }
- else
- {
- memcpy(data,replay_buf,data_len);
- }
- return 0;
- }
- void handler(int num) {
- int status;
- int pid;
- while ((pid = waitpid(-1, &status, WNOHANG)) > 0) {
- if (WIFEXITED(status)) {
- //printf("The child exit with code %d",WEXITSTATUS(status));
- }
- }
- }
- void setnonblocking(int sock) {
- int opts;
- opts = fcntl(sock, F_GETFL);
- if (opts < 0) {
- perror("fcntl(sock,GETFL)");
- exit(1);
- }
- opts = opts | O_NONBLOCK;
- if (fcntl(sock, F_SETFL, opts) < 0) {
- perror("fcntl(sock,SETFL,opts)");
- exit(1);
- }
- }
- int set_buf_size(int fd)
- {
- if(setsockopt(fd, SOL_SOCKET, SO_SNDBUFFORCE, &socket_buf_size, sizeof(socket_buf_size))<0)
- {
- printf("SO_SNDBUFFORCE fail\n");
- exit(1);
- }
- if(setsockopt(fd, SOL_SOCKET, SO_RCVBUFFORCE, &socket_buf_size, sizeof(socket_buf_size))<0)
- {
- printf("SO_RCVBUFFORCE fail\n");
- exit(1);
- }
- return 0;
- }
- int init_raw_socket()
- {
- raw_send_fd = socket(AF_INET , SOCK_RAW , IPPROTO_TCP);
- if(raw_send_fd == -1) {
- perror("Failed to create raw_send_fd");
- exit(1);
- }
- if(setsockopt(raw_send_fd, SOL_SOCKET, SO_SNDBUFFORCE, &socket_buf_size, sizeof(socket_buf_size))<0)
- {
- printf("SO_SNDBUFFORCE fail\n");
- exit(1);
- }
- //raw_fd = socket(AF_PACKET, SOCK_DGRAM, htons(ETH_P_ALL));
- raw_recv_fd= socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_IP));
- if(setsockopt(raw_recv_fd, SOL_SOCKET, SO_RCVBUFFORCE, &socket_buf_size, sizeof(socket_buf_size))<0)
- {
- printf("SO_RCVBUFFORCE fail\n");
- exit(1);
- }
- //raw_fd=socket(AF_PACKET , SOCK_RAW , htons(ETH_P_IP));
- // packet_recv_sd = socket(AF_INET , SOCK_RAW , IPPROTO_TCP);
- if(raw_recv_fd == -1) {
- //socket creation failed, may be because of non-root privileges
- perror("Failed to create raw_recv_fd");
- exit(1);
- }
- //IP_HDRINCL to tell the kernel that headers are included in the packet
- int one = 1;
- const int *val = &one;
- if (setsockopt (raw_send_fd, IPPROTO_IP, IP_HDRINCL, val, sizeof (one)) < 0) {
- perror("Error setting IP_HDRINCL");
- exit(2);
- }
- setnonblocking(raw_send_fd); //not really necessary
- setnonblocking(raw_recv_fd);
- return 0;
- }
- void init_filter(int port)
- {
- filter_port=port;
- if(disable_bpf_filter) return;
- code_tcp[8].k=code_tcp[10].k=port;
- bpf.len = sizeof(code_tcp)/sizeof(code_tcp[0]);
- bpf.filter = code_tcp;
- //printf("<%d>\n",bpf.len);
- int dummy;
- int ret=setsockopt(raw_recv_fd, SOL_SOCKET, SO_DETACH_FILTER, &dummy, sizeof(dummy));
- if (ret != 0)
- {
- printf("error remove fiter\n");
- perror("filter");
- //exit(-1);
- }
- ret = setsockopt(raw_recv_fd, SOL_SOCKET, SO_ATTACH_FILTER, &bpf, sizeof(bpf));
- //memset(code,0,sizeof(code));
- if (ret != 0)
- {
- printf("error set fiter\n");
- perror("filter");
- exit(-1);
- }
- }
- long long get_current_time()
- {
- timespec tmp_time;
- clock_gettime(CLOCK_MONOTONIC, &tmp_time);
- return tmp_time.tv_sec*1000+tmp_time.tv_nsec/(1000*1000l);
- }
- void server_clear(uint64_t u64)
- {
- int fd=int((u64<<32u)>>32u);
- epoll_event ev;
- ev.events = EPOLLIN;
- ev.data.u64 = u64;
- int ret = epoll_ctl(epollfd, EPOLL_CTL_DEL, fd, &ev);
- if (ret!=0)
- {
- printf("fd:%d epoll delete failed!!!!\n",fd);
- }
- ret= close(fd);
- if (ret!=0)
- {
- printf("close fd %d failed !!!!\n",fd);
- }
- }
- void process_arg(int argc, char *argv[])
- {
- int i,j,k,opt;
- static struct option long_options[] =
- {
- /* These options set a flag. */
- {"source-ip", required_argument, 0, 1},
- };
- int option_index = 0;
- printf("argc=%d ", argc);
- for (i = 0; i < argc; i++)
- printf("%s ", argv[i]);
- printf("\n");
- if (argc == 1)
- {
- printf(
- "proc -l [adress:]port -r [adress:]port [-a passwd] [-b passwd]\n");
- exit(-1);
- }
- int no_l = 1, no_r = 1;
- while ((opt = getopt_long(argc, argv, "l:r:sch",long_options,&option_index)) != -1) {
- //string opt_key;
- //opt_key+=opt;
- switch (opt) {
- case 'l':
- no_l = 0;
- if (strchr(optarg, ':') != 0) {
- sscanf(optarg, "%[^:]:%d", local_address, &local_port);
- } else {
- strcpy(local_address, "127.0.0.1");
- sscanf(optarg, "%d", &local_port);
- }
- break;
- case 'r':
- no_r = 0;
- if (strchr(optarg, ':') != 0) {
- sscanf(optarg, "%[^:]:%d", remote_address, &remote_port);
- } else {
- strcpy(remote_address, "127.0.0.1");
- sscanf(optarg, "%d", &remote_port);
- }
- break;
- case 's':
- if(prog_mode==0)
- {
- prog_mode=server_mode;
- }
- else
- {
- printf("-s /-c has already been set,-s option conflict");
- exit(-1);
- }
- break;
- case 'c':
- if(prog_mode==0)
- {
- prog_mode=client_mode;
- }
- else
- {
- printf("-s /-c has already been set,-c option conflict");
- exit(-1);
- }
- break;
- case 'h':
- break;
- case 1:
- //if (strchr(optarg, ':') != 0) {
- sscanf(optarg, "%s", source_address);
- printf("source: %s",source_address);
- //} else {
- //printf("format --source-ip :adress");
- //exit(-1);
- //}
- break;
- default:
- printf("ignore unknown <%s>", optopt);
- }
- }
- if (no_l)
- printf("error: -i not found\n");
- if (no_r)
- printf("error: -o not found\n");
- if(prog_mode==0)
- printf("error: -s /-r hasnt been set\n");
- if (no_l || no_r||prog_mode==0)
- {
- exit(-1);
- }
- }
- /*
- Generic checksum calculation function
- */
- unsigned short csum(unsigned short *ptr,int nbytes) {
- register long sum;
- unsigned short oddbyte;
- register short answer;
- sum=0;
- while(nbytes>1) {
- sum+=*ptr++;
- nbytes-=2;
- }
- if(nbytes==1) {
- oddbyte=0;
- *((u_char*)&oddbyte)=*(u_char*)ptr;
- sum+=oddbyte;
- }
- sum = (sum>>16)+(sum & 0xffff);
- sum = sum + (sum>>16);
- answer=(short)~sum;
- return(answer);
- }
- int send_raw_ip(packet_info_t &info,char * payload,int payloadlen)
- {
- char send_raw_ip_buf[buf_len];
- struct iphdr *iph = (struct iphdr *) send_raw_ip_buf;
- memset(iph,0,sizeof(iphdr));
- struct sockaddr_in sin;
- sin.sin_family = AF_INET;
- //sin.sin_port = htons(info.dst_port); //dont need this
- sin.sin_addr.s_addr = info.dst_ip;
- iph->ihl = sizeof(iphdr)/4; //we dont use ip options,so the length is just sizeof(iphdr)
- iph->version = 4;
- iph->tos = 0;
- iph->id = htonl (ip_id++); //Id of this packet
- iph->frag_off = htons(0x4000); //DF set,others are zero
- iph->ttl = 64;
- iph->protocol = info.protocol;
- iph->check = 0; //Set to 0 before calculating checksum
- iph->saddr = info.src_ip; //Spoof the source ip address
- iph->daddr = info.dst_ip;
- iph->tot_len = sizeof (struct iphdr)+payloadlen;
- memcpy(send_raw_ip_buf+sizeof(iphdr) , payload, payloadlen);
- iph->check = csum ((unsigned short *) send_raw_ip_buf, iph->tot_len);
- int ret = sendto(raw_send_fd, send_raw_ip_buf, iph->tot_len , 0, (struct sockaddr *) &sin, sizeof (sin));
- if(ret==-1)
- {
- printf("sendto failed\n");
- return -1;
- }
- return 0;
- }
- int recv_raw_ip(packet_info_t &info,char * &payload,int &payloadlen)
- {
- static char recv_raw_ip_buf[buf_len];
- iphdr * iph;
- struct sockaddr saddr;
- socklen_t saddr_size;
- saddr_size = sizeof(saddr);
- int recv_len = recvfrom(raw_recv_fd, recv_raw_ip_buf, buf_len, 0 ,&saddr , &saddr_size);
- if(recv_len<0)
- {
- printf("recv_len %d",recv_len);
- return -1;
- }
- if(recv_len<link_level_header_len)
- {
- printf("length error");
- }
- if(link_level_header_len ==14&&(recv_raw_ip_buf[12]!=8||recv_raw_ip_buf[13]!=0))
- {
- printf("not an ipv4 packet!\n");
- return -1;
- }
- char *ip_begin=recv_raw_ip_buf+link_level_header_len; //14 is eth net header
- iph = (struct iphdr *) (ip_begin);
- info.src_ip=iph->saddr;
- info.dst_ip=iph->daddr;
- info.protocol=iph->protocol;
- if (!(iph->ihl > 0 && iph->ihl <=60)) {
- if(debug_mode) printf("iph ihl error");
- return -1;
- }
- int ip_len=ntohs(iph->tot_len);
- if(recv_len-link_level_header_len <ip_len)
- {
- printf("incomplete packet\n");
- return -1;
- }
- unsigned short iphdrlen =iph->ihl*4;
- uint32_t ip_chk=csum ((unsigned short *) ip_begin, iphdrlen);
- if(ip_chk!=0)
- {
- printf("ip header error %d\n",ip_chk);
- return -1;
- }
- payload=ip_begin+iphdrlen;
- payloadlen=ip_len-iphdrlen;
- if(payloadlen<0)
- {
- printf("error payload len");
- return -1;
- }
- return 0;
- }
- int send_raw_icmp(packet_info_t &info, char * payload, int payloadlen)
- {
- char send_raw_icmp_buf[buf_len];
- icmphdr *icmph=(struct icmphdr *) (send_raw_icmp_buf);
- memset(icmph,0,sizeof(icmphdr));
- if(prog_mode==client_mode)
- {
- icmph->type=8;
- }
- else
- {
- icmph->type=0;
- }
- icmph->code=0;
- icmph->id=htons(g_packet_info_send.src_port);
- icmph->seq=htons(g_packet_info_send.icmp_seq++);
- memcpy(send_raw_icmp_buf+sizeof(icmphdr),payload,payloadlen);
- icmph->check_sum = csum( (unsigned short*) send_raw_icmp_buf, sizeof(icmphdr)+payloadlen);
- if(send_raw_ip(info,send_raw_icmp_buf,sizeof(icmphdr)+payloadlen)!=0)
- {
- return -1;
- }
- return 0;
- }
- int send_raw_udp(packet_info_t &info, char * payload, int payloadlen)
- {
- char send_raw_udp_buf[buf_len];
- udphdr *udph=(struct udphdr *) (send_raw_udp_buf
- + sizeof(struct pseudo_header));
- memset(udph,0,sizeof(udphdr));
- struct pseudo_header *psh = (struct pseudo_header *) (send_raw_udp_buf);
- udph->source = htons(info.src_port);
- udph->dest = htons(info.dst_port);
- int udp_tot_len=payloadlen+sizeof(udph);
- if(udp_tot_len>65535)
- {
- printf("invalid len\n");
- return -1;
- }
- udph->len=htons(uint16_t(udp_tot_len));
- memcpy(send_raw_udp_buf+sizeof(struct pseudo_header)+sizeof(udph),payload,payloadlen);
- psh->source_address = info.src_ip;
- psh->dest_address = info.dst_ip;
- psh->placeholder = 0;
- psh->protocol = IPPROTO_UDP;
- psh->tcp_length = htons(uint16_t(udp_tot_len));
- int csum_size = sizeof(struct pseudo_header) +udp_tot_len ;
- udph->check = csum( (unsigned short*) send_raw_udp_buf, csum_size);
- if(send_raw_ip(info,send_raw_udp_buf+ sizeof(struct pseudo_header),udp_tot_len)!=0)
- {
- return -1;
- }
- return 0;
- }
- int send_raw_tcp(packet_info_t &info, char * payload, int payloadlen) { //TODO seq increase
- char send_raw_tcp_buf[buf_len];
- struct tcphdr *tcph = (struct tcphdr *) (send_raw_tcp_buf
- + sizeof(struct pseudo_header));
- memset(tcph,0,sizeof(tcphdr));
- struct pseudo_header *psh = (struct pseudo_header *) (send_raw_tcp_buf);
- //TCP Header
- tcph->source = htons(info.src_port);
- tcph->dest = htons(info.dst_port);
- tcph->seq = htonl(info.seq);
- tcph->ack_seq = htonl(info.ack_seq);
- tcph->fin = 0;
- tcph->syn = info.syn;
- tcph->rst = 0;
- tcph->psh = info.psh;
- tcph->ack = info.ack;
- if (tcph->syn == 1) {
- tcph->doff = 10; //tcp header size
- int i = sizeof(pseudo_header)+sizeof(tcphdr);
- send_raw_tcp_buf[i++] = 0x02; //mss
- send_raw_tcp_buf[i++] = 0x04;
- send_raw_tcp_buf[i++] = 0x05;
- send_raw_tcp_buf[i++] = 0xb4;
- //raw_send_buf[i++]=0x01;
- //raw_send_buf[i++]=0x01;
- send_raw_tcp_buf[i++] = 0x04; //sack ok
- send_raw_tcp_buf[i++] = 0x02; //sack ok
- send_raw_tcp_buf[i++] = 0x08; //i=6;
- send_raw_tcp_buf[i++] = 0x0a;
- *(uint32_t*) (&send_raw_tcp_buf[i]) = htonl(
- (uint32_t) get_current_time());
- i += 4;
- *(uint32_t*) (&send_raw_tcp_buf[i]) = htonl(info.ts_ack);
- i += 4;
- send_raw_tcp_buf[i++] = 0x01;
- send_raw_tcp_buf[i++] = 0x03;
- send_raw_tcp_buf[i++] = 0x03;
- send_raw_tcp_buf[i++] = 0x05;
- } else {
- tcph->doff = 8;
- int i = sizeof(pseudo_header)+sizeof(tcphdr);
- send_raw_tcp_buf[i++] = 0x01;
- send_raw_tcp_buf[i++] = 0x01;
- send_raw_tcp_buf[i++] = 0x08; //i=0;
- send_raw_tcp_buf[i++] = 0x0a;
- *(uint32_t*) (&send_raw_tcp_buf[i]) = htonl(
- (uint32_t) get_current_time());
- i += 4;
- *(uint32_t*) (&send_raw_tcp_buf[i]) = htonl(info.ts_ack);
- i += 4;
- }
- tcph->urg = 0;
- //tcph->window = htons((uint16_t)(1024));
- tcph->window = htons((uint16_t) (10240 + random() % 100));
- tcph->check = 0; //leave checksum 0 now, filled later by pseudo header
- tcph->urg_ptr = 0;
- char *tcp_data = send_raw_tcp_buf+sizeof(struct pseudo_header) + tcph->doff * 4;
- memcpy(tcp_data, payload, payloadlen);
- psh->source_address = info.src_ip;
- psh->dest_address = info.dst_ip;
- psh->placeholder = 0;
- psh->protocol = IPPROTO_TCP;
- psh->tcp_length = htons(tcph->doff * 4 + payloadlen);
- int csum_size = sizeof(struct pseudo_header) + tcph->doff*4 + payloadlen;
- tcph->check = csum( (unsigned short*) send_raw_tcp_buf, csum_size);
- int tcp_totlen=tcph->doff*4 + payloadlen;
- if(send_raw_ip(info,send_raw_tcp_buf+ sizeof(struct pseudo_header),tcp_totlen)!=0)
- {
- return -1;
- }
- return 0;
- }
- int send_raw_tcp_deprecated(packet_info_t &info,char * payload,int payloadlen)
- {
- char raw_send_buf[buf_len];
- char raw_send_buf2[buf_len];
- if((prog_mode==client_mode&& payloadlen!=9) ||(prog_mode==server_mode&& payloadlen!=5 ) )
- printf("send raw from to %d %d %d %d\n",info.src_ip,info.src_port,info.dst_ip,info.dst_port);
- char *data;
- memset(raw_send_buf,0,payloadlen+100);
- struct iphdr *iph = (struct iphdr *) raw_send_buf;
- //TCP header
- struct tcphdr *tcph = (struct tcphdr *) (raw_send_buf + sizeof (struct ip));
- struct sockaddr_in sin;
- struct pseudo_header psh;
- //some address resolution
- sin.sin_family = AF_INET;
- sin.sin_port = htons(info.dst_port);
- sin.sin_addr.s_addr = info.dst_ip;
- //Fill in the IP Header
- iph->ihl = 5;
- iph->version = 4;
- iph->tos = 0;
- iph->id = htonl (ip_id++); //Id of this packet
- iph->frag_off = htons(0x4000); //DF set,others are zero
- iph->ttl = 64;
- iph->protocol = IPPROTO_TCP;
- iph->check = 0; //Set to 0 before calculating checksum
- iph->saddr = info.src_ip; //Spoof the source ip address
- iph->daddr = info.dst_ip;
- //TCP Header
- tcph->source = htons(info.src_port);
- tcph->dest = htons(info.dst_port);
- tcph->seq =htonl(info.seq);
- tcph->ack_seq = htonl(info.ack_seq);
- tcph->fin=0;
- tcph->syn=info.syn;
- tcph->rst=0;
- tcph->psh=info.psh;
- tcph->ack=info.ack;
- if(tcph->syn==1)
- {
- tcph->doff = 10; //tcp header size
- int i=sizeof (struct iphdr)+20;
- raw_send_buf[i++]=0x02;//mss
- raw_send_buf[i++]=0x04;
- raw_send_buf[i++]=0x05;
- raw_send_buf[i++]=0xb4;
- //raw_send_buf[i++]=0x01;
- //raw_send_buf[i++]=0x01;
- raw_send_buf[i++]=0x04; //sack ok
- raw_send_buf[i++]=0x02; //sack ok
- raw_send_buf[i++]=0x08; //i=6;
- raw_send_buf[i++]=0x0a;
- *(uint32_t*)(& raw_send_buf[i])=htonl((uint32_t)get_current_time());
- i+=4;
- *(uint32_t*)(& raw_send_buf[i])=htonl(info.ts_ack);
- i+=4;
- raw_send_buf[i++]=0x01;
- raw_send_buf[i++]=0x03;
- raw_send_buf[i++]=0x03;
- raw_send_buf[i++]=0x05;
- }
- else
- {
- tcph->doff=8;
- int i=sizeof (struct iphdr)+20;
- raw_send_buf[i++]=0x01;
- raw_send_buf[i++]=0x01;
- raw_send_buf[i++]=0x08; //i=0;
- raw_send_buf[i++]=0x0a;
- *(uint32_t*)(& raw_send_buf[i])=htonl((uint32_t)get_current_time());
- i+=4;
- *(uint32_t*)(& raw_send_buf[i])=htonl(info.ts_ack);
- i+=4;
- }
- tcph->urg=0;
- //tcph->window = htons((uint16_t)(1024));
- tcph->window = htons((uint16_t)(10240+random()%100));
- tcph->check = 0; //leave checksum 0 now, filled later by pseudo header
- tcph->urg_ptr = 0;
- //Data part
- data = raw_send_buf + sizeof(struct iphdr) + tcph->doff*4;
- iph->tot_len = sizeof (struct iphdr) + tcph->doff*4 + payloadlen;
- memcpy(data , payload, payloadlen);
- psh.source_address = info.src_ip;
- psh.dest_address = sin.sin_addr.s_addr;
- psh.placeholder = 0;
- psh.protocol = IPPROTO_TCP;
- psh.tcp_length = htons(tcph->doff*4 + payloadlen );
- int psize = sizeof(struct pseudo_header) + tcph->doff*4 + payloadlen;
- memcpy(raw_send_buf2 , (char*) &psh , sizeof (struct pseudo_header));
- memcpy(raw_send_buf2 + sizeof(struct pseudo_header) , tcph , tcph->doff*4 + payloadlen);
- tcph->check = csum( (unsigned short*) raw_send_buf2, psize);
- //Ip checksum
- iph->check = csum ((unsigned short *) raw_send_buf, iph->tot_len);
- if(prog_mode==client_mode&& payloadlen!=9 ||prog_mode==server_mode&& payloadlen!=5)
- printf("sent seq ack_seq len<%u %u %d>\n",g_packet_info_send.seq,g_packet_info_send.ack_seq,payloadlen);
- int ret = sendto(raw_send_fd, raw_send_buf, iph->tot_len , 0, (struct sockaddr *) &sin, sizeof (sin));
- if(g_packet_info_send.syn==0&&g_packet_info_send.ack==1&&payloadlen!=0)
- {
- if(seq_mode==0)
- {
- }
- else if(seq_mode==1)
- {
- g_packet_info_send.seq+=payloadlen;
- }
- else if(seq_mode==2)
- {
- if(random()% 5==3 )
- g_packet_info_send.seq+=payloadlen;
- }
- }
- if(debug_mode) printf("<ret:%d>\n",ret);
- if(ret<0)
- {
- perror("raw send error");
- //printf("send error\n");
- }
- return 0;
- }
- int recv_raw_icmp(packet_info_t &info, char *&payload, int &payloadlen)
- {
- static char recv_raw_icmp_buf[buf_len];
- char * ip_payload;
- int ip_payloadlen;
- if(recv_raw_ip(info,ip_payload,ip_payloadlen)!=0)
- {
- printf("recv_raw_ip error");
- return -1;
- }
- if(info.protocol!=IPPROTO_ICMP)
- {
- //printf("not udp protocol\n");
- return -1;
- }
- icmphdr *icmph=(struct icmphdr *) (ip_payload);
- info.src_port=info.dst_port=ntohs(icmph->id);
- if(prog_mode==client_mode)
- {
- if(icmph->type!=0)
- return -1;
- }
- else
- {
- if(icmph->type!=8)
- return -1;
- }
- if(icmph->code!=0)
- return -1;
- unsigned short check = csum( (unsigned short*) ip_payload, ip_payloadlen);
- if(check!=0)
- {
- printf("icmp checksum fail %x\n",check);
- return -1;
- }
- payload=ip_payload+sizeof(icmphdr);
- payloadlen=ip_payloadlen-sizeof(icmphdr);
- printf("get a packet len=%d\n",payloadlen);
- return 0;
- }
- int recv_raw_udp(packet_info_t &info, char *&payload, int &payloadlen)
- {
- static char recv_raw_udp_buf[buf_len];
- char * ip_payload;
- int ip_payloadlen;
- if(recv_raw_ip(info,ip_payload,ip_payloadlen)!=0)
- {
- printf("recv_raw_ip error");
- return -1;
- }
- if(info.protocol!=IPPROTO_UDP)
- {
- //printf("not udp protocol\n");
- return -1;
- }
- if(ip_payloadlen<sizeof(udphdr))
- {
- printf("too short to hold udpheader\n");
- return -1;
- }
- udphdr *udph=(struct udphdr*)ip_payload;
- if(ntohs(udph->len)!=ip_payloadlen)
- {
- printf("udp length error %d %d \n",ntohs(udph->len),ip_payloadlen);
- return -1;
- }
- if(udph->dest!=ntohs(uint16_t(filter_port)))
- {
- //printf("%x %x",tcph->dest,);
- return -1;
- }
- memcpy(recv_raw_udp_buf+ sizeof(struct pseudo_header) , ip_payload , ip_payloadlen);
- struct pseudo_header *psh=(pseudo_header *)recv_raw_udp_buf ;
- psh->source_address = info.src_ip;
- psh->dest_address = info.dst_ip;
- psh->placeholder = 0;
- psh->protocol = IPPROTO_UDP;
- psh->tcp_length = htons(ip_payloadlen);
- int csum_len=sizeof(struct pseudo_header)+ip_payloadlen;
- uint16_t udp_chk = csum( (unsigned short*) recv_raw_udp_buf, csum_len);
- if(udp_chk!=0)
- {
- printf("udp_chk:%x\n",udp_chk);
- printf("udp header error\n");
- return -1;
- }
- char *udp_begin=recv_raw_udp_buf+sizeof(struct pseudo_header);
- info.src_port=ntohs(udph->source);
- info.dst_port=ntohs(udph->dest);
- payloadlen = ip_payloadlen-sizeof(udphdr);
- payload=udp_begin+sizeof(udphdr);
- return 0;
- }
- int recv_raw_tcp(packet_info_t &info,char * &payload,int &payloadlen)
- {
- static char recv_raw_tcp_buf[buf_len];
- char * ip_payload;
- int ip_payloadlen;
- if(recv_raw_ip(info,ip_payload,ip_payloadlen)!=0)
- {
- printf("recv_raw_ip error");
- return -1;
- }
- if(info.protocol!=IPPROTO_TCP)
- {
- //printf("not tcp protocol\n");
- return -1;
- }
- tcphdr * tcph=(struct tcphdr*)ip_payload;
- unsigned short tcphdrlen = tcph->doff*4;
- if (!(tcph->doff > 0 && tcph->doff <=60)) {
- if(debug_mode) printf("tcph error");
- return 0;
- }
- if(tcph->dest!=ntohs(uint16_t(filter_port)))
- {
- //printf("%x %x",tcph->dest,);
- return -1;
- }
- memcpy(recv_raw_tcp_buf+ sizeof(struct pseudo_header) , ip_payload , ip_payloadlen);
- struct pseudo_header *psh=(pseudo_header *)recv_raw_tcp_buf ;
- psh->source_address = info.src_ip;
- psh->dest_address = info.dst_ip;
- psh->placeholder = 0;
- psh->protocol = IPPROTO_TCP;
- psh->tcp_length = htons(ip_payloadlen);
- int csum_len=sizeof(struct pseudo_header)+ip_payloadlen;
- uint16_t tcp_chk = csum( (unsigned short*) recv_raw_tcp_buf, csum_len);
- if(tcp_chk!=0)
- {
- printf("tcp_chk:%x\n",tcp_chk);
- printf("tcp header error\n");
- return -1;
- }
- char *tcp_begin=recv_raw_tcp_buf+sizeof(struct pseudo_header); //ip packet's data part
- char *tcp_option=recv_raw_tcp_buf+sizeof(struct pseudo_header)+sizeof(tcphdr);
- info.has_ts=0;
- if(tcph->doff==10)
- {
- if(tcp_option[6]==0x08 &&tcp_option[7]==0x0a)
- {
- info.has_ts=1;
- info.ts=ntohl(*(uint32_t*)(&tcp_option[8]));
- info.ts_ack=ntohl(*(uint32_t*)(&tcp_option[12]));
- //g_packet_info_send.ts_ack= ntohl(*(uint32_t*)(&tcp_option[8]));
- }
- }
- else if(tcph->doff==8)
- {
- if(tcp_option[3]==0x08 &&tcp_option[4]==0x0a)
- {
- info.has_ts=1;
- info.ts=ntohl(*(uint32_t*)(&tcp_option[0]));
- info.ts_ack=ntohl(*(uint32_t*)(&tcp_option[4]));
- //g_packet_info_send.ts_ack= ntohl(*(uint32_t*)(&tcp_option[0]));
- }
- }
- if(tcph->rst==1)
- {
- printf("%%%%%%%%%%rst==1%%%%%%%%%%%%%\n");
- }
- info.ack=tcph->ack;
- info.syn=tcph->syn;
- info.rst=tcph->rst;
- info.src_port=ntohs(tcph->source);
- info.dst_port=ntohs(tcph->dest);
- info.seq=ntohl(tcph->seq);
- info.ack_seq=ntohl(tcph->ack_seq);
- info.psh=tcph->psh;
- if(info.has_ts)
- {
- g_packet_info_send.ts_ack=info.ts;
- }
- payloadlen = ip_payloadlen-tcphdrlen;
- payload=tcp_begin+tcphdrlen;
- return 0;
- }
- int recv_raw_tcp_deprecated(packet_info_t &info,char * &payload,int &payloadlen)
- {
- static char buf[buf_len];
- char raw_recv_buf[buf_len];
- char raw_recv_buf2[buf_len];
- char raw_recv_buf3[buf_len];
- iphdr * iph;
- tcphdr * tcph;
- int size;
- struct sockaddr saddr;
- socklen_t saddr_size;
- saddr_size = sizeof(saddr);
- if(debug_mode)printf("raw!\n");
- size = recvfrom(raw_recv_fd, buf, buf_len, 0 ,&saddr , &saddr_size);
- if(buf[12]!=8||buf[13]!=0)
- {
- printf("not an ipv4 packet!\n");
- fflush(stdout);
- return -1;
- }
- char *ip_begin=buf+14;
- iph = (struct iphdr *) (ip_begin);
- if (!(iph->ihl > 0 && iph->ihl <=60)) {
- if(debug_mode) printf("iph ihl error");
- return -1;
- }
- if (iph->protocol != IPPROTO_TCP) {
- if(debug_mode)printf("iph protocal != tcp\n");
- return -1;
- }
- int ip_len=ntohs(iph->tot_len);
- unsigned short iphdrlen =iph->ihl*4;
- tcph=(struct tcphdr*)(ip_begin+ iphdrlen);
- unsigned short tcphdrlen = tcph->doff*4;
- if (!(tcph->doff > 0 && tcph->doff <=60)) {
- if(debug_mode) printf("tcph error");
- return 0;
- }
- if(tcph->dest!=ntohs(uint16_t(filter_port)))
- {
- //printf("%x %x",tcph->dest,);
- return -1;
- }
- /////ip
- uint32_t ip_chk=csum ((unsigned short *) ip_begin, iphdrlen);
- int psize = sizeof(struct pseudo_header) + ip_len-iphdrlen;
- /////ip end
- ///tcp
- struct pseudo_header psh;
- psh.source_address = iph->saddr;
- psh.dest_address = iph->daddr;
- psh.placeholder = 0;
- psh.protocol = IPPROTO_TCP;
- psh.tcp_length = htons(ip_len-iphdrlen);
- memcpy(raw_recv_buf2 , (char*) &psh , sizeof (struct pseudo_header));
- memcpy(raw_recv_buf2 + sizeof(struct pseudo_header) , ip_begin+ iphdrlen , ip_len-iphdrlen);
- uint16_t tcp_chk = csum( (unsigned short*) raw_recv_buf2, psize);
- if(ip_chk!=0)
- {
- printf("ip header error %d\n",ip_chk);
- return -1;
- }
- if(tcp_chk!=0)
- {
- printf("tcp_chk:%x\n",tcp_chk);
- printf("tcp header error\n");
- return -1;
- }
- char *tcp_begin=raw_recv_buf2+sizeof(struct pseudo_header); //ip packet's data part
- char *tcp_option=raw_recv_buf2+sizeof(struct pseudo_header)+sizeof(tcphdr);
- info.has_ts=0;
- if(tcph->doff==10)
- {
- if(tcp_option[6]==0x08 &&tcp_option[7]==0x0a)
- {
- info.has_ts=1;
- info.ts=ntohl(*(uint32_t*)(&tcp_option[8]));
- info.ts_ack=ntohl(*(uint32_t*)(&tcp_option[12]));
- //g_packet_info_send.ts_ack= ntohl(*(uint32_t*)(&tcp_option[8]));
- }
- }
- else if(tcph->doff==8)
- {
- if(tcp_option[3]==0x08 &&tcp_option[4]==0x0a)
- {
- info.has_ts=1;
- info.ts=ntohl(*(uint32_t*)(&tcp_option[0]));
- info.ts_ack=ntohl(*(uint32_t*)(&tcp_option[4]));
- //g_packet_info_send.ts_ack= ntohl(*(uint32_t*)(&tcp_option[0]));
- }
- }
- if(tcph->rst==1)
- {
- printf("%%%%%%%%%%rst==1%%%%%%%%%%%%%\n");
- }
- info.ack=tcph->ack;
- info.syn=tcph->syn;
- info.rst=tcph->rst;
- info.src_port=ntohs(tcph->source);
- info.src_ip=iph->saddr;
- info.seq=ntohl(tcph->seq);
- info.ack_seq=ntohl(tcph->ack_seq);
- info.psh=tcph->psh;
- if(info.has_ts)
- {
- g_packet_info_send.ts_ack=info.ts;
- }
- ////tcp end
- payloadlen = ip_len-tcphdrlen-iphdrlen;
- payload=ip_begin+tcphdrlen+iphdrlen;
- if(payloadlen>0&&payload[0]=='h')
- {
- printf("recvd <%u %u %d>\n",ntohl(tcph->seq ),ntohl(tcph->ack_seq), payloadlen);
- }
- if(payloadlen>0&&tcph->syn==0&&tcph->ack==1)
- {
- //if(seq_increse)
- g_packet_info_send.ack_seq=ntohl(tcph->seq)+(uint32_t)payloadlen;
- }
- //printf("%d\n",ip_len);
- /*
- for(int i=0;i<size;i++)
- {
- printf("<%x>",(unsigned char)buf[i]);
- }
- printf("\n");
- */
- /*
- for(int i=0;i<data_len;i++)
- {
- printf("<%x>",(unsigned char)data[i]);
- }*/
- if(debug_mode)
- {
- printf("\n");
- printf("<%u,%u,%u,%u,%d>\n",(unsigned int)iphdrlen,(unsigned int)tcphdrlen,(unsigned int)tcph->syn,(unsigned int)tcph->ack,payloadlen);
- //fflush(stdout);
- }
- return 0;
- }
- int send_raw(packet_info_t &info,char * payload,int payloadlen)
- {
- if(raw_mode==mode_tcp) return send_raw_tcp(info,payload,payloadlen);
- else if(raw_mode==mode_udp) return send_raw_udp(info,payload,payloadlen);
- else if(raw_mode==mode_icmp) return send_raw_icmp(info,payload,payloadlen);
- }
- int recv_raw(packet_info_t &info,char * &payload,int &payloadlen)
- {
- if(raw_mode==mode_tcp) return recv_raw_tcp(info,payload,payloadlen);
- else if(raw_mode==mode_udp) return recv_raw_udp(info,payload,payloadlen);
- else if(raw_mode==mode_icmp) return recv_raw_icmp(info,payload,payloadlen);
- }
- int send_bare(packet_info_t &info,char* data,int len)
- {
- char send_data_buf[buf_len]; //buf for send data and send hb
- char send_data_buf2[buf_len];
- if(len==0) //dont encrpyt zero length packet;
- {
- send_raw(info,data,len);
- return 0;
- }
- //static send_bare[buf_len];
- iv_t iv=get_true_random_number_64();
- memcpy(send_data_buf,&iv,sizeof(iv_t));
- memcpy(send_data_buf+sizeof(iv_t),data,len);
- int new_len=len+sizeof(iv_t);
- if(my_encrypt((uint8_t *)send_data_buf,(uint8_t*)send_data_buf2,new_len,key_me)!=0)
- {
- return -1;
- }
- send_raw(info,send_data_buf2,new_len);
- return 0;
- }
- int recv_bare(packet_info_t &info,char* & data,int & len)
- {
- static char recv_data_buf[buf_len];
- if(recv_raw(info,data,len)<0)
- {
- return -1;
- }
- if(len==0) //dont decrpyt zero length packet;
- {
- return 0;
- }
- if(my_decrypt((uint8_t *)data,(uint8_t*)recv_data_buf,len,key_oppsite)!=0)
- {
- return -1;
- }
- data=recv_data_buf+sizeof(iv_t);
- len-=sizeof(iv_t);
- return 0;
- }
- int send_safe(packet_info_t &info,char* data,int len)
- {
- char send_data_buf[buf_len]; //buf for send data and send hb
- char send_data_buf2[buf_len];
- id_t n_tmp_id=hton64(my_id);
- memcpy(send_data_buf,&n_tmp_id,sizeof(n_tmp_id));
- n_tmp_id=hton64(oppsite_id);
- memcpy(send_data_buf+sizeof(n_tmp_id),&n_tmp_id,sizeof(n_tmp_id));
- anti_replay_seq_t n_seq=hton64(anti_replay_seq++);
- memcpy(send_data_buf+sizeof(n_tmp_id)*2,&n_seq,sizeof(n_seq));
- memcpy(send_data_buf+sizeof(n_tmp_id)*2+sizeof(n_seq),data,len);//data;
- int new_len=len+sizeof(n_seq)+sizeof(n_tmp_id)*2;
- if(my_encrypt((uint8_t *)send_data_buf,(uint8_t*)send_data_buf2,new_len,key_me)!=0)
- {
- return -1;
- }
- send_raw(info,send_data_buf2,new_len);
- return 0;
- }
- int recv_safe(packet_info_t &info,char* data,int len)
- {
- static char recv_data_buf[buf_len];
- if(my_decrypt((uint8_t *)data,(uint8_t*)recv_data_buf,len,key_oppsite)!=0)
- {
- return -1;
- }
- id_t h_oppiste_id= ntoh64 ( *((anti_replay_seq_t * )(data)) );
- id_t h_my_id= ntoh64 ( *((anti_replay_seq_t * )(data)) +sizeof(h_my_id) );
- anti_replay_seq_t h_seq= ntoh64 ( *((anti_replay_seq_t * )(data +sizeof(h_my_id) *2 )) );
- if(h_oppiste_id!=oppsite_id||h_my_id!=my_id)
- {
- printf("auth fail\n");
- return -1;
- }
- if (anti_replay.is_vaild(h_seq) != 1) {
- printf("dropped replay packet\n");
- return -1;
- }
- return 0;
- }
- int send_bare_data(packet_info_t &info,char* data,int len)
- {
- char send_data_buf[buf_len]; //buf for send data and send hb
- char send_data_buf2[buf_len];
- int new_len=len;
- memcpy(send_data_buf,data,len);
- if(pre_send(send_data_buf,new_len)<0)
- {
- return -1;
- }
- send_raw(info,send_data_buf,new_len);
- return 0;
- }
- int send_data(packet_info_t &info,char* data,int len,uint32_t id1,uint32_t id2,uint32_t conv_id)
- {
- char send_data_buf[buf_len]; //buf for send data and send hb
- char send_data_buf2[buf_len];
- int new_len=1+sizeof(my_id)*3+len;
- send_data_buf[0]='d';
- uint32_t tmp;
- tmp=htonl(id1);
- memcpy(send_data_buf+1,&tmp,sizeof(my_id));
- tmp=htonl(id2);
- memcpy(send_data_buf+1+sizeof(my_id),&tmp,sizeof(my_id));
- tmp=htonl(conv_id);
- memcpy(send_data_buf+1+sizeof(my_id)*2,&tmp,sizeof(my_id));
- memcpy(send_data_buf+1+sizeof(my_id)*3,data,len);
- if(pre_send(send_data_buf,new_len)<0)
- {
- return -1;
- }
- send_raw(info,send_data_buf,new_len);
- return 0;
- }
- int send_hb(packet_info_t &info,uint32_t id1,uint32_t id2 ,uint32_t id3)
- {
- char send_data_buf[buf_len]; //buf for send data and send hb
- char send_data_buf2[buf_len];
- int new_len=1+sizeof(my_id)*3;
- send_data_buf[0]='h';
- uint32_t tmp;
- tmp=htonl(id1);
- memcpy(send_data_buf+1,&tmp,sizeof(my_id));
- tmp=htonl(id2);
- memcpy(send_data_buf+1+sizeof(my_id),&tmp,sizeof(my_id));
- tmp=htonl(id3);
- memcpy(send_data_buf+1+sizeof(my_id)*2,&tmp,sizeof(my_id));
- if(pre_send(send_data_buf,new_len)<0)
- {
- return -1;
- }
- send_raw(info,send_data_buf,new_len);
- return 0;
- }
- /*int send_sync()
- {
- //g_packet_info.seq=3;
- g_packet_info.ack=0;
- g_packet_info.syn=1;
- //g_packet_info.ack_seq=5;
- g_packet_info.psh=0;
- send_raw(g_packet_info,0,0);
- return 0;
- }*/
- int try_to_list_and_bind(int port)
- {
- int old_bind_fd=bind_fd;
- if(raw_mode==mode_tcp)
- {
- bind_fd=socket(AF_INET,SOCK_STREAM,0);
- }
- else if(raw_mode==mode_udp||raw_mode==mode_icmp)
- {
- bind_fd=socket(AF_INET,SOCK_DGRAM,0);
- }
- if(old_bind_fd!=-1)
- {
- close(old_bind_fd);
- }
- struct sockaddr_in temp_bind_addr;
- bzero(&temp_bind_addr, sizeof(temp_bind_addr));
- temp_bind_addr.sin_family = AF_INET;
- temp_bind_addr.sin_port = htons(port);
- temp_bind_addr.sin_addr.s_addr = inet_addr(local_address);
- if (bind(bind_fd, (struct sockaddr*)&temp_bind_addr, sizeof(temp_bind_addr)) !=0)
- {
- printf("bind fail\n");
- return -1;
- }
- if(raw_mode==mode_tcp)
- {
- if (listen(bind_fd, SOMAXCONN) != 0) {
- printf("listen fail\n");
- return -1;
- }
- }
- return 0;
- }
- int client_bind_to_a_new_port()
- {
- int raw_send_port=10000+get_true_random_number_nz()%(65535-10000);
- for(int i=0;i<1000;i++)//try 1000 times at max,this should be enough
- {
- if (try_to_list_and_bind(raw_send_port)==0)
- {
- return raw_send_port;
- }
- }
- printf("bind port fail\n");
- fflush(stdout);
- exit(-1);
- return -1;////for compiler check
- }
- int keep_connection_client() //for client
- {
- conv_manager.clean_inactive();
- if(debug_mode)printf("timer!\n");
- //fflush(stdout);
- begin:
- if(client_current_state==client_nothing)
- {
- anti_replay.re_init(); // this is not safe
- g_packet_info_send.src_port = client_bind_to_a_new_port();
- if(raw_mode==mode_icmp)
- {
- g_packet_info_send.dst_port =g_packet_info_send.src_port ;
- }
- printf("using port %d\n", g_packet_info_send.src_port);
- g_packet_info_send.src_ip = inet_addr(source_address);
- init_filter(g_packet_info_send.src_port);
- if(raw_mode==mode_tcp)
- {
- client_current_state = client_syn_sent;
- last_state_time = get_current_time();
- printf("state changed from nothing to syn_sent\n");
- retry_counter = RETRY_TIME;
- g_packet_info_send.seq = get_true_random_number_nz();
- g_packet_info_send.ack_seq = 0; //get_true_random_number();
- g_packet_info_send.ts_ack = 0;
- g_packet_info_send.ack = 0;
- g_packet_info_send.syn = 1;
- g_packet_info_send.psh = 0;
- send_raw(g_packet_info_send, 0, 0);
- }
- else if(raw_mode==mode_udp||raw_mode==mode_icmp)
- {
- client_current_state = client_ack_sent;
- last_state_time = get_current_time();
- printf("state changed from nothing to ack_sent\n");
- retry_counter = RETRY_TIME;
- g_packet_info_send.icmp_seq=0;
- send_bare_data(g_packet_info_send, (char*)"hello", strlen("hello"));
- }
- }
- if(client_current_state==client_syn_sent &&get_current_time()-last_state_time>handshake_timeout)
- {
- if(retry_counter==0)
- {
- client_current_state=client_nothing;
- printf("state back to nothing\n");
- return 0;
- //goto begin;
- }
- else
- {
- retry_counter--;
- printf("retry send sync\n");
- send_raw(g_packet_info_send,0,0);
- last_state_time=get_current_time();
- }
- }
- if(client_current_state==client_ack_sent &&get_current_time()-last_state_time>handshake_timeout)
- {
- if(retry_counter==0)
- {
- client_current_state=client_nothing;
- printf("state back to nothing\n");
- return 0;
- //goto begin;
- }
- else
- {
- retry_counter--;
- if(raw_mode==mode_tcp)
- {
- send_raw(g_packet_info_send,0,0);
- }
- else if(raw_mode==mode_udp||raw_mode==mode_icmp)
- {
- send_bare_data(g_packet_info_send, (char*)"hello", strlen("hello"));
- }
- last_state_time=get_current_time();
- printf("retry send ack counter left:%d\n",retry_counter);
- }
- }
- if(client_current_state==client_heartbeat_sent&&get_current_time()-last_state_time>handshake_timeout)
- {
- if(retry_counter==0)
- {
- client_current_state=client_nothing;
- printf("state back to nothing\n");
- return 0;
- //goto begin;
- }
- else
- {
- retry_counter--;
- send_hb(g_packet_info_send,my_id,oppsite_id,const_id);
- last_state_time=get_current_time();
- printf("retry send heart_beat counter left:%d\n",retry_counter);
- printf("heartbeat sent <%x,%x>\n",oppsite_id,my_id);
- }
- }
- if(client_current_state==client_ready)
- {
- if(debug_mode)printf("time %lld %lld\n",get_current_time(),last_state_time);
- if(get_current_time()-last_hb_recv_time>heartbeat_timeout)
- {
- client_current_state=client_nothing;
- my_id=get_true_random_number_nz();
- printf("state back to nothing\n");
- return 0;
- }
- if(get_current_time()-last_hb_sent_time<heartbeat_interval)
- {
- return 0;
- }
- if(debug_mode)printf("heartbeat sent <%x,%x>\n",oppsite_id,my_id);
- send_hb(g_packet_info_send,my_id,oppsite_id,const_id);
- last_hb_sent_time=get_current_time();
- }
- return 0;
- }
- int keep_connection_server()
- {
- conv_manager.clean_inactive();
- //begin:
- if(debug_mode) printf("timer!\n");
- if(server_current_state==server_nothing)
- {
- return 0;
- }
- if(server_current_state==server_syn_ack_sent &&get_current_time()-last_state_time>handshake_timeout )
- {
- if(retry_counter==0)
- {
- server_current_state=server_nothing;
- printf("state back to nothing\n");
- }
- else
- {
- retry_counter--;
- send_raw(g_packet_info_send,0,0);
- last_state_time=get_current_time();
- printf("resend syn ack\n");
- }
- }
- if(server_current_state==server_heartbeat_sent &&get_current_time()-last_state_time>handshake_timeout)
- {
- if(retry_counter==0)
- {
- server_current_state=server_nothing;
- printf("state back to nothing\n");
- }
- else
- {
- retry_counter--;
- send_hb(g_packet_info_send,my_id,random(),const_id);
- last_state_time=get_current_time();
- printf("half heart beat sent<%x>\n",my_id);
- }
- }
- if(server_current_state==server_ready)
- {
- if( get_current_time()-last_hb_recv_time>heartbeat_timeout )
- {
- printf("%lld %lld",get_current_time(),last_state_time);
- server_current_state=server_nothing;
- printf("changed session id\n");
- my_id=get_true_random_number_nz();
- printf("state back to nothing\n");
- printf("changed state to server_nothing111\n");
- return 0;
- }
- if(get_current_time()-last_hb_sent_time<heartbeat_interval)
- {
- return 0;
- }
- send_hb(g_packet_info_send,my_id,oppsite_id,const_id);
- last_hb_sent_time=get_current_time();
- if(debug_mode) printf("heart beat sent<%x>\n",my_id);
- }
- }
- int set_timer(int epollfd,int &timer_fd)
- {
- int ret;
- epoll_event ev;
- itimerspec its;
- memset(&its,0,sizeof(its));
- if((timer_fd=timerfd_create(CLOCK_MONOTONIC,TFD_NONBLOCK)) < 0)
- {
- printf("timer_fd create error");
- exit(1);
- }
- its.it_interval.tv_nsec=timer_interval*1000ll*1000ll;
- its.it_value.tv_nsec=1; //imidiately
- timerfd_settime(timer_fd,0,&its,0);
- ev.events = EPOLLIN;
- ev.data.u64 = epoll_timer_fd_sn;
- epoll_ctl(epollfd, EPOLL_CTL_ADD, timer_fd, &ev);
- if (ret < 0) {
- printf("epoll_ctl return %d\n", ret);
- exit(-1);
- }
- return 0;
- }
- int client_on_raw_recv(packet_info_t &info,char * data,int data_len)
- {
- if(client_current_state==client_syn_sent )
- {
- if (raw_mode==mode_tcp&&!(info.syn==1&&info.ack==1&&data_len==0)) return 0;
- if(info.src_ip!=g_packet_info_send.dst_ip||info.src_port!=g_packet_info_send.dst_port)
- {
- printf("unexpected adress\n");
- return 0;
- }
- g_packet_info_send.ack_seq=info.seq+1;
- g_packet_info_send.psh=0;
- g_packet_info_send.syn=0;
- g_packet_info_send.ack=1;
- g_packet_info_send.seq+=1;
- printf("sent ack back\n");
- send_raw(g_packet_info_send,0,0);
- client_current_state=client_ack_sent;
- last_state_time=get_current_time();
- retry_counter=RETRY_TIME;
- printf("changed state to client_ack_sent\n");
- }
- if(client_current_state==client_ack_sent )
- {
- if(raw_mode==mode_tcp&& (info.syn==1||info.ack!=1 ||data_len==0))
- {
- printf("unexpected syn ack or other zero lenght packet\n");
- return 0;
- }
- if(info.src_ip!=g_packet_info_send.dst_ip||info.src_port!=g_packet_info_send.dst_port)
- {
- printf("unexpected adress\n");
- return 0;
- }
- if(data_len<hb_length||data[0]!='h')
- {
- printf("not a heartbeat\n");
- return 0;
- }
- oppsite_id= ntohl(* ((uint32_t *)&data[1]));
- printf("====first hb received %x\n==",oppsite_id);
- printf("changed state to client_heartbeat_sent\n");
- send_hb(g_packet_info_send,my_id,oppsite_id,const_id);
- client_current_state=client_heartbeat_sent;
- last_state_time=get_current_time();
- retry_counter=RETRY_TIME;
- }
- if(client_current_state==client_heartbeat_sent)
- {
- if((raw_mode==mode_tcp&&( info.syn==1||info.ack!=1 ) )||data_len==0 )
- {
- printf("unexpected syn ack or other zero lenght packet\n");
- return 0;
- }
- if(info.src_ip!=g_packet_info_send.dst_ip||info.src_port!=g_packet_info_send.dst_port)
- {
- printf("unexpected adress\n");
- return 0;
- }
- if(data_len<hb_length||data[0]!='h')
- {
- printf("not a heartbeat\n");
- return 0;
- }
- uint32_t tmp_my_id= ntohl(* ((uint32_t *)&data[1+sizeof(my_id)]));
- if(tmp_my_id!=my_id)
- {
- printf("auth fail\n");
- return 0;
- }
- uint32_t tmp_oppsite_session_id=ntohl(* ((uint32_t *)&data[1]));
- if(tmp_oppsite_session_id!=oppsite_id)
- {
- printf("oppsite id mismatch%x %x,ignore\n",tmp_oppsite_session_id,my_id);
- return 0;
- }
- printf("changed state to client_ready\n");
- client_current_state=client_ready;
- last_state_time=get_current_time();
- last_hb_recv_time=get_current_time();
- }
- if(client_current_state==client_ready )
- {
- if((raw_mode==mode_tcp&&( info.syn==1||info.ack!=1) )||data_len==0)
- {
- printf("unexpected syn ack");
- return 0;
- }
- if(info.src_ip!=g_packet_info_send.dst_ip||info.src_port!=g_packet_info_send.dst_port)
- {
- printf("unexpected adress\n");
- return 0;
- }
- if(data_len>=hb_length&&data[0]=='h')
- {
- if(debug_mode)printf("heart beat received\n");
- last_hb_recv_time=get_current_time();
- return 0;
- }
- else if(data_len>=sizeof(my_id)*3+1&&data[0]=='d')
- {
- printf("received a data from fake tcp,len:%d\n",data_len);
- uint32_t tmp_session_id= ntohl(* ((uint32_t *)&data[1+sizeof(my_id)]));
- if(tmp_session_id!=my_id)
- {
- printf("client session id mismatch%x %x,ignore\n",tmp_session_id,my_id);
- return 0;
- }
- uint32_t tmp_oppsite_session_id=ntohl(* ((uint32_t *)&data[1]));
- if(tmp_oppsite_session_id!=oppsite_id)
- {
- printf("server session id mismatch%x %x,ignore\n",tmp_oppsite_session_id,my_id);
- return 0;
- }
- last_hb_recv_time=get_current_time();
- uint32_t tmp_conv_id= ntohl(* ((uint32_t *)&data[1+sizeof(my_id)*2]));
- /*
- if(tmp_conv_id!=conv_id)
- {
- printf("conv id mismatch%x %x,ignore\n",tmp_oppsite_session_id,my_id);
- return 0;
- }*/
- if(!conv_manager.is_conv_used(tmp_conv_id))
- {
- printf("unknow conv %d,ignore\n",tmp_conv_id);
- return 0;
- }
- conv_manager.update_active_time(tmp_conv_id);
- uint64_t u64=conv_manager.find_u64_by_conv(tmp_conv_id);
- sockaddr_in tmp_sockaddr;
- memset(&tmp_sockaddr,0,sizeof(tmp_sockaddr));
- tmp_sockaddr.sin_family = AF_INET;
- tmp_sockaddr.sin_addr.s_addr=(u64>>32u);
- tmp_sockaddr.sin_port= htons(uint16_t((u64<<32u)>>32u));
- 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));
- if(ret<0)perror("ret<0");
- printf("%s :%d\n",inet_ntoa(tmp_sockaddr.sin_addr),ntohs(tmp_sockaddr.sin_port));
- printf("%d byte sent!!!!!!!!!!!!!!!!!!\n",ret);
- }
- return 0;
- }
- return 0;
- }
- int server_on_raw_recv(packet_info_t &info,char * data,int data_len)
- {
- if(server_current_state==server_nothing)
- {
- anti_replay.re_init();
- if(raw_mode==mode_icmp)
- {
- g_packet_info_send.src_port = info.src_port;;
- }
- g_packet_info_send.dst_port = info.src_port;
- g_packet_info_send.dst_ip = info.src_ip;
- if(raw_mode==mode_tcp)
- {
- if (!(info.syn == 1 && info.ack == 0 && data_len == 0))
- return 0;
- g_packet_info_send.ack_seq = info.seq + 1;
- g_packet_info_send.psh = 0;
- g_packet_info_send.syn = 1;
- g_packet_info_send.ack = 1;
- g_packet_info_send.seq = get_true_random_number_nz(); //not necessary to set
- printf("sent syn ack\n");
- send_raw(g_packet_info_send, 0, 0);
- printf("changed state to server_syn_ack_sent\n");
- server_current_state = server_syn_ack_sent;
- retry_counter = RETRY_TIME;
- last_state_time = get_current_time();
- }
- else if(raw_mode==mode_udp||raw_mode==mode_icmp)
- {
- if(data_len==strlen("hello")&& memcmp((char *)"hello",data,strlen("hello"))!=0)
- {
- //data[6]=0;
- printf("not a hello packet %d\n",data,data_len);
- return 0;
- }
- else
- {
- printf("got a hello packet\n");
- }
- printf("sent half heart_beat\n");
- //send_raw(g_packet_info_send, 0, 0);
- send_hb(g_packet_info_send,my_id,random(),const_id);
- printf("changed state to server_heartbeat_sent_sent\n");
- server_current_state = server_heartbeat_sent;
- retry_counter = RETRY_TIME;
- last_state_time = get_current_time();
- }
- }
- else if(server_current_state==server_syn_ack_sent)
- {
- if(raw_mode==mode_tcp&&!( info.syn==0&&info.ack==1 &&data_len==0)) return 0;
- if(info.src_ip!=g_packet_info_send.dst_ip||info.src_port!=g_packet_info_send.dst_port)
- {
- printf("unexpected adress\n");
- return 0;
- }
- g_packet_info_send.syn=0;
- g_packet_info_send.ack=1;
- g_packet_info_send.seq+=1;////////is this right?
- send_hb(g_packet_info_send,my_id,0,const_id); // send a hb immidately
- printf("changed state to server_heartbeat_sent\n");
- server_current_state=server_heartbeat_sent;
- last_state_time=get_current_time();
- retry_counter=RETRY_TIME;
- }
- else if(server_current_state==server_heartbeat_sent)//heart beat received
- {
- if(( raw_mode==mode_tcp&& (info.syn==1||info.ack!=1)) ||data_len==0) return 0;
- if(info.src_ip!=g_packet_info_send.dst_ip||info.src_port!=g_packet_info_send.dst_port)
- {
- printf("unexpected adress\n");
- return 0;
- }
- if(data_len<hb_length||data[0]!='h')
- {
- return 0;
- }
- uint32_t tmp_session_id= ntohl(* ((uint32_t *)&data[1+sizeof(my_id)]));
- uint32_t tmp_oppsite_const_id=ntohl(* ((uint32_t *)&data[1+sizeof(my_id)*2]));
- if(oppsite_const_id!=0&&tmp_oppsite_const_id!=oppsite_const_id)
- {
- conv_manager.clear();
- }
- oppsite_const_id=tmp_oppsite_const_id;
- printf("received hb %x %x\n",oppsite_id,tmp_session_id);
- if(tmp_session_id!=my_id)
- {
- printf("auth fail!!\n");
- return 0;
- }
- int tmp_oppsite_session_id= ntohl(* ((uint32_t *)&data[1]));
- oppsite_id=tmp_oppsite_session_id;
- send_hb(g_packet_info_send,my_id,oppsite_id,const_id);
- server_current_state=server_ready;
- last_state_time=get_current_time();
- last_hb_recv_time=get_current_time();
- //first_data_packet=1;
- printf("changed state to server_ready\n");
- }
- else if(server_current_state==server_ready)
- {
- if( (raw_mode==mode_tcp&&(info.syn==1||info.ack!=1)) ||data_len==0) return 0;
- if(info.src_ip!=g_packet_info_send.dst_ip||info.src_port!=g_packet_info_send.dst_port)
- {
- printf("unexpected adress\n");
- return 0;
- }
- if(data[0]=='h'&&data_len>=hb_length)
- {
- uint32_t tmp= ntohl(* ((uint32_t *)&data[1+sizeof(uint32_t)]));
- if(debug_mode)printf("received hb <%x,%x>\n",oppsite_id,tmp);
- last_hb_recv_time=get_current_time();
- return 0;
- }
- else if(data[0]=='d'&&data_len>=sizeof(my_id)*3+1)
- {
- uint32_t tmp_oppsite_session_id=ntohl(* ((uint32_t *)&data[1]));
- uint32_t tmp_session_id=ntohl(* ((uint32_t *)&data[1+sizeof(my_id)]));
- uint32_t tmp_conv_id=ntohl(* ((uint32_t *)&data[1+sizeof(my_id)*2]));
- if(tmp_session_id!=my_id)
- {
- printf("my id mismatch,ignore\n");
- return 0;
- }
- if(tmp_oppsite_session_id!=oppsite_id)
- {
- printf("oppsite id mismatch,ignore\n");
- return 0;
- }
- last_hb_recv_time=get_current_time();
- printf("<<<<conv:%u>>>>\n",tmp_conv_id);
- if(!conv_manager.is_conv_used(tmp_conv_id))
- {
- struct sockaddr_in remote_addr_in;
- socklen_t slen = sizeof(sockaddr_in);
- memset(&remote_addr_in, 0, sizeof(remote_addr_in));
- remote_addr_in.sin_family = AF_INET;
- remote_addr_in.sin_port = htons(remote_port);
- remote_addr_in.sin_addr.s_addr = inet_addr(remote_address);
- int new_udp_fd=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
- if(new_udp_fd<0)
- {
- printf("create udp_fd error");
- return -1;
- }
- set_buf_size(new_udp_fd);
- printf("created new udp_fd %d\n",new_udp_fd);
- int ret = connect(new_udp_fd, (struct sockaddr *) &remote_addr_in, slen);
- if(ret!=0)
- {
- printf("udp fd connect fail\n");
- close(new_udp_fd);
- return -1;
- }
- struct epoll_event ev;
- uint64_t u64=((u_int64_t(tmp_conv_id))<<32u)+(uint32_t)new_udp_fd;
- printf("u64: %ld\n",u64);
- ev.events = EPOLLIN;
- ev.data.u64 = u64;
- ret = epoll_ctl(epollfd, EPOLL_CTL_ADD, new_udp_fd, &ev);
- if (ret!= 0) {
- printf("add udp_fd error\n");
- close(new_udp_fd);
- return -1;
- }
- conv_manager.insert_conv(tmp_conv_id,u64);
- }
- uint64_t u64=conv_manager.find_u64_by_conv(tmp_conv_id);
- conv_manager.update_active_time(tmp_conv_id);
- int fd=int((u64<<32u)>>32u);
- printf("received a data from fake tcp,len:%d\n",data_len);
- int ret=send(fd,data+1+sizeof(my_id)*3,data_len -(1+sizeof(my_id)*3),0);
- printf("%d byte sent ,fd :%d\n ",ret,fd);
- if(ret<0)
- {
- perror("what happened????");
- }
- /*
- if(first_data_packet==0&& tmp_conv_id!=conv_id) //magic to find out which one is actually larger
- //consider 0xffffffff+1= 0x0 ,in this case 0x0 is "actually" larger
- {
- uint32_t smaller,bigger;
- smaller=min(conv_id,tmp_conv_id);//smaller in normal sense
- bigger=max(conv_id,tmp_conv_id);
- uint32_t distance=min(bigger-smaller,smaller+(0xffffffff-bigger+1));
- if(distance==bigger-smaller)
- {
- if(bigger==conv_id) //received_session_id is acutally bigger
- {
- printf("old_session_id ,ingored1\n");
- return 0;
- }
- }
- else
- {
- if(smaller==conv_id) //received_session_id is acutally bigger
- {
- printf("old_session_id ,ingored2\n");
- return 0;
- }
- }
- }
- first_data_packet=0;
- if(udp_fd==-1||tmp_conv_id!=conv_id)// this is first send or client changed session
- {
- int old_fd=udp_fd;
- struct sockaddr_in remote_addr_in;
- socklen_t slen = sizeof(sockaddr_in);
- memset(&remote_addr_in, 0, sizeof(remote_addr_in));
- remote_addr_in.sin_family = AF_INET;
- remote_addr_in.sin_port = htons(remote_port);
- remote_addr_in.sin_addr.s_addr = inet_addr(remote_address);
- udp_fd=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
- set_udp_buf_size(udp_fd);
- printf("created new udp_fd");
- int ret = connect(udp_fd, (struct sockaddr *) &remote_addr_in, slen);
- if(ret!=0)
- {
- printf("udp fd connect fail\n");
- }
- struct epoll_event ev;
- ev.events = EPOLLIN;
- epoll_udp_fd_sn+=256;
- ev.data.u64 = epoll_udp_fd_sn;
- ret = epoll_ctl(epollfd, EPOLL_CTL_ADD, udp_fd, &ev);
- if (ret!= 0) {
- printf("add udp_fd error\n");
- exit(-1);
- }
- if(old_fd!=-1)
- {
- epoll_ctl(epollfd, EPOLL_CTL_DEL, old_fd, 0);
- close(old_fd);
- }
- }
- if(tmp_conv_id!=conv_id)
- {
- conv_id=tmp_conv_id;
- }
- */
- }
- }
- return 0;
- }
- int client_event_loop()
- {
- char buf[buf_len];
- char raw_recv_buf3[buf_len];
- int i, j, k;int ret;
- init_raw_socket();
- my_id=get_true_random_number_nz();
- conv_num=get_true_random_number_nz();
- //init_filter(source_port);
- g_packet_info_send.dst_ip=inet_addr(remote_address);
- g_packet_info_send.dst_port=remote_port;
- //g_packet_info.src_ip=inet_addr(source_address);
- //g_packet_info.src_port=source_port;
- udp_fd=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
- set_buf_size(udp_fd);
- int yes = 1;
- //setsockopt(udp_fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes));
- struct sockaddr_in local_me;
- socklen_t slen = sizeof(sockaddr_in);
- memset(&local_me, 0, sizeof(local_me));
- local_me.sin_family = AF_INET;
- local_me.sin_port = htons(local_port);
- local_me.sin_addr.s_addr = inet_addr(local_address);
- if (bind(udp_fd, (struct sockaddr*) &local_me, slen) == -1) {
- perror("socket bind error");
- exit(1);
- }
- setnonblocking(udp_fd);
- int epollfd = epoll_create1(0);
- const int max_events = 4096;
- struct epoll_event ev, events[max_events];
- if (epollfd < 0) {
- printf("epoll return %d\n", epollfd);
- exit(-1);
- }
- ev.events = EPOLLIN;
- ev.data.u64 = epoll_udp_fd_sn;
- ret = epoll_ctl(epollfd, EPOLL_CTL_ADD, udp_fd, &ev);
- if (ret!=0) {
- printf("add udp_listen_fd error\n");
- exit(-1);
- }
- ev.events = EPOLLIN;
- ev.data.u64 = epoll_raw_recv_fd_sn;
- ret = epoll_ctl(epollfd, EPOLL_CTL_ADD, raw_recv_fd, &ev);
- if (ret!= 0) {
- printf("add raw_fd error\n");
- exit(-1);
- }
- ////add_timer for fake_tcp_keep_connection_client
- //sleep(10);
- memset(&udp_old_addr_in,0,sizeof(sockaddr_in));
- int unbind=1;
- int timer_fd;
- set_timer(epollfd,timer_fd);
- while(1)////////////////////////
- {
- int nfds = epoll_wait(epollfd, events, max_events, 180 * 1000);
- if (nfds < 0) { //allow zero
- printf("epoll_wait return %d\n", nfds);
- exit(-1);
- }
- int n;
- for (n = 0; n < nfds; ++n) {
- if (events[n].data.u64 == epoll_raw_recv_fd_sn)
- {
- iphdr *iph;tcphdr *tcph;char* data;int data_len;
- if(recv_raw(g_packet_info_recv,data,data_len)!=0)
- {
- continue;
- }
- int new_len=data_len;
- memcpy(raw_recv_buf3,data,new_len); //for safety,copy to a new buffer,will remove later
- if(data_len!=0)
- {
- if(pre_recv(raw_recv_buf3,new_len)<0)
- continue;
- }
- client_on_raw_recv(g_packet_info_recv,raw_recv_buf3,new_len);
- }
- if(events[n].data.u64 ==epoll_timer_fd_sn)
- {
- //printf("timer!\n");
- //fflush(stdout);
- uint64_t value;
- read(timer_fd, &value, 8);
- keep_connection_client();
- }
- if (events[n].data.u64 == epoll_udp_fd_sn)
- {
- socklen_t recv_len;
- struct sockaddr_in udp_new_addr_in;
- if ((recv_len = recvfrom(udp_fd, buf, buf_len, 0,
- (struct sockaddr *) &udp_new_addr_in, &slen)) == -1) {
- printf("recv_from error");
- exit(1);
- };
- printf("Received packet from %s:%d,len: %d\n", inet_ntoa(udp_new_addr_in.sin_addr),
- ntohs(udp_new_addr_in.sin_port),recv_len);
- /*
- if(udp_old_addr_in.sin_addr.s_addr==0&&udp_old_addr_in.sin_port==0)
- {
- memcpy(&udp_old_addr_in,&udp_new_addr_in,sizeof(udp_new_addr_in));
- }
- else if(udp_new_addr_in.sin_addr.s_addr!=udp_old_addr_in.sin_addr.s_addr
- ||udp_new_addr_in.sin_port!=udp_old_addr_in.sin_port)
- {
- if(get_current_time()- last_udp_recv_time <udp_timeout)
- {
- printf("new <ip,port> connected in,ignored,bc last connection is still active\n");
- continue;
- }
- else
- {
- printf("new <ip,port> connected in,accpeted\n");
- memcpy(&udp_old_addr_in,&udp_new_addr_in,sizeof(udp_new_addr_in));
- conv_id++;
- }
- }*/
- //last_udp_recv_time=get_current_time();
- uint64_t u64=((uint64_t(udp_new_addr_in.sin_addr.s_addr))<<32u)+ntohs(udp_new_addr_in.sin_port);
- uint32_t conv;
- if(!conv_manager.is_u64_used(u64))
- {
- printf("new connection!!!!!!!!!!!\n");
- conv=conv_manager.get_new_conv();
- conv_manager.insert_conv(conv,u64);
- }
- else
- {
- conv=conv_manager.find_conv_by_u64(u64);
- }
- conv_manager.update_active_time(conv);
- if(client_current_state==client_ready)
- {
- send_data(g_packet_info_send,buf,recv_len,my_id,oppsite_id,conv);
- }
- }
- }
- }
- return 0;
- }
- int server_event_loop()
- {
- char buf[buf_len];
- char raw_recv_buf3[buf_len];
- conv_manager.set_clear_function(server_clear);
- int i, j, k;int ret;
- g_packet_info_send.src_ip=inet_addr(local_address);
- g_packet_info_send.src_port=local_port;
- if(raw_mode==mode_tcp)
- {
- bind_fd=socket(AF_INET,SOCK_STREAM,0);
- }
- else if(raw_mode==mode_udp||raw_mode==mode_icmp)
- {
- bind_fd=socket(AF_INET,SOCK_DGRAM,0);
- }
- struct sockaddr_in temp_bind_addr;
- bzero(&temp_bind_addr, sizeof(temp_bind_addr));
- temp_bind_addr.sin_family = AF_INET;
- temp_bind_addr.sin_port = htons(local_port);
- temp_bind_addr.sin_addr.s_addr = inet_addr(local_address);
- if (bind(bind_fd, (struct sockaddr*)&temp_bind_addr, sizeof(temp_bind_addr)) !=0)
- {
- printf("bind fail\n");
- exit(-1);
- }
- if(raw_mode==mode_tcp)
- {
- if(listen(bind_fd, SOMAXCONN) != 0 )
- {
- printf("listen fail\n");
- exit(-1);
- }
- }
- init_raw_socket();
- init_filter(local_port);
- epollfd = epoll_create1(0);
- const int max_events = 4096;
- struct epoll_event ev, events[max_events];
- if (epollfd < 0) {
- printf("epoll return %d\n", epollfd);
- exit(-1);
- }
- ev.events = EPOLLIN;
- ev.data.u64 = epoll_raw_recv_fd_sn;
- ret = epoll_ctl(epollfd, EPOLL_CTL_ADD, raw_recv_fd, &ev);
- if (ret!= 0) {
- printf("add raw_fd error\n");
- exit(-1);
- }
- int timer_fd;
- set_timer(epollfd,timer_fd);
- while(1)////////////////////////
- {
- int nfds = epoll_wait(epollfd, events, max_events, 180 * 1000);
- if (nfds < 0) { //allow zero
- printf("epoll_wait return %d\n", nfds);
- exit(-1);
- }
- int n;
- const int MTU=1440;
- for (n = 0; n < nfds; ++n)
- {
- if ((events[n].data.u64 >>32u) > 0u)
- {
- uint32_t conv_id=events[n].data.u64>>32u;
- if(!conv_manager.is_u64_used(events[n].data.u64))
- {
- printf("conv no longer exists");
- continue;
- }
- int fd=int((events[n].data.u64<<32u)>>32u);
- int recv_len=recv(fd,buf,buf_len,0);
- printf("received a packet from udp_fd,len:%d\n",recv_len);
- if(recv_len<0)
- {
- printf("continue\n");
- perror("wtf?");
- continue;
- //return 0;
- }
- conv_manager.update_active_time(conv_id);
- if(server_current_state==server_ready)
- {
- send_data(g_packet_info_send,buf,recv_len,my_id,oppsite_id,conv_id);
- printf("send !!!!!!!!!!!!!!!!!!");
- }
- }
- //printf("%d %d %d %d\n",timer_fd,raw_recv_fd,raw_send_fd,n);
- if (events[n].data.u64 == epoll_timer_fd_sn)
- {
- uint64_t value;
- read(timer_fd, &value, 8);
- keep_connection_server();
- }
- if (events[n].data.u64 == epoll_raw_recv_fd_sn)
- {
- iphdr *iph;tcphdr *tcph;char* data;int data_len;
- if(recv_raw(g_packet_info_recv,data,data_len)!=0)
- {
- continue;
- }
- int new_len=data_len;
- memcpy(raw_recv_buf3,data,new_len);
- if(data_len!=0)
- {
- //if(raw_mode==mode_tcp || ((raw_mode==mode_udp||raw_mode==mode_icmp) &&server_current_state!=server_nothing ))
- //{
- if(pre_recv(raw_recv_buf3,new_len)<0)
- continue;
- //}
- }
- server_on_raw_recv(g_packet_info_recv,raw_recv_buf3,new_len);
- }
- }
- }
- return 0;
- }
- int main(int argc, char *argv[])
- {
- srand(time(0));
- if(raw_mode==mode_tcp)
- {
- g_packet_info_send.protocol=IPPROTO_TCP;
- }
- else if(raw_mode==mode_udp)
- {
- g_packet_info_send.protocol=IPPROTO_UDP;
- }
- else if(raw_mode==mode_icmp)
- {
- g_packet_info_send.protocol=IPPROTO_ICMP;
- }
- init_random_number_fd();
- const_id=get_true_random_number_nz();
- anti_replay_seq=get_true_random_number_nz();
- g_packet_info_send.ack_seq=get_true_random_number_nz();
- g_packet_info_send.seq=get_true_random_number_nz();
- int i, j, k;
- signal(SIGCHLD, handler);
- process_arg(argc,argv);
- if(prog_mode==client_mode)
- {
- for(int i=0;i<16;i++)
- {
- key_me[i]=key[i];
- key_oppsite[i]=key[i]+1;
- }
- client_event_loop();
- }
- else
- {
- for(int i=0;i<16;i++)
- {
- key_me[i]=key[i]+1;
- key_oppsite[i]=key[i];
- }
- server_event_loop();
- }
- return 0;
- }
|