From 5fb31e15463d1939c1c69c54f27fbf6c1e7f26af Mon Sep 17 00:00:00 2001 From: Sadagopan Date: Mon, 31 Aug 2015 11:09:53 +0530 Subject: [PATCH] Aug 31 11:05AM --- Documentation | 9 +- client.cpp | 1 + hss.cpp | 13 +-- mme.cpp | 212 ++++++++++++++++++++++++++++++++++++++++- mme.h | 43 ++++++++- mme_server.cpp | 239 ++++++++--------------------------------------- mme_server.h | 45 ++------- packet.cpp | 18 ++++ packet.h | 5 +- pgwc.cpp | 4 +- pgwu.cpp | 4 +- ran.cpp | 18 ++-- server.cpp | 2 - server.h | 2 - sgw_server.cpp | 10 -- sgw_server.h | 3 - sgwu.cpp | 4 +- sink_monitor.cpp | 3 - sink_monitor.h | 3 - sink_server.cpp | 2 - ue.cpp | 7 +- utils.cpp | 23 ++++- utils.h | 2 + 23 files changed, 362 insertions(+), 310 deletions(-) diff --git a/Documentation b/Documentation index cba96cf..99d3b97 100644 --- a/Documentation +++ b/Documentation @@ -1,10 +1,13 @@ +Problems facing so-far: + +- Too many open files (For connections < 100) + Things to do: - Encryption - Integrity check -- Detach procedure -- Running program for a fixed amount of time - Employing Distribution function +- Employing APN during registration Doubts in the implementation: @@ -84,7 +87,7 @@ When the authentication match was successful at the MME, the RAN is supposed to MME to SGW:(Plain) MME GTP-C TEID -UE IMSI +UE IMSI (UE NUM is made to send instead) UE Bearer ID Step-2: diff --git a/client.cpp b/client.cpp index 0de7489..993bb90 100644 --- a/client.cpp +++ b/client.cpp @@ -5,6 +5,7 @@ Client::Client(){ report_error(client_socket); client_addr = allocate_str_mem(INET_ADDRSTRLEN); server_addr = allocate_str_mem(INET_ADDRSTRLEN); + setsockopt(client_socket, SOL_SOCKET, SO_REUSEADDR, &g_reuse, sizeof(int)); signal(SIGPIPE, SIG_IGN); //status = 0; //setsockopt(client_socket, SOL_SOCKET, SO_RCVTIMEO, (char*)&g_timeout, sizeof(timeval)); diff --git a/hss.cpp b/hss.cpp index bb9e8a3..a414e7a 100644 --- a/hss.cpp +++ b/hss.cpp @@ -14,14 +14,11 @@ void* multithreading_func(void *arg){ hss.client_num = mme.num; hss.connect_with_client(); hss.read_data(); - memcpy(&type, hss.pkt.data, sizeof(int)); - if(type == 1){ - memcpy(&imsi, hss.pkt.data + sizeof(type), sizeof(imsi)); - memcpy(&msisdn, hss.pkt.data + sizeof(type) + sizeof(imsi), sizeof(msisdn)); - authenticate_query(imsi, msisdn, hss.pkt); - hss.pkt.make_data_packet(); - hss.write_data(); - } + memcpy(&imsi, hss.pkt.data, sizeof(unsigned long long)); + memcpy(&msisdn, hss.pkt.data + sizeof(unsigned long long), sizeof(unsigned long long)); + authenticate_query(imsi, msisdn, hss.pkt); + hss.pkt.make_data_packet(); + hss.write_data(); mysql_thread_end(); } diff --git a/mme.cpp b/mme.cpp index 685f8f2..42c2370 100644 --- a/mme.cpp +++ b/mme.cpp @@ -18,21 +18,225 @@ MME::MME(){ reply = allocate_str_mem(BUFFER_SIZE); } -void MME::set_ue_num(){ +void MME::set_cteid(){ + tun_data.mme_cteid = generate_cteid(ue_num); +} + +uint16_t MME::generate_cteid(int &ue_number){ + return ue_number; } -void MME::set_cteid(){ +void MME::set_bearer_id(){ - cteid = generate_cteid(ue_num); + bearer_id = generate_bearer_id(ue_num); } -uint16_t MME::generate_cteid(int &ue_number){ +int MME::generate_bearer_id(int &ue_number){ return ue_number; } +void MME::set_sgw(){ + + tun_data.sgw_port = g_sgw1_port; + strcpy(tun_data.sgw_addr, g_sgw1_addr); +} + +void MME::set_pgw(){ + + tun_data.pgw_port = g_pgw_port; + strcpy(tun_data.pgw_addr, g_pgw_addr); +} + +void MME::startup_mme_server(ClientDetails &entity){ + + mme_server.fill_server_details(g_freeport, g_mme_addr); + mme_server.bind_server(); + mme_server.client_sock_addr = entity.client_sock_addr; + mme_server.client_num = ue_num; + mme_server.connect_with_client(); +} + +void MME::set_ue_num(){ + + ue_num = mme_server.client_num; +} + +void MME::attach_req_from_ue(){ + + mme_server.read_data(); +} + +void MME::setup_hss_client(){ + + to_hss.bind_client(); + to_hss.fill_server_details(g_hss_port, g_hss_addr); + to_hss.connect_with_server(ue_num); +} + +void MME::fetch_ue_data(){ + + to_hss.pkt.clear_data(); + to_hss.pkt.fill_data(0, mme_server.pkt.data_len, mme_server.pkt.data); + to_hss.pkt.make_data_packet(); + to_hss.write_data(); + to_hss.read_data(); + memcpy(&autn_num, to_hss.pkt.data, sizeof(unsigned long long)); + memcpy(&rand_num, to_hss.pkt.data + sizeof(unsigned long long), sizeof(unsigned long long)); + memcpy(&autn_xres, to_hss.pkt.data + 2 * sizeof(unsigned long long), sizeof(unsigned long long)); +} + +void MME::authenticate_ue(){ + + mme_server.pkt.clear_data(); + mme_server.pkt.fill_data(0, 2 * sizeof(unsigned long long), to_hss.pkt.data); + mme_server.pkt.make_data_packet(); + mme_server.write_data(); + mme_server.read_data(); + memcpy(&autn_res, mme_server.pkt.data, sizeof(unsigned long long)); + if(autn_xres == autn_res){ + strcpy(reply, "OK"); + mme_server.pkt.clear_data(); + mme_server.pkt.fill_data(0, strlen(reply), reply); + mme_server.pkt.make_data_packet(); + mme_server.write_data(); + cout<<"Authentication is successful for UE - "< g_bearer_table; -double g_req_duration; -time_t g_start_time; +void* process_reqs(void *arg){ + ClientDetails entity; + MME mme; -Gateways::Gateways(){ - sgw_addr = allocate_str_mem(INET_ADDRSTRLEN); - pgw_addr = allocate_str_mem(INET_ADDRSTRLEN); + entity = *((ClientDetails*)arg); + mme.startup_mme_server(entity); + mme.set_ue_num(); + attach_ue(mme); + detach_ue(mme); } -void Gateways::set_sgw(){ - sgw_port = g_sgw1_port; - strcpy(sgw_addr, g_sgw1_addr); -} - -void Gateways::set_pgw(){ - pgw_port = g_pgw_port; - strcpy(pgw_addr, g_pgw_addr); -} - -Gateways::~Gateways(){ - free(sgw_addr); - free(pgw_addr); -} - -void Tunnel::set_mme_cteid(int ue_num){ - mme_cteid = ue_num; -} - -void *multithreading_func(void *arg){ - int type; - - ClientDetails ue = *(ClientDetails*)arg; - Server mme_server; - mme_server.fill_server_details(g_freeport, g_mme_addr); - mme_server.bind_server(); - //setsockopt(mme_server.server_socket, SOL_SOCKET, SO_RCVTIMEO, (char*)&g_timeout, sizeof(timeval)); - mme_server.client_sock_addr = ue.client_sock_addr; - mme_server.client_num = ue.num; - mme_server.connect_with_client(); - mme_server.read_data(); - Client to_hss; - Client to_sgw; - Tunnel tun; - to_hss.bind_client(); - to_hss.fill_server_details(g_hss_port, g_hss_addr); - to_hss.connect_with_server(ue.num); - memcpy(&type, mme_server.pkt.data, sizeof(type)); - if(type == 1){ - authenticate(mme_server, to_hss); - setup_tunnel(mme_server, to_sgw, mme_server.client_num, tun); - detach(mme_server, to_sgw, tun, mme_server.client_num); - } - // time_check(g_start_time, g_req_duration); -} - -void authenticate(Server &mme_server, Client &to_hss){ - unsigned long long xres, res; - const char *msg; - to_hss.pkt.clear_data(); - to_hss.pkt.fill_data(0, mme_server.pkt.data_len, (const char*)mme_server.pkt.data); - to_hss.pkt.make_data_packet(); - to_hss.write_data(); - to_hss.read_data(); - memcpy(&xres, to_hss.pkt.data + 2*sizeof(unsigned long long), sizeof(xres)); - mme_server.pkt.clear_data(); - mme_server.pkt.fill_data(0, 2*sizeof(unsigned long long), (const char*)to_hss.pkt.data); - mme_server.pkt.make_data_packet(); - mme_server.write_data(); - mme_server.read_data(); - memcpy(&res, mme_server.pkt.data, sizeof(res)); - if(xres == res){ - msg = "OK"; - mme_server.pkt.clear_data(); - mme_server.pkt.fill_data(0, strlen(msg), msg); - mme_server.pkt.make_data_packet(); - mme_server.write_data(); - } -} - -void setup_tunnel(Server &mme_server, Client &to_sgw, int &ue_num, Tunnel &tun){ - Gateways gw; - char *ue_addr = allocate_str_mem(IP_MAXPACKET); - gw.set_sgw(); - gw.set_pgw(); - tun.set_mme_cteid(ue_num); - mme_server.read_data(); - to_sgw.bind_client(); - to_sgw.fill_server_details(gw.sgw_port, gw.sgw_addr); - to_sgw.connect_with_server(ue_num); - // to_sgw.pkt.fill_ip_hdr(g_mme_addr, gw.sgw_addr); - // to_sgw.pkt.fill_udp_addr(); - create_session(to_sgw, ue_num, tun); - memcpy(&tun.enodeb_uteid, mme_server.pkt.data, sizeof(uint16_t)); - modify_session(to_sgw, ue_num, tun); - memcpy(&tun.sgw_uteid, to_sgw.pkt.data, sizeof(uint16_t)); - memcpy(ue_addr, to_sgw.pkt.data + sizeof(uint16_t), INET_ADDRSTRLEN); - cout<<"UE addr is "< g_bearer_table; -extern double g_req_duration; -extern time_t g_start_time; - -struct Gateways{ - int sgw_port; - int pgw_port; - char *sgw_addr; - char *pgw_addr; - - Gateways(); - void set_sgw(); - void set_pgw(); - ~Gateways(); -}; - -struct Tunnel{ - uint16_t mme_cteid; - uint16_t sgw_cteid; - uint16_t enodeb_uteid; - uint16_t sgw_uteid; - - void set_mme_cteid(int); -}; - -void* multithreading_func(void*); -void authenticate(Server&, Client&); -void setup_tunnel(Server&, Client&, int&, Tunnel&); -void detach(Server&, Client&, Tunnel&, int&); -void create_session(Client&, int, Tunnel&); -void modify_session(Client&, int, Tunnel&); -void set_bearer_id(int); -int generate_bearer_id(int); -void detach_req_from_ran(Server&, int&); -void delete_session_req_to_sgw(Server&, Client&, Tunnel&); -void delete_bearer_id(int&); -void delete_session_res_from_sgw(Server&, Client&, int&); -void detach_res(Server&, int&); -void startup_mme(char *argv[]); +void* process_reqs(void*); +void attach_ue(MME&); +void recv_req_from_ue(MME&); +void authenticate_ue(MME&); +void setup_tunnel(MME&); +void detach_ue(MME&); #endif //MME_SERVER_H \ No newline at end of file diff --git a/packet.cpp b/packet.cpp index da58106..d92dac4 100644 --- a/packet.cpp +++ b/packet.cpp @@ -227,6 +227,24 @@ void Packet::clear_packet(){ packet_len = 0; } +void Packet::copy_pkts(Packet &dst, Packet &src){ + + dst.clear_data(); + dst.fill_data(0, src.data_len, src.data); +} + +void Packet::copy_frompkt(Packet &src){ + + this->clear_data(); + this->fill_data(0, src.data_len, src.data); +} + +void Packet::copy_topkt(Packet &dst){ + + dst.clear_data(); + dst.fill_data(0, data_len, data); +} + Packet::~Packet(){ free(src_ip); free(dst_ip); diff --git a/packet.h b/packet.h index 5205fb8..3b68d23 100644 --- a/packet.h +++ b/packet.h @@ -59,7 +59,10 @@ struct Packet{ void decap(); void clear_data(); void clear_packet(); - + void copy_pkts(Packet&, Packet&); + void copy_frompkt(Packet&); + void copy_topkt(Packet&); + ~Packet(); }; diff --git a/pgwc.cpp b/pgwc.cpp index 50a7694..a3ddb5e 100644 --- a/pgwc.cpp +++ b/pgwc.cpp @@ -62,9 +62,9 @@ void PGWc::set_cteid(){ cteid = generate_cteid(ue_num); } -uint16_t PGWc::generate_cteid(int &ue_num){ +uint16_t PGWc::generate_cteid(int &ue_number){ - return ue_num; + return ue_number; } void PGWc::create_session_response_to_sgw(Server &pgw_server, uint16_t &uteid){ diff --git a/pgwu.cpp b/pgwu.cpp index 3139142..008278f 100644 --- a/pgwu.cpp +++ b/pgwu.cpp @@ -17,9 +17,9 @@ PGWu::PGWu(){ ue_ip = allocate_str_mem(INET_ADDRSTRLEN); } -uint16_t PGWu::generate_uteid(int &ue_num){ +uint16_t PGWu::generate_uteid(int &ue_number){ - return ue_num; + return ue_number; } void PGWu::configure_raw_client(){ diff --git a/ran.cpp b/ran.cpp index 171d359..a852824 100644 --- a/ran.cpp +++ b/ran.cpp @@ -21,7 +21,6 @@ void* monitor_traffic(void *arg){ int status; enodeb.attach_to_tun(); - // while(g_tun_table.empty()); while(1){ FD_ZERO(&read_set); FD_SET(enodeb.tun_fd, &read_set); @@ -51,8 +50,9 @@ void* monitor_traffic(void *arg){ } void* generate_traffic(void *arg){ - int ue_num = *(int*)arg; + int ue_num; + ue_num = *((int*)arg); while(1){ Client to_mme; @@ -61,9 +61,9 @@ void* generate_traffic(void *arg){ to_mme.connect_with_server(ue_num); UserEquipment ue(ue_num); attach(ue, to_mme); - send_traffic(ue); - detach(ue, to_mme); - time_check(g_start_time, g_req_duration); + // send_traffic(ue); + // detach(ue, to_mme); + // time_check(g_start_time, g_req_duration); } } @@ -74,10 +74,10 @@ void attach(UserEquipment &ue, Client &to_mme){ string ue_ip_str; ue.authenticate(to_mme); - enodeb_uteid = enodeb.generate_uteid(ue.num); - ue.setup_tunnel(to_mme, enodeb_uteid, tun_data.sgw_uteid, tun_data.sgw_port, tun_data.sgw_addr); - ue_ip_str.assign(ue.ip_addr); - g_tun_table[ue_ip_str] = tun_data; + // enodeb_uteid = enodeb.generate_uteid(ue.num); + // ue.setup_tunnel(to_mme, enodeb_uteid, tun_data.sgw_uteid, tun_data.sgw_port, tun_data.sgw_addr); + // ue_ip_str.assign(ue.ip_addr); + // g_tun_table[ue_ip_str] = tun_data; } void send_traffic(UserEquipment &ue){ diff --git a/server.cpp b/server.cpp index c6ac45d..0370a04 100644 --- a/server.cpp +++ b/server.cpp @@ -1,7 +1,5 @@ #include "server.h" -int g_reuse = 1; - Server::Server(){ server_socket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); report_error(server_socket); diff --git a/server.h b/server.h index 21e42f5..49a5c05 100644 --- a/server.h +++ b/server.h @@ -5,8 +5,6 @@ #include "packet.h" #include "thread_pool.h" -extern int g_reuse; - class Server{ public: int status; diff --git a/sgw_server.cpp b/sgw_server.cpp index 9e1adb6..94b77b3 100644 --- a/sgw_server.cpp +++ b/sgw_server.cpp @@ -1,8 +1,5 @@ #include "sgw_server.h" -double g_req_duration; -time_t g_start_time; - void* process_traffic(void *arg){ int type; ClientDetails entity = *(ClientDetails*)arg; @@ -88,17 +85,10 @@ void handle_udata(Server &sgw_server){ } } -void startup_sgw_server(char *argv[]){ - - g_start_time = time(0); - //g_req_duration = atof(argv[2]); -} - int main(int argc, char *argv[]){ Server sgw_server; usage(argc, argv); - startup_sgw_server(argv); sgw_server.begin_thread_pool(2 * atoi(argv[1]), process_traffic); sgw_server.fill_server_details(g_sgw1_port, g_sgw1_addr); sgw_server.bind_server(); diff --git a/sgw_server.h b/sgw_server.h index 759f5d0..c3e12ec 100644 --- a/sgw_server.h +++ b/sgw_server.h @@ -9,9 +9,6 @@ #include "sgwc.h" #include "sgwu.h" -extern double g_req_duration; -extern time_t g_start_time; - void* process_traffic(void*); void handle_cdata(Server&); void handle_udata(Server&); diff --git a/sgwu.cpp b/sgwu.cpp index dfdbeda..6906803 100644 --- a/sgwu.cpp +++ b/sgwu.cpp @@ -20,9 +20,9 @@ SGWu::SGWu(){ type = 2; } -uint16_t SGWu::generate_uteid(int &ue_num){ +uint16_t SGWu::generate_uteid(int &ue_number){ - return ue_num; + return ue_number; } void SGWu::set_uteid(){ diff --git a/sink_monitor.cpp b/sink_monitor.cpp index 3ed2a1a..931abd2 100644 --- a/sink_monitor.cpp +++ b/sink_monitor.cpp @@ -3,8 +3,6 @@ Client SinkMonitor::to_pgw; int SinkMonitor::tun_fd; const char* SinkMonitor::tun_name; -double g_req_duration; -time_t g_start_time; SinkMonitor::SinkMonitor(){ @@ -150,6 +148,5 @@ void* start_monitor(void *arg){ sink_monitor.write_tun(); //cout<<"Successfully written to private sink"< &tnum, vector &tid){ - g_start_time = time(0); g_total_connections = atoi(argv[1]); - // g_req_duration = atof(argv[2]); tnum.resize(g_total_connections); tid.resize(g_total_connections); } diff --git a/ue.cpp b/ue.cpp index 47a6ef0..5b239e9 100644 --- a/ue.cpp +++ b/ue.cpp @@ -7,7 +7,6 @@ UserEquipment::UserEquipment(int ue_num){ imsi = key*1000; msisdn = 9000000000 + key; - cout<<"IMSI is "<