UNPKG

4.04 kBtext/x-cView Raw
1/*
2Copyright (c) 2010,2011,2013 Roger Light <roger@atchoo.org>
3All rights reserved.
4
5Redistribution and use in source and binary forms, with or without
6modification, are permitted provided that the following conditions are met:
7
81. Redistributions of source code must retain the above copyright notice,
9 this list of conditions and the following disclaimer.
102. 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.
133. 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
17THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27POSSIBILITY 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>
36typedef int ssize_t;
37#endif
38
39#include "mosquitto_internal.h"
40#include "mosquitto.h"
41
42#ifdef WITH_BROKER
43struct 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
69void _mosquitto_net_init(void);
70void _mosquitto_net_cleanup(void);
71
72void _mosquitto_packet_cleanup(struct _mosquitto_packet *packet);
73int _mosquitto_packet_queue(struct mosquitto *mosq, struct _mosquitto_packet *packet);
74int _mosquitto_socket_connect(struct mosquitto *mosq, const char *host, uint16_t port, const char *bind_address, bool blocking);
75int _mosquitto_socket_close(struct mosquitto *mosq);
76int _mosquitto_try_connect(const char *host, uint16_t port, int *sock, const char *bind_address, bool blocking);
77int _mosquitto_socket_nonblock(int sock);
78int _mosquitto_socketpair(int *sp1, int *sp2);
79
80int _mosquitto_read_byte(struct _mosquitto_packet *packet, uint8_t *byte);
81int _mosquitto_read_bytes(struct _mosquitto_packet *packet, void *bytes, uint32_t count);
82int _mosquitto_read_string(struct _mosquitto_packet *packet, char **str);
83int _mosquitto_read_uint16(struct _mosquitto_packet *packet, uint16_t *word);
84
85void _mosquitto_write_byte(struct _mosquitto_packet *packet, uint8_t byte);
86void _mosquitto_write_bytes(struct _mosquitto_packet *packet, const void *bytes, uint32_t count);
87void _mosquitto_write_string(struct _mosquitto_packet *packet, const char *str, uint16_t length);
88void _mosquitto_write_uint16(struct _mosquitto_packet *packet, uint16_t word);
89
90ssize_t _mosquitto_net_read(struct mosquitto *mosq, void *buf, size_t count);
91ssize_t _mosquitto_net_write(struct mosquitto *mosq, void *buf, size_t count);
92
93int _mosquitto_packet_write(struct mosquitto *mosq);
94#ifdef WITH_BROKER
95int _mosquitto_packet_read(struct mosquitto_db *db, struct mosquitto *mosq);
96#else
97int _mosquitto_packet_read(struct mosquitto *mosq);
98#endif
99
100#ifdef WITH_TLS
101int _mosquitto_socket_apply_tls(struct mosquitto *mosq);
102#endif
103
104#endif