iotivity 0.9.0
[platform/upstream/iotivity.git] / service / protocol-plugin / plugins / mqtt-light / lib / net_mosq.h
1 /*
2 Copyright (c) 2010,2011,2013 Roger Light <roger@atchoo.org>
3 All rights reserved.
4
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
7
8 1. Redistributions of source code must retain the above copyright notice,
9    this list of conditions and the following disclaimer.
10 2. Redistributions in binary form must reproduce the above copyright
11    notice, this list of conditions and the following disclaimer in the
12    documentation and/or other materials provided with the distribution.
13 3. Neither the name of mosquitto nor the names of its
14    contributors may be used to endorse or promote products derived from
15    this software without specific prior written permission.
16
17 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 POSSIBILITY OF SUCH DAMAGE.
28 */
29 #ifndef _NET_MOSQ_H_
30 #define _NET_MOSQ_H_
31
32 #ifndef WIN32
33 #include <unistd.h>
34 #else
35 #include <winsock2.h>
36 typedef int ssize_t;
37 #endif
38
39 #include "mosquitto_internal.h"
40 #include "mosquitto.h"
41
42 #ifdef WITH_BROKER
43 struct mosquitto_db;
44 #endif
45
46 #ifdef WIN32
47 #  define COMPAT_CLOSE(a) closesocket(a)
48 #  define COMPAT_ECONNRESET WSAECONNRESET
49 #  define COMPAT_EWOULDBLOCK WSAEWOULDBLOCK
50 #else
51 #  define COMPAT_CLOSE(a) close(a)
52 #  define COMPAT_ECONNRESET ECONNRESET
53 #  define COMPAT_EWOULDBLOCK EWOULDBLOCK
54 #endif
55
56 #ifndef WIN32
57 #else
58 #endif
59
60 /* For when not using winsock libraries. */
61 #ifndef INVALID_SOCKET
62 #define INVALID_SOCKET -1
63 #endif
64
65 /* Macros for accessing the MSB and LSB of a uint16_t */
66 #define MOSQ_MSB(A) (uint8_t)((A & 0xFF00) >> 8)
67 #define MOSQ_LSB(A) (uint8_t)(A & 0x00FF)
68
69 void _mosquitto_net_init(void);
70 void _mosquitto_net_cleanup(void);
71
72 void _mosquitto_packet_cleanup(struct _mosquitto_packet *packet);
73 int _mosquitto_packet_queue(struct mosquitto *mosq, struct _mosquitto_packet *packet);
74 int _mosquitto_socket_connect(struct mosquitto *mosq, const char *host, uint16_t port, const char *bind_address, bool blocking);
75 int _mosquitto_socket_close(struct mosquitto *mosq);
76 int _mosquitto_try_connect(const char *host, uint16_t port, int *sock, const char *bind_address, bool blocking);
77 int _mosquitto_socket_nonblock(int sock);
78 int _mosquitto_socketpair(int *sp1, int *sp2);
79
80 int _mosquitto_read_byte(struct _mosquitto_packet *packet, uint8_t *byte);
81 int _mosquitto_read_bytes(struct _mosquitto_packet *packet, void *bytes, uint32_t count);
82 int _mosquitto_read_string(struct _mosquitto_packet *packet, char **str);
83 int _mosquitto_read_uint16(struct _mosquitto_packet *packet, uint16_t *word);
84
85 void _mosquitto_write_byte(struct _mosquitto_packet *packet, uint8_t byte);
86 void _mosquitto_write_bytes(struct _mosquitto_packet *packet, const void *bytes, uint32_t count);
87 void _mosquitto_write_string(struct _mosquitto_packet *packet, const char *str, uint16_t length);
88 void _mosquitto_write_uint16(struct _mosquitto_packet *packet, uint16_t word);
89
90 ssize_t _mosquitto_net_read(struct mosquitto *mosq, void *buf, size_t count);
91 ssize_t _mosquitto_net_write(struct mosquitto *mosq, void *buf, size_t count);
92
93 int _mosquitto_packet_write(struct mosquitto *mosq);
94 #ifdef WITH_BROKER
95 int _mosquitto_packet_read(struct mosquitto_db *db, struct mosquitto *mosq);
96 #else
97 int _mosquitto_packet_read(struct mosquitto *mosq);
98 #endif
99
100 #ifdef WITH_TLS
101 int _mosquitto_socket_apply_tls(struct mosquitto *mosq);
102 #endif
103
104 #endif