1efea6ba7SBen Huddleston/* -*- Mode: C; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2efea6ba7SBen Huddleston/*
3efea6ba7SBen Huddleston *     Copyright 2019 Couchbase, Inc.
4efea6ba7SBen Huddleston *
5efea6ba7SBen Huddleston *   Licensed under the Apache License, Version 2.0 (the "License");
6efea6ba7SBen Huddleston *   you may not use this file except in compliance with the License.
7efea6ba7SBen Huddleston *   You may obtain a copy of the License at
8efea6ba7SBen Huddleston *
9efea6ba7SBen Huddleston *       http://www.apache.org/licenses/LICENSE-2.0
10efea6ba7SBen Huddleston *
11efea6ba7SBen Huddleston *   Unless required by applicable law or agreed to in writing, software
12efea6ba7SBen Huddleston *   distributed under the License is distributed on an "AS IS" BASIS,
13efea6ba7SBen Huddleston *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14efea6ba7SBen Huddleston *   See the License for the specific language governing permissions and
15efea6ba7SBen Huddleston *   limitations under the License.
16efea6ba7SBen Huddleston */
17efea6ba7SBen Huddleston
18efea6ba7SBen Huddleston/*
19efea6ba7SBen Huddleston * Header including C socket portability declarations
20efea6ba7SBen Huddleston */
21efea6ba7SBen Huddleston#pragma once
22efea6ba7SBen Huddleston
23efea6ba7SBen Huddleston#include <platform/dynamic.h>
24efea6ba7SBen Huddleston#include <platform/visibility.h>
25efea6ba7SBen Huddleston
26efea6ba7SBen Huddleston#include <stdint.h>
27efea6ba7SBen Huddleston
28efea6ba7SBen Huddleston#ifdef WIN32
29efea6ba7SBen Huddleston#include <winsock2.h>
30efea6ba7SBen Huddleston#endif
31efea6ba7SBen Huddleston
32efea6ba7SBen Huddleston#ifdef __cplusplus
33efea6ba7SBen Huddlestonextern "C" {
34efea6ba7SBen Huddleston#endif
35efea6ba7SBen Huddleston
36efea6ba7SBen Huddleston#ifdef WIN32
37efea6ba7SBen Huddleston#define CB_DONT_NEED_BYTEORDER 1
38efea6ba7SBen Huddlestonstruct iovec {
39efea6ba7SBen Huddleston    size_t iov_len;
40efea6ba7SBen Huddleston    void* iov_base;
41efea6ba7SBen Huddleston};
42efea6ba7SBen Huddleston
43efea6ba7SBen Huddleston#define IOV_MAX 1024
44efea6ba7SBen Huddleston
45efea6ba7SBen Huddlestonstruct msghdr {
46efea6ba7SBen Huddleston    void* msg_name; /* Socket name */
47efea6ba7SBen Huddleston    int msg_namelen; /* Length of name */
48efea6ba7SBen Huddleston    struct iovec* msg_iov; /* Data blocks */
49efea6ba7SBen Huddleston    int msg_iovlen; /* Number of blocks */
50efea6ba7SBen Huddleston};
51efea6ba7SBen Huddleston
52efea6ba7SBen HuddlestonPLATFORM_PUBLIC_API
53efea6ba7SBen Huddlestonint sendmsg(SOCKET sock, const struct msghdr* msg, int flags);
54efea6ba7SBen Huddleston
55efea6ba7SBen Huddleston/**
56efea6ba7SBen Huddleston * Initialize the winsock library
57efea6ba7SBen Huddleston */
58efea6ba7SBen HuddlestonPLATFORM_PUBLIC_API
59efea6ba7SBen Huddlestonvoid cb_initialize_sockets(void);
60efea6ba7SBen Huddleston#else // WIN32
61efea6ba7SBen Huddleston#define cb_initialize_sockets()
62efea6ba7SBen Huddleston#endif // WIN32
63efea6ba7SBen Huddleston
64d74d705aSDave Rigby#ifdef __cplusplus
65d74d705aSDave Rigby}
66d74d705aSDave Rigby#endif
67d74d705aSDave Rigby
68efea6ba7SBen Huddleston#ifndef CB_DONT_NEED_BYTEORDER
69d74d705aSDave Rigby#include <folly/Bits.h>
70efea6ba7SBen Huddleston
71efea6ba7SBen HuddlestonPLATFORM_PUBLIC_API
72d74d705aSDave Rigbyinline uint64_t ntohll(uint64_t x) {
73d74d705aSDave Rigby    return folly::Endian::big(x);
74d74d705aSDave Rigby}
75efea6ba7SBen Huddleston
76d74d705aSDave RigbyPLATFORM_PUBLIC_API
77d74d705aSDave Rigbyinline uint64_t htonll(uint64_t x) {
78d74d705aSDave Rigby    return folly::Endian::big(x);
79efea6ba7SBen Huddleston}
80d74d705aSDave Rigby#endif // CB_DONT_NEED_BYTEORDER