1*0920b4f2Sagc /*- 2*0920b4f2Sagc * Copyright (c) 1998-2004 Dag-Erling Co�dan Sm�rgrav 3*0920b4f2Sagc * All rights reserved. 4*0920b4f2Sagc * 5*0920b4f2Sagc * Redistribution and use in source and binary forms, with or without 6*0920b4f2Sagc * modification, are permitted provided that the following conditions 7*0920b4f2Sagc * are met: 8*0920b4f2Sagc * 1. Redistributions of source code must retain the above copyright 9*0920b4f2Sagc * notice, this list of conditions and the following disclaimer 10*0920b4f2Sagc * in this position and unchanged. 11*0920b4f2Sagc * 2. Redistributions in binary form must reproduce the above copyright 12*0920b4f2Sagc * notice, this list of conditions and the following disclaimer in the 13*0920b4f2Sagc * documentation and/or other materials provided with the distribution. 14*0920b4f2Sagc * 3. The name of the author may not be used to endorse or promote products 15*0920b4f2Sagc * derived from this software without specific prior written permission 16*0920b4f2Sagc * 17*0920b4f2Sagc * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 18*0920b4f2Sagc * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 19*0920b4f2Sagc * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 20*0920b4f2Sagc * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 21*0920b4f2Sagc * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 22*0920b4f2Sagc * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23*0920b4f2Sagc * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24*0920b4f2Sagc * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25*0920b4f2Sagc * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 26*0920b4f2Sagc * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27*0920b4f2Sagc * 28*0920b4f2Sagc * $FreeBSD: src/lib/libfetch/common.h,v 1.27.6.1 2006/11/11 00:16:07 des Exp $ 29*0920b4f2Sagc */ 30*0920b4f2Sagc 31*0920b4f2Sagc #ifndef _COMMON_H_INCLUDED 32*0920b4f2Sagc #define _COMMON_H_INCLUDED 33*0920b4f2Sagc 34*0920b4f2Sagc #define FTP_DEFAULT_PORT 21 35*0920b4f2Sagc #define HTTP_DEFAULT_PORT 80 36*0920b4f2Sagc #define FTP_DEFAULT_PROXY_PORT 21 37*0920b4f2Sagc #define HTTP_DEFAULT_PROXY_PORT 3128 38*0920b4f2Sagc 39*0920b4f2Sagc #ifdef WITH_SSL 40*0920b4f2Sagc #include <openssl/crypto.h> 41*0920b4f2Sagc #include <openssl/x509.h> 42*0920b4f2Sagc #include <openssl/pem.h> 43*0920b4f2Sagc #include <openssl/ssl.h> 44*0920b4f2Sagc #include <openssl/err.h> 45*0920b4f2Sagc #endif 46*0920b4f2Sagc 47*0920b4f2Sagc /* Connection */ 48*0920b4f2Sagc typedef struct fetchconn conn_t; 49*0920b4f2Sagc struct fetchconn { 50*0920b4f2Sagc int sd; /* socket descriptor */ 51*0920b4f2Sagc char *buf; /* buffer */ 52*0920b4f2Sagc size_t bufsize; /* buffer size */ 53*0920b4f2Sagc size_t buflen; /* length of buffer contents */ 54*0920b4f2Sagc int err; /* last protocol reply code */ 55*0920b4f2Sagc #ifdef WITH_SSL 56*0920b4f2Sagc SSL *ssl; /* SSL handle */ 57*0920b4f2Sagc SSL_CTX *ssl_ctx; /* SSL context */ 58*0920b4f2Sagc X509 *ssl_cert; /* server certificate */ 59*0920b4f2Sagc SSL_METHOD *ssl_meth; /* SSL method */ 60*0920b4f2Sagc #endif 61*0920b4f2Sagc int ref; /* reference count */ 62*0920b4f2Sagc }; 63*0920b4f2Sagc 64*0920b4f2Sagc /* Structure used for error message lists */ 65*0920b4f2Sagc struct fetcherr { 66*0920b4f2Sagc const int num; 67*0920b4f2Sagc const int cat; 68*0920b4f2Sagc const char *string; 69*0920b4f2Sagc }; 70*0920b4f2Sagc 71*0920b4f2Sagc /* for _fetch_writev */ 72*0920b4f2Sagc struct iovec; 73*0920b4f2Sagc 74*0920b4f2Sagc void _fetch_seterr(struct fetcherr *, int); 75*0920b4f2Sagc void _fetch_syserr(void); 76*0920b4f2Sagc void _fetch_info(const char *, ...); 77*0920b4f2Sagc int _fetch_default_port(const char *); 78*0920b4f2Sagc int _fetch_default_proxy_port(const char *); 79*0920b4f2Sagc int _fetch_bind(int, int, const char *); 80*0920b4f2Sagc conn_t *_fetch_connect(const char *, int, int, int); 81*0920b4f2Sagc conn_t *_fetch_reopen(int); 82*0920b4f2Sagc conn_t *_fetch_ref(conn_t *); 83*0920b4f2Sagc int _fetch_ssl(conn_t *, int); 84*0920b4f2Sagc ssize_t _fetch_read(conn_t *, char *, size_t); 85*0920b4f2Sagc int _fetch_getln(conn_t *); 86*0920b4f2Sagc ssize_t _fetch_write(conn_t *, const char *, size_t); 87*0920b4f2Sagc ssize_t _fetch_writev(conn_t *, struct iovec *, int); 88*0920b4f2Sagc int _fetch_putln(conn_t *, const char *, size_t); 89*0920b4f2Sagc int _fetch_close(conn_t *); 90*0920b4f2Sagc int _fetch_add_entry(struct url_ent **, int *, int *, 91*0920b4f2Sagc const char *, struct url_stat *); 92*0920b4f2Sagc int _fetch_netrc_auth(struct url *url); 93*0920b4f2Sagc 94*0920b4f2Sagc #define _ftp_seterr(n) _fetch_seterr(_ftp_errlist, n) 95*0920b4f2Sagc #define _http_seterr(n) _fetch_seterr(_http_errlist, n) 96*0920b4f2Sagc #define _netdb_seterr(n) _fetch_seterr(_netdb_errlist, n) 97*0920b4f2Sagc #define _url_seterr(n) _fetch_seterr(_url_errlist, n) 98*0920b4f2Sagc 99*0920b4f2Sagc #ifndef NDEBUG 100*0920b4f2Sagc #define DEBUG(x) do { if (fetchDebug) { x; } } while (/* CONSTCOND */ 0) 101*0920b4f2Sagc #else 102*0920b4f2Sagc #define DEBUG(x) do { } while (/* CONSTCOND */ 0) 103*0920b4f2Sagc #endif 104*0920b4f2Sagc 105*0920b4f2Sagc /* 106*0920b4f2Sagc * I don't really like exporting _http_request() and _ftp_request(), 107*0920b4f2Sagc * but the HTTP and FTP code occasionally needs to cross-call 108*0920b4f2Sagc * eachother, and this saves me from adding a lot of special-case code 109*0920b4f2Sagc * to handle those cases. 110*0920b4f2Sagc * 111*0920b4f2Sagc * Note that _*_request() free purl, which is way ugly but saves us a 112*0920b4f2Sagc * whole lot of trouble. 113*0920b4f2Sagc */ 114*0920b4f2Sagc FILE *_http_request(struct url *, const char *, 115*0920b4f2Sagc struct url_stat *, struct url *, const char *); 116*0920b4f2Sagc FILE *_ftp_request(struct url *, const char *, 117*0920b4f2Sagc struct url_stat *, struct url *, const char *); 118*0920b4f2Sagc 119*0920b4f2Sagc /* 120*0920b4f2Sagc * Check whether a particular flag is set 121*0920b4f2Sagc */ 122*0920b4f2Sagc #define CHECK_FLAG(x) (flags && strchr(flags, (x))) 123*0920b4f2Sagc 124*0920b4f2Sagc #endif 125