140939Sdes/*- 2262560Sdes * Copyright (c) 1998-2014 Dag-Erling Sm��rgrav 340939Sdes * All rights reserved. 440939Sdes * 540939Sdes * Redistribution and use in source and binary forms, with or without 640939Sdes * modification, are permitted provided that the following conditions 740939Sdes * are met: 840939Sdes * 1. Redistributions of source code must retain the above copyright 940939Sdes * notice, this list of conditions and the following disclaimer 1040939Sdes * in this position and unchanged. 1140939Sdes * 2. Redistributions in binary form must reproduce the above copyright 1240939Sdes * notice, this list of conditions and the following disclaimer in the 1340939Sdes * documentation and/or other materials provided with the distribution. 1440939Sdes * 3. The name of the author may not be used to endorse or promote products 1540939Sdes * derived from this software without specific prior written permission 1640939Sdes * 1740939Sdes * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 1840939Sdes * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 1940939Sdes * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 2040939Sdes * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 2140939Sdes * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 2240939Sdes * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 2340939Sdes * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 2440939Sdes * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 2540939Sdes * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 2640939Sdes * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 2740939Sdes * 2850476Speter * $FreeBSD: stable/10/lib/libfetch/common.h 315904 2017-03-24 14:26:01Z des $ 2940939Sdes */ 3040939Sdes 3140939Sdes#ifndef _COMMON_H_INCLUDED 3240939Sdes#define _COMMON_H_INCLUDED 3340939Sdes 3463842Sdes#define FTP_DEFAULT_PORT 21 3563842Sdes#define HTTP_DEFAULT_PORT 80 3668551Sdes#define FTP_DEFAULT_PROXY_PORT 21 3768551Sdes#define HTTP_DEFAULT_PROXY_PORT 3128 3863842Sdes 3997891Sdes#ifdef WITH_SSL 4097856Sdes#include <openssl/crypto.h> 4197856Sdes#include <openssl/x509.h> 4297856Sdes#include <openssl/pem.h> 4397856Sdes#include <openssl/ssl.h> 4497856Sdes#include <openssl/err.h> 4597891Sdes#endif 4697856Sdes 4797856Sdes/* Connection */ 4897856Sdestypedef struct fetchconn conn_t; 4997856Sdesstruct fetchconn { 5097856Sdes int sd; /* socket descriptor */ 5197856Sdes char *buf; /* buffer */ 5297856Sdes size_t bufsize; /* buffer size */ 5397856Sdes size_t buflen; /* length of buffer contents */ 5497856Sdes int err; /* last protocol reply code */ 5597891Sdes#ifdef WITH_SSL 5697866Sdes SSL *ssl; /* SSL handle */ 5797866Sdes SSL_CTX *ssl_ctx; /* SSL context */ 5897856Sdes X509 *ssl_cert; /* server certificate */ 59238405Sjkim const SSL_METHOD *ssl_meth; /* SSL method */ 6097891Sdes#endif 6198117Sdes int ref; /* reference count */ 6297856Sdes}; 6397856Sdes 6440939Sdes/* Structure used for error message lists */ 6597868Sdesstruct fetcherr { 6690267Sdes const int num; 6790267Sdes const int cat; 6890267Sdes const char *string; 6940939Sdes}; 7040939Sdes 71174588Sdes/* for fetch_writev */ 72106046Sdesstruct iovec; 73106046Sdes 74174588Sdesvoid fetch_seterr(struct fetcherr *, int); 75174588Sdesvoid fetch_syserr(void); 76315904Sdesvoid fetch_info(const char *, ...) __printflike(1, 2); 77174588Sdesint fetch_default_port(const char *); 78174588Sdesint fetch_default_proxy_port(const char *); 79310060Sdesstruct addrinfo *fetch_resolve(const char *, int, int); 80174588Sdesint fetch_bind(int, int, const char *); 81174588Sdesconn_t *fetch_connect(const char *, int, int, int); 82174588Sdesconn_t *fetch_reopen(int); 83174588Sdesconn_t *fetch_ref(conn_t *); 84253680Sdes#ifdef WITH_SSL 85253680Sdesint fetch_ssl_cb_verify_crt(int, X509_STORE_CTX*); 86253680Sdes#endif 87253680Sdesint fetch_ssl(conn_t *, const struct url *, int); 88174588Sdesssize_t fetch_read(conn_t *, char *, size_t); 89174588Sdesint fetch_getln(conn_t *); 90174588Sdesssize_t fetch_write(conn_t *, const char *, size_t); 91174588Sdesssize_t fetch_writev(conn_t *, struct iovec *, int); 92174588Sdesint fetch_putln(conn_t *, const char *, size_t); 93174588Sdesint fetch_close(conn_t *); 94174588Sdesint fetch_add_entry(struct url_ent **, int *, int *, 9590267Sdes const char *, struct url_stat *); 96174588Sdesint fetch_netrc_auth(struct url *url); 97174752Sdesint fetch_no_proxy_match(const char *); 9841989Sdes 99174588Sdes#define ftp_seterr(n) fetch_seterr(ftp_errlist, n) 100174588Sdes#define http_seterr(n) fetch_seterr(http_errlist, n) 101174588Sdes#define netdb_seterr(n) fetch_seterr(netdb_errlist, n) 102174588Sdes#define url_seterr(n) fetch_seterr(url_errlist, n) 10340975Sdes 10440975Sdes#ifndef NDEBUG 10587560Sdes#define DEBUG(x) do { if (fetchDebug) { x; } } while (0) 10640975Sdes#else 10740975Sdes#define DEBUG(x) do { } while (0) 10840939Sdes#endif 10940975Sdes 11067044Sdes/* 111174588Sdes * I don't really like exporting http_request() and ftp_request(), 11287316Sdes * but the HTTP and FTP code occasionally needs to cross-call 11387316Sdes * eachother, and this saves me from adding a lot of special-case code 11487316Sdes * to handle those cases. 11567044Sdes * 11687316Sdes * Note that _*_request() free purl, which is way ugly but saves us a 11767044Sdes * whole lot of trouble. 11867044Sdes */ 119174588SdesFILE *http_request(struct url *, const char *, 12090267Sdes struct url_stat *, struct url *, const char *); 121268900SbaptFILE *http_request_body(struct url *, const char *, 122268900Sbapt struct url_stat *, struct url *, const char *, 123268900Sbapt const char *, const char *); 124174588SdesFILE *ftp_request(struct url *, const char *, 12590267Sdes struct url_stat *, struct url *, const char *); 12667044Sdes 12767887Sdes/* 12867887Sdes * Check whether a particular flag is set 12967887Sdes */ 13067887Sdes#define CHECK_FLAG(x) (flags && strchr(flags, (x))) 13167887Sdes 13240975Sdes#endif 133