Debian Codegraph @

interfaces/artnet/network.c

raw symbols SCIP
1 /*
2 * This library is free software; you can redistribute it and/or
3 * modify it under the terms of the GNU Lesser General Public
4 * License as published by the Free Software Foundation; either
5 * version 2.1 of the License, or (at your option) any later version.
6 *
7 * This library is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10 * Lesser General Public License for more details.
11 *
12 * You should have received a copy of the GNU Lesser General Public
13 * License along with this library; if not, write to the Free Software
14 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
15 *
16 * network.c
17 * Network code for libartnet
18 * Copyright (C) 2004-2007 Simon Newton
19 *
20 */
21
22 #include <errno.h>
23
24 #ifndef WIN32
25 #include <sys/socket.h> // socket before net/if.h for mac
26 #include <net/if.h>
27 #include <sys/ioctl.h>
28 #else
29 typedef int socklen_t;
30 #include <winsock2.h>
31 #include <Lm.h>
32 #include <iphlpapi.h>
33 #endif
34
35 #include <unistd.h>
36
37 #include "private.h"
38
39 #ifdef HAVE_GETIFADDRS
40 #ifdef HAVE_LINUX_IF_PACKET_H
41 #define USE_GETIFADDRS
42 #endif
43 #endif
44
45 #ifdef USE_GETIFADDRS
46 #include <ifaddrs.h>
47 #include <linux/types.h> // required by if_packet
48 #include <linux/if_packet.h>
49 #endif
50
51
52 enum { INITIAL_IFACE_COUNT = 10 };
53 enum { IFACE_COUNT_INC = 5 };
54 enum { IFNAME_SIZE = 32 }; // 32 sounds a reasonable size
55
56 typedef struct iface_s {
57 struct sockaddr_in ip_addr;
58 struct sockaddr_in bcast_addr;
59 int8_t hw_addr[ARTNET_MAC_SIZE];
60 char if_name[IFNAME_SIZE];
61 struct iface_s *next;
62 } iface_t;
63
64 unsigned long LOOPBACK_IP = 0x7F000001;
65
66
67 /*
68 * Free memory used by the iface's list
69 * @param head a pointer to the head of the list
70 */
71 static void free_ifaces(iface_t *head) {
72 iface_t *ift, *ift_next;
73
74 for (ift = head; ift != NULL; ift = ift_next) {
75 ift_next = ift->next;
76 free(ift);
77 }
78 }
79
80
81 /*
82 * Add a new interface to an interface list
83 * @param head pointer to the head of the list
84 * @param tail pointer to the end of the list
85 * @return a new iface_t or void
86 */
87 static iface_t *new_iface(iface_t **head, iface_t **tail) {
88 iface_t *iface = (iface_t*) calloc(1, sizeof(iface_t));
89
90 if (!iface) {
91 artnet_error("%s: calloc error %s" , __FUNCTION__, strerror(errno));
92 return NULL;
93 }
94 memset(iface, 0, sizeof(iface_t));
95
96 if (!*head) {
97 *head = *tail = iface;
98 } else {
99 (*tail)->next = iface;
100 *tail = iface;
101 }
102 return iface;
103 }
104
105
106 #ifdef WIN32
107
108 /*
109 * Set if_head to point to a list of iface_t structures which represent the
110 * interfaces on this machine
111 * @param ift_head the address of the pointer to the head of the list
112 */
113 static int get_ifaces(iface_t **if_head) {
114 iface_t *if_tail, *iface;
115 PIP_ADAPTER_INFO pAdapter = NULL;
116 PIP_ADAPTER_INFO pAdapterInfo;
117 IP_ADDR_STRING *ipAddress;
118 ULONG ulOutBufLen = sizeof(IP_ADAPTER_INFO);
119 unsigned long net, mask;
120 if_tail = NULL;
121
122 while (1) {
123 pAdapterInfo = (IP_ADAPTER_INFO*) malloc(ulOutBufLen);
124 if (!pAdapterInfo) {
125 artnet_error("Error allocating memory needed for GetAdaptersinfo");
126 return ARTNET_EMEM;
127 }
128
129 DWORD status = GetAdaptersInfo(pAdapterInfo, &ulOutBufLen);
130 if (status == NO_ERROR)
131 break;
132
133 free(pAdapterInfo);
134 if (status != ERROR_BUFFER_OVERFLOW) {
135 printf("GetAdaptersInfo failed with error: %d\n", (int) status);
136 return ARTNET_ENET;
137 }
138 }
139
140 for (pAdapter = pAdapterInfo;
141 pAdapter && pAdapter < pAdapterInfo + ulOutBufLen;
142 pAdapter = pAdapter->Next) {
143
144 if(pAdapter->Type != MIB_IF_TYPE_ETHERNET && pAdapter->Type != IF_TYPE_IEEE80211)
145 continue;
146
147 for (ipAddress = &pAdapter->IpAddressList; ipAddress;
148 ipAddress = ipAddress->Next) {
149
150 net = inet_addr(ipAddress->IpAddress.String);
151 if (net) {
152 // Windows doesn't seem to have the notion of an interface being 'up'
153 // so we check if this interface has an address assigned.
154 iface = new_iface(if_head, &if_tail);
155 if (!iface)
156 continue;
157
158 mask = inet_addr(ipAddress->IpMask.String);
159 strncpy(iface->if_name, pAdapter->AdapterName, IFNAME_SIZE);
160 memcpy(iface->hw_addr, pAdapter->Address, ARTNET_MAC_SIZE);
161 iface->ip_addr.sin_addr.s_addr = net;
162 iface->bcast_addr.sin_addr.s_addr = (
163 (net & mask) | (0xFFFFFFFF ^ mask));
164 }
165 }
166 }
167
168 free(pAdapterInfo);
169 return (ARTNET_EOK);
170 }
171
172 # else // not WIN32
173
174 #ifdef USE_GETIFADDRS
175
176 /*
177 * Check if we are interested in this interface
178 * @param ifa a pointer to a ifaddr struct
179 */
180 static void add_iface_if_needed(iface_t **head, iface_t **tail,
181 struct ifaddrs *ifa) {
182
183 // skip down, loopback and non inet interfaces
184 if (!ifa || !ifa->ifa_addr) return;
185 if (!(ifa->ifa_flags & IFF_UP)) return;
186 if (ifa->ifa_flags & IFF_LOOPBACK) return;
187 if (ifa->ifa_addr->sa_family != AF_INET) return;
188
189 iface_t *iface = new_iface(head, tail);
190 struct sockaddr_in *sin = (struct sockaddr_in*) ifa->ifa_addr;
191 iface->ip_addr.sin_addr = sin->sin_addr;
192 strncpy(iface->if_name, ifa->ifa_name, IFNAME_SIZE - 1);
193
194 if (ifa->ifa_flags & IFF_BROADCAST) {
195 sin = (struct sockaddr_in *) ifa->ifa_broadaddr;
196 iface->bcast_addr.sin_addr = sin->sin_addr;
197 }
198 }
199
200
201 /*
202 * Set if_head to point to a list of iface_t structures which represent the
203 * interfaces on this machine
204 * @param ift_head the address of the pointer to the head of the list
205 */
206 static int get_ifaces(iface_t **if_head) {
207 struct ifaddrs *ifa_list, *ifa_iter;
208 iface_t *if_tail, *if_iter;
209 struct sockaddr_ll *sll;
210 char *if_name, *cptr;
211 *if_head = if_tail = NULL;
212
213 if (getifaddrs(&ifa_list) != 0) {
214 artnet_error("Error getting interfaces: %s", strerror(errno));
215 return ARTNET_ENET;
216 }
217
218 for (ifa_iter = ifa_list; ifa_iter; ifa_iter = ifa_iter->ifa_next)
219 add_iface_if_needed(if_head, &if_tail, ifa_iter);
220
221 // Match up the interfaces with the corrosponding AF_PACKET interface
222 // to fetch the hw addresses
223 //
224 // TODO: Will probably not work on OS X, it should
225 // return AF_LINK -type sockaddr
226 for (if_iter = *if_head; if_iter; if_iter = if_iter->next) {
227 if_name = strdup(if_iter->if_name);
228
229 // if this is an alias, get mac of real interface
230 if ((cptr = strchr(if_name, ':')))
231 *cptr = 0;
232
233 // Find corresponding iface_t structure
234 for (ifa_iter = ifa_list; ifa_iter; ifa_iter = ifa_iter->ifa_next) {
235 if ((!ifa_iter->ifa_addr) || ifa_iter->ifa_addr->sa_family != AF_PACKET)
236 continue;
237
238 if (strncmp(if_name, ifa_iter->ifa_name, IFNAME_SIZE) == 0) {
239 // Found matching hw-address
240 sll = (struct sockaddr_ll*) ifa_iter->ifa_addr;
241 memcpy(if_iter->hw_addr, sll->sll_addr, ARTNET_MAC_SIZE);
242 break;
243 }
244 }
245 free(if_name);
246 }
247 freeifaddrs(ifa_list);
248 return 0;
249 }
250
251 #else // no GETIFADDRS
252
253 /*
254 * Set if_head to point to a list of iface_t structures which represent the
255 * interfaces on this machine
256 * @param ift_head the address of the pointer to the head of the list
257 */
258 static int get_ifaces(iface_t **if_head) {
259 struct ifconf ifc;
260 struct ifreq *ifr, ifrcopy;
261 struct sockaddr_in *sin;
262 int len, lastlen, flags;
263 char *buf, *ptr;
264 iface_t *if_tail, *iface;
265 int ret = ARTNET_EOK;
266 int sd;
267
268 *if_head = if_tail = NULL;
269
270 // create socket to get iface config
271 sd = socket(PF_INET, SOCK_DGRAM, 0);
272
273 if (sd < 0) {
274 artnet_error("%s : Could not create socket %s", __FUNCTION__, strerror(errno));
275 ret = ARTNET_ENET;
276 goto e_return;
277 }
278
279 // first use ioctl to get a listing of interfaces
280 lastlen = 0;
281 len = INITIAL_IFACE_COUNT * sizeof(struct ifreq);
282
283 for (;;) {
284 buf = malloc(len);
285
286 if (buf == NULL) {
287 artnet_error_malloc();
288 ret = ARTNET_EMEM;
289 goto e_free;
290 }
291
292 ifc.ifc_len = len;
293 ifc.ifc_buf = buf;
294 if (ioctl(sd, SIOCGIFCONF, &ifc) < 0) {
295 if (errno != EINVAL || lastlen != 0) {
296 artnet_error("%s : ioctl error %s", __FUNCTION__, strerror(errno));
297 ret = ARTNET_ENET;
298 goto e_free;
299 }
300 } else {
301 if (ifc.ifc_len == lastlen)
302 break;
303 lastlen = ifc.ifc_len;
304 }
305 len += IFACE_COUNT_INC * sizeof(struct ifreq);
306 free(buf);
307 }
308
309 // loop through each iface
310 for (ptr = buf; ptr < buf + ifc.ifc_len;) {
311 ifr = (struct ifreq*) ptr;
312
313 // work out length here
314 #ifdef HAVE_SOCKADDR_SA_LEN
315 len = max(sizeof(struct sockaddr), ifr->ifr_addr.sa_len);
316 #else
317 switch (ifr->ifr_addr.sa_family) {
318 #ifdef IPV6
319 case AF_INET6:
320 len = sizeof(struct sockaddr_in6);
321 break;
322 #endif
323 case AF_INET:
324 default:
325 len = sizeof(SA);
326 break;
327 }
328 #endif
329
330 ptr += sizeof(ifr->ifr_name) + len;
331
332 // look for AF_INET interfaces
333 if (ifr->ifr_addr.sa_family == AF_INET) {
334 ifrcopy = *ifr;
335 if (ioctl(sd, SIOCGIFFLAGS, &ifrcopy) < 0) {
336 artnet_error("%s : ioctl error %s" , __FUNCTION__, strerror(errno));
337 ret = ARTNET_ENET;
338 goto e_free_list;
339 }
340
341 flags = ifrcopy.ifr_flags;
342 if ((flags & IFF_UP) == 0)
343 continue; //skip down interfaces
344
345 if ((flags & IFF_LOOPBACK))
346 continue; //skip lookback
347
348 iface = new_iface(if_head, &if_tail);
349 if (!iface)
350 goto e_free_list;
351
352 sin = (struct sockaddr_in *) &ifr->ifr_addr;
353 iface->ip_addr.sin_addr = sin->sin_addr;
354
355 // fetch bcast address
356 #ifdef SIOCGIFBRDADDR
357 if (flags & IFF_BROADCAST) {
358 if (ioctl(sd, SIOCGIFBRDADDR, &ifrcopy) < 0) {
359 artnet_error("%s : ioctl error %s" , __FUNCTION__, strerror(errno));
360 ret = ARTNET_ENET;
361 goto e_free_list;
362 }
363
364 sin = (struct sockaddr_in *) &ifrcopy.ifr_broadaddr;
365 iface->bcast_addr.sin_addr = sin->sin_addr;
366 }
367 #endif
368 // fetch hardware address
369 #ifdef SIOCGIFHWADDR
370 if (flags & SIOCGIFHWADDR) {
371 if (ioctl(sd, SIOCGIFHWADDR, &ifrcopy) < 0) {
372 artnet_error("%s : ioctl error %s", __FUNCTION__, strerror(errno));
373 ret = ARTNET_ENET;
374 goto e_free_list;
375 }
376 memcpy(&iface->hw_addr, ifrcopy.ifr_hwaddr.sa_data, ARTNET_MAC_SIZE);
377 }
378 #endif
379
380 /* ok, if that all failed we should prob try and use sysctl to work out the bcast
381 * and hware addresses
382 * i'll leave that for another day
383 */
384 }
385 }
386 free(buf);
387 close(sd);
388 return ARTNET_EOK;
389
390 e_free_list:
391 free_ifaces(*if_head);
392 e_free:
393 free(buf);
394 close(sd);
395 e_return:
396 return ret;
397 }
398
399 #endif // GETIFADDRS
400 #endif // not WIN32
401
402
403 /*
404 * Scan for interfaces, and work out which one the user wanted to use.
405 */
406 int artnet_net_init(node n, const char *preferred_ip) {
407 iface_t *ift, *ift_head = NULL;
408 struct in_addr wanted_ip;
409
410 int found = FALSE;
411 int i;
412 int ret = ARTNET_EOK;
413
414 if ((ret = get_ifaces(&ift_head)))
415 goto e_return;
416
417 if (n->state.verbose) {
418 printf("#### INTERFACES FOUND ####\n");
419 for (ift = ift_head; ift != NULL; ift = ift->next) {
420 printf("IP: %s\n", inet_ntoa(ift->ip_addr.sin_addr));
421 printf(" bcast: %s\n" , inet_ntoa(ift->bcast_addr.sin_addr));
422 printf(" hwaddr: ");
423 for (i = 0; i < ARTNET_MAC_SIZE; i++) {
424 if (i)
425 printf(":");
426 printf("%02x", (uint8_t) ift->hw_addr[i]);
427 }
428 printf("\n");
429 }
430 printf("#########################\n");
431 }
432
433 if (preferred_ip) {
434 // search through list of interfaces for one with the correct address
435 ret = artnet_net_inet_aton(preferred_ip, &wanted_ip);
436 if (ret)
437 goto e_cleanup;
438
439 for (ift = ift_head; ift != NULL; ift = ift->next) {
440 if (ift->ip_addr.sin_addr.s_addr == wanted_ip.s_addr) {
441 found = TRUE;
442 n->state.ip_addr = ift->ip_addr.sin_addr;
443 n->state.bcast_addr = ift->bcast_addr.sin_addr;
444 memcpy(&n->state.hw_addr, &ift->hw_addr, ARTNET_MAC_SIZE);
445 break;
446 }
447 }
448 if (!found) {
449 artnet_error("Cannot find ip %s", preferred_ip);
450 ret = ARTNET_ENET;
451 goto e_cleanup;
452 }
453 } else {
454 if (ift_head) {
455 // pick first address
456 // copy ip address, bcast address and hardware address
457 n->state.ip_addr = ift_head->ip_addr.sin_addr;
458 n->state.bcast_addr = ift_head->bcast_addr.sin_addr;
459 memcpy(&n->state.hw_addr, &ift_head->hw_addr, ARTNET_MAC_SIZE);
460 } else {
461 artnet_error("No interfaces found!");
462 ret = ARTNET_ENET;
463 }
464 }
465
466 e_cleanup:
467 free_ifaces(ift_head);
468 e_return :
469 return ret;
470 }
471
472
473 /*
474 * Start listening on the socket
475 */
476 int artnet_net_start(node n) {
477 int sock;
478 struct sockaddr_in servAddr;
479 int true_flag = TRUE;
480 node tmp;
481
482 // only attempt to bind if we are the group master
483 if (n->peering.master == TRUE) {
484
485 #ifdef WIN32
486 // check winsock version
487 WSADATA wsaData;
488 WORD wVersionRequested = MAKEWORD(2, 2);
489 if (WSAStartup(wVersionRequested, &wsaData) != 0)
490 return (-1);
491 if (wsaData.wVersion != wVersionRequested)
492 return (-2);
493 #endif
494
495 // create socket
496 sock = socket(PF_INET, SOCK_DGRAM, 0);
497
498 if (sock < 0) {
499 artnet_error("Could not create socket %s", artnet_net_last_error());
500 return ARTNET_ENET;
501 }
502
503 memset(&servAddr, 0x00, sizeof(servAddr));
504 servAddr.sin_family = AF_INET;
505 servAddr.sin_port = htons(ARTNET_PORT);
506 servAddr.sin_addr.s_addr = htonl(INADDR_ANY);
507
508 if (n->state.verbose)
509 printf("Binding to %s \n", inet_ntoa(servAddr.sin_addr));
510
511 // allow bcasting
512 if (setsockopt(sock,
513 SOL_SOCKET,
514 SO_BROADCAST,
515 (char*) &true_flag, // char* for win32
516 sizeof(int)) == -1) {
517 artnet_error("Failed to bind to socket %s", artnet_net_last_error());
518 artnet_net_close(sock);
519 return ARTNET_ENET;
520 }
521
522 #ifdef WIN32
523 // ### LH - 22.08.2008
524 // make it possible to reuse port, if SO_REUSEADDR
525 // exists on operating system
526
527 // NEVER USE SO_EXCLUSIVEADDRUSE, as that freezes the application
528 // on WinXP, if port is in use !
529 if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (char *) &true_flag,
530 sizeof(true_flag)) < 0) {
531
532 artnet_error("Set reuse failed", artnet_net_last_error());
533 artnet_net_close(sock);
534 return ARTNET_ENET;
535 }
536
537 u_long true = 1;
538 if (SOCKET_ERROR == ioctlsocket(sock, FIONBIO, &true)) {
539
540 artnet_error("ioctlsocket", artnet_net_last_error());
541 artnet_net_close(sock);
542 return ARTNET_ENET;
543 }
544 #else
545 // allow reusing 6454 port _
546 if (setsockopt(sock,
547 SOL_SOCKET,
548 SO_REUSEPORT,
549 (char*) &true_flag, // char* for win32
550 sizeof(int)) == -1) {
551 artnet_error("Failed to bind to socket %s", artnet_net_last_error());
552 artnet_net_close(sock);
553 return ARTNET_ENET;
554 }
555 #endif
556
557 if (n->state.verbose)
558 printf("Binding to %s \n", inet_ntoa(servAddr.sin_addr));
559
560 // bind sockets
561 if (bind(sock, (SA *) &servAddr, sizeof(servAddr)) == -1) {
562 artnet_error("Failed to bind to socket %s", artnet_net_last_error());
563 artnet_net_close(sock);
564 return ARTNET_ENET;
565 }
566
567
568 n->sd = sock;
569 // Propagate the socket to all our peers
570 for (tmp = n->peering.peer; tmp && tmp != n; tmp = tmp->peering.peer)
571 tmp->sd = sock;
572 }
573 return ARTNET_EOK;
574 }
575
576
577 /*
578 * Receive a packet.
579 */
580 int artnet_net_recv(node n, artnet_packet p, int delay) {
581 ssize_t len;
582 struct sockaddr_in cliAddr;
583 socklen_t cliLen = sizeof(cliAddr);
584 fd_set rset;
585 struct timeval tv;
586 int maxfdp1 = n->sd + 1;
587
588 FD_ZERO(&rset);
589 FD_SET((unsigned int) n->sd, &rset);
590
591 tv.tv_usec = 0;
592 tv.tv_sec = delay;
593 p->length = 0;
594
595 switch (select(maxfdp1, &rset, NULL, NULL, &tv)) {
596 case 0:
597 // timeout
598 return RECV_NO_DATA;
599 break;
600 case -1:
601 if ( errno != EINTR) {
602 artnet_error("Select error %s", artnet_net_last_error());
603 return ARTNET_ENET;
604 }
605 return ARTNET_EOK;
606 break;
607 default:
608 break;
609 }
610
611 // need a check here for the amount of data read
612 // should prob allow an extra byte after data, and pass the size as sizeof(Data) +1
613 // then check the size read and if equal to size(data)+1 we have an error
614 len = recvfrom(n->sd,
615 (char*) &(p->data), // char* for win32
616 sizeof(p->data),
617 0,
618 (SA*) &cliAddr,
619 &cliLen);
620 if (len < 0) {
621 artnet_error("Recvfrom error %s", artnet_net_last_error());
622 return ARTNET_ENET;
623 }
624
625 if (cliAddr.sin_addr.s_addr == n->state.ip_addr.s_addr ||
626 ntohl(cliAddr.sin_addr.s_addr) == LOOPBACK_IP) {
627 p->length = 0;
628 return ARTNET_EOK;
629 }
630
631 p->length = len;
632 memcpy(&(p->from), &cliAddr.sin_addr, sizeof(struct in_addr));
633 // should set to in here if we need it
634 return ARTNET_EOK;
635 }
636
637
638 /*
639 * Send a packet.
640 */
641 int artnet_net_send(node n, artnet_packet p) {
642 struct sockaddr_in addr;
643 int ret;
644
645 if (n->state.mode != ARTNET_ON)
646 return ARTNET_EACTION;
647
648 addr.sin_family = AF_INET;
649 addr.sin_port = htons(ARTNET_PORT);
650 addr.sin_addr = p->to;
651 p->from = n->state.ip_addr;
652
653 if (n->state.verbose)
654 printf("sending to %s\n" , inet_ntoa(addr.sin_addr));
655
656 ret = sendto(n->sd,
657 (char*) &p->data, // char* required for win32
658 p->length,
659 0,
660 (SA*) &addr,
661 sizeof(addr));
662 if (ret == -1) {
663 artnet_error("Sendto failed: %s", artnet_net_last_error());
664 n->state.report_code = ARTNET_RCUDPFAIL;
665 return ARTNET_ENET;
666
667 } else if (p->length != ret) {
668 artnet_error("failed to send full datagram");
669 n->state.report_code = ARTNET_RCSOCKETWR1;
670 return ARTNET_ENET;
671 }
672
673 if (n->callbacks.send.fh) {
674 get_type(p);
675 n->callbacks.send.fh(n, p, n->callbacks.send.data);
676 }
677 return ARTNET_EOK;
678 }
679
680
681 /*
682 int artnet_net_reprogram(node n) {
683 iface_t *ift_head, *ift;
684 int i;
685
686 ift_head = get_ifaces(n->sd[0]);
687
688 for (ift = ift_head;ift != NULL; ift = ift->next ) {
689 printf("IP: %s\n", inet_ntoa(ift->ip_addr.sin_addr) );
690 printf(" bcast: %s\n" , inet_ntoa(ift->bcast_addr.sin_addr) );
691 printf(" hwaddr: ");
692 for(i = 0; i < 6; i++ ) {
693 printf("%hhx:", ift->hw_addr[i] );
694 }
695 printf("\n");
696 }
697
698 free_ifaces(ift_head);
699
700 }*/
701
702
703 int artnet_net_set_fdset(node n, fd_set *fdset) {
704 FD_SET((unsigned int) n->sd, fdset);
705 return ARTNET_EOK;
706 }
707
708
709 /*
710 * Close a socket
711 */
712 int artnet_net_close(int sock) {
713 #ifdef WIN32
714 shutdown(sock, SD_BOTH);
715 closesocket(sock);
716 //WSACancelBlockingCall();
717 WSACleanup();
718 #else
719 if (close(sock)) {
720 artnet_error(artnet_net_last_error());
721 return ARTNET_ENET;
722 }
723 #endif
724 return ARTNET_EOK;
725 }
726
727
728 /*
729 * Convert a string to an in_addr
730 */
731 int artnet_net_inet_aton(const char *ip_address, struct in_addr *address) {
732 #ifdef HAVE_INET_ATON
733 if (!inet_aton(ip_address, address)) {
734 #else
735 in_addr_t *addr = (in_addr_t*) address;
736 if ((*addr = inet_addr(ip_address)) == INADDR_NONE &&
737 strcmp(ip_address, "255.255.255.255")) {
738 #endif
739 artnet_error("IP conversion from %s failed", ip_address);
740 return ARTNET_EARG;
741 }
742 return ARTNET_EOK;
743 }
744
745
746 /*
747 *
748 */
749 const char *artnet_net_last_error() {
750 #ifdef WIN32
751 static char error_str[10];
752 int error = WSAGetLastError();
753 snprintf(error_str, sizeof(error_str), "%d", error);
754 return error_str;
755 #else
756 return strerror(errno);
757 #endif
758 }
759