Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unstable #2

Open
wants to merge 2 commits into
base: unstable
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions ProtocolProcessing/ProtocolProcessing.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,11 @@
<Link>
<SubSystem>Windows</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
<ModuleDefinitionFile>source.def</ModuleDefinitionFile>
</Link>
<PostBuildEvent>
<Command>copy /Y "$(TargetDir)$(ProjectName).dll" "$(SolutionDir)\ProtocolProcessingGUI\bin\Debug\$(ProjectName).dll"</Command>
</PostBuildEvent>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<ClCompile>
Expand All @@ -72,6 +76,7 @@
<GenerateDebugInformation>true</GenerateDebugInformation>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<ModuleDefinitionFile>source.def</ModuleDefinitionFile>
</Link>
</ItemDefinitionGroup>
<ItemGroup>
Expand All @@ -80,14 +85,21 @@
<ClCompile Include="network.c" />
<ClCompile Include="router.c" />
<ClCompile Include="simulation.c" />
<ClCompile Include="simulation_socket.c" />
<ClCompile Include="tcp_socket.c" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="list.h" />
<ClInclude Include="network.h" />
<ClInclude Include="router.h" />
<ClInclude Include="simulation.h" />
<ClInclude Include="simulation_socket.h" />
<ClInclude Include="tcp_socket.h" />
<ClInclude Include="types.h" />
</ItemGroup>
<ItemGroup>
<None Include="source.def" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
Expand Down
17 changes: 17 additions & 0 deletions ProtocolProcessing/ProtocolProcessing.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@
<ClCompile Include="network.c">
<Filter>Source Files\Simulation</Filter>
</ClCompile>
<ClCompile Include="simulation_socket.c">
<Filter>Source Files\Simulation</Filter>
</ClCompile>
<ClCompile Include="tcp_socket.c">
<Filter>Source Files\Simulation</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="simulation.h">
Expand All @@ -59,5 +65,16 @@
<ClInclude Include="network.h">
<Filter>Header Files\Simulation</Filter>
</ClInclude>
<ClInclude Include="simulation_socket.h">
<Filter>Header Files\Simulation</Filter>
</ClInclude>
<ClInclude Include="tcp_socket.h">
<Filter>Header Files\Simulation</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<None Include="source.def">
<Filter>Source Files</Filter>
</None>
</ItemGroup>
</Project>
25 changes: 21 additions & 4 deletions ProtocolProcessing/list.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@

#include "list.h"

#include <malloc.h>
#include <memory.h>

typedef struct{
Expand All @@ -13,7 +14,8 @@ typedef struct{

#define POINTER_SIZE 4

#define MEMPOS(pointer,index) ((UInt)(pointer) + index*POINTER_SIZE)
#define MEMPOS(pointer,index) (Pointer)(((UInt)(pointer)) + (index*POINTER_SIZE))
#define MEMPOS_ITEM(pointer,index) *(Pointer*)MEMPOS(pointer,index)
#define PLIST(list) ((LIST*)list)

Pointer list_create(void){
Expand All @@ -27,19 +29,34 @@ Pointer list_create(void){
void list_resize(Pointer list, Int nCapacity){

Pointer nHead = malloc(nCapacity*POINTER_SIZE);
if(!PLIST(list)->m_capacity){
if(PLIST(list)->m_capacity){
memcpy(nHead,PLIST(list)->m_head,PLIST(list)->m_capacity*POINTER_SIZE);
}

PLIST(list)->m_capacity = nCapacity;
free(PLIST(list)->m_head);
PLIST(list)->m_head = nHead;

}

int list_get_count(Pointer list){
return PLIST(list)->m_count;
}

Pointer list_get_item(Pointer list, Int index){
return MEMPOS(PLIST(list)->m_head,index);
return MEMPOS_ITEM(PLIST(list)->m_head,index);
}

void list_set_item(Pointer list, Pointer item, Int index){
memcpy(MEMPOS(PLIST(list)->m_head,index),&item,4);
}

void list_add_item(Pointer list, Pointer item){
MEMPOS(PLIST(list),PLIST(list)->m_count++);

if(PLIST(list)->m_capacity == PLIST(list)->m_count){
list_resize(list,PLIST(list)->m_capacity*2 + 1);
}

memcpy(MEMPOS(PLIST(list)->m_head,PLIST(list)->m_count++),&item,4);
//PLIST(list)->m_count++;
}
3 changes: 2 additions & 1 deletion ProtocolProcessing/list.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
Pointer list_create(void);

void list_add_item(Pointer list, Pointer item);
Pointer list_get_item(Pointer list, int index);
Pointer list_get_item(Pointer list, Int index);
void list_set_item(Pointer list, Pointer item, Int index);

int list_get_count(Pointer list);
191 changes: 187 additions & 4 deletions ProtocolProcessing/network.c
Original file line number Diff line number Diff line change
@@ -1,21 +1,204 @@

#define WIN32_LEAN_AND_MEAN
#include <Windows.h>

#include "network.h"

#ifndef _SIMULATION_SOCKET_H_
#include "simulation_socket.h"
#endif

#ifndef _LIST_H_
#include "list.h"
#endif

network_node* create_network_node(void){
#ifndef _TCP_SOCKET_H_
#include "tcp_socket.h"
#endif

#ifndef _ROUTER_H_
#include "router.h"
#endif

#include <malloc.h>
#include <memory.h>


typedef struct{
//network_node* m_gateway;
Pointer m_node_list;
} SIMULATION_NETWORK;

#define PSIMULATION_NETWORK(network) ((SIMULATION_NETWORK*)network)

//needs a proper bitwise operations.
Int is_in_network_prefix(network_prefix prefix, network_addr addr){

int i;

for(i=0;8*(i+1)<prefix.prefix_len;i++){
if(prefix.prefix[i] != addr[i]){
return 1;
}
}
return 0;
}

Pointer network_create_simulation_network(void){

SIMULATION_NETWORK* simulation_network = malloc(sizeof(SIMULATION_NETWORK));


simulation_network->m_node_list = list_create();

return simulation_network;
}

void network_link_network(network_node* node1, network_node* node2){
router_node_link(node1->node_router,node2);
}

int network_get_route_count(network_node* node){
return list_get_count(((ROUTER*)node->node_router)->route_advert_list);
}

Pointer network_get_route_by_index(network_node* node, Int index){
return list_get_item(((ROUTER*)node->node_router)->route_advert_list,index);
}

Int network_get_node_count(Pointer simulation_network){
return list_get_count(PSIMULATION_NETWORK(simulation_network)->m_node_list);
}

network_node* network_get_node_by_index(Pointer simulation_network, Int index){
network_node* node = list_get_item(PSIMULATION_NETWORK(simulation_network)->m_node_list,index);
return list_get_item(PSIMULATION_NETWORK(simulation_network)->m_node_list,index);
}

//returns network_node with the address given by addr parameter, if it exists in the network, returns null if it doesnt.
network_node* network_get_node(Pointer simulation_network, network_addr addr){

int i,c;

/*if(is_in_network_prefix(PSIMULATION_NETWORK(simulation_network)->m_area.local_subnet,addr)){
return 0;
}*/

c = list_get_count(PSIMULATION_NETWORK(simulation_network)->m_node_list);

for(i = 0;i<c;i++){
network_node* node = list_get_item(PSIMULATION_NETWORK(simulation_network)->m_node_list,i);
if(IS_SAME_ADDRESS(node->address,addr)){
return node;
}
}
return 0;
}


static Int m_network_last_id = 0;

network_node* network_create_node(network_addr addr){

network_node* node = malloc(sizeof(network_node));

memset(node,0,sizeof(network_node));
node->node_id = ++ m_network_last_id;
memcpy(node->address,addr,4);

node->peer.socket = simulation_socket(node,socket_type_raw);
node->peer.bound_socket_list = list_create();

node->node_router = router_create();
router_init(node->node_router,node);

return node;
}
//needs a proper bitwise operations.
Int is_in_network_prefix(network_prefix prefix, network_node node){

Int network_node_get_id(network_node* node){
return node->node_id;
}

UInt8* network_node_get_address(network_node* node){
return (UInt8*)node->address;
}


void network_add_simulation_network(Pointer simulation_network, network_node* node){

//memcpy(&node->prefix.prefix,&PSIMULATION_NETWORK(simulation_network)->m_gateway->prefix,sizeof(network_prefix));
node->simulation_network = simulation_network;
list_add_item(PSIMULATION_NETWORK(simulation_network)->m_node_list,node);

}

network_node_chain* network_create_node_chain(void){

network_node_chain* chain = malloc(sizeof(network_node_chain));
memset(chain,0,sizeof(network_node_chain));

return chain;
}

Pointer network_get_listen(network_node* node, int port){

int count = list_get_count(node->peer.bound_socket_list);
int i;
for(i = 0;i<count;i++){
Pointer socket = list_get_item(node->peer.bound_socket_list,i);
if(!is_listen_port(socket,port)){
return socket;
}
}
return 0;
}

void network_node_install_packet_sniffer(network_node* node, PACKET_SNIFFER sniffer){
node->sniffer = sniffer;
}

void network_do_loop(Pointer simulation_network){

int count = network_get_node_count(simulation_network);
int i;

for(i=0;i<count;i++){
network_node* node = network_get_node_by_index(simulation_network,i);

SOCKET_PACKET* packet = simulation_receive_raw_socket(node);

if(!packet){
continue;
}
//packet needs to be routed.
if(!IS_SAME_ADDRESS(packet->addr,node->address))
{
simulation_send_packet(node,packet);
}else if(!is_tcp_segment(packet)){
TCP_SEGMENT* packet_tcp = packet->paket_payload;

Pointer socket = network_get_listen(node,packet_tcp->dest_port);
if(socket){
simulation_send(socket,packet->paket_payload + sizeof(TCP_SEGMENT),packet->packet_len - (SOCKET_PACKET_SIZE + sizeof(TCP_SEGMENT)));
}
}
router_do_loop(node->node_router);
//free_ip_packet(packet);
}

}

void network_free_node(network_node* node){
free(node);
}

void network_free_node_chain(network_node_chain* chain){

if(chain){
network_free_node_chain(chain->next);
}

network_free_node(chain->current);

free(chain);

}
Loading