-
Notifications
You must be signed in to change notification settings - Fork 2
/
AddressManager.cpp
99 lines (80 loc) · 1.92 KB
/
AddressManager.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#include <limits.h>
#include <netdb.h>
#include <iostream>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include <netinet/in.h>
#include <signal.h>
#include <unistd.h>
#include <sys/un.h>
#include <arpa/inet.h>
#include <vector>
#include "AddressManager.h"
#include "AddressInfo.h"
using namespace std;
void AddressManager::AddItem(char *IP)
{
if (FindIP(IP) == -1)//No need to add it second time
{
AddressInfo tmp;
tmp.si_len = sizeof(tmp.si_DestInfo);
if ((tmp.Socket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1)
{
fprintf(stderr, "Failed to create socket");
//exit(1);
}
memset((char *) &tmp.si_DestInfo, 0, sizeof(tmp.si_DestInfo));
tmp.si_DestInfo.sin_family = AF_INET;
tmp.si_DestInfo.sin_port = htons(UDPForwardPort);
if (inet_aton(IP, &tmp.si_DestInfo.sin_addr) == 0)
{
fprintf(stderr, "inet_aton() failed\n");
//exit(1);
}
//Add info to vector
AddressInfoVector.push_back(tmp);
}
else
{
fprintf(stderr, "Address exists. No need to add second time.");
}
}
void AddressManager::DelItem(char *IP)
{
int Position = FindIP(IP);
if (Position != -1)
{
close(AddressInfoVector[Position].Socket);//close socket
AddressInfoVector.erase(AddressInfoVector.begin() + Position);
}
else
{
fprintf(stderr, "Address not exists. No need to remove it.");
}
}
int AddressManager::FindIP(char *IP)
{
int result = -1;
if (AddressInfoVector.empty() == false)
{
struct in_addr inAddress;
if (inet_aton(IP, &inAddress) == true)
{
int size = AddressInfoVector.size();
for (int i = 0; i < size; i++)
{
if (AddressInfoVector[i].si_DestInfo.sin_addr.s_addr == inAddress.s_addr)
{
return i;
}
}
}
}
return result;
}
void AddressManager::SetUDPForwardPort(int port)
{
UDPForwardPort = port;
}