aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/main.c219
-rw-r--r--src/main.h48
-rw-r--r--src/misc.c64
-rw-r--r--src/misc.h39
-rw-r--r--src/modem.c30
-rw-r--r--src/modem.h3
-rw-r--r--src/telnet.c126
-rw-r--r--src/telnet.h19
8 files changed, 282 insertions, 266 deletions
diff --git a/src/main.c b/src/main.c
index 4a6c5e8..cef7bb1 100644
--- a/src/main.c
+++ b/src/main.c
@@ -7,157 +7,6 @@
#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[0] = '\n';
- if ((needle - buff + 1) < buffsize)
- needle[1]='\0';
- }
-
- 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);
@@ -213,71 +62,3 @@ int main(int argc, char* argv[])
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);
- }
-}
diff --git a/src/main.h b/src/main.h
index 2f2a7b1..bf6a495 100644
--- a/src/main.h
+++ b/src/main.h
@@ -10,69 +10,23 @@
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
-#include <sys/socket.h>
#include <sys/types.h>
#include <sys/wait.h>
-#include <netinet/in.h>
#include <stdint.h>
#include <errno.h>
#include <string.h>
#include <signal.h>
-#include <arpa/inet.h>
#include "misc.h"
#include "log.h"
#include "serial.h"
#include "modem.h"
-
+#include "telnet.h"
#define _DEF_MAX_BACKLOG 20
#define _DEF_PORT 23
#define _DEF_IP 0
#define _DEF_CONFIG_FILE "/etc/bbs.conf"
-#ifdef _DEBUG
-//DEBUG Macros
-#warning "Compiling in DEBUG mode"
-#define DEBUG_PRINTF( ... ) { \
- printf("%s:%d: ", __FILE__, __LINE__); \
- printf(__VA_ARGS__ ); }
-#define PRINT_ERROR( str ) { \
- printf("%s:%d: %s: %s\n", __FILE__, __LINE__, str, strerror(errno)); }
-#else
-//Release Macros
-#define DEBUG_PRINTF( ... ) { }
-#define PRINT_ERROR( str ) { printf("%s: %s\n", str, strerror(errno)); }
-#endif
-
-#define ERROR_HELP( ... ) { \
- printf(__VA_ARGS__); \
- printf("bbs\n-p PORT: telnet port\n-i IP: telnet listen ip\n-s DEVIVE: modem serial device\n-b BAUD: serial baudrate\n-f FILE: use pidfile\n"); \
- exit(1); }
-
-struct prog_params
-{
- uint8_t telnet;
- uint16_t port;
- uint16_t backlog;
- char* ip;
- char** run_argv;
- int run_argc;
-
- uint8_t serial;
- char* serial_port;
- uint32_t serial_baudrate;
-
- uint8_t fork;
- char* pidfile;
-};
-
-struct prog_params parse_args(int argc, char* argv[]);
-
-void handle_connection(int _socket, struct sockaddr_in _addr, int argc, char* argv[]);
-
int main(int argc, char* argv[]);
-void telnet_server(struct prog_params params);
-
-void dialup_server(struct prog_params params);
diff --git a/src/misc.c b/src/misc.c
index 0bf7233..c3cbf7b 100644
--- a/src/misc.c
+++ b/src/misc.c
@@ -57,3 +57,67 @@ int try_write(int _fd, char *_buff, int _size, int _retry)
}
return 0;
}
+
+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;
+}
diff --git a/src/misc.h b/src/misc.h
index 21b2d75..34ca3ec 100644
--- a/src/misc.h
+++ b/src/misc.h
@@ -12,6 +12,45 @@
#include <stdio.h>
#include <errno.h>
#include <string.h>
+#include <stdint.h>
+
+#ifdef _DEBUG
+//DEBUG Macros
+#warning "Compiling in DEBUG mode"
+#define DEBUG_PRINTF( ... ) { \
+ printf("%s:%d: ", __FILE__, __LINE__); \
+ printf(__VA_ARGS__ ); }
+#define PRINT_ERROR( str ) { \
+ printf("%s:%d: %s: %s\n", __FILE__, __LINE__, str, strerror(errno)); }
+#else
+//Release Macros
+#define DEBUG_PRINTF( ... ) { }
+#define PRINT_ERROR( str ) { printf("%s: %s\n", str, strerror(errno)); }
+#endif
+
+#define ERROR_HELP( ... ) { \
+ printf(__VA_ARGS__); \
+ printf("bbs\n-p PORT: telnet port\n-i IP: telnet listen ip\n-s DEVIVE: modem serial device\n-b BAUD: serial baudrate\n-f FILE: use pidfile\n"); \
+ exit(1); }
+
+struct prog_params
+{
+ uint8_t telnet;
+ uint16_t port;
+ uint16_t backlog;
+ char* ip;
+ char** run_argv;
+ int run_argc;
+
+ uint8_t serial;
+ char* serial_port;
+ uint32_t serial_baudrate;
+
+ uint8_t fork;
+ char* pidfile;
+};
+
+struct prog_params parse_args(int argc, char* argv[]);
/*
* Try writing to _fd for _retry times. Retries are reset after every succesful write.
diff --git a/src/modem.c b/src/modem.c
index ab95593..d8b09fc 100644
--- a/src/modem.c
+++ b/src/modem.c
@@ -193,3 +193,33 @@ int modem_run(int fd, int argc, char* argv[])
close(fd); //Auto closes connection
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);
+ }
+}
diff --git a/src/modem.h b/src/modem.h
index aa43d25..31f9fee 100644
--- a/src/modem.h
+++ b/src/modem.h
@@ -13,6 +13,7 @@
#include "misc.h"
#include "log.h"
+#include "serial.h"
#define _AT "AT\r\n"
#define _AT_ANSWER "ATA\r\n"
@@ -40,3 +41,5 @@ int modem_run(int fd, int argc, char* argv[]);
* Run a program with modem as STDIO. checks if connection is still alive & process is still active.
* will close fd on successful return
* */
+
+void dialup_server(struct prog_params params);
diff --git a/src/telnet.c b/src/telnet.c
new file mode 100644
index 0000000..c4ac551
--- /dev/null
+++ b/src/telnet.c
@@ -0,0 +1,126 @@
+#include "telnet.h"
+
+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[0] = '\n';
+ if ((needle - buff + 1) < buffsize)
+ needle[1]='\0';
+ }
+
+ 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);
+}
+
+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);
+ }
+}
diff --git a/src/telnet.h b/src/telnet.h
new file mode 100644
index 0000000..104b5bc
--- /dev/null
+++ b/src/telnet.h
@@ -0,0 +1,19 @@
+#pragma once
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <errno.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <arpa/inet.h>
+#include <signal.h>
+#include <poll.h>
+#include <stdint.h>
+
+#include "misc.h"
+#include "log.h"
+
+void telnet_server(struct prog_params params);
+
+void handle_connection(int _socket, struct sockaddr_in _addr, int argc, char* argv[]);