+unsigned int getLeadersIpAddr()
+{
+ unsigned int reply_ip;
+ int bytesReceived;
+ char myMessage;
+ char response;
+
+#ifdef DHT_LOG
+ fprintf(logfile, "getLeadersIpAddr(): broadcasting...\n");
+ fflush(logfile);
+#endif
+
+ myMessage = FIND_LEADER_CMD;
+
+ bytesReceived = udpBroadcastWaitForResponse(&reply_ip, UDP_PORT,
+ (void *)&myMessage, sizeof(myMessage), (void *)&response,
+ sizeof(response), TIMEOUT_MS, MAX_RETRIES);
+
+ if (bytesReceived == -1)
+ {
+#ifdef DHT_LOG
+ fprintf(logfile, "getLeadersIpAddr(): no response\n");
+ fflush(logfile);
+#endif
+ return 0;
+ }
+ else if (response == FIND_LEADER_RES)
+ {
+#ifdef DHT_LOG
+ struct in_addr reply_addr;
+ reply_addr.s_addr = htonl(reply_ip);
+ fprintf(logfile, "getLeadersIpAddr(): leader found:%s\n",
+ inet_ntoa(reply_addr));
+ fflush(logfile);
+#endif
+ return reply_ip;
+ }
+ else
+ {
+#ifdef DHT_LOG
+ fprintf(logfile, "getLeadersIpAddr(): unexpected response\n");
+ fflush(logfile);
+#endif
+ return 0;
+ }
+}
+
+int getDHTdata()
+{
+ struct sockaddr_in leader_addr;
+ int sock;
+ char msg;
+ int bytesReceived;
+
+ if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1)
+ {
+ perror("getDHTdata():socket()");
+ return -1;
+ }
+
+ bzero((char *)&leader_addr, sizeof(leader_addr));
+ leader_addr.sin_family = AF_INET;
+ leader_addr.sin_port = htons(TCP_PORT);
+ leader_addr.sin_addr.s_addr = htonl(leader);
+
+ if (connect(sock, (struct sockaddr *)&leader_addr, sizeof(leader_addr)) == -1)
+ {
+ perror("getDHTdata():connect()");
+ close(sock);
+ return -1;
+ }
+ msg = DHT_INFO_CMD;
+ if (send(sock, &msg, sizeof(char), 0) == -1)
+ {
+ perror("getDHTdata():send()");
+ close(sock);
+ return -1;
+ }
+ bytesReceived = recv(sock, &numHosts, sizeof(numHosts), 0);
+ if (bytesReceived == -1)
+ {
+ perror("getDHTdata():recv()");
+ close(sock);
+ return -1;
+ }
+ if (bytesReceived != sizeof(numHosts))
+ {
+#ifdef DHT_LOG
+ fprintf(logfile,"getDHTdata(): ERROR: numHosts not completely received\n");
+ fflush(logfile);
+ close(sock);
+ return -1;
+#endif
+ }
+ bytesReceived = recv(sock, &numBlocks, sizeof(numBlocks), 0);
+ if (bytesReceived == -1)
+ {
+ perror("getDHTdata():recv()");
+ close(sock);
+ return -1;
+ }
+ if (bytesReceived != sizeof(numBlocks))
+ {
+#ifdef DHT_LOG
+ fprintf(logfile,"getDHTdata(): ERROR: numBlocks not completely received\n");
+ fflush(logfile);
+ close(sock);
+ return -1;
+#endif
+ }
+ if (hostArray != NULL)
+ free(hostArray);
+ hostArray = calloc(numHosts, sizeof(struct hostData));
+ bytesReceived = recv(sock, hostArray, numHosts*sizeof(struct hostData), 0);
+ if (bytesReceived == -1)
+ {
+ perror("getDHTdata():recv()");
+ close(sock);
+ return -1;
+ }
+ if (bytesReceived != numHosts*sizeof(struct hostData))
+ {
+#ifdef DHT_LOG
+ fprintf(logfile,"getDHTdata(): ERROR: hostArray not completely received\n");
+ fflush(logfile);
+ close(sock);
+ return -1;
+#endif
+ }
+ if (blockOwnerArray != NULL)
+ free(blockOwnerArray);
+ blockOwnerArray = calloc(numBlocks, sizeof(unsigned int));
+ bytesReceived = recv(sock, blockOwnerArray, numBlocks*sizeof(unsigned int), 0);
+ if (bytesReceived == -1)
+ {
+ perror("getDHTdata():recv()");
+ close(sock);
+ return -1;
+ }
+ if (bytesReceived != numBlocks*sizeof(unsigned int))
+ {
+#ifdef DHT_LOG
+ fprintf(logfile,"getDHTdata(): ERROR: blockOwnerArray not completely received\n");
+ fflush(logfile);
+ close(sock);
+ return -1;
+#endif
+ }
+#ifdef DHT_LOG
+ fprintf(logfile,"getDHTdata(): got data:\n");
+ writeDHTdata(logfile);
+ fflush(logfile);
+#endif
+ return 0;
+}
+