"Fossies" - the Fresh Open Source Software Archive

Member "msmtp-1.8.17/src/net.h" (16 Dec 2020, 6040 Bytes) of package /linux/privat/msmtp-1.8.17.tar.xz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "net.h" see the Fossies "Dox" file reference documentation and the last Fossies "Diffs" side-by-side code changes report: 1.8.13_vs_1.8.14.

    1 /*
    2  * net.h
    3  *
    4  * This file is part of msmtp, an SMTP client, and of mpop, a POP3 client.
    5  *
    6  * Copyright (C) 2000, 2003, 2004, 2005, 2006, 2007, 2008, 2014, 2018, 2019,
    7  * 2020
    8  * Martin Lambers <marlam@marlam.de>
    9  *
   10  *   This program is free software; you can redistribute it and/or modify
   11  *   it under the terms of the GNU General Public License as published by
   12  *   the Free Software Foundation; either version 3 of the License, or
   13  *   (at your option) any later version.
   14  *
   15  *   This program is distributed in the hope that it will be useful,
   16  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
   17  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   18  *   GNU General Public License for more details.
   19  *
   20  *   You should have received a copy of the GNU General Public License
   21  *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
   22  */
   23 
   24 #ifndef NET_H
   25 #define NET_H
   26 
   27 #include <stddef.h>
   28 
   29 #include "readbuf.h"
   30 
   31 
   32 /*
   33  * If a function with an 'errstr' argument returns a value != NET_EOK,
   34  * '*errstr' either points to an allocates string containing an error
   35  * description or is NULL.
   36  * If such a function returns NET_EOK, 'errstr' will not be changed.
   37  */
   38 #define NET_EOK                 0       /* no error */
   39 #define NET_ELIBFAILED          1       /* The underlying library failed */
   40 #define NET_EHOSTNOTFOUND       2       /* Host not found */
   41 #define NET_ESOCKET             3       /* Cannot create socket */
   42 #define NET_ECONNECT            4       /* Cannot connect */
   43 #define NET_EIO                 5       /* Input/output error */
   44 #define NET_EPROXY              6       /* Proxy failure */
   45 #define NET_ESRVNOTFOUND        7       /* SRV record not found */
   46 
   47 /*
   48  * net_lib_init()
   49  *
   50  * Initializes the networking libraries. If this function returns
   51  * NET_ELIBFAILED, *errstr will always point to an error string.
   52  * Used error codes: NET_ELIBFAILED
   53  */
   54 int net_lib_init(char **errstr);
   55 
   56 /*
   57  * net_open_socket()
   58  *
   59  * Opens a TCP socket to 'hostname':'port'.
   60  * 'proxy_hostname' and 'proxy_port' define a SOCKS5 proxy to use, unless they
   61  * are NULL/-1, in which case no proxy will be used.
   62  * If 'socketname' is not NULL, it overrides both 'hostname':'port' and 'proxy_*' by
   63  * specifying the file name of a local socket to connect to.
   64  * 'hostname' may be a host name or a network address.
   65  * 'source_ip' may be NULL or a string representation of an IPv6 or IPv4 address
   66  * that will be bound as the source address for the outgoing connection.
   67  * 'timeout' is measured in secondes. If it is <= 0, no timeout will be set,
   68  * which means that the OS dependent default timeout value will be used.
   69  * The timeout will not only apply to the connection attempt but also to all
   70  * following read/write operations on the socket.
   71  * If 'canonical_name' is not NULL, a pointer to a string containing the
   72  * canonical hostname of the server will be stored in '*canonical_name', or NULL
   73  * if this information is not available.
   74  * If 'address' is not NULL, a pointer to a string containing the network
   75  * address of the server will be stored in '*address', or NULL if this
   76  * information is not available.
   77  * The strings must be deallocated when not used anymore.
   78  * The file descriptor is returned in 'fd'. It can be closed with close().
   79  *
   80  * Used error codes: NET_EHOSTNOTFOUND, NET_ESOCKET, NET_ECONNECT, NET_EPROXY
   81  */
   82 int net_open_socket(
   83         const char *socketname,
   84         const char *proxy_hostname, int proxy_port,
   85         const char *hostname, int port,
   86         const char *source_ip,
   87         int timeout,
   88         int *fd, char **canonical_name, char **address,
   89         char **errstr);
   90 
   91 /*
   92  * net_gets()
   93  *
   94  * Reads in at most one less than 'size' characters from 'fd' and stores them
   95  * into the buffer pointed to by 'str'. Reading stops after an EOF or a newline.
   96  * If a newline is read, it is stored into the buffer. A '\0' is stored after
   97  * the last character in the buffer. The length of the resulting string (the
   98  * number of characters excluding the terminating '\0') will be stored in 'len'.
   99  * 'readbuf' will be used as an input buffer and must of course be the same for
  100  * all read operations on 'fd'.
  101  * Used error codes: NET_EIO
  102  */
  103 int net_gets(int fd, readbuf_t *readbuf,
  104         char *str, size_t size, size_t *len, char **errstr);
  105 
  106 /*
  107  * net_puts()
  108  *
  109  * Writes 'len' characters from the string 's' to 'fd'.
  110  * Used error codes: NET_EIO
  111  */
  112 int net_puts(int fd, const char *s, size_t len, char **errstr);
  113 
  114 /*
  115  * net_close_socket()
  116  *
  117  * Closes a socket.
  118  */
  119 void net_close_socket(int fd);
  120 
  121 /*
  122  * net_get_canonical_hostname()
  123  *
  124  * Get a canonical host name. This means that the name is meaningful to
  125  * other hosts. Usually, it is the fully qualified domain name.
  126  *
  127  * You can optionally specify a hostname to try and canonicalize; if this is
  128  * NULL, the name of the local host is used.
  129  */
  130 char *net_get_canonical_hostname(const char *hostname);
  131 
  132 /*
  133  * net_get_srv_query()
  134  *
  135  * Construct a SRV record query for the given service at the given domain.
  136  * For example, with service "pop3s" and domain "example.com", this function
  137  * returns the SRV query "_pop3._tcp.example.com" as an allocated string.
  138  */
  139 char* net_get_srv_query(const char *domain, const char *service);
  140 
  141 /*
  142  * net_get_srv()
  143  *
  144  * Fetches a SRV record for the given query string (typically constructed with
  145  * net_get_srv_query()), and returns its hostname and port.
  146  * If more than one matching SRV record exists, this chooses the record based
  147  * on its priority and weight.
  148  * Used error codes:
  149  * - NET_ELIBFAILED: libresolv is missing so we cannot get SRV records
  150  * - NET_ESRVNOTFOUND: the SRV record was not found
  151  * - NET_EIO: a SRV record was found but could not be interpreted
  152  */
  153 int net_get_srv_record(const char* query, char **hostname, int *port);
  154 
  155 /*
  156  * net_lib_deinit()
  157  *
  158  * Deinit networking library
  159  */
  160 void net_lib_deinit(void);
  161 
  162 /*
  163  * net_exitcode()
  164  *
  165  * Translate NET_* error code to an error code from sysexits.h
  166  */
  167 int net_exitcode(int net_error_code);
  168 
  169 #endif