Added listening socket support

This commit is contained in:
vvaltman 2014-09-21 02:22:35 +04:00
parent 55b763c87f
commit 091b0608cb
6 changed files with 697 additions and 457 deletions

View File

@ -149,13 +149,6 @@ void tgl_paramed_type_free (struct paramed_type *P) {
free (P);
}
static void print_offset (void) {
int i;
for (i = 0; i < multiline_offset; i++) {
printf (" ");
}
}
static char *buffer_pos, *buffer_end;
static int is_wspc (char c) {
@ -430,6 +423,13 @@ static void print_escaped_string (const char *str, int len) {
eprintf ("\"");
}
static void print_offset (void) {
int i;
for (i = 0; i < multiline_offset; i++) {
eprintf (" ");
}
}
char *tglf_extf_fetch (struct paramed_type *T) {
out_buf_pos = 0;
fetch_type_any (T);

File diff suppressed because it is too large Load Diff

View File

@ -38,6 +38,7 @@ char *get_default_prompt (void);
char *complete_none (const char *text, int state);
char **complete_text (char *text, int start, int end);
void interpreter (char *line);
void interpreter_ex (char *line, void *ex);
void rprintf (const char *format, ...) __attribute__ ((format (printf, 1, 2)));
void logprintf (const char *format, ...) __attribute__ ((format (printf, 1, 2)));
@ -52,19 +53,31 @@ void logprintf (const char *format, ...) __attribute__ ((format (printf, 1, 2)))
//void hexdump (int *in_ptr, int *in_end);
struct bufferevent;
struct in_ev {
struct bufferevent *bev;
char in_buf[4096];
int in_buf_pos;
int refcnt;
int error;
};
struct tgl_message;
void print_message (struct tgl_message *M);
void print_chat_name (tgl_peer_id_t id, tgl_peer_t *C);
void print_user_name (tgl_peer_id_t id, tgl_peer_t *U);
void print_encr_chat_name_full (tgl_peer_id_t id, tgl_peer_t *C);
void print_encr_chat_name (tgl_peer_id_t id, tgl_peer_t *C);
struct in_ev;
void print_message (struct in_ev *ev, struct tgl_message *M);
void print_chat_name (struct in_ev *ev, tgl_peer_id_t id, tgl_peer_t *C);
void print_user_name (struct in_ev *ev, tgl_peer_id_t id, tgl_peer_t *U);
void print_encr_chat_name_full (struct in_ev *ev, tgl_peer_id_t id, tgl_peer_t *C);
void print_encr_chat_name (struct in_ev *ev, tgl_peer_id_t id, tgl_peer_t *C);
//void print_media (struct tgl_message_media *M);
//
void pop_color (void);
void push_color (const char *color);
void print_start (void);
void print_end (void);
void print_date_full (long t);
void print_date (long t);
void print_date_full (struct in_ev *ev, long t);
void print_date (struct in_ev *ev, long t);
void play_sound (void);
void update_prompt (void);

71
loop.c
View File

@ -45,6 +45,7 @@
#ifdef EVENT_V2
#include <event2/event.h>
#include <event2/bufferevent.h>
#else
#include <event.h>
#include "event-old.h"
@ -75,6 +76,8 @@ extern int sync_from_start;
extern int disable_output;
extern int reset_authorization;
extern int sfd;
void got_it (char *line, int len);
void write_state_file (void);
@ -584,6 +587,68 @@ void dlist_cb (void *callback_extra, int success, int size, tgl_peer_id_t peers[
d_got_ok = 1;
}
static void read_incoming (struct bufferevent *bev, void *_arg) {
vlogprintf (E_WARNING, "Read from incoming connection\n");
struct in_ev *ev = _arg;
assert (ev->bev == bev);
ev->in_buf_pos += bufferevent_read (bev, ev->in_buf + ev->in_buf_pos, 4096 - ev->in_buf_pos);
while (1) {
int pos = 0;
int ok = 0;
while (pos < ev->in_buf_pos) {
if (ev->in_buf[pos] == '\n') {
if (!ev->error) {
ev->in_buf[pos] = 0;
interpreter_ex (ev->in_buf, ev);
} else {
ev->error = 0;
}
ok = 1;
ev->in_buf_pos -= (pos + 1);
memmove (ev->in_buf, ev->in_buf + pos + 1, ev->in_buf_pos);
pos = 0;
} else {
pos ++;
}
}
if (ok) {
ev->in_buf_pos += bufferevent_read (bev, ev->in_buf + ev->in_buf_pos, 4096 - ev->in_buf_pos);
} else {
if (ev->in_buf_pos == 4096) {
ev->error = 1;
}
break;
}
}
}
static void event_incoming (struct bufferevent *bev, short what, void *_arg) {
struct in_ev *ev = _arg;
if (what & (BEV_EVENT_EOF | BEV_EVENT_ERROR)) {
vlogprintf (E_WARNING, "Closing incoming connection\n");
bufferevent_free (bev);
ev->bev = 0;
ev->refcnt --;
}
}
static void accept_incoming (evutil_socket_t efd, short what, void *arg) {
unsigned clilen;
struct sockaddr_in cli_addr;
int fd = accept (sfd, (struct sockaddr *)&cli_addr, &clilen);
assert (fd >= 0);
struct bufferevent *bev = bufferevent_socket_new (tgl_state.ev_base, fd, BEV_OPT_CLOSE_ON_FREE);
struct in_ev *e = malloc (sizeof (*e));
e->bev = bev;
e->refcnt = 1;
e->in_buf_pos = 0;
e->error = 0;
bufferevent_setcb (bev, read_incoming, 0, event_incoming, e);
bufferevent_enable(bev, EV_READ|EV_WRITE);
}
int loop (void) {
//on_start ();
tgl_set_callback (&upd_cb);
@ -605,10 +670,16 @@ int loop (void) {
read_state_file ();
read_secret_chat_file ();
}
binlog_read = 1;
#ifdef USE_LUA
lua_binlog_end ();
#endif
if (sfd >= 0) {
struct event *ev = event_new (tgl_state.ev_base, sfd, EV_READ, accept_incoming, 0);
event_add (ev, 0);
}
update_prompt ();
if (reset_authorization) {

38
main.c
View File

@ -48,6 +48,7 @@
#endif
#include <grp.h>
#include <arpa/inet.h>
#include "telegram.h"
#include "loop.h"
@ -98,6 +99,7 @@ int disable_colors;
int readline_disabled;
int disable_output;
int reset_authorization;
int port;
void set_default_username (const char *s) {
if (default_username) {
@ -420,6 +422,7 @@ void usage (void) {
printf (" -U <user-name> change uid after start\n");
printf (" -G <group-name> change gid after start\n");
printf (" -D disable output\n");
printf (" -P <port> port to listen for input commands\n");
exit (1);
}
@ -520,7 +523,7 @@ int change_user_group () {
void args_parse (int argc, char **argv) {
int opt = 0;
while ((opt = getopt (argc, argv, "u:hk:vNl:fEwWCRdL:DU:G:q"
while ((opt = getopt (argc, argv, "u:hk:vNl:fEwWCRdL:DU:G:qP:"
#ifdef HAVE_LIBCONFIG
"c:p:"
#else
@ -603,6 +606,9 @@ void args_parse (int argc, char **argv) {
case 'q':
reset_authorization ++;
break;
case 'P':
port = atoi (optarg);
break;
case 'h':
default:
usage ();
@ -643,14 +649,42 @@ void sig_abrt_handler (int signum __attribute__ ((unused))) {
exit (EXIT_FAILURE);
}
int sfd;
int main (int argc, char **argv) {
change_user_group ();
signal (SIGSEGV, sig_segv_handler);
signal (SIGABRT, sig_abrt_handler);
log_level = 10;
args_parse (argc, argv);
change_user_group ();
if (port > 0) {
struct sockaddr_in serv_addr;
sfd = socket (AF_INET, SOCK_STREAM, 0);
if (sfd < 0) {
perror ("socket");
exit(1);
}
memset (&serv_addr, 0, sizeof (serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = htonl (0x7f000001);
serv_addr.sin_port = htons (port);
if (bind (sfd, (struct sockaddr *) &serv_addr, sizeof (serv_addr)) < 0) {
perror ("bind");
exit(1);
}
listen (sfd,5);
} else {
sfd = -1;
}
if (daemonize) {
signal (SIGHUP, sighup_handler);

4
tgl.h
View File

@ -30,8 +30,8 @@
#define TG_SERVER_TEST_DC 2
// JUST RANDOM STRING
#define TGL_BUILD "2013"
#define TGL_VERSION "1.0.4"
#define TGL_BUILD "2014"
#define TGL_VERSION "1.0.5"
#define TGL_ENCRYPTED_LAYER 16