TOR: fix compilation
[tomato.git] / release / src / router / miniupnpd / testgetroute.c
blobb8571d872d18b167e348bc88474dcf6de350e42a
1 /* $Id: testgetroute.c,v 1.6 2015/11/16 19:20:21 nanard Exp $ */
2 /* MiniUPnP project
3 * http://miniupnp.free.fr/ or http://miniupnp.tuxfamily.org/
4 * (c) 2006-2015 Thomas Bernard
5 * This software is subject to the conditions detailed
6 * in the LICENCE file provided within the distribution */
8 #include <stdio.h>
9 #include <string.h>
10 #include <syslog.h>
11 #include <sys/types.h>
12 #include <sys/socket.h>
13 #include <netinet/in.h>
14 #include <arpa/inet.h>
16 #include "getroute.h"
17 #include "upnputils.h"
18 #include "upnpglobalvars.h"
20 #ifndef LOG_PERROR
21 /* solaris does not define LOG_PERROR */
22 #define LOG_PERROR 0
23 #endif
25 struct lan_addr_list lan_addrs;
27 int
28 main(int argc, char ** argv)
30 struct sockaddr_in dst4;
31 struct sockaddr_in6 dst6;
32 struct sockaddr * dst;
33 void * src;
34 size_t src_len;
35 int r;
36 int index = -1;
38 memset(&dst4, 0, sizeof(dst4));
39 memset(&dst6, 0, sizeof(dst6));
40 dst = NULL;
41 if(argc < 2) {
42 fprintf(stderr, "usage: %s <ip address>\n", argv[0]);
43 fprintf(stderr, "both v4 and v6 IP addresses are supported.\n");
44 return 1;
46 openlog("testgetroute", LOG_CONS|LOG_PERROR, LOG_USER);
47 r = inet_pton (AF_INET, argv[1], &dst4.sin_addr);
48 if(r < 0) {
49 syslog(LOG_ERR, "inet_pton(AF_INET, %s) : %m", argv[1]);
50 closelog();
51 return 2;
53 if (r == 0) {
54 r = inet_pton (AF_INET6, argv[1], &dst6.sin6_addr);
55 if(r < 0) {
56 syslog(LOG_ERR, "inet_pton(AF_INET6, %s) : %m", argv[1]);
57 closelog();
58 return 2;
59 } else if(r > 0) {
60 dst6.sin6_family = AF_INET6;
61 dst = (struct sockaddr *)&dst6;
62 src = &dst6.sin6_addr;
63 src_len = sizeof(dst6.sin6_addr);
64 } else {
65 /* r == 0 */
66 syslog(LOG_ERR, "%s is not a correct IPv4 or IPv6 address", argv[1]);
67 closelog();
68 return 1;
70 } else {
71 dst4.sin_family = AF_INET;
72 dst = (struct sockaddr *)&dst4;
73 src = &dst4.sin_addr;
74 src_len = sizeof(dst4.sin_addr);
77 if (dst) {
78 syslog(LOG_DEBUG, "calling get_src_for_route_to(%p, NULL, NULL, %p)",
79 dst, &index);
80 r = get_src_for_route_to (dst, NULL, NULL, &index);
81 syslog(LOG_DEBUG, "get_src_for_route_to() returned %d", r);
82 if(r >= 0) {
83 syslog(LOG_DEBUG, "index=%d", index);
85 syslog(LOG_DEBUG, "calling get_src_for_route_to(%p, %p, %p(%u), %p)",
86 dst, src, &src_len, (unsigned)src_len, &index);
87 r = get_src_for_route_to (dst, src, &src_len, &index);
88 syslog(LOG_DEBUG, "get_src_for_route_to() returned %d", r);
89 if(r >= 0) {
90 char src_str[128];
91 sockaddr_to_string(dst, src_str, sizeof(src_str));
92 syslog(LOG_DEBUG, "src=%s", src_str);
93 syslog(LOG_DEBUG, "index=%d", index);
96 closelog();
97 return 0;