From 78ecedcc4e318aebb8d66a3d848f7cc5f3d00bc6 Mon Sep 17 00:00:00 2001 From: Olaf Rempel Date: Sat, 25 Nov 2006 14:31:52 +0100 Subject: [PATCH] initial --- .gitignore | 3 + Makefile | 22 ++++ configfile.c | 200 ++++++++++++++++++++++++++++++ configfile.h | 16 +++ connection.c | 5 + connection.h | 23 ++++ event.c | 310 +++++++++++++++++++++++++++++++++++++++++++++++ event.h | 26 ++++ list.h | 268 ++++++++++++++++++++++++++++++++++++++++ logging.c | 103 ++++++++++++++++ logging.h | 16 +++ network.c | 78 ++++++++++++ network.h | 12 ++ telnetproxy.c | 117 ++++++++++++++++++ telnetproxy.conf | 2 + 15 files changed, 1201 insertions(+) create mode 100644 .gitignore create mode 100644 Makefile create mode 100644 configfile.c create mode 100644 configfile.h create mode 100644 connection.c create mode 100644 connection.h create mode 100644 event.c create mode 100644 event.h create mode 100644 list.h create mode 100644 logging.c create mode 100644 logging.h create mode 100644 network.c create mode 100644 network.h create mode 100644 telnetproxy.c create mode 100644 telnetproxy.conf diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3712a6f --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +*.d +*.o +telnetproxy diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..e7c1c2b --- /dev/null +++ b/Makefile @@ -0,0 +1,22 @@ +VERSION := v0.01 + +CFLAGS := -O2 -pipe -Wall -DVERSION='"$(VERSION)"' + +OBJS := configfile.o connection.o event.o logging.o network.o telnetproxy.o + +all: telnetproxy + +telnetproxy: $(OBJS) + $(CC) $(CFLAGS) $^ -o $@ + +%.o: %.c + $(CC) $(CFLAGS) -c $< -o $@ + +%.d: %.c + $(CC) $(CFLAGS) -MM -c $< -o $@ + +clean: + rm -f telnetproxy *.d *.o *.log + +DEPS := $(wildcard *.c) +-include $(DEPS:.c=.d) diff --git a/configfile.c b/configfile.c new file mode 100644 index 0000000..73ff3ae --- /dev/null +++ b/configfile.c @@ -0,0 +1,200 @@ +/*************************************************************************** + * Copyright (C) 06/2006 by Olaf Rempel * + * razzor@kopf-tisch.de * + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 2 of the License, or * + * (at your option) any later version. * + * * + * This program is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program; if not, write to the * + * Free Software Foundation, Inc., * + * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * + ***************************************************************************/ +#include +#include +#include +#include + +#include +#include +#include +#include + +#include "configfile.h" +#include "list.h" +#include "logging.h" + +#define BUFSIZE 1024 + +struct conf_section { + struct list_head list; + struct list_head tupel_list; + char *name; +}; + +struct conf_tupel { + struct list_head list; + char *option; + char *parameter; +}; + +static LIST_HEAD(config_list); + +static struct conf_section * config_add_section(const char *name) +{ + struct conf_section *section; + section = malloc(sizeof(struct conf_section) + strlen(name)); + if (section == NULL) + return NULL; + + INIT_LIST_HEAD(§ion->list); + INIT_LIST_HEAD(§ion->tupel_list); + + section->name = strdup(name); + if (section->name == NULL) { + free(section); + return NULL; + } + + list_add_tail(§ion->list, &config_list); + return section; +} + +static int config_add_tupel(struct conf_section *section, const char *option, const char *parameter) +{ + struct conf_tupel *tupel = malloc(sizeof(struct conf_tupel)); + if (tupel == NULL) + return -1; + + INIT_LIST_HEAD(&tupel->list); + tupel->option = strdup(option); + tupel->parameter = strdup(parameter); + + if (tupel->option == NULL || tupel->parameter == NULL) { + free(tupel); + return -1; + } + + list_add_tail(&tupel->list, §ion->tupel_list); + return 0; +} + +int config_parse(const char *config) +{ + FILE *fz = fopen(config, "r"); + if (fz == NULL) { + log_print(LOG_ERROR, "config_parse(): %s", config); + return -1; + } + + char *line = malloc(BUFSIZE); + if (line == NULL) { + log_print(LOG_ERROR, "config_parse(): out of memory"); + fclose(fz); + return -1; + } + + int linenum = 0; + struct conf_section *section = NULL; + while (fgets(line, BUFSIZE, fz) != NULL) { + linenum++; + + if (line[0] == '#' || line[0] <= ' ') { + continue; + + } else if (line[0] == '[') { + char *tok = strtok(line +1, " ]\n"); + + if (tok == NULL || (section = config_add_section(tok)) == NULL) { + log_print(LOG_WARN, "config_parse(): invalid section in row %d", linenum); + free(line); + fclose(fz); + return -1; + } + continue; + + } else if (section == NULL) { + log_print(LOG_WARN, "config_parse(): missing section in row %d", linenum); + free(line); + fclose(fz); + return -1; + } + + char *tok = strtok(line, " \n"); + if (tok != NULL) { + char *tok2; + while ((tok2 = strtok(NULL, " \n"))) { + if (config_add_tupel(section, tok, tok2) != 0) + log_print(LOG_WARN, "config_parse(): invalid row %d", linenum); + } + } + } + + fclose(fz); + free(line); + return 0; +} + +static struct conf_section * config_get_section(const char *name) +{ + struct conf_section *section; + + list_for_each_entry(section, &config_list, list) { + if (!strcmp(section->name, name)) + return section; + } + return NULL; +} + +char * config_get_string(const char *section_str, const char *option, char *def) +{ + struct conf_section *section = config_get_section(section_str); + if (section != NULL) { + struct conf_tupel *tupel; + list_for_each_entry(tupel, §ion->tupel_list, list) { + if (!strcmp(tupel->option, option)) + return tupel->parameter; + } + } + + if (def != NULL) + log_print(LOG_WARN, "config [%s:%s] not found, using default: '%s'", + section_str, option, def); + return def; +} + +int config_get_int(const char *section, const char *option, int def) +{ + char *ret = config_get_string(section, option, NULL); + if (ret == NULL) { + log_print(LOG_WARN, "config [%s:%s] not found, using default: '%d'", + section, option, def); + return def; + } + return atoi(ret); +} + +int config_get_strings(const char *section_str, const char *option, + int (*callback)(const char *value, void *privdata), + void *privdata) +{ + struct conf_section *section = config_get_section(section_str); + if (section == NULL) + return -1; + + int cnt = 0; + struct conf_tupel *tupel; + list_for_each_entry(tupel, §ion->tupel_list, list) { + if (!strcmp(tupel->option, option)) + if (callback(tupel->parameter, privdata) == 0) + cnt++; + } + return cnt; +} diff --git a/configfile.h b/configfile.h new file mode 100644 index 0000000..6528459 --- /dev/null +++ b/configfile.h @@ -0,0 +1,16 @@ +#ifndef _CONFIG_H_ +#define _CONFIG_H_ + +#include + +int config_parse(const char *config); + +char * config_get_string(const char *section_str, const char *option, char *def); + +int config_get_int(const char *section, const char *option, int def); + +int config_get_strings(const char *section_str, const char *option, + int (*callback)(const char *value, void *privdata), + void *privdata); + +#endif /* _CONFIG_H_ */ diff --git a/connection.c b/connection.c new file mode 100644 index 0000000..eb88ca4 --- /dev/null +++ b/connection.c @@ -0,0 +1,5 @@ +#include +#include +#include + +#include "connection.h" diff --git a/connection.h b/connection.h new file mode 100644 index 0000000..724bce9 --- /dev/null +++ b/connection.h @@ -0,0 +1,23 @@ +#ifndef _CONNECTION_H_ +#define _CONNECTION_H_ + +#include + +#include "list.h" + +struct connection { + struct list_head list; + + struct sockaddr_in src_addr; + struct sockaddr_in dst_addr; + + int src_fd; + int dst_fd; + + unsigned long login_time; + unsigned long idle_time; + + char user[32]; +}; + +#endif // _CONNECTION_H_ diff --git a/event.c b/event.c new file mode 100644 index 0000000..2d34826 --- /dev/null +++ b/event.c @@ -0,0 +1,310 @@ +/*************************************************************************** + * Copyright (C) 10/2006 by Olaf Rempel * + * razzor@kopf-tisch.de * + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 2 of the License, or * + * (at your option) any later version. * + * * + * This program is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program; if not, write to the * + * Free Software Foundation, Inc., * + * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * + ***************************************************************************/ +#include +#include +#include + +#include +#include + +#include "list.h" +#include "logging.h" + +#include "event.h" + +static LIST_HEAD(readfd_list); +static LIST_HEAD(writefd_list); +static LIST_HEAD(exceptfd_list); +static LIST_HEAD(timeout_list); + +struct fd_entry { + struct list_head list; + int fd; + int type; + int (*callback)(int fd, void *privdata); + void *privdata; +}; + +struct timeout_entry { + struct list_head list; + struct timeval timeout; + struct timeval nextrun; + int (*callback)(void *privdata); + void *privdata; +}; + +int event_add_fd(int fd, int type, int (*callback)(int fd, void *privdata), void *privdata) +{ + if (fd < 0 || fd > FD_SETSIZE) + return -1; + + struct fd_entry *entry; + entry = malloc(sizeof(struct fd_entry)); + if (entry == NULL) { + log_print(LOG_ERROR, "event_add_fd(): out of memory"); + return -1; + } + + entry->fd = fd; + entry->type = type; + entry->callback = callback; + entry->privdata = privdata; + + switch (type) { + case FD_READ: + list_add_tail(&entry->list, &readfd_list); + break; + + case FD_WRITE: + list_add_tail(&entry->list, &writefd_list); + break; + + case FD_EXCEPT: + list_add_tail(&entry->list, &exceptfd_list); + break; + + default: + log_print(LOG_ERROR, "add_fd(): unknown type"); + free(entry); + return -1; + } + + return 0; +} + +int event_remove_fd(int fd) +{ + struct fd_entry *entry, *tmp; + list_for_each_entry_safe(entry, tmp, &readfd_list, list) { + if (entry->fd == fd) { + list_del(&entry->list); + free(entry); + return 0; + } + } + + list_for_each_entry_safe(entry, tmp, &writefd_list, list) { + if (entry->fd == fd) { + list_del(&entry->list); + free(entry); + return 0; + } + } + + list_for_each_entry_safe(entry, tmp, &exceptfd_list, list) { + if (entry->fd == fd) { + list_del(&entry->list); + free(entry); + return 0; + } + } + + return -1; +} + +static void calc_nextrun(struct timeval *timeout, struct timeval *nextrun) +{ + struct timeval now; + gettimeofday(&now, NULL); + + nextrun->tv_usec = now.tv_usec + timeout->tv_usec; + nextrun->tv_sec = now.tv_sec + timeout->tv_sec; + + if (nextrun->tv_usec >= 1000000) { + nextrun->tv_usec -= 1000000; + nextrun->tv_sec++; + } +} + +static void calc_timeout(struct timeval *timeout, struct timeval *nextrun) +{ + struct timeval now; + gettimeofday(&now, NULL); + + timeout->tv_usec = nextrun->tv_usec - now.tv_usec; + timeout->tv_sec = nextrun->tv_sec - now.tv_sec; + + if (timeout->tv_usec < 0) { + timeout->tv_usec += 1000000; + timeout->tv_sec--; + } +} + +static void schedule_nextrun(struct timeout_entry *entry) +{ + struct timeout_entry *search; + + list_for_each_entry(search, &timeout_list, list) { + if (search->nextrun.tv_sec > entry->nextrun.tv_sec) { + list_add_tail(&entry->list, &search->list); + return; + + } else if (search->nextrun.tv_sec == entry->nextrun.tv_sec && + search->nextrun.tv_usec > entry->nextrun.tv_usec) { + list_add_tail(&entry->list, &search->list); + return; + } + } + list_add_tail(&entry->list, &timeout_list); +} + +int event_add_timeout(struct timeval *timeout, int (*callback)(void *privdata), void *privdata) +{ + struct timeout_entry *entry; + entry = malloc(sizeof(struct timeout_entry)); + if (entry == NULL) { + log_print(LOG_ERROR, "event_add_timeout(): out of memory"); + return -1; + } + + memcpy(&entry->timeout, timeout, sizeof(entry->timeout)); + entry->callback = callback; + entry->privdata = privdata; + + calc_nextrun(&entry->timeout, &entry->nextrun); + schedule_nextrun(entry); + return 0; +} + +void event_loop(void) +{ + while (1) { + fd_set readfds, *readfds_p = NULL; + fd_set writefds, *writefds_p = NULL; + fd_set exceptfds, *exceptfds_p =NULL; + struct timeval timeout, *timeout_p = NULL; + + if (!list_empty(&readfd_list)) { + struct fd_entry *entry; + + FD_ZERO(&readfds); + list_for_each_entry(entry, &readfd_list, list) + FD_SET(entry->fd, &readfds); + + readfds_p = &readfds; + } + + if (!list_empty(&writefd_list)) { + struct fd_entry *entry; + + FD_ZERO(&writefds); + list_for_each_entry(entry, &writefd_list, list) + FD_SET(entry->fd, &writefds); + + writefds_p = &writefds; + } + + if (!list_empty(&exceptfd_list)) { + struct fd_entry *entry; + + FD_ZERO(&exceptfds); + list_for_each_entry(entry, &exceptfd_list, list) + FD_SET(entry->fd, &exceptfds); + + exceptfds_p = &exceptfds; + } + + if (!list_empty(&timeout_list)) { + struct timeout_entry *entry, *tmp; + list_for_each_entry_safe(entry, tmp, &timeout_list, list) { + + calc_timeout(&timeout, &entry->nextrun); + if (timeout.tv_sec >= 0 && timeout.tv_usec > 0) { + timeout_p = &timeout; + break; + } + + // delayed timeout, exec NOW! + list_del(&entry->list); + int ret = entry->callback(entry->privdata); + if (ret == 0) { + calc_nextrun(&entry->timeout, &entry->nextrun); + schedule_nextrun(entry); + + } else { + free(entry); + } + } + } + + int i = select(FD_SETSIZE, readfds_p, writefds_p, exceptfds_p, timeout_p); + if (i < 0) { + /* On error, -1 is returned, and errno is set + * appropriately; the sets and timeout become + * undefined, so do not rely on their contents + * after an error. + */ + continue; + + } else if (i == 0 && !list_empty(&timeout_list)) { + struct timeout_entry *entry; + entry = list_entry(timeout_list.next, typeof(*entry), list); + list_del(&entry->list); + + int ret = entry->callback(entry->privdata); + if (ret == 0) { + calc_nextrun(&entry->timeout, &entry->nextrun); + schedule_nextrun(entry); + + } else { + free(entry); + } + } + + if (readfds_p) { + struct fd_entry *entry, *tmp; + list_for_each_entry_safe(entry, tmp, &readfd_list, list) { + if (!FD_ISSET(entry->fd, &readfds)) + continue; + + if (entry->callback(entry->fd, entry->privdata) != 0) { + list_del(&entry->list); + free(entry); + } + } + } + + if (writefds_p) { + struct fd_entry *entry, *tmp; + list_for_each_entry_safe(entry, tmp, &writefd_list, list) { + if (FD_ISSET(entry->fd, &writefds)) + continue; + + if (entry->callback(entry->fd, entry->privdata) != 0) { + list_del(&entry->list); + free(entry); + } + } + } + + if (exceptfds_p) { + struct fd_entry *entry, *tmp; + list_for_each_entry_safe(entry, tmp, &exceptfd_list, list) { + if (FD_ISSET(entry->fd, &exceptfds)) + continue; + + if (entry->callback(entry->fd, entry->privdata) != 0) { + list_del(&entry->list); + free(entry); + } + } + } + } +} diff --git a/event.h b/event.h new file mode 100644 index 0000000..df7a396 --- /dev/null +++ b/event.h @@ -0,0 +1,26 @@ +#ifndef _EVENT_H_ +#define _EVENT_H_ + +#include + +#define FD_READ 0x01 +#define FD_WRITE 0x02 +#define FD_EXCEPT 0x04 + +#define event_add_readfd(fd, callback, privdata) \ + event_add_fd(fd, FD_READ, callback, privdata) + +#define event_add_writefd(fd, callback, privdata) \ + event_add_fd(fd, FD_WRITE, callback, privdata) + +#define event_add_exceptfd(fd, callback, privdata) \ + event_add_fd(fd, FD_EXCEPT, callback, privdata) + +int event_add_fd(int fd, int type, int (*callback)(int fd, void *privdata), void *privdata); +int event_add_timeout(struct timeval *timeout, int (*callback)(void *privdata), void *privdata); + +int event_remove_fd(int fd); + +void event_loop(void); + +#endif /* _EVENT_H_ */ diff --git a/list.h b/list.h new file mode 100644 index 0000000..61f8d93 --- /dev/null +++ b/list.h @@ -0,0 +1,268 @@ +#ifndef _LIST_H_ +#define _LIST_H_ + +/* + * stolen from linux kernel 2.6.11 (http://kernel.org/) + * linux/include/linux/stddef.h (offsetoff) + * linux/include/linux/kernel.h (container_of) + * linux/include/linux/list.h (*list*) + * linux/include/linux/netfilter_ipv4/listhelp.h (LIST_FIND) + * + * modified by Olaf Rempel + */ +#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER) + +#define container_of(ptr, type, member) ({ \ + const typeof( ((type *)0)->member ) *__mptr = (ptr); \ + (type *)( (char *)__mptr - offsetof(type,member) );}) + +struct list_head { + struct list_head *next, *prev; +}; + +#define LIST_HEAD_INIT(name) { &(name), &(name) } + +#define LIST_HEAD(name) \ + struct list_head name = LIST_HEAD_INIT(name) + +#define INIT_LIST_HEAD(ptr) do { \ + (ptr)->next = (ptr); (ptr)->prev = (ptr); \ +} while (0) + +/* + * Insert a new entry between two known consecutive entries. + * + * This is only for internal list manipulation where we know + * the prev/next entries already! + */ +static inline void __list_add(struct list_head *new, + struct list_head *prev, + struct list_head *next) +{ + next->prev = new; + new->next = next; + new->prev = prev; + prev->next = new; +} + +/* + * list_add - add a new entry + * @new: new entry to be added + * @head: list head to add it after + * + * Insert a new entry after the specified head. + * This is good for implementing stacks. + */ +static inline void list_add(struct list_head *new, struct list_head *head) +{ + __list_add(new, head, head->next); +} + +/* + * list_add_tail - add a new entry + * @new: new entry to be added + * @head: list head to add it before + * + * Insert a new entry before the specified head. + * This is useful for implementing queues. + */ +static inline void list_add_tail(struct list_head *new, struct list_head *head) +{ + __list_add(new, head->prev, head); +} + +/* + * Delete a list entry by making the prev/next entries + * point to each other. + * + * This is only for internal list manipulation where we know + * the prev/next entries already! + */ +static inline void __list_del(struct list_head * prev, struct list_head * next) +{ + next->prev = prev; + prev->next = next; +} + +/* + * list_del - deletes entry from list. + * @entry: the element to delete from the list. + * Note: list_empty on entry does not return true after this, the entry is + * in an undefined state. + */ +static inline void list_del(struct list_head *entry) +{ + __list_del(entry->prev, entry->next); + entry->next = NULL; + entry->prev = NULL; +} + +/* + * list_del_init - deletes entry from list and reinitialize it. + * entry: the element to delete from the list. + */ +static inline void list_del_init(struct list_head *entry) +{ + __list_del(entry->prev, entry->next); + INIT_LIST_HEAD(entry); +} + +/* + * list_move - delete from one list and add as another's head + * @list: the entry to move + * @head: the head that will precede our entry + */ +static inline void list_move(struct list_head *list, struct list_head *head) +{ + __list_del(list->prev, list->next); + list_add(list, head); +} + +/* + * list_move_tail - delete from one list and add as another's tail + * @list: the entry to move + * @head: the head that will follow our entry + */ +static inline void list_move_tail(struct list_head *list, + struct list_head *head) +{ + __list_del(list->prev, list->next); + list_add_tail(list, head); +} + +/* + * list_empty - tests whether a list is empty + * @head: the list to test. + */ +static inline int list_empty(const struct list_head *head) +{ + return head->next == head; +} + +static inline void __list_splice(struct list_head *list, + struct list_head *head) +{ + struct list_head *first = list->next; + struct list_head *last = list->prev; + struct list_head *at = head->next; + + first->prev = head; + head->next = first; + + last->next = at; + at->prev = last; +} + +/* + * list_splice - join two lists + * @list: the new list to add. + * @head: the place to add it in the first list. + */ +static inline void list_splice(struct list_head *list, struct list_head *head) +{ + if (!list_empty(list)) + __list_splice(list, head); +} + +/* + * list_splice_init - join two lists and reinitialise the emptied list. + * @list: the new list to add. + * @head: the place to add it in the first list. + * + * The list at @list is reinitialised + */ +static inline void list_splice_init(struct list_head *list, + struct list_head *head) +{ + if (!list_empty(list)) { + __list_splice(list, head); + INIT_LIST_HEAD(list); + } +} + +/* + * list_entry - get the struct for this entry + * @ptr: the &struct list_head pointer. + * @type: the type of the struct this is embedded in. + * @member: the name of the list_struct within the struct. + */ +#define list_entry(ptr, type, member) \ + container_of(ptr, type, member) + +/* + * list_for_each - iterate over a list + * @pos: the &struct list_head to use as a loop counter. + * @head: the head for your list. + */ +#define list_for_each(pos, head) \ + for (pos = (head)->next; pos != (head); pos = pos->next) + +/* + * list_for_each_prev - iterate over a list backwards + * @pos: the &struct list_head to use as a loop counter. + * @head: the head for your list. + */ +#define list_for_each_prev(pos, head) \ + for (pos = (head)->prev; pos != (head); pos = pos->prev) + +/* + * list_for_each_safe - iterate over a list safe against removal of list entry + * @pos: the &struct list_head to use as a loop counter. + * @n: another &struct list_head to use as temporary storage + * @head: the head for your list. + */ +#define list_for_each_safe(pos, n, head) \ + for (pos = (head)->next, n = pos->next; pos != (head); \ + pos = n, n = pos->next) + +/* + * list_for_each_entry - iterate over list of given type + * @pos: the type * to use as a loop counter. + * @head: the head for your list. + * @member: the name of the list_struct within the struct. + */ +#define list_for_each_entry(pos, head, member) \ + for (pos = list_entry((head)->next, typeof(*pos), member); \ + &pos->member != (head); \ + pos = list_entry(pos->member.next, typeof(*pos), member)) + +/* + * list_for_each_entry_reverse - iterate backwards over list of given type. + * @pos: the type * to use as a loop counter. + * @head: the head for your list. + * @member: the name of the list_struct within the struct. + */ +#define list_for_each_entry_reverse(pos, head, member) \ + for (pos = list_entry((head)->prev, typeof(*pos), member); \ + &pos->member != (head); \ + pos = list_entry(pos->member.prev, typeof(*pos), member)) + +/* + * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry + * @pos: the type * to use as a loop counter. + * @n: another type * to use as temporary storage + * @head: the head for your list. + * @member: the name of the list_struct within the struct. + */ +#define list_for_each_entry_safe(pos, n, head, member) \ + for (pos = list_entry((head)->next, typeof(*pos), member), \ + n = list_entry(pos->member.next, typeof(*pos), member); \ + &pos->member != (head); \ + pos = n, n = list_entry(n->member.next, typeof(*n), member)) + + +/* Return pointer to first true entry, if any, or NULL. A macro + required to allow inlining of cmpfn. */ +#define LIST_FIND(head, cmpfn, type, args...) \ +({ \ + const struct list_head *__i, *__j = NULL; \ + \ + list_for_each(__i, (head)) \ + if (cmpfn((const type)__i , ## args)) { \ + __j = __i; \ + break; \ + } \ + (type)__j; \ +}) + +#endif /* _LIST_H_ */ diff --git a/logging.c b/logging.c new file mode 100644 index 0000000..d4fd706 --- /dev/null +++ b/logging.c @@ -0,0 +1,103 @@ +/*************************************************************************** + * Copyright (C) 06/2006 by Olaf Rempel * + * razzor@kopf-tisch.de * + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 2 of the License, or * + * (at your option) any later version. * + * * + * This program is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program; if not, write to the * + * Free Software Foundation, Inc., * + * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * + ***************************************************************************/ +#include +#include +#include +#include +#include +#include + +#include "logging.h" + +#define BUFSIZE 8192 + +static FILE *log_fd = NULL; +static char *buffer = NULL; + +void log_print(int prio, const char *fmt, ...) +{ + va_list az; + int len; + + if (buffer == NULL) { + buffer = malloc(BUFSIZE); + if (buffer == NULL) { + fprintf(stderr, "log_print: out of memory\nBailing out!\n"); + exit(-1); + } + } + + va_start(az, fmt); + len = vsnprintf(buffer, BUFSIZE, fmt, az); + va_end(az); + + if (len < 0 || len >= BUFSIZE) { + log_print(LOG_ERROR, "log_print: arguments too long"); + errno = 0; + return; + } + + if (errno) { + strncpy(buffer + len, ": ", BUFSIZE - len); + len += 2; + strncpy(buffer + len, strerror(errno), BUFSIZE - len); + } + + if (log_fd) { + char tbuf[64]; + time_t tzgr; + + time(&tzgr); + strftime(tbuf, sizeof(tbuf), "%b %d %H:%M:%S :", localtime(&tzgr)); + + fprintf(log_fd, "%s %s\n", tbuf, buffer); + fflush(log_fd); + + } else { + fprintf(stderr, "%s\n", buffer); + } + + errno = 0; +} + +static void log_close(void) +{ + if (buffer) + free(buffer); + + fclose(log_fd); +} + +int log_init(char *logfile) +{ + log_fd = fopen(logfile, "a"); + if (log_fd == NULL) { + log_print(LOG_ERROR, "log_open('%s'): %s", logfile); + return 0; + } + + if (atexit(log_close) != 0) { + log_print(LOG_ERROR, "log_open(): atexit()"); + return 0; + } + + log_print(LOG_EVERYTIME, "=========================="); + return 1; +} diff --git a/logging.h b/logging.h new file mode 100644 index 0000000..c6e32c0 --- /dev/null +++ b/logging.h @@ -0,0 +1,16 @@ +#ifndef _LOGGING_H_ +#define _LOGGING_H_ + +#define LOG_DEBUG 5 +#define LOG_INFO 4 +#define LOG_NOTICE 3 +#define LOG_WARN 2 +#define LOG_ERROR 1 +#define LOG_CRIT 0 + +#define LOG_EVERYTIME 0 + +int log_init(char *logfile); +void log_print(int prio, const char *fmt, ... ); + +#endif /* _LOGGING_H_ */ diff --git a/network.c b/network.c new file mode 100644 index 0000000..f82dcc1 --- /dev/null +++ b/network.c @@ -0,0 +1,78 @@ +#include +#include +#include +#include +#include +#include + +#include "logging.h" + +int parse_saddr(const char *addr, struct sockaddr_in *sa) +{ + char *addr_cpy = strdup(addr); + if (addr_cpy == NULL) { + log_print(LOG_WARN, "parse_saddr(): out of memory"); + return -1; + } + + char *tmp; + char *ip = strtok_r(addr_cpy, ":", &tmp); + if (ip == NULL) { + free(addr_cpy); + return -1; + } + + char *port = strtok_r(NULL, ":", &tmp); + if (port == NULL) { + free(addr_cpy); + return -1; + } + + sa->sin_family = AF_INET; + sa->sin_port = htons(atoi(port)); + int ret = inet_aton(ip, &sa->sin_addr); + + free(addr_cpy); + return (ret != 0) ? 0 : -1; +} + +int get_sockaddr(struct sockaddr_in *sa, char *buf, int size) +{ + return snprintf(buf, size, "%s:%d", inet_ntoa(sa->sin_addr), ntohs(sa->sin_port)); +} + +char * get_sockaddr_buf(struct sockaddr_in *sa) +{ + static char buf[24]; + get_sockaddr(sa, buf, sizeof(buf)); + return buf; +} + +int tcp_listen_socket(struct sockaddr_in *sa, int cnt) +{ + int sock = socket(AF_INET, SOCK_STREAM, 0); + if (sock < 0 ) { + log_print(LOG_ERROR, "tcp_listen_socket(): socket()"); + return -1; + } + + int i = 1; + if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &i, sizeof(i))) { + log_print(LOG_ERROR, "tcp_listen_socket(): setsockopt(SO_REUSEADDR)"); + close(sock); + return -1; + } + + if (bind(sock, (struct sockaddr *)sa, sizeof(*sa))) { + log_print(LOG_ERROR, "tcp_listen_socket(): bind(%s)", get_sockaddr_buf(sa)); + close(sock); + return -1; + } + + if (listen(sock, cnt)) { + log_print(LOG_ERROR, "tcp_listen_socket(): listen()"); + close(sock); + return -1; + } + return sock; +} diff --git a/network.h b/network.h new file mode 100644 index 0000000..25923ae --- /dev/null +++ b/network.h @@ -0,0 +1,12 @@ +#ifndef _NETWORK_H_ +#define _NETWORK_H_ + +#include + +int parse_saddr(const char *addr, struct sockaddr_in *sa); +int tcp_listen_socket(struct sockaddr_in *sa, int cnt); + +int get_sockaddr(struct sockaddr_in *sa, char *buf, int size); +char * get_sockaddr_buf(struct sockaddr_in *sa); + +#endif // _NETWORK_H_ diff --git a/telnetproxy.c b/telnetproxy.c new file mode 100644 index 0000000..97cbcef --- /dev/null +++ b/telnetproxy.c @@ -0,0 +1,117 @@ +#include +#include +#include + +#include "configfile.h" +#include "connection.h" +#include "event.h" +#include "list.h" +#include "logging.h" +#include "network.h" + +static LIST_HEAD(connection_list); + +static int server_handler(int fd, void *privdata) +{ + struct connection *con = (struct connection *)privdata; + char buf[256]; + + int len = read(fd, buf, sizeof(buf)); + if (len <= 0) { + list_del(&con->list); + destroy_connection(con); + return -1; + } + + write(con->src_fd, buf, len); + return 0; +} + +static int client_handler(int fd, void *privdata) +{ + struct connection *con = (struct connection *)privdata; + char buf[256]; + + int len = read(fd, buf, sizeof(buf)); + if (len <= 0) { + list_del(&con->list); + destroy_connection(con); + return -1; + } + + if (con->dst_fd != -1) { + write(con->dst_fd, buf, len); + return 0; + } + + // check destination + int sock = tcp_connect_socket(&con->dst_addr); + if (sock < 0) { + list_del(&con->list); + destroy_connection(con); + return -1; + } + + event_add_readfd(sock, server_handler, con); + log_print(LOG_INFO, "forwarding to %s", get_sockaddr_buf(&con->dst_addr)); + return 0; +} + +static int listen_handler(int fd, void *privdata) +{ + struct connection *con = malloc(sizeof(struct connection)); + if (con == NULL) { + log_print(LOG_WARN, "listen_handler(): out of memory"); + return 0; + } + + int i = sizeof(con->src_addr); + con->src_fd = accept(fd, (struct sockaddr *)&con->src_addr, &i); + if (con->src_fd < 0) { + free(con); + return 0; + } + + con->dst_fd = -1; + con->login_time = con->idle_time = time(NULL); + + log_print(LOG_INFO, "accepted connection from %s", get_sockaddr_buf(&con->src_addr)); + + list_add_tail(&con->list, &connection_list); + event_add_readfd(con->src_fd, client_handler, con); + return 0; +} + +static int listen_init(const char *value, void *privdata) +{ + struct sockaddr_in addr; + + if (parse_saddr(value, &addr) == -1) { + log_print(LOG_WARN, "invalid listen addr: '%s'", value); + return -1; + } + + int sock = tcp_listen_socket(&addr, 10); + if (sock < 0) + return -1; + + event_add_readfd(sock, listen_handler, NULL); + log_print(LOG_INFO, "listen on %s", get_sockaddr_buf(&addr)); + return 0; +} + +int main(int argc, char *argv[]) +{ + if (config_parse("telnetproxy.conf") == -1) + return -1; + + int cnt = config_get_strings("global", "listen", listen_init, NULL); + if (cnt <= 0) { + log_print(LOG_ERROR, "no listen sockets defined!"); + return -1; + } + + event_loop(); + + return 0; +} diff --git a/telnetproxy.conf b/telnetproxy.conf new file mode 100644 index 0000000..647179e --- /dev/null +++ b/telnetproxy.conf @@ -0,0 +1,2 @@ +[global] +listen 0.0.0.0:8000