gateway_rpc.c

Go to the documentation of this file.
00001 
00002 #include "gateway_rpc.h"
00003 #include "gateway_prot.h"
00004 #include <netdb.h>                  // needed for gethostbyname
00005 #include <string.h>
00006 #include <stdio.h>
00007 
00008 static const int DHT_PORT = 5852;
00009 
00011 // Functions for interfacing with OpenDHT
00012 
00013 // lookup hostname, store address in addr
00014 int
00015 lookup_host(const char* host, int port, struct sockaddr_in* addr)
00016 {
00017     struct hostent* h = gethostbyname (host);
00018     if (h == NULL) return 0;
00019 
00020     bzero (addr, sizeof(struct sockaddr_in));
00021     addr->sin_family = AF_INET;
00022     addr->sin_port = htons(port);
00023 
00024     // demmer: rewrote the following as a memcpy to avoid -Wcast-align bugs
00025     //addr->sin_addr = *((struct in_addr *) h->h_addr);
00026     memcpy(&addr->sin_addr, h->h_addr, sizeof(struct in_addr));
00027     return 1;
00028 }
00029 
00030 
00031 // try to open connection to given dht node by addr
00032 CLIENT*
00033 get_connection(struct sockaddr_in* addr)
00034 {
00035     int sockp = RPC_ANYSOCK;
00036     CLIENT * c = clnttcp_create(addr, BAMBOO_DHT_GATEWAY_PROGRAM,
00037                         BAMBOO_DHT_GATEWAY_VERSION, &sockp, 0, 0);
00038     return c;
00039 }
00040 
00041 
00042 /*
00043 // try to open connection to given dht node by hostname
00044 static CLIENT*
00045 get_connection(const char* hostname)
00046 {
00047     struct sockaddr_in addr;
00048     if(lookup_host(hostname, DHT_PORT, &addr) < 0) return NULL;
00049     return get_connection(addr);
00050 }
00051 */
00052 
00053 
00054 // useful for probing a dht node to see if it's alive:
00055 int
00056 do_null_op(CLIENT* c)
00057 {
00058     void* null_args = NULL;
00059     void* res = bamboo_dht_proc_null_2(&null_args, c);
00060     return (res != NULL);
00061 }
00062 
00063 
00064 // test dht node, printing status messages
00065 // if successful, addr contains a valid sockaddr_in
00066 int
00067 test_node(const char* hostname, struct sockaddr_in* addr)
00068 {
00069     printf("   testing dht node %s... ", hostname);
00070 
00071     // try to get host addr
00072     if (!lookup_host(hostname, DHT_PORT, addr))
00073     {
00074         printf("lookup_host failed\n");
00075         return 0;
00076     }
00077 
00078     // try to connect to node
00079     // Note: This step seems to be insanely slow when it fails. Is there
00080     // a way to timeout faster?
00081     CLIENT* p_client = get_connection(addr);
00082     if (p_client == NULL)
00083     {
00084         printf("get_connection failed\n");
00085         return 0;
00086     }
00087 
00088     // try a null op
00089     if (!do_null_op(p_client))
00090     {
00091         printf("null_op failed\n");
00092         clnt_destroy(p_client);
00093         return 0;
00094     }
00095 
00096     printf("succeeded.\n");
00097     clnt_destroy(p_client);
00098     return 1;
00099 }

Generated on Fri Dec 22 14:47:59 2006 for DTN Reference Implementation by  doxygen 1.5.1