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