gateway_rpc.c

Go to the documentation of this file.
00001 /*
00002  *    Copyright 2006 Intel Corporation
00003  * 
00004  *    Licensed under the Apache License, Version 2.0 (the "License");
00005  *    you may not use this file except in compliance with the License.
00006  *    You may obtain a copy of the License at
00007  * 
00008  *        http://www.apache.org/licenses/LICENSE-2.0
00009  * 
00010  *    Unless required by applicable law or agreed to in writing, software
00011  *    distributed under the License is distributed on an "AS IS" BASIS,
00012  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  *    See the License for the specific language governing permissions and
00014  *    limitations under the License.
00015  */
00016 
00017 
00018 #include "gateway_rpc.h"
00019 #include "gateway_prot.h"
00020 #include <netdb.h>                  // needed for gethostbyname
00021 #include <string.h>
00022 #include <stdio.h>
00023 
00024 static const int DHT_PORT = 5852;
00025 
00027 // Functions for interfacing with OpenDHT
00028 
00029 // lookup hostname, store address in addr
00030 int
00031 lookup_host(const char* host, int port, struct sockaddr_in* addr)
00032 {
00033     struct hostent* h = gethostbyname (host);
00034     if (h == NULL) return 0;
00035 
00036     bzero (addr, sizeof(struct sockaddr_in));
00037     addr->sin_family = AF_INET;
00038     addr->sin_port = htons(port);
00039 
00040     // demmer: rewrote the following as a memcpy to avoid -Wcast-align bugs
00041     //addr->sin_addr = *((struct in_addr *) h->h_addr);
00042     memcpy(&addr->sin_addr, h->h_addr, sizeof(struct in_addr));
00043     return 1;
00044 }
00045 
00046 
00047 // try to open connection to given dht node by addr
00048 CLIENT*
00049 get_connection(struct sockaddr_in* addr)
00050 {
00051     int sockp = RPC_ANYSOCK;
00052     CLIENT * c = clnttcp_create(addr, BAMBOO_DHT_GATEWAY_PROGRAM,
00053                         BAMBOO_DHT_GATEWAY_VERSION, &sockp, 0, 0);
00054     return c;
00055 }
00056 
00057 
00058 /*
00059 // try to open connection to given dht node by hostname
00060 static CLIENT*
00061 get_connection(const char* hostname)
00062 {
00063     struct sockaddr_in addr;
00064     if(lookup_host(hostname, DHT_PORT, &addr) < 0) return NULL;
00065     return get_connection(addr);
00066 }
00067 */
00068 
00069 
00070 // useful for probing a dht node to see if it's alive:
00071 int
00072 do_null_op(CLIENT* c)
00073 {
00074     void* null_args = NULL;
00075     void* res = bamboo_dht_proc_null_2(&null_args, c);
00076     return (res != NULL);
00077 }
00078 
00079 
00080 // test dht node, printing status messages
00081 // if successful, addr contains a valid sockaddr_in
00082 int
00083 test_node(const char* hostname, struct sockaddr_in* addr)
00084 {
00085     printf("   testing dht node %s... ", hostname);
00086 
00087     // try to get host addr
00088     if (!lookup_host(hostname, DHT_PORT, addr))
00089     {
00090         printf("lookup_host failed\n");
00091         return 0;
00092     }
00093 
00094     // try to connect to node
00095     // Note: This step seems to be insanely slow when it fails. Is there
00096     // a way to timeout faster?
00097     CLIENT* p_client = get_connection(addr);
00098     if (p_client == NULL)
00099     {
00100         printf("get_connection failed\n");
00101         return 0;
00102     }
00103 
00104     // try a null op
00105     if (!do_null_op(p_client))
00106     {
00107         printf("null_op failed\n");
00108         clnt_destroy(p_client);
00109         return 0;
00110     }
00111 
00112     printf("succeeded.\n");
00113     clnt_destroy(p_client);
00114     return 1;
00115 }

Generated on Sat Sep 8 08:36:16 2007 for DTN Reference Implementation by  doxygen 1.5.3