1 /*
2  * ProFTPD - FTP server daemon
3  * Copyright (c) 1997, 1998 Public Flood Software
4  * Copyright (c) 1999, 2000 MacGyver aka Habeeb J. Dihu <macgyver@tos.net>
5  * Copyright (c) 2001-2021 The ProFTPD Project team
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA.
20  *
21  * As a special exemption, Public Flood Software/MacGyver aka Habeeb J. Dihu
22  * and other respective copyright holders give permission to link this program
23  * with OpenSSL, and distribute the resulting executable, without including
24  * the source code for OpenSSL in the source distribution.
25  */
26 
27 /* BSD socket manipulation tools. */
28 
29 #ifndef PR_INET_H
30 #define PR_INET_H
31 
32 #include "conf.h"
33 
34 #ifndef AF_INET6
35 # define AF_INET6	AF_UNSPEC
36 #endif /* AF_INET6 */
37 
38 #ifndef INADDR_ANY
39 # define INADDR_ANY	((unsigned long int) 0x00000000)
40 #endif /* INADDR_ANY */
41 
42 #ifndef INADDR_LOOPBACK
43 # define INADDR_LOOPBACK	((unsigned long int) 0x7f000001)
44 #endif /* INADDR_LOOPBACK */
45 
46 #ifndef INADDR_NONE
47 # define INADDR_NONE	0xffffffff
48 #endif /* INADDR_NONE */
49 
50 #ifndef INPORT_ANY
51 # define INPORT_ANY	0
52 #endif
53 
54 #ifndef IN6_IS_ADDR_UNSPECIFIED
55 # define IN6_IS_ADDR_UNSPECIFIED(a)	0
56 #endif
57 
58 #ifndef IN6_IS_ADDR_LOOPBACK
59 # define IN6_IS_ADDR_LOOPBACK(a)	0
60 #endif
61 
62 #ifndef IN6_IS_ADDR_MULTICAST
63 # define IN6_IS_ADDR_MULTICAST(a)	0
64 #endif
65 
66 #ifndef IN6_IS_ADDR_LINKLOCAL
67 # define IN6_IS_ADDR_LINKLOCAL(a)	0
68 #endif
69 
70 #ifndef IN6_IS_ADDR_SITELOCAL
71 # define IN6_IS_ADDR_SITELOCAL(a)	0
72 #endif
73 
74 #ifndef IN6_IS_ADDR_V4MAPPED
75 # define IN6_IS_ADDR_V4MAPPED(a)	0
76 #endif
77 
78 #ifndef IN6_IS_ADDR_V4COMPAT
79 # define IN6_IS_ADDR_V4COMPAT(a)	0
80 #endif
81 
82 #ifndef IN6_ARE_ADDR_EQUAL
83 # define IN6_ARE_ADDR_EQUAL(a, b)	0
84 #endif
85 
86 #ifndef U32BITS
87 # define U32BITS	0xffffffff
88 #endif
89 
90 /* Connection modes */
91 #define CM_NONE         0
92 #define CM_LISTEN       1
93 #define CM_OPEN         2
94 #define CM_ACCEPT       3
95 #define CM_CONNECT      4
96 #define CM_CLOSED       5
97 #define CM_ERROR        6
98 
99 /* connection structure */
100 typedef struct conn_struc {
101   struct conn_struc *next;
102   struct pool_rec *pool;
103   int mode;				/* Current connection mode */
104   int listen_fd;			/* Listening file descriptor */
105   int rcvbuf, sndbuf;			/* Socket recv and send sizes */
106 
107   int xerrno;				/* Set to error if mode == CM_ERROR */
108 
109   int rfd,wfd;				/* Read and write fds */
110   pr_netio_stream_t *instrm, *outstrm;	/* Input/Output streams */
111 
112   /* Remote address of the connection. */
113   const pr_netaddr_t *remote_addr;
114 
115   /* Remote port of the connection. */
116   int remote_port;
117 
118   /* Remote FQDN of the connection. */
119   const char *remote_name;
120 
121   /* Local address of the connection. */
122   const pr_netaddr_t *local_addr;
123 
124   /* Local port of the connection. */
125   int local_port;
126 
127 } conn_t;
128 
129 /* Used for event data for events related to opening of sockets */
130 struct socket_ctx {
131   server_rec *server;
132   const pr_netaddr_t *addr;
133   int sockfd;
134 };
135 
136 /* Prototypes */
137 void pr_inet_clear(void);
138 int pr_inet_reverse_dns(pool *, int);
139 int pr_inet_getservport(pool *, const char *, const char *);
140 pr_netaddr_t *pr_inet_getaddr(pool *, const char *, array_header **);
141 conn_t *pr_inet_copy_conn(pool *, conn_t *);
142 conn_t *pr_inet_create_conn(pool *, int, const pr_netaddr_t *, int, int);
143 conn_t *pr_inet_create_conn_portrange(pool *, const pr_netaddr_t *, int, int);
144 void pr_inet_close(pool *, conn_t *);
145 void pr_inet_lingering_abort(pool *, conn_t *, long);
146 void pr_inet_lingering_close(pool *, conn_t *, long);
147 int pr_inet_set_default_family(pool *, int);
148 int pr_inet_set_async(pool *, conn_t *);
149 int pr_inet_set_block(pool *, conn_t *);
150 int pr_inet_set_nonblock(pool *, conn_t *);
151 int pr_inet_set_proto_cork(int, int);
152 int pr_inet_set_proto_nodelay(pool *, conn_t *, int);
153 int pr_inet_set_proto_opts(pool *, conn_t *, int, int, int, int);
154 int pr_inet_set_socket_opts(pool *, conn_t *, int, int, struct tcp_keepalive *);
155 int pr_inet_set_socket_opts2(pool *, conn_t *, int, int, struct tcp_keepalive *,
156   int);
157 
158 int pr_inet_listen(pool *p, conn_t *conn, int backlog, int flags);
159 #define PR_INET_LISTEN_FL_FATAL_ON_ERROR		0x0001
160 
161 int pr_inet_resetlisten(pool *, conn_t *);
162 int pr_inet_accept_nowait(pool *, conn_t *);
163 int pr_inet_connect(pool *, conn_t *, const pr_netaddr_t *, int);
164 int pr_inet_connect_nowait(pool *, conn_t *, const pr_netaddr_t *, int);
165 int pr_inet_get_conn_info(conn_t *, int);
166 conn_t *pr_inet_accept(pool *, conn_t *, conn_t *, int, int, unsigned char);
167 conn_t *pr_inet_openrw(pool *, conn_t *, const pr_netaddr_t *, int, int, int,
168   int, int);
169 int pr_inet_generate_socket_event(const char *, server_rec *,
170   const pr_netaddr_t *, int);
171 
172 void init_inet(void);
173 
174 #endif /* PR_INET_H */
175