1 /* -*- Mode: C; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 /*
3  *     Copyright 2019 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 /*
19  * Header including C socket portability declarations
20  */
21 #pragma once
22 
23 #include <platform/dynamic.h>
24 #include <platform/visibility.h>
25 
26 #include <stdint.h>
27 
28 #ifdef WIN32
29 #include <winsock2.h>
30 #endif
31 
32 #ifdef __cplusplus
33 extern "C" {
34 #endif
35 
36 #ifdef WIN32
37 #define CB_DONT_NEED_BYTEORDER 1
38 struct iovec {
39     size_t iov_len;
40     void* iov_base;
41 };
42 
43 #define IOV_MAX 1024
44 
45 struct msghdr {
46     void* msg_name; /* Socket name */
47     int msg_namelen; /* Length of name */
48     struct iovec* msg_iov; /* Data blocks */
49     int msg_iovlen; /* Number of blocks */
50 };
51 
52 PLATFORM_PUBLIC_API
53 int sendmsg(SOCKET sock, const struct msghdr* msg, int flags);
54 
55 /**
56  * Initialize the winsock library
57  */
58 PLATFORM_PUBLIC_API
59 void cb_initialize_sockets(void);
60 #else // WIN32
61 #define cb_initialize_sockets()
62 #endif // WIN32
63 
64 #ifdef __cplusplus
65 }
66 #endif
67 
68 #ifndef CB_DONT_NEED_BYTEORDER
69 #include <folly/Bits.h>
70 
71 PLATFORM_PUBLIC_API
ntohll(uint64_t x)72 inline uint64_t ntohll(uint64_t x) {
73     return folly::Endian::big(x);
74 }
75 
76 PLATFORM_PUBLIC_API
htonll(uint64_t x)77 inline uint64_t htonll(uint64_t x) {
78     return folly::Endian::big(x);
79 }
80 #endif // CB_DONT_NEED_BYTEORDER
81