1 /* -*- Mode: C; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 /*
3  *     Copyright 2014-2020 Couchbase, Inc.
4  *
5  *   Licensed under the Apache License, Version 2.0 (the "License");
6  *   you may not use this file except in compliance with the License.
7  *   You may obtain a copy of the License at
8  *
9  *       http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *   Unless required by applicable law or agreed to in writing, software
12  *   distributed under the License is distributed on an "AS IS" BASIS,
13  *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *   See the License for the specific language governing permissions and
15  *   limitations under the License.
16  */
17 
18 #ifndef LCBIO_UTILS_H
19 #define LCBIO_UTILS_H
20 
21 #ifdef __cplusplus
22 extern "C" {
23 #endif
24 
25 /**
26  * @file
27  * @brief Various I/O-related utilities
28  */
29 
30 typedef enum {
31     LCBIO_CSERR_BUSY,     /* request pending */
32     LCBIO_CSERR_INTR,     /* eintr */
33     LCBIO_CSERR_EINVAL,   /* einval */
34     LCBIO_CSERR_EFAIL,    /* hard failure */
35     LCBIO_CSERR_CONNECTED /* connection established */
36 } lcbio_CSERR;
37 
38 /**
39  * Convert the system errno (indicated by 'syserr')
40  * @param syserr system error code
41  * @return a status code simplifying the error
42  */
43 lcbio_CSERR lcbio_mkcserr(int syserr);
44 
45 /**
46  * Assigns the target error code if it indicates a 'fatal' or 'relevant' error
47  * code.
48  *
49  * @param in Error code to inspect
50  * @param[out] out target pointer
51  */
52 void lcbio_mksyserr(lcbio_OSERR in, lcbio_OSERR *out);
53 
54 /**
55  * Convert a system error code into one suitable for returning to the user
56  * @param in The code received. This can be 0, for graceful shutdown
57  * @param settings The settings for the library
58  * @return An error code.
59  */
60 lcb_STATUS lcbio_mklcberr(lcbio_OSERR in, const lcb_settings *settings);
61 
62 /**
63  * Traverse the addrinfo structure and return a socket.
64  * @param io the iotable structure used to create the socket
65  * @param[in,out] ai an addrinfo structure
66  * @param[out] connerr an error if a socket could not be established.
67  * @return a new socket, or INVALID_SOCKET
68  *
69  * The ai structure should be considered as an opaque iterator. This function
70  * will look at the first entry in the list and attempt to create a socket.
71  * It will traverse through each entry and break when either a socket has
72  * been successfully created, or no more addrinfo entries remain.
73  */
74 lcb_socket_t lcbio_E_ai2sock(lcbio_pTABLE io, struct addrinfo **ai, int *connerr);
75 
76 lcb_sockdata_t *lcbio_C_ai2sock(lcbio_pTABLE io, struct addrinfo **ai, int *conerr);
77 
78 struct lcbio_NAMEINFO {
79     char local[NI_MAXHOST + NI_MAXSERV + 2];
80     char remote[NI_MAXHOST + NI_MAXSERV + 2];
81 };
82 
83 int lcbio_get_nameinfo(lcbio_SOCKET *sock, struct lcbio_NAMEINFO *nistrs);
84 
85 /** Basic wrapper around the @ref lcb_ioE_chkclosed_fn family */
86 int lcbio_is_netclosed(lcbio_SOCKET *sock, int flags);
87 
88 /**
89  * Enable an option on a socket
90  * @param sock The socket
91  * @param cntl The option (LCB_IO_CNTL_xxx)
92  * @return
93  */
94 lcb_STATUS lcbio_enable_sockopt(lcbio_SOCKET *sock, int cntl);
95 
96 const char *lcbio_strsockopt(int cntl);
97 
98 void lcbio__load_socknames(lcbio_SOCKET *sock);
99 
100 #ifdef _WIN32
101 #define lcbio_syserrno GetLastError()
102 #else
103 #define lcbio_syserrno errno
104 #endif
105 
106 #ifdef __cplusplus
107 }
108 
109 namespace lcb
110 {
111 namespace io
112 {
113 
114 /**
115  * This interface defines a pending connection request. It may be
116  * cancelled.
117  */
118 class ConnectionRequest
119 {
120   public:
121     virtual void cancel() = 0;
122     virtual ~ConnectionRequest() = default;
cancel(ConnectionRequest **pp)123     static void cancel(ConnectionRequest **pp)
124     {
125         if (*pp) {
126             (*pp)->cancel();
127             *pp = nullptr;
128         }
129     }
130 };
131 
132 } // namespace io
133 } // namespace lcb
134 
135 #endif
136 #endif
137