1 #ifndef LO_TYPES_H
2 #define LO_TYPES_H
3 
4 #ifdef HAVE_CONFIG_H
5 #include "config.h"
6 #endif
7 
8 #ifdef HAVE_SYS_SOCKET_H
9 #include <sys/types.h>
10 #include <sys/socket.h>
11 #endif
12 
13 #ifdef HAVE_POLL
14 #include <poll.h>
15 #endif
16 
17 #if defined(WIN32) || defined(_MSC_VER)
18 #include <winsock2.h>
19 #include <ws2tcpip.h>
20 #else
21 #ifndef ESP_PLATFORM
22 #define closesocket close
23 #endif
24 #include <netdb.h>
25 #include <netinet/in.h>
26 #include <arpa/inet.h>
27 #endif
28 
29 #ifdef _MSC_VER
30 typedef SSIZE_T ssize_t;
31 #endif
32 
33 #ifndef UINT_PTR
34   #ifdef HAVE_UINTPTR_T
35     #include <stdint.h>
36     #define UINT_PTR uintptr_t
37   #else
38     #define UINT_PTR unsigned long
39   #endif
40 #endif
41 
42 #ifdef ENABLE_THREADS
43 #ifdef HAVE_LIBPTHREAD
44 #include <pthread.h>
45 #endif
46 #endif
47 
48 #include "lo/lo_osc_types.h"
49 
50 #define LO_HOST_SIZE 1024
51 
52 /** \brief Bitflags for optional protocol features, set by
53  *         lo_address_set_flags(). */
54 typedef enum {
55     LO_SLIP=0x01,     /*!< SLIP decoding */
56     LO_NODELAY=0x02,  /*!< Set the TCP_NODELAY socket option. */
57 } lo_proto_flags;
58 
59 /** \brief Bitflags for optional server features. */
60 typedef enum {
61     LO_SERVER_COERCE=0x01,     /*!< whether or not to coerce args
62                                 * during dispatch */
63     LO_SERVER_ENQUEUE=0x02,    /*!< whether or not to enqueue early
64                                 * messages */
65 } lo_server_flags;
66 
67 #define LO_SERVER_DEFAULT_FLAGS (LO_SERVER_COERCE | LO_SERVER_ENQUEUE)
68 
69 typedef void (*lo_err_handler) (int num, const char *msg,
70                                 const char *path);
71 
72 struct _lo_method;
73 
74 typedef struct _lo_inaddr {
75     union {
76         struct in_addr addr;
77         struct in6_addr addr6;
78     } a;
79     size_t size;
80     char *iface;
81 } *lo_inaddr;
82 
83 typedef struct _lo_address {
84     char *host;
85     int socket;
86     int ownsocket;
87     char *port;
88     int protocol;
89     lo_proto_flags flags;
90     struct addrinfo *ai;
91     struct addrinfo *ai_first;
92     int errnum;
93     const char *errstr;
94     int ttl;
95     struct _lo_inaddr addr;
96     struct _lo_server *source_server;
97     const char *source_path; /* does not need to be freed since it
98                               * will always point to stack memory in
99                               * dispatch_method() */
100 } *lo_address;
101 
102 typedef struct _lo_blob {
103     uint32_t size;
104     char *data;
105 } *lo_blob;
106 
107 typedef struct _lo_message {
108     char *types;
109     size_t typelen;
110     size_t typesize;
111     void *data;
112     size_t datalen;
113     size_t datasize;
114     lo_address source;
115     lo_arg **argv;
116     /* timestamp from bundle (LO_TT_IMMEDIATE for unbundled messages) */
117     lo_timetag ts;
118     int refcount;
119 } *lo_message;
120 
121 typedef int (*lo_method_handler) (const char *path, const char *types,
122                                   lo_arg ** argv, int argc,
123                                   struct _lo_message * msg,
124                                   void *user_data);
125 
126 typedef int (*lo_bundle_start_handler) (lo_timetag time, void *user_data);
127 typedef int (*lo_bundle_end_handler) (void *user_data);
128 
129 typedef struct _lo_method {
130     const char *path;
131     const char *typespec;
132     lo_method_handler handler;
133     char *user_data;
134     struct _lo_method *next;
135 } *lo_method;
136 
137 struct socket_context {
138     char *buffer;
139     size_t buffer_size;
140     unsigned int buffer_msg_offset;
141     unsigned int buffer_read_offset;
142     int is_slip;                        //<! 1 if slip mode, 0 otherwise, -1 for unknown
143     int slip_state;                     //<! state variable for slip decoding
144 };
145 
146 #ifdef HAVE_POLL
147     typedef struct pollfd lo_server_fd_type;
148 #else
149     typedef struct { int fd; } lo_server_fd_type;
150 #endif
151 
152 typedef struct _lo_server {
153     struct addrinfo *ai;
154     lo_method first;
155     lo_err_handler err_h;
156     int port;
157     char *hostname;
158     char *path;
159     int protocol;
160     lo_server_flags flags;
161     void *queued;
162     struct sockaddr_storage addr;
163     socklen_t addr_len;
164     int sockets_len;
165     int sockets_alloc;
166     lo_server_fd_type *sockets;
167 
168     // Some extra data needed per open socket.  Note that we don't put
169     // it in the socket struct, because that layout is needed for
170     // passing in the list of sockets to poll().
171     struct socket_context *contexts;
172 
173     struct _lo_address *sources;
174     int sources_len;
175     lo_bundle_start_handler bundle_start_handler;
176     lo_bundle_end_handler bundle_end_handler;
177     void *bundle_handler_user_data;
178     struct _lo_inaddr addr_if;
179     void *error_user_data;
180     int max_msg_size;
181 } *lo_server;
182 
183 #ifdef ENABLE_THREADS
184 struct _lo_server_thread;
185 typedef int (*lo_server_thread_init_callback)(struct _lo_server_thread *s,
186                                               void *user_data);
187 typedef void (*lo_server_thread_cleanup_callback)(struct _lo_server_thread *s,
188                                                   void *user_data);
189 typedef struct _lo_server_thread {
190     lo_server s;
191 #ifdef HAVE_LIBPTHREAD
192     pthread_t thread;
193 #else
194 #ifdef HAVE_WIN32_THREADS
195     HANDLE thread;
196 #endif
197 #endif
198     volatile int active;
199     volatile int done;
200     lo_server_thread_init_callback cb_init;
201     lo_server_thread_cleanup_callback cb_cleanup;
202     void *user_data;
203 } *lo_server_thread;
204 #else
205 typedef void *lo_server_thread;
206 #endif
207 
208 typedef struct _lo_bundle *lo_bundle;
209 
210 typedef struct _lo_element {
211     lo_element_type type;
212     union {
213         lo_bundle bundle;
214         struct {
215             lo_message msg;
216             const char *path;
217         } message;
218     } content;
219 } lo_element;
220 
221 struct _lo_bundle {
222     size_t size;
223     size_t len;
224     lo_timetag ts;
225     lo_element *elmnts;
226     int refcount;
227 };
228 
229 typedef struct _lo_strlist {
230     char *str;
231     struct _lo_strlist *next;
232 } lo_strlist;
233 
234 typedef union {
235     int32_t i;
236     float f;
237     char c;
238     uint32_t nl;
239 } lo_pcast32;
240 
241 typedef union {
242     int64_t i;
243     double f;
244     uint64_t nl;
245 } lo_pcast64;
246 
247 extern struct lo_cs {
248     int udp;
249     int tcp;
250 } lo_client_sockets;
251 
252 #endif
253