Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/pkg
Path: blob/main/external/libfetch/common.h
4744 views
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
31
#ifndef _COMMON_H_INCLUDED
32
#define _COMMON_H_INCLUDED
33
34
#define FTP_DEFAULT_PORT 21
35
#define HTTP_DEFAULT_PORT 80
36
#define FTP_DEFAULT_PROXY_PORT 21
37
#define HTTP_DEFAULT_PROXY_PORT 3128
38
39
#ifdef WITH_SSL
40
#include <openssl/crypto.h>
41
#include <openssl/x509.h>
42
#include <openssl/pem.h>
43
#include <openssl/ssl.h>
44
#include <openssl/err.h>
45
#endif
46
47
/* Connection */
48
typedef struct fetchconn conn_t;
49
struct fetchconn {
50
int sd; /* socket descriptor */
51
char *buf; /* buffer */
52
size_t bufsize; /* buffer size */
53
size_t buflen; /* length of buffer contents */
54
int err; /* last protocol reply code */
55
#ifdef WITH_SSL
56
SSL *ssl; /* SSL handle */
57
SSL_CTX *ssl_ctx; /* SSL context */
58
X509 *ssl_cert; /* server certificate */
59
#endif
60
int ref; /* reference count */
61
char cache_host[MAXHOSTNAMELEN + 1];
62
int cache_port;
63
int cache_ssl; /* 1 if HTTPS */
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 SOCKS header size */
74
#define HEAD_SIZE 4
75
#define FQDN_SIZE 256
76
#define PACK_SIZE 1
77
#define PORT_SIZE 2
78
#define BUFF_SIZE HEAD_SIZE + FQDN_SIZE + PACK_SIZE + PORT_SIZE
79
80
/* SOCKS5 Request Header */
81
#define SOCKS_VERSION_5 0x05
82
/* SOCKS5 CMD */
83
#define SOCKS_CONNECTION 0x01
84
#define SOCKS_BIND 0x02
85
#define SOCKS_UDP 0x03
86
#define SOCKS_NOMETHODS 0xFF
87
#define SOCKS5_NOTIMPLEMENTED 0x00
88
/* SOCKS5 Reserved */
89
#define SOCKS_RSV 0x00
90
/* SOCKS5 Address Type */
91
#define SOCKS_ATYP_IPV4 0x01
92
#define SOCKS_ATYP_DOMAINNAME 0x03
93
#define SOCKS_ATYP_IPV6 0x04
94
/* SOCKS5 Reply Field */
95
#define SOCKS_SUCCESS 0x00
96
#define SOCKS_GENERAL_FAILURE 0x01
97
#define SOCKS_CONNECTION_NOT_ALLOWED 0x02
98
#define SOCKS_NETWORK_UNREACHABLE 0x03
99
#define SOCKS_HOST_UNREACHABLE 0x04
100
#define SOCKS_CONNECTION_REFUSED 0x05
101
#define SOCKS_TTL_EXPIRED 0x06
102
#define SOCKS_COMMAND_NOT_SUPPORTED 0x07
103
#define SOCKS_ADDRESS_NOT_SUPPORTED 0x08
104
105
/* for fetch_writev */
106
struct iovec;
107
108
void fetch_seterr(struct fetcherr *, int);
109
void fetch_syserr(void);
110
void fetch_info(const char *, ...) __printflike(1, 2);
111
int fetch_socks5_getenv(char **host, int *port);
112
int fetch_socks5_init(conn_t *conn, const char *host,
113
int port, int verbose);
114
int fetch_default_port(const char *);
115
int fetch_default_proxy_port(const char *);
116
struct addrinfo *fetch_resolve(const char *, int, int);
117
int fetch_bind(int, int, const char *);
118
conn_t *fetch_connect(const char *, int, int, int);
119
conn_t *fetch_reopen(int);
120
conn_t *fetch_ref(conn_t *);
121
#ifdef WITH_SSL
122
int fetch_ssl_cb_verify_crt(int, X509_STORE_CTX*);
123
#endif
124
int fetch_ssl(conn_t *, const struct url *, int);
125
ssize_t fetch_read(conn_t *, char *, size_t);
126
int fetch_getln(conn_t *);
127
ssize_t fetch_write(conn_t *, const char *, size_t);
128
ssize_t fetch_writev(conn_t *, struct iovec *, int);
129
int fetch_putln(conn_t *, const char *, size_t);
130
int fetch_close(conn_t *);
131
int fetch_add_entry(struct url_ent **, int *, int *,
132
const char *, struct url_stat *);
133
int fetch_netrc_auth(struct url *url);
134
int fetch_no_proxy_match(const char *);
135
136
#define ftp_seterr(n) fetch_seterr(ftp_errlist, n)
137
#define http_seterr(n) fetch_seterr(http_errlist, n)
138
#define netdb_seterr(n) fetch_seterr(netdb_errlist, n)
139
#define url_seterr(n) fetch_seterr(url_errlist, n)
140
#define socks5_seterr(n) fetch_seterr(socks5_errlist, n)
141
142
#ifndef NDEBUG
143
#define DEBUGF(...) \
144
do { \
145
if (fetchDebug) \
146
fprintf(stderr, __VA_ARGS__); \
147
} while (0)
148
#else
149
#define DEBUGF(...) \
150
do { \
151
/* nothing */ \
152
} while (0)
153
#endif
154
155
/*
156
* I don't really like exporting http_request(),
157
* but the HTTP and FTP code occasionally needs to cross-call
158
* eachother, and this saves me from adding a lot of special-case code
159
* to handle those cases.
160
*
161
* Note that _*_request() free purl, which is way ugly but saves us a
162
* whole lot of trouble.
163
*/
164
FILE *http_request(struct url *, const char *,
165
struct url_stat *, struct url *, const char *);
166
FILE *http_request_body(struct url *, const char *,
167
struct url_stat *, struct url *, const char *,
168
const char *, const char *);
169
170
/*
171
* Check whether a particular flag is set
172
*/
173
#define CHECK_FLAG(x) (flags && strchr(flags, (x)))
174
175
#endif
176
177