This code provides underlying sockets to the RPC calls.
It was remade to use the new function that gather port information and
to use new API to retrieve hostname data.

Signed-off-by: Richard Maciel <[email protected]>
---
 src/include/rpc_tcstp_tsp.h |   4 +-
 src/tspi/rpc/tcstp/rpc.c    | 102 +++++++++++++++++++++++---------------------
 2 files changed, 57 insertions(+), 49 deletions(-)

diff --git a/src/include/rpc_tcstp_tsp.h b/src/include/rpc_tcstp_tsp.h
index 79f5e9f..2d53a00 100644
--- a/src/include/rpc_tcstp_tsp.h
+++ b/src/include/rpc_tcstp_tsp.h
@@ -22,7 +22,9 @@ void initData(struct tcsd_comm_data *, int);
 TSS_RESULT sendTCSDPacket(struct host_table_entry *);
 TSS_RESULT send_init(struct host_table_entry *);
 TSS_RESULT tcs_sendit(struct host_table_entry *);
-short get_port();
+
+/* Underlying socket-related calls */
+TSS_RESULT get_socket(struct host_table_entry *hte, int *sd);
 
 /* Context commands always included */
 TSS_RESULT RPC_OpenContext_TP(struct host_table_entry *, UINT32 *, 
TCS_CONTEXT_HANDLE *);
diff --git a/src/tspi/rpc/tcstp/rpc.c b/src/tspi/rpc/tcstp/rpc.c
index 3b4ed12..afe1844 100644
--- a/src/tspi/rpc/tcstp/rpc.c
+++ b/src/tspi/rpc/tcstp/rpc.c
@@ -31,6 +31,7 @@
 #include "tcsd_wrap.h"
 #include "obj.h"
 #include "rpc_tcstp_tsp.h"
+#include "tsp_tcsi_param.h"
 
 
 void
@@ -342,42 +343,9 @@ send_init(struct host_table_entry *hte)
        BYTE *buffer;
        TSS_RESULT result;
 
-       struct sockaddr_in addr;
-       struct hostent *hEnt = NULL;
-
-       sd = socket(PF_INET, SOCK_STREAM, 0);
-       if (sd == -1) {
-               LogError("socket: %s", strerror(errno));
-               result = TSPERR(TSS_E_COMM_FAILURE);
-               goto err_exit;
-       }
-
-       __tspi_memset(&addr, 0, sizeof(addr));
-       addr.sin_family = AF_INET;
-       addr.sin_port = htons(get_port());
-
-       LogDebug("Sending TSP packet to host %s.", hte->hostname);
-
-       /* try to resolve by hostname first */
-       hEnt = gethostbyname((char *)hte->hostname);
-       if (hEnt == NULL) {
-               /* if by hostname fails, try by dot notation */
-               if (inet_aton((char *)hte->hostname, &addr.sin_addr) == 0) {
-                       LogError("hostname %s does not resolve to a valid 
address.", hte->hostname);
-                       result = TSPERR(TSS_E_CONNECTION_FAILED);
-                       goto err_exit;
-               }
-       } else {
-               memcpy(&addr.sin_addr, hEnt->h_addr_list[0], 4);
-       }
-
-       LogDebug("Connecting to %s", inet_ntoa(addr.sin_addr));
-
-       if (connect(sd, (struct sockaddr *) &addr, sizeof (addr))) {
-               LogError("connect: %s", strerror(errno));
-               result = TSPERR(TSS_E_COMM_FAILURE);
+       result = get_socket(hte, &sd);
+       if (result != TSS_SUCCESS)
                goto err_exit;
-       }
 
        if (send_to_socket(sd, hte->comm.buf, hte->comm.hdr.packet_size) < 0) {
                result = TSPERR(TSS_E_COMM_FAILURE);
@@ -489,23 +457,61 @@ err_exit:
        return result;
 }
 
-/* XXX this should be moved out of an RPC-specific file */
-short
-get_port(void)
+/* TODO: Future work - remove socket creation/manipulation from RPC-specific 
file */
+TSS_RESULT
+get_socket(struct host_table_entry *hte, int *sd)
 {
-       char *env_port;
-       int port = 0;
+       char port_str[TCP_PORT_STR_MAX_LEN]; // To accomodate string 65535
+       struct addrinfo hints, *res, *p;
+       int rv;
+       TSS_RESULT result = TSS_SUCCESS;
 
-       env_port = getenv("TSS_TCSD_PORT");
+       __tspi_memset(&hints, 0, sizeof(hints));
+       hints.ai_socktype = SOCK_STREAM;
+       hints.ai_flags = AI_NUMERICSERV;
 
-       if (env_port == NULL)
-               return TCSD_DEFAULT_PORT;
+       __tspi_memset(&port_str, 0, sizeof(port_str));
 
-       port = atoi(env_port);
+       if (get_tcsd_port(port_str) != TSS_SUCCESS) {
+               LogError("Could not retrieve TCP port information.");
+               goto exit;
+       }
+
+       LogDebug("Retrieving address information from host: %s", (char 
*)hte->hostname);
+       rv = getaddrinfo((char *)hte->hostname, port_str,
+                       &hints, &res);
+       if (rv != 0) {
+               LogError("hostname %s does not resolve to a valid address.", 
hte->hostname);
+               result = TSPERR(TSS_E_CONNECTION_FAILED);
+               res = NULL;
+               goto exit;
+       }
 
-       if (port == 0 || port > 65535)
-               return TCSD_DEFAULT_PORT;
+       LogWarn("Got a list of valid IPs");
 
-       return (short)port;
-}
+       for (p = res; p != NULL; p = p->ai_next) {
 
+               *sd = socket(p->ai_family, SOCK_STREAM, 0);
+               if (*sd == -1)
+                       continue;
+
+               if (connect(*sd, p->ai_addr, p->ai_addrlen) != -1)
+                       break; // Got a connection
+
+               LogWarn("Could not connect to machine: %s", 
(char*)hte->hostname);
+
+               close(*sd);
+       }
+
+       if (p == NULL) {
+               LogError("Could not connect to any machine in the list.");
+               result = TSPERR(TSS_E_COMM_FAILURE);
+               goto exit;
+       }
+
+exit:
+       if (res != NULL)
+               freeaddrinfo(res);
+
+       return result;
+}
-- 
1.8.1.4


------------------------------------------------------------------------------
October Webinars: Code for Performance
Free Intel webinars can help you accelerate application performance.
Explore tips for MPI, OpenMP, advanced profiling, and more. Get the most from 
the latest Intel processors and coprocessors. See abstracts and register >
http://pubads.g.doubleclick.net/gampad/clk?id=60134791&iu=/4140/ostg.clktrk
_______________________________________________
TrouSerS-tech mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/trousers-tech

Reply via email to