tg/net.h

104 lines
2.9 KiB
C
Raw Normal View History

/*
This file is part of tgl-library
2013-10-23 10:26:17 -04:00
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
2013-10-23 10:26:17 -04:00
This library is distributed in the hope that it will be useful,
2013-10-23 10:26:17 -04:00
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
2013-10-23 10:26:17 -04:00
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
2013-10-23 10:26:17 -04:00
Copyright Vitaly Valtman 2013-2014
2013-10-23 10:26:17 -04:00
*/
2013-10-11 16:52:20 -04:00
#ifndef __NET_H__
#define __NET_H__
#include <poll.h>
#define DC_SERIALIZED_MAGIC 0x64582faa
#define DC_SERIALIZED_MAGIC_V2 0x94032abb
2013-11-04 12:34:27 -05:00
#define STATE_FILE_MAGIC 0x84217a0d
#define SECRET_CHAT_FILE_MAGIC 0xa9840add
2014-08-20 23:24:52 -04:00
struct tgl_dc_serialized {
2013-10-11 16:52:20 -04:00
int magic;
int port;
char ip[64];
char user[64];
char auth_key[256];
long long auth_key_id, server_salt;
int authorized;
};
struct connection_buffer {
unsigned char *start;
unsigned char *end;
unsigned char *rptr;
unsigned char *wptr;
2013-10-11 16:52:20 -04:00
struct connection_buffer *next;
};
enum conn_state {
conn_none,
conn_connecting,
conn_ready,
conn_failed,
conn_stopped
};
struct connection {
int fd;
char *ip;
2013-10-11 16:52:20 -04:00
int port;
int flags;
enum conn_state state;
int ipv6[4];
struct connection_buffer *in_head;
struct connection_buffer *in_tail;
struct connection_buffer *out_head;
struct connection_buffer *out_tail;
int in_bytes;
int out_bytes;
int packet_num;
int out_packet_num;
int last_connect_time;
2014-01-27 12:23:58 -05:00
int in_fail_timer;
2014-08-14 14:03:33 -04:00
struct mtproto_methods *methods;
2014-08-20 23:24:52 -04:00
struct tgl_session *session;
struct tgl_dc *dc;
2013-10-11 16:52:20 -04:00
void *extra;
2014-08-14 14:03:33 -04:00
struct event *ping_ev;
struct event *fail_ev;
struct event *read_ev;
struct event *write_ev;
double last_receive_time;
2013-10-11 16:52:20 -04:00
};
2014-08-14 14:03:33 -04:00
//extern struct connection *Connections[];
int tgln_write_out (struct connection *c, const void *data, int len);
void tgln_flush_out (struct connection *c);
int tgln_read_in (struct connection *c, void *data, int len);
int tgln_read_in_lookup (struct connection *c, void *data, int len);
2014-08-20 23:24:52 -04:00
void tgln_insert_msg_id (struct tgl_session *S, long long id);
2013-10-11 16:52:20 -04:00
2014-08-15 10:57:43 -04:00
extern struct tgl_net_methods tgl_conn_methods;
2013-10-11 16:52:20 -04:00
2014-08-14 14:03:33 -04:00
//void create_all_outbound_connections (void);
2013-10-11 16:52:20 -04:00
2014-08-20 23:24:52 -04:00
//struct connection *create_connection (const char *host, int port, struct tgl_session *session, struct connection_methods *methods);
struct tgl_dc *tgln_alloc_dc (int id, char *ip, int port);
void tgln_dc_create_session (struct tgl_dc *DC, struct mtproto_methods *methods);
struct connection *tgln_create_connection (const char *host, int port, struct tgl_session *session, struct tgl_dc *dc, struct mtproto_methods *methods);
2013-10-11 16:52:20 -04:00
#define GET_DC(c) (c->session->dc)
#endif