| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
#include "config.h" |
| 18 |
|
| 19 |
#include <assert.h> |
| 20 |
#include <stdio.h> |
| 21 |
#include <stdlib.h> |
| 22 |
#include <string.h> |
| 23 |
#include <sys/types.h> |
| 24 |
|
| 25 |
#if defined(_WIN32) || defined(_MSC_VER) |
| 26 |
#include <io.h> |
| 27 |
#define snprintf _snprintf |
| 28 |
#include <winsock2.h> |
| 29 |
#include <ws2tcpip.h> |
| 30 |
#include <iphlpapi.h> |
| 31 |
#else |
| 32 |
#include <unistd.h> |
| 33 |
#include <netdb.h> |
| 34 |
#include <sys/socket.h> |
| 35 |
#include <sys/un.h> |
| 36 |
#include <arpa/inet.h> |
| 37 |
#include <netinet/tcp.h> |
| 38 |
#ifdef HAVE_GETIFADDRS |
| 39 |
#include <ifaddrs.h> |
| 40 |
#endif |
| 41 |
#endif |
| 42 |
|
| 43 |
#include "lo_types_internal.h" |
| 44 |
#include "lo_internal.h" |
| 45 |
#include "lo.h" |
| 46 |
#include "lo_throw.h" |
| 47 |
|
| 48 |
static void lo_address_set_flags(lo_address t, int flags); |
| 49 |
|
| 50 |
lo_address lo_address_new_with_proto(int proto, const char *host, |
| 51 |
const char *port) |
| 52 |
{ |
| 53 |
lo_address a; |
| 54 |
|
| 55 |
if (proto != LO_UDP && proto != LO_TCP && proto != LO_UNIX) |
| 56 |
return NULL; |
| 57 |
|
| 58 |
a = (lo_address) calloc(1, sizeof(struct _lo_address)); |
| 59 |
if (a == NULL) |
| 60 |
return NULL; |
| 61 |
|
| 62 |
a->ai = NULL; |
| 63 |
a->ai_first = NULL; |
| 64 |
a->socket = -1; |
| 65 |
a->ownsocket = 1; |
| 66 |
a->protocol = proto; |
| 67 |
a->flags = (lo_proto_flags) 0; |
| 68 |
switch (proto) { |
| 69 |
default: |
| 70 |
case LO_UDP: |
| 71 |
case LO_TCP: |
| 72 |
if (host) { |
| 73 |
a->host = strdup(host); |
| 74 |
} else { |
| 75 |
a->host = strdup("localhost"); |
| 76 |
} |
| 77 |
break; |
| 78 |
case LO_UNIX: |
| 79 |
a->host = strdup("localhost"); |
| 80 |
break; |
| 81 |
} |
| 82 |
if (port) { |
| 83 |
a->port = strdup(port); |
| 84 |
} else { |
| 85 |
a->port = NULL; |
| 86 |
} |
| 87 |
|
| 88 |
a->ttl = -1; |
| 89 |
a->addr.size = 0; |
| 90 |
a->addr.iface = 0; |
| 91 |
a->source_server = 0; |
| 92 |
a->source_path = 0; |
| 93 |
|
| 94 |
return a; |
| 95 |
} |
| 96 |
|
| 97 |
lo_address lo_address_new(const char *host, const char *port) |
| 98 |
{ |
| 99 |
return lo_address_new_with_proto(LO_UDP, host, port); |
| 100 |
} |
| 101 |
|
| 102 |
lo_address lo_address_new_from_url(const char *url) |
| 103 |
{ |
| 104 |
lo_address a; |
| 105 |
int protocol; |
| 106 |
char *host, *port, *proto; |
| 107 |
|
| 108 |
if (!url || !*url) { |
| 109 |
return NULL; |
| 110 |
} |
| 111 |
|
| 112 |
protocol = lo_url_get_protocol_id(url); |
| 113 |
if (protocol == LO_UDP || protocol == LO_TCP) { |
| 114 |
host = lo_url_get_hostname(url); |
| 115 |
port = lo_url_get_port(url); |
| 116 |
a = lo_address_new_with_proto(protocol, host, port); |
| 117 |
if (host) |
| 118 |
free(host); |
| 119 |
if (port) |
| 120 |
free(port); |
| 121 |
#if !defined(_WIN32) && !defined(_MSC_VER) |
| 122 |
} else if (protocol == LO_UNIX) { |
| 123 |
port = lo_url_get_path(url); |
| 124 |
a = lo_address_new_with_proto(LO_UNIX, NULL, port); |
| 125 |
if (port) |
| 126 |
free(port); |
| 127 |
#endif |
| 128 |
} else { |
| 129 |
proto = lo_url_get_protocol(url); |
| 130 |
fprintf(stderr, |
| 131 |
PACKAGE_NAME ": protocol '%s' not supported by this " |
| 132 |
"version\n", proto); |
| 133 |
if (proto) |
| 134 |
free(proto); |
| 135 |
|
| 136 |
return NULL; |
| 137 |
} |
| 138 |
|
| 139 |
return a; |
| 140 |
} |
| 141 |
|
| 142 |
static void lo_address_resolve_source(lo_address a) |
| 143 |
{ |
| 144 |
char hostname[LO_HOST_SIZE]; |
| 145 |
char portname[32]; |
| 146 |
int err; |
| 147 |
lo_server s = a->source_server; |
| 148 |
|
| 149 |
if (a->protocol == LO_UDP && s && s->addr_len > 0) |
| 150 |
{ |
| 151 |
err = getnameinfo((struct sockaddr *) &s->addr, s->addr_len, |
| 152 |
hostname, sizeof(hostname), |
| 153 |
portname, sizeof(portname), |
| 154 |
NI_NUMERICHOST | NI_NUMERICSERV); |
| 155 |
if (err) { |
| 156 |
switch (err) { |
| 157 |
case EAI_AGAIN: |
| 158 |
lo_throw(s, err, "Try again", a->source_path); |
| 159 |
break; |
| 160 |
case EAI_BADFLAGS: |
| 161 |
lo_throw(s, err, "Bad flags", a->source_path); |
| 162 |
break; |
| 163 |
case EAI_FAIL: |
| 164 |
lo_throw(s, err, "Failed", a->source_path); |
| 165 |
break; |
| 166 |
case EAI_FAMILY: |
| 167 |
lo_throw(s, err, "Cannot resolve address family", |
| 168 |
a->source_path); |
| 169 |
break; |
| 170 |
case EAI_MEMORY: |
| 171 |
lo_throw(s, err, "Out of memory", a->source_path); |
| 172 |
break; |
| 173 |
case EAI_NONAME: |
| 174 |
lo_throw(s, err, "Cannot resolve", a->source_path); |
| 175 |
break; |
| 176 |
#if !defined(_WIN32) && !defined(_MSC_VER) |
| 177 |
case EAI_SYSTEM: |
| 178 |
lo_throw(s, err, strerror(err), a->source_path); |
| 179 |
break; |
| 180 |
#endif |
| 181 |
default: |
| 182 |
lo_throw(s, err, "Unknown error", a->source_path); |
| 183 |
break; |
| 184 |
} |
| 185 |
|
| 186 |
return; |
| 187 |
} |
| 188 |
|
| 189 |
a->host = strdup(hostname); |
| 190 |
a->port = strdup(portname); |
| 191 |
#if !defined(_WIN32) && !defined(_MSC_VER) |
| 192 |
} else if (a->protocol== LO_UNIX) { |
| 193 |
struct sockaddr_un * addr = (struct sockaddr_un *) &s->addr; |
| 194 |
a->host = strdup(""); |
| 195 |
a->port = strdup(addr->sun_path); |
| 196 |
#endif |
| 197 |
} else { |
| 198 |
a->host = strdup(""); |
| 199 |
a->port = strdup(""); |
| 200 |
} |
| 201 |
} |
| 202 |
|
| 203 |
const char *lo_address_get_hostname(lo_address a) |
| 204 |
{ |
| 205 |
if (!a) { |
| 206 |
return NULL; |
| 207 |
} |
| 208 |
|
| 209 |
if (!a->host) |
| 210 |
lo_address_resolve_source(a); |
| 211 |
|
| 212 |
return a->host; |
| 213 |
} |
| 214 |
|
| 215 |
int lo_address_get_protocol(lo_address a) |
| 216 |
{ |
| 217 |
if (!a) { |
| 218 |
return -1; |
| 219 |
} |
| 220 |
|
| 221 |
return a->protocol; |
| 222 |
} |
| 223 |
|
| 224 |
const char *lo_address_get_port(lo_address a) |
| 225 |
{ |
| 226 |
if (!a) { |
| 227 |
return NULL; |
| 228 |
} |
| 229 |
|
| 230 |
if (!a->host) |
| 231 |
lo_address_resolve_source(a); |
| 232 |
|
| 233 |
return a->port; |
| 234 |
} |
| 235 |
|
| 236 |
static const char *get_protocol_name(int proto) |
| 237 |
{ |
| 238 |
switch (proto) { |
| 239 |
case LO_UDP: |
| 240 |
return "udp"; |
| 241 |
case LO_TCP: |
| 242 |
return "tcp"; |
| 243 |
#if !defined(_WIN32) && !defined(_MSC_VER) |
| 244 |
case LO_UNIX: |
| 245 |
return "unix"; |
| 246 |
#endif |
| 247 |
} |
| 248 |
return NULL; |
| 249 |
} |
| 250 |
|
| 251 |
|
| 252 |
char *lo_address_get_url(lo_address a) |
| 253 |
{ |
| 254 |
char *buf; |
| 255 |
int ret = 0; |
| 256 |
int needquote = 0; |
| 257 |
const char *fmt; |
| 258 |
|
| 259 |
if (!a->host) |
| 260 |
lo_address_resolve_source(a); |
| 261 |
|
| 262 |
if (!a->host) |
| 263 |
return NULL; |
| 264 |
|
| 265 |
needquote = strchr(a->host, ':') ? 1 : 0; |
| 266 |
|
| 267 |
if (needquote) { |
| 268 |
fmt = "osc.%s://[%s]:%s/"; |
| 269 |
} else { |
| 270 |
fmt = "osc.%s://%s:%s/"; |
| 271 |
} |
| 272 |
#ifndef _MSC_VER |
| 273 |
ret = snprintf(NULL, 0, fmt, |
| 274 |
get_protocol_name(a->protocol), a->host, a->port); |
| 275 |
#endif |
| 276 |
if (ret <= 0) { |
| 277 |
|
| 278 |
ret = 1023; |
| 279 |
} |
| 280 |
buf = (char*) malloc((ret + 2) * sizeof(char)); |
| 281 |
snprintf(buf, ret + 1, fmt, |
| 282 |
get_protocol_name(a->protocol), a->host, a->port); |
| 283 |
|
| 284 |
if (a->protocol==LO_UNIX) { |
| 285 |
buf[ret-1] = 0; |
| 286 |
} |
| 287 |
|
| 288 |
return buf; |
| 289 |
} |
| 290 |
|
| 291 |
void lo_address_free(lo_address a) |
| 292 |
{ |
| 293 |
if (a) { |
| 294 |
if (a->socket != -1 && a->ownsocket) { |
| 295 |
#ifdef SHUT_WR |
| 296 |
shutdown(a->socket, SHUT_WR); |
| 297 |
#endif |
| 298 |
closesocket(a->socket); |
| 299 |
} |
| 300 |
lo_address_free_mem(a); |
| 301 |
free(a); |
| 302 |
} |
| 303 |
} |
| 304 |
|
| 305 |
void lo_address_free_mem(lo_address a) |
| 306 |
{ |
| 307 |
if (a) { |
| 308 |
if (a->host) |
| 309 |
free(a->host); |
| 310 |
if (a->port) |
| 311 |
free(a->port); |
| 312 |
if (a->ai_first) |
| 313 |
freeaddrinfo(a->ai_first); |
| 314 |
if (a->addr.iface) |
| 315 |
free(a->addr.iface); |
| 316 |
|
| 317 |
memset(a, 0, sizeof(struct _lo_address)); |
| 318 |
a->socket = -1; |
| 319 |
} |
| 320 |
} |
| 321 |
|
| 322 |
int lo_address_errno(lo_address a) |
| 323 |
{ |
| 324 |
return a->errnum; |
| 325 |
} |
| 326 |
|
| 327 |
const char *lo_address_errstr(lo_address a) |
| 328 |
{ |
| 329 |
char *msg; |
| 330 |
|
| 331 |
if (a->errstr) { |
| 332 |
return a->errstr; |
| 333 |
} |
| 334 |
|
| 335 |
if (a->errnum == 0) |
| 336 |
return "Success"; |
| 337 |
|
| 338 |
msg = strerror(a->errnum); |
| 339 |
if (msg) { |
| 340 |
return msg; |
| 341 |
} else { |
| 342 |
return "unknown error"; |
| 343 |
} |
| 344 |
|
| 345 |
return "unknown error"; |
| 346 |
} |
| 347 |
|
| 348 |
char *lo_url_get_protocol(const char *url) |
| 349 |
{ |
| 350 |
char *protocol, *ret; |
| 351 |
|
| 352 |
if (!url) { |
| 353 |
return NULL; |
| 354 |
} |
| 355 |
|
| 356 |
protocol = (char*) malloc(strlen(url)); |
| 357 |
|
| 358 |
if (sscanf(url, "osc://%s", protocol)) { |
| 359 |
fprintf(stderr, |
| 360 |
PACKAGE_NAME " warning: no protocol specified in URL, " |
| 361 |
"assuming UDP.\n"); |
| 362 |
ret = strdup("udp"); |
| 363 |
} else if (sscanf(url, "osc.%[^:/[]", protocol)) { |
| 364 |
ret = strdup(protocol); |
| 365 |
} else { |
| 366 |
ret = NULL; |
| 367 |
} |
| 368 |
|
| 369 |
free(protocol); |
| 370 |
|
| 371 |
return ret; |
| 372 |
} |
| 373 |
|
| 374 |
int lo_url_get_protocol_id(const char *url) |
| 375 |
{ |
| 376 |
if (!url) { |
| 377 |
return -1; |
| 378 |
} |
| 379 |
|
| 380 |
if (!strncmp(url, "osc:", 4)) { |
| 381 |
fprintf(stderr, |
| 382 |
PACKAGE_NAME " warning: no protocol specified in URL, " |
| 383 |
"assuming UDP.\n"); |
| 384 |
return LO_UDP; |
| 385 |
} else if (!strncmp(url, "osc.udp:", 8)) { |
| 386 |
return LO_UDP; |
| 387 |
} else if (!strncmp(url, "osc.tcp:", 8)) { |
| 388 |
return LO_TCP; |
| 389 |
} else if (!strncmp(url, "osc.unix:", 9)) { |
| 390 |
return LO_UNIX; |
| 391 |
} |
| 392 |
return -1; |
| 393 |
} |
| 394 |
|
| 395 |
char *lo_url_get_hostname(const char *url) |
| 396 |
{ |
| 397 |
char *hostname = (char*) malloc(strlen(url)); |
| 398 |
|
| 399 |
if (sscanf(url, "osc://%[^[:/]", hostname)) { |
| 400 |
return hostname; |
| 401 |
} |
| 402 |
if (sscanf(url, "osc.%*[^:/]://[%[^]/]]", hostname)) { |
| 403 |
return hostname; |
| 404 |
} |
| 405 |
if (sscanf(url, "osc.%*[^:/]://%[^[:/]", hostname)) { |
| 406 |
return hostname; |
| 407 |
} |
| 408 |
|
| 409 |
|
| 410 |
free(hostname); |
| 411 |
|
| 412 |
return NULL; |
| 413 |
} |
| 414 |
|
| 415 |
char *lo_url_get_port(const char *url) |
| 416 |
{ |
| 417 |
char *port = (char*) malloc(strlen(url)); |
| 418 |
|
| 419 |
if (sscanf(url, "osc://%*[^:]:%[0-9]", port) > 0) { |
| 420 |
return port; |
| 421 |
} |
| 422 |
if (sscanf(url, "osc.%*[^:]://%*[^:]:%[0-9]", port) > 0) { |
| 423 |
return port; |
| 424 |
} |
| 425 |
if (sscanf(url, "osc://[%*[^]]]:%[0-9]", port) > 0) { |
| 426 |
return port; |
| 427 |
} |
| 428 |
if (sscanf(url, "osc.%*[^:]://[%*[^]]]:%[0-9]", port) > 0) { |
| 429 |
return port; |
| 430 |
} |
| 431 |
if (sscanf(url, "osc://:%[0-9]", port) > 0) { |
| 432 |
return port; |
| 433 |
} |
| 434 |
if (sscanf(url, "osc.%*[^:]://:%[0-9]", port) > 0) { |
| 435 |
return port; |
| 436 |
} |
| 437 |
|
| 438 |
|
| 439 |
free(port); |
| 440 |
|
| 441 |
return NULL; |
| 442 |
} |
| 443 |
|
| 444 |
char *lo_url_get_path(const char *url) |
| 445 |
{ |
| 446 |
char *path = (char*) malloc(strlen(url)); |
| 447 |
|
| 448 |
if (sscanf(url, "osc://%*[^:]:%*[0-9]%s", path)) { |
| 449 |
return path; |
| 450 |
} |
| 451 |
if (sscanf(url, "osc.%*[^:]://%*[^:]:%*[0-9]%s", path) == 1) { |
| 452 |
return path; |
| 453 |
} |
| 454 |
if (sscanf(url, "osc.unix://%*[^/]%s", path)) { |
| 455 |
int i = (int) strlen(path)-1; |
| 456 |
if (path[i]=='/') |
| 457 |
path[i] = 0; |
| 458 |
return path; |
| 459 |
} |
| 460 |
if (sscanf(url, "osc.%*[^:]://%s", path)) { |
| 461 |
int i = (int) strlen(path)-1; |
| 462 |
if (path[i]=='/') |
| 463 |
path[i] = 0; |
| 464 |
return path; |
| 465 |
} |
| 466 |
|
| 467 |
|
| 468 |
free(path); |
| 469 |
|
| 470 |
return NULL; |
| 471 |
} |
| 472 |
|
| 473 |
void lo_address_set_ttl(lo_address t, int ttl) |
| 474 |
{ |
| 475 |
if (t->protocol == LO_UDP) |
| 476 |
t->ttl = ttl; |
| 477 |
} |
| 478 |
|
| 479 |
int lo_address_get_ttl(lo_address t) |
| 480 |
{ |
| 481 |
return t->ttl; |
| 482 |
} |
| 483 |
|
| 484 |
int lo_address_set_tcp_nodelay(lo_address t, int enable) |
| 485 |
{ |
| 486 |
int r = (t->flags & LO_NODELAY) != 0; |
| 487 |
lo_address_set_flags(t, enable |
| 488 |
? t->flags | LO_NODELAY |
| 489 |
: t->flags & ~LO_NODELAY); |
| 490 |
return r; |
| 491 |
} |
| 492 |
|
| 493 |
int lo_address_set_stream_slip(lo_address t, lo_slip_encoding encoding) |
| 494 |
{ |
| 495 |
int r = t->flags & LO_SLIP_DBL_END |
| 496 |
? LO_SLIP_DOUBLE |
| 497 |
: t->flags & LO_SLIP |
| 498 |
? LO_SLIP_SINGLE |
| 499 |
: LO_SLIP_NONE; |
| 500 |
lo_address_set_flags(t, encoding > 0 |
| 501 |
? t->flags | LO_SLIP |
| 502 |
: t->flags & ~LO_SLIP); |
| 503 |
lo_address_set_flags(t, encoding > 1 |
| 504 |
? t->flags | LO_SLIP_DBL_END |
| 505 |
: t->flags & ~LO_SLIP_DBL_END); |
| 506 |
return r; |
| 507 |
} |
| 508 |
|
| 509 |
static |
| 510 |
void lo_address_set_flags(lo_address t, int flags) |
| 511 |
{ |
| 512 |
if (((t->flags & LO_NODELAY) != (flags & LO_NODELAY)) |
| 513 |
&& t->socket > 0) |
| 514 |
{ |
| 515 |
int option = (t->flags & LO_NODELAY)!=0; |
| 516 |
setsockopt(t->socket, IPPROTO_TCP, TCP_NODELAY, |
| 517 |
(const char*)&option, sizeof(option)); |
| 518 |
} |
| 519 |
|
| 520 |
t->flags = (lo_proto_flags) flags; |
| 521 |
} |
| 522 |
|
| 523 |
#ifdef ENABLE_IPV6 |
| 524 |
static int is_dotted_ipv4_address (const char* address) |
| 525 |
{ |
| 526 |
int a[4]; |
| 527 |
return sscanf(address, "%u.%u.%u.%u", &a[0], &a[1], &a[2], &a[3]); |
| 528 |
} |
| 529 |
#endif |
| 530 |
|
| 531 |
void lo_address_copy(lo_address to, lo_address from) |
| 532 |
{ |
| 533 |
|
| 534 |
|
| 535 |
memset(to, 0, sizeof(struct _lo_address)); |
| 536 |
to->socket = from->socket; |
| 537 |
if (from->host) { |
| 538 |
free(to->host); |
| 539 |
to->host = strdup(from->host); |
| 540 |
} |
| 541 |
if (from->port) { |
| 542 |
free(to->port); |
| 543 |
to->port = strdup(from->port); |
| 544 |
} |
| 545 |
to->protocol = from->protocol; |
| 546 |
to->flags = from->flags; |
| 547 |
to->ttl = from->ttl; |
| 548 |
to->addr = from->addr; |
| 549 |
if (from->addr.iface) |
| 550 |
to->addr.iface = strdup(from->addr.iface); |
| 551 |
} |
| 552 |
|
| 553 |
void lo_address_init_with_sockaddr(lo_address a, |
| 554 |
void *sa, size_t sa_len, |
| 555 |
int sock, int prot) |
| 556 |
{ |
| 557 |
int err = 0; |
| 558 |
assert(a != NULL); |
| 559 |
lo_address_free_mem(a); |
| 560 |
a->host = (char*) malloc(INET_ADDRSTRLEN); |
| 561 |
a->port = (char*) malloc(8); |
| 562 |
|
| 563 |
err = getnameinfo((struct sockaddr *)sa, (socklen_t) sa_len, |
| 564 |
a->host, INET_ADDRSTRLEN, a->port, 8, |
| 565 |
NI_NUMERICHOST | NI_NUMERICSERV); |
| 566 |
|
| 567 |
if (err) { |
| 568 |
free(a->host); |
| 569 |
free(a->port); |
| 570 |
a->host = a->port = 0; |
| 571 |
} |
| 572 |
|
| 573 |
a->socket = sock; |
| 574 |
a->protocol = prot; |
| 575 |
} |
| 576 |
|
| 577 |
int lo_address_resolve(lo_address a) |
| 578 |
{ |
| 579 |
int ret; |
| 580 |
|
| 581 |
if (a->protocol == LO_UDP || a->protocol == LO_TCP) { |
| 582 |
struct addrinfo *ai=NULL; |
| 583 |
struct addrinfo hints; |
| 584 |
const char* host = lo_address_get_hostname(a); |
| 585 |
#ifdef ENABLE_IPV6 |
| 586 |
char hosttmp[7+16+1]; |
| 587 |
#endif |
| 588 |
|
| 589 |
memset(&hints, 0, sizeof(hints)); |
| 590 |
#ifdef ENABLE_IPV6 |
| 591 |
hints.ai_family = PF_UNSPEC; |
| 592 |
|
| 593 |
if (is_dotted_ipv4_address(host)) { |
| 594 |
host = hosttmp; |
| 595 |
strcpy(hosttmp, "::FFFF:"); |
| 596 |
strncpy(hosttmp + 7, lo_address_get_hostname(a), 16); |
| 597 |
} |
| 598 |
#else |
| 599 |
hints.ai_family = PF_INET; |
| 600 |
#endif |
| 601 |
hints.ai_socktype = |
| 602 |
a->protocol == LO_UDP ? SOCK_DGRAM : SOCK_STREAM; |
| 603 |
|
| 604 |
if ((ret = getaddrinfo(host, lo_address_get_port(a), &hints, &ai))) { |
| 605 |
a->errnum = ret; |
| 606 |
a->errstr = gai_strerror(ret); |
| 607 |
a->ai = NULL; |
| 608 |
a->ai_first = NULL; |
| 609 |
return -1; |
| 610 |
} |
| 611 |
|
| 612 |
a->ai = ai; |
| 613 |
a->ai_first = ai; |
| 614 |
} |
| 615 |
|
| 616 |
return 0; |
| 617 |
} |
| 618 |
|
| 619 |
#if defined(_WIN32) || defined(_MSC_VER) || defined(HAVE_GETIFADDRS) |
| 620 |
|
| 621 |
int lo_address_set_iface(lo_address t, const char *iface, const char *ip) |
| 622 |
{ |
| 623 |
int fam; |
| 624 |
if (!t->ai) { |
| 625 |
lo_address_resolve(t); |
| 626 |
if (!t->ai) |
| 627 |
return 2; |
| 628 |
} |
| 629 |
fam = t->ai->ai_family; |
| 630 |
|
| 631 |
return lo_inaddr_find_iface(&t->addr, fam, iface, ip); |
| 632 |
} |
| 633 |
|
| 634 |
int lo_inaddr_find_iface(lo_inaddr t, int fam, |
| 635 |
const char *iface, const char *ip) |
| 636 |
{ |
| 637 |
#if defined(_WIN32) || defined(_MSC_VER) |
| 638 |
ULONG size; |
| 639 |
int tries; |
| 640 |
PIP_ADAPTER_ADDRESSES paa, aa; |
| 641 |
DWORD rc; |
| 642 |
int found; |
| 643 |
#endif |
| 644 |
|
| 645 |
union { |
| 646 |
struct in_addr addr; |
| 647 |
#ifdef ENABLE_IPV6 |
| 648 |
struct in6_addr addr6; |
| 649 |
#endif |
| 650 |
} a; |
| 651 |
|
| 652 |
if (ip) { |
| 653 |
#ifdef HAVE_INET_PTON |
| 654 |
int rc = inet_pton(fam, ip, &a); |
| 655 |
if (rc!=1) |
| 656 |
return (rc<0) ? 3 : 4; |
| 657 |
#else |
| 658 |
if (fam!=AF_INET6) |
| 659 |
*((unsigned long*)&a.addr) = inet_addr(ip); |
| 660 |
#endif |
| 661 |
} |
| 662 |
|
| 663 |
#if defined(_WIN32) || defined(_MSC_VER) |
| 664 |
|
| 665 |
|
| 666 |
size = 15*1024/2; |
| 667 |
tries = 3; |
| 668 |
paa = malloc(size*2); |
| 669 |
rc = ERROR_SUCCESS-1; |
| 670 |
while (rc!=ERROR_SUCCESS && paa && tries-- > 0) { |
| 671 |
size *= 2; |
| 672 |
paa = realloc(paa, size); |
| 673 |
rc = GetAdaptersAddresses(fam, 0, 0, paa, &size); |
| 674 |
} |
| 675 |
if (rc!=ERROR_SUCCESS) |
| 676 |
return 2; |
| 677 |
|
| 678 |
aa = paa; |
| 679 |
found=0; |
| 680 |
while (aa && rc==ERROR_SUCCESS) { |
| 681 |
if (iface) { |
| 682 |
if (strcmp(iface, aa->AdapterName)==0) |
| 683 |
found = 1; |
| 684 |
else { |
| 685 |
WCHAR ifaceW[256]; |
| 686 |
MultiByteToWideChar(CP_ACP, 0, iface, -1, |
| 687 |
ifaceW, 256); |
| 688 |
if (lstrcmpW(ifaceW, aa->FriendlyName)==0) |
| 689 |
found = 1; |
| 690 |
} |
| 691 |
} |
| 692 |
if (ip) { |
| 693 |
PIP_ADAPTER_UNICAST_ADDRESS pua = aa->FirstUnicastAddress; |
| 694 |
while (pua && !found) { |
| 695 |
if (fam==AF_INET) { |
| 696 |
struct sockaddr_in *s = |
| 697 |
(struct sockaddr_in*)pua->Address.lpSockaddr; |
| 698 |
if (fam == s->sin_family |
| 699 |
&& memcmp(&a.addr, &s->sin_addr, |
| 700 |
sizeof(struct in_addr))==0) { |
| 701 |
memcpy(&t->a.addr, &s->sin_addr, |
| 702 |
sizeof(struct in_addr)); |
| 703 |
found = 1; |
| 704 |
} |
| 705 |
} |
| 706 |
#ifdef ENABLE_IPV6 |
| 707 |
else if (fam==AF_INET6) { |
| 708 |
struct sockaddr_in6 *s = |
| 709 |
(struct sockaddr_in6*)pua->Address.lpSockaddr; |
| 710 |
if (fam == s->sin6_family |
| 711 |
&& memcmp(&a.addr6, &s->sin6_addr, |
| 712 |
sizeof(struct in6_addr))==0) { |
| 713 |
memcpy(&t->a.addr6, &s->sin6_addr, |
| 714 |
sizeof(struct in6_addr)); |
| 715 |
found = 1; |
| 716 |
} |
| 717 |
} |
| 718 |
#endif |
| 719 |
pua = pua->Next; |
| 720 |
} |
| 721 |
} |
| 722 |
|
| 723 |
if (aa && found) { |
| 724 |
t->iface = strdup(aa->AdapterName); |
| 725 |
if (!ip && aa->FirstUnicastAddress) { |
| 726 |
PIP_ADAPTER_UNICAST_ADDRESS pua = aa->FirstUnicastAddress; |
| 727 |
while (pua) { |
| 728 |
struct sockaddr_in *s = |
| 729 |
(struct sockaddr_in*)pua->Address.lpSockaddr; |
| 730 |
if (s->sin_family==fam) { |
| 731 |
if (fam==AF_INET) { |
| 732 |
memcpy(&t->a.addr, &s->sin_addr, |
| 733 |
sizeof(struct in_addr)); |
| 734 |
break; |
| 735 |
} |
| 736 |
#ifdef ENABLE_IPV6 |
| 737 |
else if (fam==AF_INET6) { |
| 738 |
struct sockaddr_in6 *s6 = |
| 739 |
(struct sockaddr_in6*)pua->Address.lpSockaddr; |
| 740 |
memcpy(&t->a.addr6, &s6->sin6_addr, |
| 741 |
sizeof(struct in6_addr)); |
| 742 |
break; |
| 743 |
} |
| 744 |
#endif |
| 745 |
} |
| 746 |
pua = pua->Next; |
| 747 |
} |
| 748 |
} |
| 749 |
break; |
| 750 |
} |
| 751 |
|
| 752 |
aa = aa->Next; |
| 753 |
} |
| 754 |
|
| 755 |
if (paa) free(paa); |
| 756 |
|
| 757 |
return !found; |
| 758 |
|
| 759 |
#else |
| 760 |
|
| 761 |
struct ifaddrs *ifa, *ifa_list; |
| 762 |
if (getifaddrs(&ifa_list)==-1) |
| 763 |
return 5; |
| 764 |
ifa = ifa_list; |
| 765 |
|
| 766 |
int found = 0; |
| 767 |
while (ifa) { |
| 768 |
if (!ifa->ifa_addr) { |
| 769 |
ifa = ifa->ifa_next; |
| 770 |
continue; |
| 771 |
} |
| 772 |
if (ip) { |
| 773 |
if (ifa->ifa_addr->sa_family == AF_INET && fam == AF_INET) |
| 774 |
{ |
| 775 |
if (memcmp(&((struct sockaddr_in*)ifa->ifa_addr)->sin_addr, |
| 776 |
&a.addr, sizeof(struct in_addr))==0) { |
| 777 |
found = 1; |
| 778 |
t->size = sizeof(struct in_addr); |
| 779 |
memcpy(&t->a, &a, t->size); |
| 780 |
break; |
| 781 |
} |
| 782 |
} |
| 783 |
#ifdef ENABLE_IPV6 |
| 784 |
else if (ifa->ifa_addr->sa_family == AF_INET6 && fam == AF_INET6) |
| 785 |
{ |
| 786 |
if (memcmp(&((struct sockaddr_in6*)ifa->ifa_addr)->sin6_addr, |
| 787 |
&a.addr6, sizeof(struct in6_addr))==0) { |
| 788 |
found = 1; |
| 789 |
t->size = sizeof(struct in6_addr); |
| 790 |
memcpy(&t->a, &a, t->size); |
| 791 |
break; |
| 792 |
} |
| 793 |
} |
| 794 |
#endif |
| 795 |
} |
| 796 |
if (iface) { |
| 797 |
if (ifa->ifa_addr->sa_family == fam |
| 798 |
&& strcmp(ifa->ifa_name, iface)==0) |
| 799 |
{ |
| 800 |
if (fam==AF_INET) { |
| 801 |
found = 1; |
| 802 |
t->size = sizeof(struct in_addr); |
| 803 |
memcpy(&t->a, &((struct sockaddr_in*) |
| 804 |
ifa->ifa_addr)->sin_addr, |
| 805 |
t->size); |
| 806 |
break; |
| 807 |
} |
| 808 |
#ifdef ENABLE_IPV6 |
| 809 |
else if (fam==AF_INET6) { |
| 810 |
found = 1; |
| 811 |
t->size = sizeof(struct in6_addr); |
| 812 |
memcpy(&t->a, &((struct sockaddr_in6*) |
| 813 |
ifa->ifa_addr)->sin6_addr, |
| 814 |
t->size); |
| 815 |
break; |
| 816 |
} |
| 817 |
#endif |
| 818 |
} |
| 819 |
} |
| 820 |
ifa = ifa->ifa_next; |
| 821 |
} |
| 822 |
|
| 823 |
if (found && ifa->ifa_name) { |
| 824 |
if (t->iface) free(t->iface); |
| 825 |
t->iface = strdup(ifa->ifa_name); |
| 826 |
} |
| 827 |
|
| 828 |
freeifaddrs(ifa_list); |
| 829 |
return !found; |
| 830 |
#endif |
| 831 |
} |
| 832 |
|
| 833 |
const char* lo_address_get_iface(lo_address t) |
| 834 |
{ |
| 835 |
if (t) |
| 836 |
return t->addr.iface; |
| 837 |
return 0; |
| 838 |
} |
| 839 |
|
| 840 |
#endif |
| 841 |
|
| 842 |
|