btrfs: Attempt to fix GCC2 build.
[haiku.git] / src / bin / network / traceroute / findsaddr-haiku.c
blob9417580a4a9a34c5a3569c03fb6d834e8e68a8ae
1 /*
2 * Copyright 2007, Haiku Inc. All rights reserved.
3 * Distributed under the terms of the MIT License.
5 * Authors:
6 * Hugo Santos <hugosantos@gmail.com>
7 */
10 #include <stdint.h>
11 #include <string.h>
12 #include <unistd.h>
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...
23 const char *
24 findsaddr(const struct sockaddr_in *to, struct sockaddr_in *from)
26 uint8_t buffer[256];
27 struct route_entry *request = (struct route_entry *)buffer;
29 int sock = socket(AF_INET, SOCK_DGRAM, 0);
30 if (sock < 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) {
37 close(sock);
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));
44 close(sock);
46 if (request->source == NULL || request->source->sa_family != AF_INET)
47 return "No address available";
49 return NULL;