1 /*
2  * Copyright (c) 2005 Atheme Development Group
3  * Rights to this code are as documented in doc/LICENSE.
4  *
5  * This contains the connection_t structure.
6  *
7  */
8 
9 #ifndef CONNECTION_H
10 #define CONNECTION_H
11 
12 #ifndef _WIN32
13 # define ioerrno()	errno
14 #else
15 # define ioerrno()	WSAGetLastError()
16 #endif
17 
18 typedef union sockaddr_any_ sockaddr_any_t;
19 
20 union sockaddr_any_
21 {
22 	struct sockaddr sa;
23 	struct sockaddr_in sin;
24 	struct sockaddr_in6 sin6;
25 };
26 
27 #define SOCKADDR(foo) 		(struct sockaddr 	*) &(foo)
28 #define SOCKADDR_IN(foo) 	(struct sockaddr_in	*) &(foo)
29 #define SOCKADDR_IN6(foo) 	(struct sockaddr_in6	*) &(foo)
30 
31 #include "res.h"
32 
33 typedef struct connection_ connection_t;
34 
35 struct connection_
36 {
37 	char name[HOSTLEN];
38 	char hbuf[BUFSIZE + 1];
39 
40 	mowgli_list_t recvq;
41 	mowgli_list_t sendq;
42 
43 	int fd;
44 	int pollslot;
45 
46 	time_t first_recv;
47 	time_t last_recv;
48 
49 	size_t sendq_limit;
50 
51 	sockaddr_any_t saddr;
52 	socklen_t saddr_size;
53 
54 	void (*read_handler)(connection_t *);
55 	void (*write_handler)(connection_t *);
56 
57 	unsigned int flags;
58 
59 	void (*recvq_handler)(connection_t *);
60 	void (*close_handler)(connection_t *);
61 
62 	connection_t *listener;
63 	void *userdata;
64 
65 	mowgli_eventloop_pollable_t *pollable;
66 };
67 
68 #define CF_UPLINK     0x00000001
69 #define CF_DCCOUT     0x00000002
70 #define CF_DCCIN      0x00000004
71 
72 #define CF_CONNECTING 0x00000008
73 #define CF_LISTENING  0x00000010
74 #define CF_CONNECTED  0x00000020
75 #define CF_DEAD       0x00000040
76 
77 #define CF_NONEWLINE  0x00000080
78 #define CF_SEND_EOF   0x00000100 /* shutdown(2) write end if sendq empty */
79 #define CF_SEND_DEAD  0x00000200 /* write end shut down */
80 
81 #define CF_IS_UPLINK(x) ((x)->flags & CF_UPLINK)
82 #define CF_IS_DCC(x) ((x)->flags & (CF_DCCOUT | CF_DCCIN))
83 #define CF_IS_DCCOUT(x) ((x)->flags & CF_DCCOUT)
84 #define CF_IS_DCCIN(x) ((x)->flags & CF_DCCIN)
85 #define CF_IS_DEAD(x) ((x)->flags & CF_DEAD)
86 #define CF_IS_CONNECTING(x) ((x)->flags & CF_CONNECTING)
87 #define CF_IS_LISTENING(x) ((x)->flags & CF_LISTENING)
88 
89 extern connection_t *connection_add(const char *, int, unsigned int,
90 	void(*)(connection_t *),
91 	void(*)(connection_t *));
92 extern connection_t *connection_open_tcp(char *, char *, unsigned int,
93 	void(*)(connection_t *),
94 	void(*)(connection_t *));
95 extern connection_t *connection_open_listener_tcp(char *, unsigned int,
96 	void(*)(connection_t *));
97 extern connection_t *connection_accept_tcp(connection_t *,
98 	void(*)(connection_t *),
99 	void(*)(connection_t *));
100 extern void connection_setselect_read(connection_t *, void(*)(connection_t *));
101 extern void connection_setselect_write(connection_t *, void(*)(connection_t *));
102 extern void connection_close(connection_t *);
103 extern void connection_close_soon(connection_t *);
104 extern void connection_close_soon_children(connection_t *);
105 extern void connection_close_all(void);
106 extern void connection_close_all_fds(void);
107 extern void connection_stats(void (*)(const char *, void *), void *);
108 extern connection_t *connection_find(int);
109 //inline int connection_count(void);
110 
111 extern mowgli_list_t connection_list;
112 
113 #endif
114 
115 /* vim:cinoptions=>s,e0,n0,f0,{0,}0,^0,=s,ps,t0,c3,+s,(2s,us,)20,*30,gs,hs
116  * vim:ts=8
117  * vim:sw=8
118  * vim:noexpandtab
119  */
120