aboutsummaryrefslogtreecommitdiff
path: root/src/main.c
blob: bdc58e2b463839322a52c88ec604d18161429ca8 (plain)
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
/*
 * main.c
 * (c) 2019, Jonas Gunz, jonasgunz.de
 * <Description>
 * License: MIT
*/

#include "main.h"

struct prog_params parse_args(int argc, char* argv[])
{
	struct prog_params ret;
	memset(&ret, 0, sizeof(ret));

	for (int i = 1; i < argc; i++)
	{
		int i_cpy = i; //i might be changed in loop

		if(argv[i_cpy][0] == '-')
		{
			for (int o = 1; o < strlen(argv[i_cpy]); o++)
			{
				switch (argv[i_cpy][o])
				{
					case 'h':
						ERROR_HELP("");
						break;
					case 'p':
						ret.telnet = 1;
						ret.port = atoi(argv[i_cpy + 1]);
						i++;
						break;
					case 'i':
						ret.telnet = 1;
						ret.ip = argv[i_cpy + 1];
						i++;
						break;
					case 's'://Serial modem
						ret.serial = 1;
						ret.serial_port = argv[i_cpy + 1];
						i++;
						break;
					case 'b':
						ret.serial = 1;
						ret.serial_baudrate = atoi(argv[i_cpy + 1]);
						i++;
						break;
					case 'f'://PID file for spawned children
						ret.fork = 1;
						ret.pidfile = argv[i_cpy + 1];
						i++;
						break;
					default:
						ERROR_HELP("Unrecognized Option: '%c'\n", argv[i_cpy][o]);
						break;
				};//switch
			}//for
		}//if
		else
		{
			//Copy the rest as arguments for prog to exec
			ret.run_argc = argc - i_cpy;
			ret.run_argv = &(argv[i_cpy]);
			break;
		}//else
	}//for

	if(ret.telnet == ret.serial)//run EITHER in telnet OR modem mode
		ERROR_HELP("Select either modem OR telnet.\n");

	return ret;
}

void handle_connection(int _socket, struct sockaddr_in _addr, int argc, char* argv[])
{
	pid_t pid = fork();
	if( pid != 0 ) {
		close(_socket);
		return;
	}

	int in[2];
	int out[2];

	//1: write, 0: read
	if(pipe(in) == -1)
		return;
	
	if(pipe(out) == -1)
		return;
	
	pid = fork_run(in[0], out[1], out[1], argc, argv);
	if (pid < 0)
		return;

	const int buffsize = 128;
	char buff[ buffsize + 1];
	buff[buffsize] = '\0';

	//close unused pipes
	close (in[0]);
	close (out[1]);

	//setup poll to listen for input
	struct pollfd fds[2];
	fds[0].fd = out[0];
	fds[0].events = POLLIN;
	fds[1].fd = _socket;
	fds[1].events = POLLIN;

	LOGPRINTF(_LOG_NOTE, "%i: Connected to %s", pid, inet_ntoa(_addr.sin_addr));

	while(1)
	{
		int ret = poll (fds, 2, 100);
		if ( fds[0].revents & POLLIN ) {
			const int cnt = read (out[0], buff, buffsize);
			if(try_write(_socket, buff, cnt, 100)) {
				LOGPRINTF(_LOG_ERROR, "%i: Consecutive write errors while writing to socket.", pid);
				break;
			}
		}
		if ( fds[1].revents & POLLIN ) {
			const int cnt = read (_socket, buff, buffsize);
			
			if(cnt == 0)
				break;

			char *needle = strstr(buff, "\r");
			if (needle) //Replace CR with space
				*needle = ' ';

			if(try_write(in[1], buff, cnt, 100)) {
				LOGPRINTF(_LOG_ERROR, "%i: Consecutive write errors while writing to STDIN.", pid);
				break;
			}

		}

		if (ret < 0){
			LOGPRINTF(_LOG_ERROR, "Poll error\n");
			break;
		}

		if(kill(pid,0)) //Check if child is still alive, if not return.
			break;
	}

	LOGPRINTF(_LOG_NOTE, "%i: Connection closed.", pid);

	kill(pid,SIGKILL);

	close(_socket);
	exit(1);
}


int main(int argc, char* argv[])
{
	log_init_stdout(_LOG_DEBUG);
	
	LOGPRINTF(_LOG_DEBUG, "debug");
	LOGPRINTF(_LOG_NOTE, "note");
	LOGPRINTF(_LOG_WARNING, "warn");
	LOGPRINTF(_LOG_ERROR, "Error");

	signal(SIGCHLD,SIG_IGN); //Ignore sigchld
	struct prog_params params = parse_args(argc, argv);

	//Fork and write PID to pidfile
	if(params.fork)
	{
		FILE* pidfile = fopen(params.pidfile, "w");
		
		if(!pidfile)
		{
			PRINT_ERROR("Unable to open pidfile for writing");
			exit(1);
		}
		pid_t pid = fork();

		if(pid < 0)
		{
			PRINT_ERROR("fork() failed");
			exit(1);
		}
		else if(pid > 0)
		{
			fprintf(pidfile, "%i", pid);
			printf("Forked with PID %i\n", pid);
			fclose (pidfile);
			exit(0);
		}

		fclose (pidfile);

		//Close STDIO
		close (STDIN_FILENO);
		close (STDOUT_FILENO);
		close (STDERR_FILENO);
	}//if params.fork

	DEBUG_PRINTF("%s, %i\n", params.run_argv[0], params.run_argc);

	if ( params.serial )
		dialup_server(params);
	else if (params.telnet)
		telnet_server(params);

	return 0;
}

void dialup_server(struct prog_params params)
{
	printf("Starting dialup server\n");

	while(1) {
		//Serial port is reopened for every new connection to reset modem over DTR
		int fd = open (params.serial_port, O_RDWR | O_NOCTTY | O_SYNC);
		if (fd < 0) {
			LOGPRINTF(_LOG_ERROR, "Failed to open serial port");
		        return;
		}
	
		set_interface_attribs (fd, params.serial_baudrate, 0);
		set_blocking (fd, 0);

		int ret = modem_accept_wait(fd);

		if(ret) {
			LOGPRINTF(_LOG_NOTE, "Connection not established: %i", ret);
			close(fd);
			break;
		}

		LOGPRINTF(_LOG_NOTE,"Connection");

		modem_run(fd, params.run_argc, params.run_argv);
		close (fd);
	}
}

void telnet_server(struct prog_params params)
{
	signal(SIGCHLD,SIG_IGN); //Ignore sigchld

	int server_socket, client_socket;
	struct sockaddr_in socket_address, client_address;
	socklen_t claddrsize = sizeof(client_address);
	
	if ( (server_socket = socket(AF_INET, SOCK_STREAM, 0)) == -1 )
	{
		printf("Error creating socket: %i: %s\n", errno, strerror(errno));
		exit(1);
	}

	memset (&socket_address, 0, sizeof(socket_address));

	socket_address.sin_family = AF_INET;
	socket_address.sin_port = htons( params.port );

	if ( (bind(server_socket, (struct sockaddr*) &socket_address, sizeof(socket_address))) == -1 )
	{
		printf("Error binding socket: %i: %s\n", errno, strerror(errno));
		exit(1);
	}

	if ( (listen(server_socket, 10)) == -1 )
	{
		printf("Error listening socket: %i: %s\n", errno, strerror(errno));
		exit(1);
	}

	while(1)
	{
		client_socket = accept(server_socket, (struct sockaddr*)&client_address, &claddrsize);
		DEBUG_PRINTF("Connection: %s\n", inet_ntoa(client_address.sin_addr));
		handle_connection(client_socket, client_address, params.run_argc, params.run_argv);
	}
}