-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.c
169 lines (124 loc) · 4.08 KB
/
main.c
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/* ----------------------------------------------------------------------------
SOURCE FILE
Name: main.c
Program: Port Forwarder
Developer: Andrew Burian
Jordan Marling
Created On: 2015-03-08
Functions:
int main(int argc, char** argv)
Description:
The main portion of the port forwarding program
* needs libconfread (github.com/andrewburian/configreader)
Revisions:
(none)
---------------------------------------------------------------------------- */
#include "portforward.h"
/* ----------------------------------------------------------------------------
FUNCTION
Name: Main
Prototype: int main(int argc, char** argv)
Developer: Andrew Burian
Created On: 2015-03-08
Parameters:
Command line args
Return Values:
0 success
-1 error conditions
Description:
Sets up the port forwarder. Reads the config file and sets up the targets list
for forwarding.
Revisions:
Jordan Marling
2015-03-13
Added the Listen function call
Andrew Burian
2015-03-15
Added Checksum calculations for TCP at every sendto call
---------------------------------------------------------------------------- */
int main(int argc, char** argv){
// the config file
struct confread_file* confFile = 0;
char* confFileName = 0;
struct confread_section* sec = 0;
// ports
unsigned short int aPort = 0;
unsigned short int bPort = 0;
// counter
size_t i = 0;
size_t j = 0;
// the array of targets
struct pf_target* targets = 0;
size_t targetCount = 0;
// our ip to replace in forwarded packets
unsigned int myIp = 0;
struct confread_pair* ip = 0;
// open the config
confFileName = (argc > 1 ? argv[1] : DEFAULT_CONFIG);
if(!(confFile = confread_open(confFileName))){
fprintf(stderr, "Failed to open conf file: %s\n", confFileName);
return -1;
}
// read our IP from the conf
if(!(ip = confread_find_pair(confFile->sections[0], "addr"))){
fprintf(stderr, "Local address required in %s\n", confFileName);
return -1;
}
if((myIp = inet_addr(ip->value)) == INADDR_NONE){
fprintf(stderr, "Invalid local address\n");
return -1;
}
// allocate as many targets as there are sections (-1 to skip root section)
targets = (struct pf_target*)malloc(sizeof(struct pf_target) * confFile->count - 1);
targetCount = confFile->count - 1;
// setup the targets
j = 1; // to skip root
for(i = 0; i < targetCount; ++i, ++j){
sec = confFile->sections[j];
// check to see all sections are there
if(!confread_find_value(sec, "port") || !confread_find_value(sec, "toport")
|| !confread_find_value(sec, "tohost")){
// error
fprintf(stderr, "Forward section %s malformed: missing field.\nIgnored.\n", sec->name);
// shrink the number of targets needed
targets = realloc(targets, sizeof(struct pf_target) * --targetCount);
// don't advance i
--i;
continue;
}
// check to see we can scan the ports
if(!sscanf(confread_find_value(sec, "port"), "%hu", &aPort) ||
!sscanf(confread_find_value(sec, "toport"), "%hu", &bPort)){
// error
fprintf(stderr, "Forward section %s malformed: port NaN\nIgnored\n", sec->name);
// shrink the number of targets needed
targets = realloc(targets, sizeof(struct pf_target) * --targetCount);
// don't advance i
--i;
continue;
}
// assign the host
if((targets[i].host = inet_addr(confread_find_value(sec, "tohost"))) == INADDR_NONE){
// error
fprintf(stderr, "Forward section %s malformed: invalid host.\nIgnored.\n", sec->name);
// shrink the number of targets needed
targets = realloc(targets, sizeof(struct pf_target) * --targetCount);
// don't advance i
--i;
continue;
}
// assign the ports
targets[i].port.a_port = htons(aPort);
targets[i].port.b_port = htons(bPort);
firewall_invoke_srcport(aPort);
firewall_invoke_dstport(bPort);
}
printf("Initialized %zu forwards\n", targetCount);
// close the config
confread_close(&confFile);
// run the forwarder
forward(targets, targetCount, myIp);
// cleanup
free(targets);
return 0;
}