Skip to content

Commit

Permalink
July 17 7:10PM
Browse files Browse the repository at this point in the history
  • Loading branch information
Your Name committed Jul 17, 2015
1 parent 6399348 commit 034a9a2
Show file tree
Hide file tree
Showing 9 changed files with 213 additions and 29 deletions.
95 changes: 87 additions & 8 deletions mme.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,27 @@
#include "mme.h"

void Gateways::set_gateways(){
sgw1 = SGW1;
sgw2 = SGW2;
sgw3 = SGW3;
pgw = PGW;
Gateways::Gateways(){
sgw_addr = allocate_str_mem(INET_ADDRSTRLEN);
pgw_addr = allocate_str_mem(INET_ADDRSTRLEN);
}

void set_sgw(){
sgw_port = g_sgw1_port;
sgw_addr = g_sgw1_addr;
}

void set_pgw(){
pgw_port = g_pgw_port;
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){
Expand All @@ -23,7 +40,7 @@ void *multithreading_func(void *arg){
memcpy(&type, mme.pkt.data, sizeof(type));
if(type == 1){
authenticate(mme, to_hss);
setup_tunnel(mme, to_hss);
setup_tunnel(mme, to_hss, ue_num);
}
}

Expand Down Expand Up @@ -51,16 +68,78 @@ void authenticate(Server &mme, Client &to_hss){
}
}

void setup_tunnel(Server &mme, Client &to_hss){
void setup_tunnel(Server &mme, Client &to_hss, int ue_num){
Gateways gw;
Tunnel tun;
char *ue_addr = allocate_str_mem(IP_MAXPACKET);
gw.set_sgw();
gw.set_pgw();
tun.set_mme_cteid(ue_num);
mme.read_data();
Client to_sgw;
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_address, gw.sgw_addr);
to_sgw.pkt.fill_udp_addr();
to_sgw.pkt.clear_data();
create_sesion(to_sgw, ue_num, tun);
memcpy(&tun.enodeb_uteid, mme.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);
mme.pkt.clear_data();
mme.pkt.fill_data(0, sizeof(uint16_t), tun.sgw_uteid);
mme.pkt.fill_data(sizeof(uint16_t), INET_ADDRSTRLEN, ue_addr);
mme.add_data();
mme.write_data();
cout<<"Tunnel Setup successful for client - "<<ue_num<<endl;
}

void create_sesion(Client &to_sgw, int ue_num, Tunnel &tun){
int type = 1;
char *reply = allocate_str_mem(IP_MAXPACKET);
set_bearer_id(ue_num);
to_sgw.pkt.clear_data();
to_sgw.pkt.fill_data(0, sizeof(int), type);
to_sgw.pkt.fill_data(sizeof(int), sizeof(int), ue_num);
to_sgw.pkt.fill_data(2 * sizeof(int), sizeof(int), g_bearer_table[ue_num]);
to_sgw.pkt.fill_data(3 * sizeof(int), sizeof(uint16_t), tun.mme_cteid);
to_sgw.pkt.add_data();
to_sgw.pkt.write_data();
to_sgw.read_data();
memcpy(tun.dst_cteid, to_sgw.pkt.data, sizeof(uint16_t));
memcpy(reply, to_sgw.pkt.data + sizeof(uint16_t), to_sgw.pkt.data_len - sizeof(uint16_t));
if(strcmp((const char*)reply, "OK"){
cout<<"Create Session Request is successful for this client - "<<ue_num<<endl;
}
}

void modify_session(Client &to_sgw, int ue_num, Tunnel &tun){
char *reply;
ue_addr = allocate_str_mem(INET_ADDRSTRLEN);
reply = allocate_str_mem(INET_ADDRSTRLEN);
to_sgw.pkt.clear_data();
to_sgw.pkt.fill_data(0, sizeof(uint16_t), tun.enodeb_uteid);
to_sgw.pkt.fill_gtpc_hdr(ue_num);
to_sgw.pkt.add_data();
to_sgw.write_data();
to_sgw.read_data();
to_sgw.pkt.rem_gtpu_hdr();
memcpy(reply, to_sgw.pkt.data + sizeof(uint16_t) + INET_ADDRSTRLEN, to_sgw.pkt.data_len - sizeof(uint16_t) - INET_ADDRSTRLEN);
if(strcmp((const char*)reply, "OK")){
cout<<"Modify Session Request is successful for this client - "<<ue_num<<endl;
}
}

void set_bearer_id(int ue_num){
g_bearer_table[ue_num] = generate_bearer_id(ue_num);
}

int generate_bearer_id(int ue_num){
return ue_num;
}

int main(){
Gateways::set_gateways();
Server mme;
mme.fill_server_details(g_mme_port, g_mme_address);
mme.bind_server();
Expand Down
30 changes: 24 additions & 6 deletions mme.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,35 @@
#include "server.h"
#include "client.h"

unordered_map<int, int> g_bearer_table;

struct Gateways{
static const char *sgw1;
static const char *sgw2;
static const char *sgw3;
static const char *pgw;
int sgw_port;
int pgw_port;
char *sgw_addr;
char *pgw_addr;

Gateways();
set_sgw();
set_pgw();
~Gateways();
};

struct Tunnel{
uint16_t mme_cteid;
uint16_t dst_cteid;
uint16_t enodeb_uteid;
uint16_t sgw_uteid;

static void set_gateways();
set_mme_cteid(int);
};

void* multithreading_func(void*);
void authenticate(Server&, Client&);
void setup_tunnel(Server&, Client&);
void setup_tunnel(Server&, Client&, int);
void create_session(Client&, int, Tunnel&);
void modify_session(Client&, int, Tunnel&);
void set_bearer_id(int);
int generate_bearer_id(int);

#endif //MME_H
25 changes: 15 additions & 10 deletions packet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@ void Packet::fill_data(int pos, int len, unsigned long long arg){
data_len+= len;
}

void Packet::fill_data(int pos, int len, uint16_t *message){
memcpy(data + pos, message, len * sizeof(uint8_t));
data_len+= len;
}

void Packet::fill_data(int pos, int len, const char *message){
memcpy(data + pos, message, len * sizeof(uint8_t));
data_len+= len;
Expand Down Expand Up @@ -150,22 +155,24 @@ void Packet::add_data(){


void Packet::add_gtpc_hdr(){
int len;
uint8_t *tem = allocate_uint8_mem(IP_MAXPACKET);
memcpy(tem, &gtpc_hdr, GTPC_LEN * sizeof(uint8_t));
memcpy((tem + GTPC_LEN), packet, packet_len * sizeof(uint8_t));
memcpy((tem + GTPC_LEN), data, data_len * sizeof(uint8_t));
len = GTPC_LEN + data_len;
clear_data();
memcpy(data, tem, (packet_len + GTPC_LEN) * sizeof(uint8_t));
data_len = packet_len + GTPC_LEN;
fill_data(0, len, tem);
free(tem);
}

void Packet::add_gtpu_hdr(){
int len;
uint8_t *tem = allocate_uint8_mem(IP_MAXPACKET);
memcpy(tem, &gtpu_hdr, GTPU_LEN * sizeof(uint8_t));
memcpy((tem + GTPU_LEN), packet, packet_len * sizeof(uint8_t));
memcpy((tem + GTPU_LEN), data, data_len * sizeof(uint8_t));
len = GTPU_LEN + data_len;
clear_data();
memcpy(data, tem, (packet_len + GTPU_LEN) * sizeof(uint8_t));
data_len = packet_len + GTPU_LEN;
fill_data(0, len, tem);
free(tem);
}

Expand All @@ -176,8 +183,7 @@ void Packet::rem_gtpc_hdr(){
memcpy(tem, (data + GTPC_LEN), (data_len - GTPC_LEN) * sizeof(uint8_t));
len = data_len - GTPC_LEN;
clear_data();
memcpy(data, tem, len * sizeof(uint8_t));
data_len = len;
fill_data(0, len, tem);
free(tem);
}

Expand All @@ -188,8 +194,7 @@ void Packet::rem_gtpu_hdr(){
memcpy(tem, (data + GTPU_LEN), (data_len - GTPU_LEN) * sizeof(uint8_t));
len = data_len - GTPU_LEN;
clear_data();
memcpy(data, tem, len * sizeof(uint8_t));
data_len = len;
fill_data(0, len, tem);
free(tem);
}

Expand Down
29 changes: 29 additions & 0 deletions pgw.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#include "pgw.h"

void* multithreading_func(void *arg){
int type;
ClientDetails *entity = *(ClientDetails*)arg;
Server pgw;
pgw.fill_server_details(g_pgw_port + entity.num, g_pgw_address);
pgw.bind_server();
pgw.client_sock_addr = entity.client_sock_addr;
pgw.connect_with_client();
pgw.read_data();
memcpy(&type, pgw.pkt.data, sizeof(int));
if(type == 1)
handle_cdata(pgw);
else if(type == 2)
handle_udata(pgw);
}

void handle_cdata(Server &pgw){

}

int main(){
Server pgw;
pgw.fill_server_details(g_pgw_port, g_pgw_address);
pgw.bind_server();
pgw.listen_accept(multithreading_func);
return 0;
}
8 changes: 8 additions & 0 deletions pgw.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
#ifndef PGW_H
#define PGW_H

#include "utils.h"
#include "packet.h"
#include "server.h"
#include "raw_socket.h"

void* multithreading_func(void*);
void handle_cdata();
void handle_udata();

#endif //PGW_H
29 changes: 29 additions & 0 deletions sgw.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#include "sgw.h"

void* multithreading_func(void *arg){
int type;
ClientDetails *entity = *(ClientDetails*)arg;
Server sgw;
sgw.fill_server_details(g_sgw1_port + entity.num, g_sgw1_address);
sgw.bind_server();
sgw.client_sock_addr = entity.client_sock_addr;
sgw.connect_with_client();
sgw.read_data();
memcpy(&type, sgw.pkt.data, sizeof(int));
if(type == 1)
handle_cdata(sgw);
else if(type == 2)
handle_udata(sgw);
}

void handle_cdata(Server &sgw){

}

int main(){
Server sgw;
sgw.fill_server_details(g_sgw1_port, g_sgw1_address);
sgw.bind_server();
sgw.listen_accept(multithreading_func);
return 0;
}
5 changes: 5 additions & 0 deletions sgw.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@
#define SGW_H

#include "utils.h"
#include "packet.h"
#include "server.h"
#include "client.h"

void* multithreading_func(void*);
void handle_cdata();
void handle_udata();

#endif //SGW_H
8 changes: 8 additions & 0 deletions utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,16 @@

int g_mme_port = 5000;
int g_hss_port = 9000;
int g_sgw1_port = 6000;
int g_sgw2_port = 7000;
int g_sgw3_port = 8000;
int g_pgw_port = 4000;
const char *g_mme_address = "127.0.0.1";
const char *g_hss_address = "127.0.0.1";
const char *g_sgw1_address = "127.0.0.1";
const char *g_sgw2_address = "127.0.0.1";
const char *g_sgw3_address = "127.0.0.1";
const char *g_pgw_address = "127.0.0.1";
socklen_t g_addr_len = sizeof(sockaddr_in);
timeval g_timeout = {2, 0};

Expand Down
13 changes: 8 additions & 5 deletions utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,18 @@ using namespace std;
#define UE_COUNT 100
#define BUFFER_SIZE 1024

#define SGW1 "10.14.13.29"
#define SGW2 "10.14.13.30"
#define SGW3 "10.14.13.31"
#define PGW "10.14.13.32"

extern int g_mme_port;
extern int g_hss_port;
extern int g_sgw1_port;
extern int g_sgw2_port;
extern int g_sgw3_port;
extern int g_pgw_port;
extern const char *g_mme_address;
extern const char *g_hss_address;
extern const char *g_sgw1_address;
extern const char *g_sgw2_address;
extern const char *g_sgw3_address;
extern const char *g_pgw_address;
extern socklen_t g_addr_len;
extern timeval g_timeout;
extern long long g_stack_buf;
Expand Down

0 comments on commit 034a9a2

Please sign in to comment.