Debian Codegraph @

src/core/lo/send.c

raw symbols SCIP
1 /*
2 * Copyright (C) 2014 Steve Harris et al. (see AUTHORS)
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser General Public License as
6 * published by the Free Software Foundation; either version 2.1 of the
7 * License, or (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU Lesser General Public License for more details.
13 *
14 * $Id$
15 */
16
17 #include "config.h"
18
19 #include <stdarg.h>
20 #include <stdlib.h>
21 #include <stdio.h>
22 #include <string.h>
23 #include <errno.h>
24 #include <sys/types.h>
25
26 #if !defined(_WIN32)
27 #include <ifaddrs.h> // ifaddr, see is_local_broadcast method
28 #endif
29
30 #if defined(_WIN32) || defined(_MSC_VER)
31 #include <io.h>
32 #include <winsock2.h>
33 #include <ws2tcpip.h>
34 #else
35 #include <netdb.h>
36 #include <sys/socket.h>
37 #include <sys/un.h>
38 #include <net/if.h>
39 #include <sys/ioctl.h>
40 #include <arpa/inet.h>
41 #include <unistd.h>
42 #include <netinet/tcp.h>
43 #endif
44
45 #include "lo_types_internal.h"
46 #include "lo_internal.h"
47 #include "lo.h"
48
49 #ifndef MSG_NOSIGNAL
50 #define MSG_NOSIGNAL 0
51 #endif
52
53 #if defined(_WIN32) || defined(_MSC_VER)
54 int initWSock();
55 #endif
56
57 #if defined(_WIN32) || defined(_MSC_VER)
58 #define geterror() WSAGetLastError()
59 #else
60 #define geterror() errno
61 #endif
62
63 static int create_socket(lo_address a);
64 static int send_data(lo_address a, lo_server from, char *data,
65 const size_t data_len);
66
67 // message.c
68 int lo_message_add_varargs_internal(lo_message m, const char *types,
69 va_list ap, const char *file,
70 int line);
71
72 static
73 int lo_send_varargs_internal(lo_address t, const char *file,
74 const int line, const char *path,
75 const char *types, va_list ap)
76 {
77 int ret;
78 lo_message msg = lo_message_new();
79
80 t->errnum = 0;
81 t->errstr = NULL;
82
83 ret = lo_message_add_varargs_internal(msg, types, ap, file, line);
84
85 if (ret) {
86 lo_message_free(msg);
87 t->errnum = ret;
88 if (ret == -1)
89 t->errstr = "unknown type";
90 else
91 t->errstr = "bad format/args";
92 return ret;
93 }
94
95 ret = lo_send_message(t, path, msg);
96 lo_message_free(msg);
97
98 return ret;
99 }
100
101 #if defined(USE_ANSI_C) || defined(DLL_EXPORT)
102 int lo_send(lo_address t, const char *path, const char *types, ...)
103 {
104 const char *file = "";
105 int line = 0;
106 va_list ap;
107 va_start(ap, types);
108 int ret = lo_send_varargs_internal(t, file, line, path, types, ap);
109 va_end(ap);
110 return ret;
111 }
112 #endif
113
114 /* Don't call lo_send_internal directly, use lo_send, a macro wrapping this
115 * function with appropriate values for file and line */
116
117 int lo_send_internal(lo_address t, const char *file, const int line,
118 const char *path, const char *types, ...)
119 {
120 va_list ap;
121 va_start(ap, types);
122 int ret = lo_send_varargs_internal(t, file, line, path, types, ap);
123 va_end(ap);
124 return ret;
125 }
126
127 static
128 int lo_send_timestamped_varargs_internal(lo_address t, const char *file,
129 const int line, lo_timetag ts,
130 const char *path, const char *types,
131 va_list ap)
132 {
133 int ret;
134 lo_message msg = lo_message_new();
135 lo_bundle b = lo_bundle_new(ts);
136
137 t->errnum = 0;
138 t->errstr = NULL;
139
140 ret = lo_message_add_varargs_internal(msg, types, ap, file, line);
141
142 if (ret == 0) {
143 lo_bundle_add_message(b, path, msg);
144 ret = lo_send_bundle(t, b);
145 }
146
147 lo_message_free(msg);
148 lo_bundle_free(b);
149
150 return ret;
151 }
152
153
154 #if defined(USE_ANSI_C) || defined(DLL_EXPORT)
155 int lo_send_timestamped(lo_address t, lo_timetag ts,
156 const char *path, const char *types, ...)
157 {
158 const char *file = "";
159 int line = 0;
160 va_list ap;
161 va_start(ap, types);
162 int ret = lo_send_timestamped_varargs_internal(t, file, line, ts, path,
163 types, ap);
164 va_end(ap);
165 return ret;
166 }
167 #endif
168
169 /* Don't call lo_send_timestamped_internal directly, use lo_send_timestamped, a
170 * macro wrapping this function with appropriate values for file and line */
171 int lo_send_timestamped_internal(lo_address t, const char *file,
172 const int line, lo_timetag ts,
173 const char *path, const char *types, ...)
174 {
175 va_list ap;
176 va_start(ap, types);
177 int ret = lo_send_timestamped_varargs_internal(t, file, line, ts, path,
178 types, ap);
179 va_end(ap);
180 return ret;
181 }
182
183 static
184 int lo_send_from_varargs_internal(lo_address to, lo_server from,
185 const char *file,
186 const int line, lo_timetag ts,
187 const char *path, const char *types,
188 va_list ap)
189 {
190 lo_bundle b = NULL;
191 int ret;
192
193 lo_message msg = lo_message_new();
194 if (ts.sec != LO_TT_IMMEDIATE.sec || ts.frac != LO_TT_IMMEDIATE.frac)
195 b = lo_bundle_new(ts);
196
197 // Clear any previous errors
198 to->errnum = 0;
199 to->errstr = NULL;
200
201 ret = lo_message_add_varargs_internal(msg, types, ap, file, line);
202
203 if (ret == 0) {
204 if (b) {
205 lo_bundle_add_message(b, path, msg);
206 ret = lo_send_bundle_from(to, from, b);
207 } else {
208 ret = lo_send_message_from(to, from, path, msg);
209 }
210 }
211 // Free-up memory
212 lo_message_free(msg);
213 if (b)
214 lo_bundle_free(b);
215
216 return ret;
217 }
218
219 #if defined(USE_ANSI_C) || defined(DLL_EXPORT)
220 int lo_send_from(lo_address to, lo_server from, lo_timetag ts,
221 const char *path, const char *types, ...)
222 {
223 const char *file = "";
224 int line = 0;
225 va_list ap;
226 va_start(ap, types);
227 int ret = lo_send_from_varargs_internal(to, from, file, line, ts,
228 path, types, ap);
229 va_end(ap);
230 return ret;
231 }
232 #endif
233
234 /* Don't call lo_send_from_internal directly, use macros wrapping this
235 * function with appropriate values for file and line */
236
237 int lo_send_from_internal(lo_address to, lo_server from, const char *file,
238 const int line, const lo_timetag ts,
239 const char *path, const char *types, ...)
240 {
241 va_list ap;
242 va_start(ap, types);
243 int ret = lo_send_from_varargs_internal(to, from, file, line, ts,
244 path, types, ap);
245 va_end(ap);
246 return ret;
247 }
248
249 #if !defined(_WIN32)
250 static int is_local_broadcast(struct addrinfo *ai){
251 struct ifaddrs *ifap, *ifa;
252 uint32_t destination_addr = ((struct sockaddr_in *) ai->ai_addr)->sin_addr.s_addr;
253 int result;
254
255 getifaddrs(&ifap);
256
257 result = 0;
258
259 // loop over all network interfaces
260 for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
261 if (ifa->ifa_addr && ifa->ifa_addr->sa_family==AF_INET) {
262 // get this machine's address for the current interface (ie. 192.168.1.6)
263 uint32_t cur_addr = ((struct sockaddr_in *) ifa->ifa_addr)->sin_addr.s_addr;
264 // get the subnet mask for the current interface (ie. 255.255.0.0)
265 uint32_t cur_subn = ((struct sockaddr_in *) ifa->ifa_netmask)->sin_addr.s_addr;
266
267 // check if the subnet part (ie. 192.168._._) of the given address matches
268 if ((cur_subn & cur_addr) == (cur_subn & destination_addr)) {
269 // check if the non-subnet (ie. _._.1.6) part of the destination address
270 // indicates that it is a broadcast address (all ones)
271 if ((cur_subn | destination_addr) == 0xFFFFFFFF) {
272 result = 1;
273 }
274
275 break;
276 }
277 }
278 }
279
280 freeifaddrs(ifap);
281 return result;
282 }
283 #endif
284
285 static int is_broadcast(struct addrinfo *ai) {
286 struct sockaddr_in *si = (struct sockaddr_in *) ai->ai_addr;
287 unsigned char *ip = (unsigned char *) &(si->sin_addr);
288 if(AF_INET == ai->ai_family) {
289 // is it the "global" broadcast address?
290 if (ip[0] == 255 && ip[1] == 255 && ip[2] == 255 && ip[3] == 255)
291 return 1;
292
293 #if !defined(_WIN32)
294 // check if the address is the "local" broadcast
295 // addres of any of the active network interfaces
296 return is_local_broadcast(ai);
297 #endif
298 }
299 return 0;
300 }
301
302 static int create_socket(lo_address a)
303 {
304 switch(a->protocol) {
305 case LO_UDP:
306 a->socket = socket(a->ai->ai_family, a->ai->ai_socktype, 0);
307 if (a->socket == -1) {
308 a->errnum = geterror();
309 a->errstr = NULL;
310 return -1;
311 }
312 // if UDP and destination address is broadcast,
313 // then allow broadcast on the socket
314 if (is_broadcast(a->ai)) {
315 int opt = 1;
316 setsockopt(a->socket, SOL_SOCKET, SO_BROADCAST,
317 (const char*)&opt, sizeof(int));
318 }
319 break;
320 case LO_TCP: {
321 struct addrinfo *ai = a->ai;
322 int sfd = -1;
323 for(ai=a->ai; ai != NULL; ai = ai->ai_next) {
324 sfd = socket(ai->ai_family, ai->ai_socktype, 0);
325 if (-1 == sfd)
326 continue;
327 if(-1 != connect(sfd, ai->ai_addr, ai->ai_addrlen))
328 break;
329 closesocket(sfd);
330 }
331 if(NULL == ai) {
332 /* all addresses failed */
333 a->socket = -1;
334 a->errnum = geterror();
335 a->errstr = NULL;
336 return -1;
337 }
338 a->socket = sfd;
339 }
340 break;
341 #if !defined(_WIN32) && !defined(_MSC_VER)
342 case LO_UNIX: {
343 struct sockaddr_un sa;
344
345 a->socket = socket(PF_UNIX, SOCK_DGRAM, 0);
346 if (a->socket == -1) {
347 a->errnum = geterror();
348 a->errstr = NULL;
349 return -1;
350 }
351
352 sa.sun_family = AF_UNIX;
353 strncpy(sa.sun_path, a->port, sizeof(sa.sun_path) - 1);
354
355 if ((connect(a->socket, (struct sockaddr *) &sa, sizeof(sa))) < 0) {
356 a->errnum = geterror();
357 a->errstr = NULL;
358 closesocket(a->socket);
359 a->socket = -1;
360 return -1;
361 }
362 }
363 break;
364 #endif
365 default:
366 /* unknown protocol */
367 return -2;
368 }
369
370 #ifdef SO_NOSIGPIPE
371 {
372 // On Mac OS X: Prevent the socket from causing a SIGPIPE.
373 // cf MSG_NOSIGNAL on Linux.
374 int option = 1; // yes, we don't want SIGPIPE
375 setsockopt(a->socket, SOL_SOCKET, SO_NOSIGPIPE, &option,
376 sizeof(option));
377 }
378 #endif
379
380 #ifdef TCP_NODELAY
381 if (a->flags & LO_NODELAY) {
382 int option = 1;
383 setsockopt(a->socket, IPPROTO_TCP, TCP_NODELAY,
384 (const char*)&option, sizeof(option));
385 }
386 #endif
387
388 return 0;
389 }
390
391
392 // From http://tools.ietf.org/html/rfc1055
393 #define SLIP_END 0300 /* indicates end of packet */
394 #define SLIP_ESC 0333 /* indicates byte stuffing */
395 #define SLIP_ESC_END 0334 /* ESC ESC_END means END data byte */
396 #define SLIP_ESC_ESC 0335 /* ESC ESC_ESC means ESC data byte */
397
398 static unsigned char *slip_encode(const unsigned char *data,
399 size_t *data_len,
400 int double_end_slip_enabled)
401 {
402 size_t i, j = 0, len=*data_len;
403 unsigned char *slipdata = (unsigned char *) malloc(len*2);
404 if (double_end_slip_enabled)
405 slipdata[j++] = SLIP_END;
406 for (i=0; i<len; i++) {
407 switch (data[i])
408 {
409 case SLIP_ESC:
410 slipdata[j++] = SLIP_ESC;
411 slipdata[j++] = SLIP_ESC_ESC;
412 break;
413 case SLIP_END:
414 slipdata[j++] = SLIP_ESC;
415 slipdata[j++] = SLIP_ESC_END;
416 break;
417 default:
418 slipdata[j++] = data[i];
419 }
420 }
421 slipdata[j++] = SLIP_END;
422 slipdata[j] = 0;
423 *data_len = j;
424 return slipdata;
425 }
426
427 static int send_data(lo_address a, lo_server from, char *data,
428 const size_t data_len)
429 {
430 ssize_t ret = 0;
431 int sock = -1;
432
433 #if defined(_WIN32) || defined(_MSC_VER)
434 if (!initWSock())
435 return -1;
436 #endif
437
438 if (a->protocol == LO_UDP && data_len > LO_MAX_UDP_MSG_SIZE) {
439 a->errnum = 99;
440 a->errstr = "Attempted to send message in excess of maximum "
441 "message size";
442 return -1;
443 }
444 // Resolve the destination address, if not done already
445 if (!a->ai) {
446 ret = lo_address_resolve(a);
447 if (ret)
448 return (int) ret;
449 }
450 // Re-use existing socket?
451 if (from && a->protocol == LO_UDP) {
452 sock = from->sockets[0].fd;
453 } else if (a->protocol == LO_UDP && lo_client_sockets.udp != -1) {
454 sock = lo_client_sockets.udp;
455 } else {
456 if (a->socket == -1) {
457 ret = create_socket(a);
458 if (ret)
459 return (int) ret;
460 }
461
462 // If we are sending TCP, we may later receive on sending
463 // socket, so add it to the from server's socket list.
464 if (from && a->protocol == LO_TCP)
465 {
466 if (a->socket >= from->sources_len
467 || from->sources[a->socket].host == NULL)
468 {
469 lo_server_add_socket(from, a->socket, a, 0, 0);
470
471 // If a socket is added to the server, the server is
472 // now responsible for closing it.
473 a->ownsocket = 0;
474 }
475 }
476 sock = a->socket;
477 }
478
479 if (a->protocol == LO_TCP && !(a->flags & LO_SLIP)) {
480 // For TCP only, send the length of the following data
481 int32_t size = htonl(data_len);
482 ret = send(sock, (const void*)&size, sizeof(size), MSG_NOSIGNAL);
483 }
484 // Send the data
485 if (ret != -1) {
486 if (a->protocol == LO_UDP) {
487 struct addrinfo* ai;
488 if (a->addr.size == sizeof(struct in_addr)) {
489 setsockopt(sock, IPPROTO_IP, IP_MULTICAST_IF,
490 (const char*)&a->addr.a, (socklen_t) a->addr.size);
491 }
492 #ifdef ENABLE_IPV6
493 else if (a->addr.size == sizeof(struct in6_addr)) {
494 setsockopt(sock, IPPROTO_IP, IPV6_MULTICAST_IF,
495 (const char*)&a->addr.a, (socklen_t) a->addr.size);
496 }
497 #endif
498 if (a->ttl >= 0) {
499 unsigned char ttl = (unsigned char) a->ttl;
500 setsockopt(sock, IPPROTO_IP, IP_MULTICAST_TTL,
501 (const char*)&ttl, sizeof(ttl));
502 }
503
504 ai = a->ai;
505
506 do {
507 ret = sendto(sock, data, data_len, MSG_NOSIGNAL,
508 ai->ai_addr, ai->ai_addrlen);
509 ai = ai->ai_next;
510 } while (ret == -1 && ai != NULL);
511 if (ret == -1 && ai != NULL && a->ai!=ai)
512 a->ai = ai;
513 #if !defined(_WIN32) && !defined(_MSC_VER)
514 } else if (a->protocol == LO_UNIX && from && from->protocol == LO_UNIX) {
515 struct sockaddr_un saddr;
516 size_t len = data_len;
517
518 saddr.sun_family = AF_UNIX;
519 strncpy(saddr.sun_path, a->port, sizeof(saddr.sun_path) - 1);
520
521 ret = sendto(from->sockets[0].fd, data, len, MSG_NOSIGNAL, (struct sockaddr*)&saddr, sizeof(struct sockaddr_un));
522 #endif
523 } else {
524
525 size_t len = data_len;
526 if (a->flags & LO_SLIP)
527 data = (char*)slip_encode((unsigned char*)data, &len,
528 a->flags & LO_SLIP_DBL_END);
529
530 ret = send(sock, data, len, MSG_NOSIGNAL);
531
532 if (a->flags & LO_SLIP)
533 free(data);
534 }
535 }
536
537 if (ret == -1) {
538 if (a->protocol == LO_TCP) {
539 closesocket(a->socket);
540 if (from)
541 lo_server_del_socket(from, -1, a->socket);
542 a->socket = -1;
543 }
544
545 a->errnum = geterror();
546 a->errstr = NULL;
547 } else {
548 a->errnum = 0;
549 a->errstr = NULL;
550 }
551
552 return (int) ret;
553 }
554
555
556 int lo_send_message(lo_address a, const char *path, lo_message msg)
557 {
558 return lo_send_message_from(a, NULL, path, msg);
559 }
560
561 int lo_send_message_from(lo_address a, lo_server from, const char *path,
562 lo_message msg)
563 {
564 const size_t data_len = lo_message_length(msg, path);
565 char *data = (char*) lo_message_serialise(msg, path, NULL, NULL);
566
567 // Send the message
568 int ret = send_data(a, from, data, data_len);
569
570 // For TCP, retry once if it failed. The first try will return
571 // error if the connection was closed, so the second try will
572 // attempt to re-open the connection.
573 if (ret == -1 && a->protocol == LO_TCP)
574 ret = send_data(a, from, data, data_len);
575
576 // Free the memory allocated by lo_message_serialise
577 if (data)
578 free(data);
579
580 return ret;
581 }
582
583
584 int lo_send_bundle(lo_address a, lo_bundle b)
585 {
586 return lo_send_bundle_from(a, NULL, b);
587 }
588
589
590 int lo_send_bundle_from(lo_address a, lo_server from, lo_bundle b)
591 {
592 size_t data_len;
593 char *data = (char*) lo_bundle_serialise(b, NULL, &data_len);
594
595 // Send the bundle
596 int ret = send_data(a, from, data, data_len);
597
598 // Free the memory allocated by lo_bundle_serialise
599 if (data)
600 free(data);
601
602 return ret;
603 }
604
605 /* vi:set ts=8 sts=4 sw=4: */