Home | History | Annotate | Line # | Download | only in libfetch
      1 /*	$NetBSD: common.h,v 1.5 2026/04/16 08:40:49 wiz Exp $	*/
      2 /*-
      3  * Copyright (c) 1998-2004 Dag-Erling Codan Smrgrav
      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     !defined(_M_XENIX)
     59 #define HAVE_SA_LEN
     60 #endif
     61 
     62 /* Connection */
     63 typedef struct fetchconn conn_t;
     64 
     65 struct fetchconn {
     66 	int		 sd;		/* socket descriptor */
     67 	char		*buf;		/* buffer */
     68 	size_t		 bufsize;	/* buffer size */
     69 	size_t		 buflen;	/* length of buffer contents */
     70 	int		 buf_events;	/* poll flags for the next cycle */
     71 	char		*next_buf;	/* pending buffer, e.g. after getln */
     72 	size_t		 next_len;	/* size of pending buffer */
     73 	int		 err;		/* last protocol reply code */
     74 #ifdef WITH_SSL
     75 	SSL		*ssl;		/* SSL handle */
     76 	SSL_CTX		*ssl_ctx;	/* SSL context */
     77 	X509		*ssl_cert;	/* server certificate */
     78 #  if OPENSSL_VERSION_NUMBER < 0x00909000L
     79 	SSL_METHOD *ssl_meth;		/* SSL method */
     80 #  else
     81 	const SSL_METHOD *ssl_meth;	/* SSL method */
     82 #  endif
     83 #endif
     84 
     85 	char		*ftp_home;
     86 
     87 	struct url	*cache_url;
     88 	int		cache_af;
     89 	int		(*cache_close)(conn_t *);
     90 	conn_t		*next_cached;
     91 };
     92 
     93 /* Structure used for error message lists */
     94 struct fetcherr {
     95 	const int	 num;
     96 	const int	 cat;
     97 	const char	*string;
     98 };
     99 
    100 void		 fetch_seterr(struct fetcherr *, int);
    101 void		 fetch_syserr(void);
    102 void		 fetch_info(const char *, ...) LIBFETCH_PRINTFLIKE(1, 2);
    103 int		 fetch_default_port(const char *);
    104 int		 fetch_default_proxy_port(const char *);
    105 int		 fetch_bind(int, int, const char *);
    106 conn_t		*fetch_cache_get(const struct url *, int);
    107 void		 fetch_cache_put(conn_t *, int (*)(conn_t *));
    108 conn_t		*fetch_connect(struct url *, int, int);
    109 conn_t		*fetch_reopen(int);
    110 int		 fetch_ssl(conn_t *, const struct url *, int);
    111 ssize_t		 fetch_read(conn_t *, char *, size_t);
    112 int		 fetch_getln(conn_t *);
    113 ssize_t		 fetch_write(conn_t *, const void *, size_t);
    114 int		 fetch_close(conn_t *);
    115 int		 fetch_add_entry(struct url_list *, struct url *, const char *, int);
    116 int		 fetch_netrc_auth(struct url *url);
    117 int		 fetch_no_proxy_match(const char *);
    118 int		 fetch_urlpath_safe(char);
    119 
    120 #define ftp_seterr(n)	 fetch_seterr(ftp_errlist, n)
    121 #define http_seterr(n)	 fetch_seterr(http_errlist, n)
    122 #define netdb_seterr(n)	 fetch_seterr(netdb_errlist, n)
    123 #define url_seterr(n)	 fetch_seterr(url_errlist, n)
    124 
    125 fetchIO		*fetchIO_unopen(void *, ssize_t (*)(void *, void *, size_t),
    126     ssize_t (*)(void *, const void *, size_t), void (*)(void *));
    127 
    128 /*
    129  * I don't really like exporting http_request() and ftp_request(),
    130  * but the HTTP and FTP code occasionally needs to cross-call
    131  * eachother, and this saves me from adding a lot of special-case code
    132  * to handle those cases.
    133  *
    134  * Note that _*_request() free purl, which is way ugly but saves us a
    135  * whole lot of trouble.
    136  */
    137 fetchIO		*http_request(struct url *, const char *,
    138 		     struct url_stat *, struct url *, const char *);
    139 fetchIO		*ftp_request(struct url *, const char *, const char *,
    140 		     struct url_stat *, struct url *, const char *);
    141 
    142 
    143 /*
    144  * Check whether a particular flag is set
    145  */
    146 #define CHECK_FLAG(x)	(flags && strchr(flags, (x)))
    147 
    148 #endif
    149