xref: /freebsd/lib/libfetch/common.h (revision b3e7694832e81d7a904a10f525f8797b753bf0d3)
1842a95ccSDag-Erling Smørgrav /*-
25e53a4f9SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
35e53a4f9SPedro F. Giffuni  *
44524013cSDag-Erling Smørgrav  * Copyright (c) 1998-2014 Dag-Erling Smørgrav
5842a95ccSDag-Erling Smørgrav  * All rights reserved.
6842a95ccSDag-Erling Smørgrav  *
7842a95ccSDag-Erling Smørgrav  * Redistribution and use in source and binary forms, with or without
8842a95ccSDag-Erling Smørgrav  * modification, are permitted provided that the following conditions
9842a95ccSDag-Erling Smørgrav  * are met:
10842a95ccSDag-Erling Smørgrav  * 1. Redistributions of source code must retain the above copyright
11842a95ccSDag-Erling Smørgrav  *    notice, this list of conditions and the following disclaimer
12842a95ccSDag-Erling Smørgrav  *    in this position and unchanged.
13842a95ccSDag-Erling Smørgrav  * 2. Redistributions in binary form must reproduce the above copyright
14842a95ccSDag-Erling Smørgrav  *    notice, this list of conditions and the following disclaimer in the
15842a95ccSDag-Erling Smørgrav  *    documentation and/or other materials provided with the distribution.
16842a95ccSDag-Erling Smørgrav  * 3. The name of the author may not be used to endorse or promote products
17842a95ccSDag-Erling Smørgrav  *    derived from this software without specific prior written permission
18842a95ccSDag-Erling Smørgrav  *
19842a95ccSDag-Erling Smørgrav  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20842a95ccSDag-Erling Smørgrav  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21842a95ccSDag-Erling Smørgrav  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22842a95ccSDag-Erling Smørgrav  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23842a95ccSDag-Erling Smørgrav  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24842a95ccSDag-Erling Smørgrav  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25842a95ccSDag-Erling Smørgrav  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26842a95ccSDag-Erling Smørgrav  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27842a95ccSDag-Erling Smørgrav  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28842a95ccSDag-Erling Smørgrav  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29842a95ccSDag-Erling Smørgrav  */
30842a95ccSDag-Erling Smørgrav 
31842a95ccSDag-Erling Smørgrav #ifndef _COMMON_H_INCLUDED
32842a95ccSDag-Erling Smørgrav #define _COMMON_H_INCLUDED
33842a95ccSDag-Erling Smørgrav 
3410851dc4SDag-Erling Smørgrav #define FTP_DEFAULT_PORT	21
3510851dc4SDag-Erling Smørgrav #define HTTP_DEFAULT_PORT	80
36e828ada7SDag-Erling Smørgrav #define FTP_DEFAULT_PROXY_PORT	21
37e828ada7SDag-Erling Smørgrav #define HTTP_DEFAULT_PROXY_PORT	3128
3810851dc4SDag-Erling Smørgrav 
393070f6cbSDag-Erling Smørgrav #ifdef WITH_SSL
40dea29ca1SDag-Erling Smørgrav #include <openssl/crypto.h>
41dea29ca1SDag-Erling Smørgrav #include <openssl/x509.h>
42dea29ca1SDag-Erling Smørgrav #include <openssl/pem.h>
43dea29ca1SDag-Erling Smørgrav #include <openssl/ssl.h>
44dea29ca1SDag-Erling Smørgrav #include <openssl/err.h>
453070f6cbSDag-Erling Smørgrav #endif
46dea29ca1SDag-Erling Smørgrav 
47dea29ca1SDag-Erling Smørgrav /* Connection */
48dea29ca1SDag-Erling Smørgrav typedef struct fetchconn conn_t;
49dea29ca1SDag-Erling Smørgrav struct fetchconn {
50dea29ca1SDag-Erling Smørgrav 	int		 sd;		/* socket descriptor */
51dea29ca1SDag-Erling Smørgrav 	char		*buf;		/* buffer */
52dea29ca1SDag-Erling Smørgrav 	size_t		 bufsize;	/* buffer size */
53dea29ca1SDag-Erling Smørgrav 	size_t		 buflen;	/* length of buffer contents */
54dea29ca1SDag-Erling Smørgrav 	int		 err;		/* last protocol reply code */
553070f6cbSDag-Erling Smørgrav #ifdef WITH_SSL
569601e333SDag-Erling Smørgrav 	SSL		*ssl;		/* SSL handle */
579601e333SDag-Erling Smørgrav 	SSL_CTX		*ssl_ctx;	/* SSL context */
58dea29ca1SDag-Erling Smørgrav 	X509		*ssl_cert;	/* server certificate */
591f13597dSJung-uk Kim 	const SSL_METHOD *ssl_meth;	/* SSL method */
603070f6cbSDag-Erling Smørgrav #endif
61f606d589SDag-Erling Smørgrav 	int		 ref;		/* reference count */
62dea29ca1SDag-Erling Smørgrav };
63dea29ca1SDag-Erling Smørgrav 
64842a95ccSDag-Erling Smørgrav /* Structure used for error message lists */
65842a95ccSDag-Erling Smørgrav struct fetcherr {
66e19e6098SDag-Erling Smørgrav 	const int	 num;
67e19e6098SDag-Erling Smørgrav 	const int	 cat;
68842a95ccSDag-Erling Smørgrav 	const char	*string;
69842a95ccSDag-Erling Smørgrav };
70842a95ccSDag-Erling Smørgrav 
71*c44be5aaSKyle Evans /* For SOCKS header size */
72*c44be5aaSKyle Evans #define HEAD_SIZE	4
73*c44be5aaSKyle Evans #define FQDN_SIZE	256
74*c44be5aaSKyle Evans #define PACK_SIZE	1
75*c44be5aaSKyle Evans #define PORT_SIZE	2
76*c44be5aaSKyle Evans #define BUFF_SIZE	HEAD_SIZE + FQDN_SIZE + PACK_SIZE + PORT_SIZE
77*c44be5aaSKyle Evans 
78*c44be5aaSKyle Evans /* SOCKS5 Request Header */
79*c44be5aaSKyle Evans #define SOCKS_VERSION_5		0x05
80*c44be5aaSKyle Evans /* SOCKS5 CMD */
81*c44be5aaSKyle Evans #define SOCKS_CONNECTION	0x01
82*c44be5aaSKyle Evans #define SOCKS_BIND		0x02
83*c44be5aaSKyle Evans #define SOCKS_UDP		0x03
84*c44be5aaSKyle Evans #define SOCKS_NOMETHODS		0xFF
85*c44be5aaSKyle Evans #define SOCKS5_NOTIMPLEMENTED	0x00
86*c44be5aaSKyle Evans /* SOCKS5 Reserved */
87*c44be5aaSKyle Evans #define SOCKS_RSV		0x00
88*c44be5aaSKyle Evans /* SOCKS5 Address Type */
89*c44be5aaSKyle Evans #define SOCKS_ATYP_IPV4		0x01
90*c44be5aaSKyle Evans #define SOCKS_ATYP_DOMAINNAME	0x03
91*c44be5aaSKyle Evans #define SOCKS_ATYP_IPV6		0x04
92*c44be5aaSKyle Evans /* SOCKS5 Reply Field */
93*c44be5aaSKyle Evans #define SOCKS_SUCCESS			0x00
94*c44be5aaSKyle Evans #define SOCKS_GENERAL_FAILURE		0x01
95*c44be5aaSKyle Evans #define SOCKS_CONNECTION_NOT_ALLOWED	0x02
96*c44be5aaSKyle Evans #define SOCKS_NETWORK_UNREACHABLE	0x03
97*c44be5aaSKyle Evans #define SOCKS_HOST_UNREACHABLE		0x04
98*c44be5aaSKyle Evans #define SOCKS_CONNECTION_REFUSED	0x05
99*c44be5aaSKyle Evans #define SOCKS_TTL_EXPIRED		0x06
100*c44be5aaSKyle Evans #define SOCKS_COMMAND_NOT_SUPPORTED	0x07
101*c44be5aaSKyle Evans #define SOCKS_ADDRESS_NOT_SUPPORTED	0x08
102*c44be5aaSKyle Evans 
103a1b37df2SDag-Erling Smørgrav /* for fetch_writev */
1042761348fSDag-Erling Smørgrav struct iovec;
1052761348fSDag-Erling Smørgrav 
106a1b37df2SDag-Erling Smørgrav void		 fetch_seterr(struct fetcherr *, int);
107a1b37df2SDag-Erling Smørgrav void		 fetch_syserr(void);
10864c42235SDag-Erling Smørgrav void		 fetch_info(const char *, ...) __printflike(1, 2);
109*c44be5aaSKyle Evans int		 fetch_socks5_getenv(char **host, int *port);
110*c44be5aaSKyle Evans int		 fetch_socks5_init(conn_t *conn, const char *host,
111*c44be5aaSKyle Evans 		     int port, int verbose);
112a1b37df2SDag-Erling Smørgrav int		 fetch_default_port(const char *);
113a1b37df2SDag-Erling Smørgrav int		 fetch_default_proxy_port(const char *);
114792ef1aeSDag-Erling Smørgrav struct addrinfo *fetch_resolve(const char *, int, int);
115a1b37df2SDag-Erling Smørgrav int		 fetch_bind(int, int, const char *);
116a1b37df2SDag-Erling Smørgrav conn_t		*fetch_connect(const char *, int, int, int);
117a1b37df2SDag-Erling Smørgrav conn_t		*fetch_reopen(int);
118a1b37df2SDag-Erling Smørgrav conn_t		*fetch_ref(conn_t *);
119dcd47379SDag-Erling Smørgrav #ifdef WITH_SSL
120dcd47379SDag-Erling Smørgrav int		 fetch_ssl_cb_verify_crt(int, X509_STORE_CTX*);
121dcd47379SDag-Erling Smørgrav #endif
122dcd47379SDag-Erling Smørgrav int		 fetch_ssl(conn_t *, const struct url *, int);
123a1b37df2SDag-Erling Smørgrav ssize_t		 fetch_read(conn_t *, char *, size_t);
124a1b37df2SDag-Erling Smørgrav int		 fetch_getln(conn_t *);
125a1b37df2SDag-Erling Smørgrav ssize_t		 fetch_write(conn_t *, const char *, size_t);
126a1b37df2SDag-Erling Smørgrav ssize_t		 fetch_writev(conn_t *, struct iovec *, int);
127a1b37df2SDag-Erling Smørgrav int		 fetch_putln(conn_t *, const char *, size_t);
128a1b37df2SDag-Erling Smørgrav int		 fetch_close(conn_t *);
129a1b37df2SDag-Erling Smørgrav int		 fetch_add_entry(struct url_ent **, int *, int *,
130f573a5fcSDag-Erling Smørgrav 		     const char *, struct url_stat *);
131a1b37df2SDag-Erling Smørgrav int		 fetch_netrc_auth(struct url *url);
13262a2681cSDag-Erling Smørgrav int		 fetch_no_proxy_match(const char *);
133d8acd8dcSDag-Erling Smørgrav 
134a1b37df2SDag-Erling Smørgrav #define ftp_seterr(n)	 fetch_seterr(ftp_errlist, n)
135a1b37df2SDag-Erling Smørgrav #define http_seterr(n)	 fetch_seterr(http_errlist, n)
136a1b37df2SDag-Erling Smørgrav #define netdb_seterr(n)	 fetch_seterr(netdb_errlist, n)
137a1b37df2SDag-Erling Smørgrav #define url_seterr(n)	 fetch_seterr(url_errlist, n)
138*c44be5aaSKyle Evans #define socks5_seterr(n) fetch_seterr(socks5_errlist, n)
139d8acd8dcSDag-Erling Smørgrav 
140d8acd8dcSDag-Erling Smørgrav #ifndef NDEBUG
141c5712d6dSDag-Erling Smørgrav #define DEBUGF(...)							\
142c5712d6dSDag-Erling Smørgrav 	do {								\
143c5712d6dSDag-Erling Smørgrav 		if (fetchDebug)						\
144c5712d6dSDag-Erling Smørgrav 			fprintf(stderr, __VA_ARGS__);			\
145c5712d6dSDag-Erling Smørgrav 	} while (0)
146d8acd8dcSDag-Erling Smørgrav #else
147c5712d6dSDag-Erling Smørgrav #define DEBUGF(...)							\
148c5712d6dSDag-Erling Smørgrav 	do {								\
149c5712d6dSDag-Erling Smørgrav 		/* nothing */						\
150c5712d6dSDag-Erling Smørgrav 	} while (0)
151d8acd8dcSDag-Erling Smørgrav #endif
152842a95ccSDag-Erling Smørgrav 
153bbc5af0cSDag-Erling Smørgrav /*
154a1b37df2SDag-Erling Smørgrav  * I don't really like exporting http_request() and ftp_request(),
15589db7b8cSDag-Erling Smørgrav  * but the HTTP and FTP code occasionally needs to cross-call
15689db7b8cSDag-Erling Smørgrav  * eachother, and this saves me from adding a lot of special-case code
15789db7b8cSDag-Erling Smørgrav  * to handle those cases.
158bbc5af0cSDag-Erling Smørgrav  *
15989db7b8cSDag-Erling Smørgrav  * Note that _*_request() free purl, which is way ugly but saves us a
160bbc5af0cSDag-Erling Smørgrav  * whole lot of trouble.
161bbc5af0cSDag-Erling Smørgrav  */
162a1b37df2SDag-Erling Smørgrav FILE		*http_request(struct url *, const char *,
163e19e6098SDag-Erling Smørgrav 		     struct url_stat *, struct url *, const char *);
164c4199130SBaptiste Daroussin FILE		*http_request_body(struct url *, const char *,
165c4199130SBaptiste Daroussin 		     struct url_stat *, struct url *, const char *,
166c4199130SBaptiste Daroussin 		     const char *, const char *);
167a1b37df2SDag-Erling Smørgrav FILE		*ftp_request(struct url *, const char *,
168e19e6098SDag-Erling Smørgrav 		     struct url_stat *, struct url *, const char *);
169bbc5af0cSDag-Erling Smørgrav 
1701c2598aaSDag-Erling Smørgrav /*
1711c2598aaSDag-Erling Smørgrav  * Check whether a particular flag is set
1721c2598aaSDag-Erling Smørgrav  */
1731c2598aaSDag-Erling Smørgrav #define CHECK_FLAG(x)	(flags && strchr(flags, (x)))
1741c2598aaSDag-Erling Smørgrav 
175842a95ccSDag-Erling Smørgrav #endif
176