00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00039 #ifndef _core_sockets_h_
00040 #define _core_sockets_h_
00041
00042 #include "common.h"
00043
00044 #include "if/core/wrap.h"
00045 #include "if/core/sockets.h"
00046
00047 #include "lib/inputevt.h"
00048
00049 #ifdef HAS_GNUTLS
00050 #include <gnutls/gnutls.h>
00051
00052 enum socket_tls_stage {
00053 SOCK_TLS_NONE = 0,
00054 SOCK_TLS_INITIALIZED = 1,
00055 SOCK_TLS_ESTABLISHED = 2
00056 };
00057
00058 struct socket_tls_ctx {
00059 gnutls_session session;
00060 gboolean enabled;
00061 enum socket_tls_stage stage;
00062 size_t snarf;
00064 inputevt_cond_t cb_cond;
00065 inputevt_handler_t cb_handler;
00066 gpointer cb_data;
00067 };
00068
00069 #define SOCKET_WITH_TLS(s) \
00070 ((s)->tls.enabled && (s)->tls.stage >= SOCK_TLS_INITIALIZED)
00071 #define SOCKET_USES_TLS(s) \
00072 ((s)->tls.enabled && (s)->tls.stage >= SOCK_TLS_ESTABLISHED)
00073 #else
00074 #define SOCKET_WITH_TLS(s) ((void) (s), 0)
00075 #define SOCKET_USES_TLS(s) ((void) (s), 0)
00076 #endif
00077
00078 struct sockaddr;
00079
00080
00081
00082
00083
00084 enum socket_direction {
00085 SOCK_CONN_INCOMING,
00086 SOCK_CONN_OUTGOING,
00087 SOCK_CONN_LISTENING,
00088 SOCK_CONN_PROXY_OUTGOING
00089 };
00090
00095 enum socket_type {
00096 SOCK_TYPE_UNKNOWN = 0,
00097 SOCK_TYPE_CONTROL,
00098 SOCK_TYPE_DOWNLOAD,
00099 SOCK_TYPE_UPLOAD,
00100 SOCK_TYPE_HTTP,
00101 SOCK_TYPE_SHELL,
00102 SOCK_TYPE_CONNBACK,
00103 SOCK_TYPE_PPROXY,
00104 SOCK_TYPE_DESTROYING,
00105 SOCK_TYPE_UDP
00106 };
00107
00108 struct gnutella_socket {
00109 gint file_desc;
00110 guint32 flags;
00111 guint gdk_tag;
00113 enum socket_direction direction;
00114 enum socket_type type;
00115 enum net_type net;
00116 gboolean corked;
00117 gboolean was_shutdown;
00118 gint adns;
00119 gchar *adns_msg;
00121 host_addr_t addr;
00122 guint16 port;
00124 guint16 local_port;
00126 time_t last_update;
00128 struct wrap_io wio;
00130 #ifdef HAS_GNUTLS
00131 struct socket_tls_ctx tls;
00132 #endif
00133
00134 union {
00135 struct gnutella_node *node;
00136 struct download *download;
00137 struct upload *upload;
00138 struct pproxy *pproxy;
00139 struct cproxy *cproxy;
00140 gpointer handle;
00141 } resource;
00142
00143 struct getline *getline;
00145 gchar buffer[SOCK_BUFSZ];
00146 size_t pos;
00147 };
00148
00149
00150
00151
00152
00153 #define SOCK_F_ESTABLISHED 0x00000001
00154 #define SOCK_F_EOF 0x00000002
00155 #define SOCK_F_UDP 0x40000000
00156 #define SOCK_F_TCP 0x80000000
00162 #define sock_is_corked(x) ((x)->corked)
00163
00168 #define udp_active() (enable_udp && NULL != s_udp_listen)
00169
00170
00171
00172
00173
00174
00175
00176 extern struct gnutella_socket *s_tcp_listen;
00177 extern struct gnutella_socket *s_udp_listen;
00178
00179
00180
00181
00182
00183 void socket_init(void);
00184 void socket_register_fd_reclaimer(reclaim_fd_t callback);
00185 void socket_eof(struct gnutella_socket *s);
00186 void socket_free(struct gnutella_socket *);
00187 struct gnutella_socket *socket_connect(const host_addr_t, guint16,
00188 enum socket_type, guint32 flags);
00189 struct gnutella_socket *socket_connect_by_name(
00190 const gchar *host, guint16, enum socket_type, guint32 flags);
00191 struct gnutella_socket *socket_tcp_listen(const host_addr_t, guint16,
00192 enum socket_type);
00193 struct gnutella_socket *socket_udp_listen(const host_addr_t, guint16);
00194 void socket_evt_set(struct gnutella_socket *s,
00195 inputevt_cond_t cond, inputevt_handler_t handler, gpointer data);
00196 void socket_evt_clear(struct gnutella_socket *s);
00197
00198 void sock_cork(struct gnutella_socket *s, gboolean on);
00199 void sock_send_buf(struct gnutella_socket *s, gint size, gboolean shrink);
00200 void sock_recv_buf(struct gnutella_socket *s, gint size, gboolean shrink);
00201 void sock_nodelay(struct gnutella_socket *s, gboolean on);
00202 void sock_tx_shutdown(struct gnutella_socket *s);
00203 void socket_tos_default(const struct gnutella_socket *s);
00204 void socket_tos_throughput(const struct gnutella_socket *s);
00205 void socket_tos_lowdelay(const struct gnutella_socket *s);
00206 void socket_tos_normal(const struct gnutella_socket *s);
00207 gboolean socket_bad_hostname(struct gnutella_socket *s);
00208
00209 void socket_timer(time_t now);
00210 void socket_shutdown(void);
00211
00212 ssize_t safe_readv(wrap_io_t *wio, struct iovec *iov, gint iovcnt);
00213 ssize_t safe_writev(wrap_io_t *wio, struct iovec *iov, gint iovcnt);
00214 ssize_t safe_writev_fd(gint fd, struct iovec *iov, gint iovcnt);
00215
00216 #endif
00217
00218