|
1 /* |
|
2 * Copyright 2006 Intel Corporation |
|
3 * |
|
4 * Licensed under the Apache License, Version 2.0 (the "License"); |
|
5 * you may not use this file except in compliance with the License. |
|
6 * You may obtain a copy of the License at |
|
7 * |
|
8 * http://www.apache.org/licenses/LICENSE-2.0 |
|
9 * |
|
10 * Unless required by applicable law or agreed to in writing, software |
|
11 * distributed under the License is distributed on an "AS IS" BASIS, |
|
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
13 * See the License for the specific language governing permissions and |
|
14 * limitations under the License. |
|
15 */ |
|
16 |
|
17 #ifdef HAVE_CONFIG_H |
|
18 # include <dtn-config.h> |
|
19 #endif |
|
20 |
|
21 #include "gateway_rpc.h" |
|
22 #include "gateway_prot.h" |
|
23 #include <sys/socket.h> // needed for AF_INET |
|
24 #include <netdb.h> // needed for gethostbyname |
|
25 #include <string.h> |
|
26 #include <stdio.h> |
|
27 |
|
28 static const int DHT_PORT = 5852; |
|
29 |
|
30 /////////////////////////////////////////////////////////////////////////////// |
|
31 // Functions for interfacing with OpenDHT |
|
32 |
|
33 // lookup hostname, store address in addr |
|
34 int |
|
35 lookup_host(const char* host, int port, struct sockaddr_in* addr) |
|
36 { |
|
37 struct hostent* h = gethostbyname (host); |
|
38 if (h == NULL) return 0; |
|
39 |
|
40 bzero (addr, sizeof(struct sockaddr_in)); |
|
41 addr->sin_family = AF_INET; |
|
42 addr->sin_port = htons(port); |
|
43 |
|
44 // demmer: rewrote the following as a memcpy to avoid -Wcast-align bugs |
|
45 //addr->sin_addr = *((struct in_addr *) h->h_addr); |
|
46 memcpy(&addr->sin_addr, h->h_addr, sizeof(struct in_addr)); |
|
47 return 1; |
|
48 } |
|
49 |
|
50 |
|
51 // try to open connection to given dht node by addr |
|
52 CLIENT* |
|
53 get_connection(struct sockaddr_in* addr) |
|
54 { |
|
55 int sockp = RPC_ANYSOCK; |
|
56 CLIENT * c = clnttcp_create(addr, BAMBOO_DHT_GATEWAY_PROGRAM, |
|
57 BAMBOO_DHT_GATEWAY_VERSION, &sockp, 0, 0); |
|
58 return c; |
|
59 } |
|
60 |
|
61 |
|
62 /* |
|
63 // try to open connection to given dht node by hostname |
|
64 static CLIENT* |
|
65 get_connection(const char* hostname) |
|
66 { |
|
67 struct sockaddr_in addr; |
|
68 if(lookup_host(hostname, DHT_PORT, &addr) < 0) return NULL; |
|
69 return get_connection(addr); |
|
70 } |
|
71 */ |
|
72 |
|
73 |
|
74 // useful for probing a dht node to see if it's alive: |
|
75 int |
|
76 do_null_op(CLIENT* c) |
|
77 { |
|
78 void* null_args = NULL; |
|
79 void* res = bamboo_dht_proc_null_2(&null_args, c); |
|
80 return (res != NULL); |
|
81 } |
|
82 |
|
83 |
|
84 // test dht node, printing status messages |
|
85 // if successful, addr contains a valid sockaddr_in |
|
86 int |
|
87 test_node(const char* hostname, struct sockaddr_in* addr) |
|
88 { |
|
89 printf(" testing dht node %s... ", hostname); |
|
90 |
|
91 // try to get host addr |
|
92 if (!lookup_host(hostname, DHT_PORT, addr)) |
|
93 { |
|
94 printf("lookup_host failed\n"); |
|
95 return 0; |
|
96 } |
|
97 |
|
98 // try to connect to node |
|
99 // Note: This step seems to be insanely slow when it fails. Is there |
|
100 // a way to timeout faster? |
|
101 CLIENT* p_client = get_connection(addr); |
|
102 if (p_client == NULL) |
|
103 { |
|
104 printf("get_connection failed\n"); |
|
105 return 0; |
|
106 } |
|
107 |
|
108 // try a null op |
|
109 if (!do_null_op(p_client)) |
|
110 { |
|
111 printf("null_op failed\n"); |
|
112 clnt_destroy(p_client); |
|
113 return 0; |
|
114 } |
|
115 |
|
116 printf("succeeded.\n"); |
|
117 clnt_destroy(p_client); |
|
118 return 1; |
|
119 } |