xref: /freebsd/lib/libfetch/common.h (revision d6eb98610fa65663bf0df4574b7cb2c5c4ffda71)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1998-2014 Dag-Erling Smørgrav
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer
12  *    in this position and unchanged.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. The name of the author may not be used to endorse or promote products
17  *    derived from this software without specific prior written permission
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  *
30  * $FreeBSD$
31  */
32 
33 #ifndef _COMMON_H_INCLUDED
34 #define _COMMON_H_INCLUDED
35 
36 #define FTP_DEFAULT_PORT	21
37 #define HTTP_DEFAULT_PORT	80
38 #define FTP_DEFAULT_PROXY_PORT	21
39 #define HTTP_DEFAULT_PROXY_PORT	3128
40 
41 #ifdef WITH_SSL
42 #include <openssl/crypto.h>
43 #include <openssl/x509.h>
44 #include <openssl/pem.h>
45 #include <openssl/ssl.h>
46 #include <openssl/err.h>
47 #endif
48 
49 /* Connection */
50 typedef struct fetchconn conn_t;
51 struct fetchconn {
52 	int		 sd;		/* socket descriptor */
53 	char		*buf;		/* buffer */
54 	size_t		 bufsize;	/* buffer size */
55 	size_t		 buflen;	/* length of buffer contents */
56 	int		 err;		/* last protocol reply code */
57 #ifdef WITH_SSL
58 	SSL		*ssl;		/* SSL handle */
59 	SSL_CTX		*ssl_ctx;	/* SSL context */
60 	X509		*ssl_cert;	/* server certificate */
61 	const SSL_METHOD *ssl_meth;	/* SSL method */
62 #endif
63 	int		 ref;		/* reference count */
64 };
65 
66 /* Structure used for error message lists */
67 struct fetcherr {
68 	const int	 num;
69 	const int	 cat;
70 	const char	*string;
71 };
72 
73 /* for fetch_writev */
74 struct iovec;
75 
76 void		 fetch_seterr(struct fetcherr *, int);
77 void		 fetch_syserr(void);
78 void		 fetch_info(const char *, ...) __printflike(1, 2);
79 int		 fetch_default_port(const char *);
80 int		 fetch_default_proxy_port(const char *);
81 struct addrinfo *fetch_resolve(const char *, int, int);
82 int		 fetch_bind(int, int, const char *);
83 conn_t		*fetch_connect(const char *, int, int, int);
84 conn_t		*fetch_reopen(int);
85 conn_t		*fetch_ref(conn_t *);
86 #ifdef WITH_SSL
87 int		 fetch_ssl_cb_verify_crt(int, X509_STORE_CTX*);
88 #endif
89 int		 fetch_ssl(conn_t *, const struct url *, int);
90 ssize_t		 fetch_read(conn_t *, char *, size_t);
91 int		 fetch_getln(conn_t *);
92 ssize_t		 fetch_write(conn_t *, const char *, size_t);
93 ssize_t		 fetch_writev(conn_t *, struct iovec *, int);
94 int		 fetch_putln(conn_t *, const char *, size_t);
95 int		 fetch_close(conn_t *);
96 int		 fetch_add_entry(struct url_ent **, int *, int *,
97 		     const char *, struct url_stat *);
98 int		 fetch_netrc_auth(struct url *url);
99 int		 fetch_no_proxy_match(const char *);
100 
101 #define ftp_seterr(n)	 fetch_seterr(ftp_errlist, n)
102 #define http_seterr(n)	 fetch_seterr(http_errlist, n)
103 #define netdb_seterr(n)	 fetch_seterr(netdb_errlist, n)
104 #define url_seterr(n)	 fetch_seterr(url_errlist, n)
105 
106 #ifndef NDEBUG
107 #define DEBUGF(...)							\
108 	do {								\
109 		if (fetchDebug)						\
110 			fprintf(stderr, __VA_ARGS__);			\
111 	} while (0)
112 #else
113 #define DEBUGF(...)							\
114 	do {								\
115 		/* nothing */						\
116 	} while (0)
117 #endif
118 
119 /*
120  * I don't really like exporting http_request() and ftp_request(),
121  * but the HTTP and FTP code occasionally needs to cross-call
122  * eachother, and this saves me from adding a lot of special-case code
123  * to handle those cases.
124  *
125  * Note that _*_request() free purl, which is way ugly but saves us a
126  * whole lot of trouble.
127  */
128 FILE		*http_request(struct url *, const char *,
129 		     struct url_stat *, struct url *, const char *);
130 FILE		*http_request_body(struct url *, const char *,
131 		     struct url_stat *, struct url *, const char *,
132 		     const char *, const char *);
133 FILE		*ftp_request(struct url *, const char *,
134 		     struct url_stat *, struct url *, const char *);
135 
136 /*
137  * Check whether a particular flag is set
138  */
139 #define CHECK_FLAG(x)	(flags && strchr(flags, (x)))
140 
141 #endif
142