2 * Copyright 2007, Haiku Inc. All rights reserved.
3 * Distributed under the terms of the MIT License.
6 * Hugo Santos <hugosantos@gmail.com>
13 #include <net/route.h>
14 #include <netinet/in.h>
15 #include <sys/ioctl.h>
16 #include <sys/socket.h>
17 #include <sys/sockio.h>
19 #include "findsaddr.h"
20 // is not self containing...
24 findsaddr(const struct sockaddr_in
*to
, struct sockaddr_in
*from
)
27 struct route_entry
*request
= (struct route_entry
*)buffer
;
29 int sock
= socket(AF_INET
, SOCK_DGRAM
, 0);
31 return "socket() failed";
33 memset(request
, 0, sizeof(struct route_entry
));
34 request
->destination
= (struct sockaddr
*)to
;
36 if (ioctl(sock
, SIOCGETRT
, buffer
, sizeof(buffer
)) < 0) {
38 return "ioctl(SIOCGETRT) failed";
41 if (request
->source
!= NULL
&& request
->source
->sa_family
== AF_INET
)
42 memcpy(from
, request
->source
, sizeof(struct sockaddr_in
));
46 if (request
->source
== NULL
|| request
->source
->sa_family
!= AF_INET
)
47 return "No address available";