TEST(XreadLocalHostAddr, InvalidPointers) { int sock = Xsocket(AF_XIA, XSOCK_STREAM, 0); int rc = XreadLocalHostAddr(sock, NULL, XID_LEN, NULL, XID_LEN, NULL, XID_LEN); ASSERT_EQ(-1, rc); Xclose(sock); }
//Just registering the service and openning the necessary sockets int registerReceiver() { int sock; say ("\n%s (%s): started\n", TITLE, VERSION); // create a socket, and listen for incoming connections if ((sock = Xsocket(AF_XIA, SOCK_STREAM, 0)) < 0) die(-1, "Unable to create the listening socket\n"); // read the localhost AD and HID if ( XreadLocalHostAddr(sock, myAD, sizeof(myAD), myHID, sizeof(myHID), my4ID, sizeof(my4ID)) < 0 ) die(-1, "Reading localhost address\n"); struct addrinfo *ai; //FIXME: SID is hardcoded if (Xgetaddrinfo(NULL, SID, NULL, &ai) != 0) die(-1, "getaddrinfo failure!\n"); sockaddr_x *dag = (sockaddr_x*)ai->ai_addr; //FIXME NAME is hard coded if (XregisterName(NAME, dag) < 0 ) die(-1, "error registering name: %s\n", NAME); if (Xbind(sock, (struct sockaddr*)dag, sizeof(dag)) < 0) { Xclose(sock); die(-1, "Unable to bind to the dag: %s\n", dag); } Graph g(dag); say("listening on dag: %s\n", g.dag_string().c_str()); return sock; }
int main(int argc, char *argv[]) { int sock, n, chunkSock, acceptSock; size_t dlen; char buf[1024],theirDAG[1024]; //char* reply="Got your message"; char reply[1024]; pid_t pid; char myAD[1024]; char myHID[1024]; //Open socket sock=Xsocket(XSOCK_DGRAM); if (sock < 0) error("Opening socket"); // read the localhost AD and HID if ( XreadLocalHostAddr(sock, myAD, sizeof(myAD), myHID, sizeof(myHID)) < 0 ) error("Reading localhost address"); // make the src DAG (the one the server listens on) char * dag = (char*) malloc(snprintf(NULL, 0, "RE %s %s %s", myAD, myHID, SID0) + 1); sprintf(dag, "RE %s %s %s", myAD, myHID, SID0); //Register this service name to the name server char * sname = (char*) malloc(snprintf(NULL, 0, "%s", SNAME) + 1); sprintf(sname, "%s", SNAME); if (XregisterName(sname, dag) < 0 ) error("name register"); //Bind to the DAG Xbind(sock,dag); while (1) { //Receive packet memset(&buf[0], 0, sizeof(buf)); dlen = sizeof(theirDAG); n = Xrecvfrom(sock,buf,1024,0,theirDAG,&dlen); //n = Xrecv(acceptSock,buf,1024,0); if (n < 0) error("recvfrom"); //printf("\nReceived a datagram from:%s len %d strlen %d\n",theirDAG, (int)dlen, (int)strlen(theirDAG)); write(1,buf,n); memset(&reply[0], 0, sizeof(reply)); strcat (reply, "Got your message: "); strcat (reply, buf); //Reply to client Xsendto(sock,reply,strlen(reply)+1,0,theirDAG,strlen(theirDAG)); //Xsend(acceptSock,reply,strlen(reply),0); } return 0; }
TEST(XreadLocalHostAddr, ShortBuffers) { char ad[XID_LEN], hid[XID_LEN], fid[XID_LEN]; int sock = Xsocket(AF_XIA, XSOCK_STREAM, 0); int rc = XreadLocalHostAddr(sock, ad, 10, hid, 10, fid, 10); ASSERT_EQ(0, rc); EXPECT_STRNE(ad, ""); EXPECT_STRNE(hid, ""); EXPECT_STRNE(fid, ""); }
int initializeClient(const char *name) { int sock, rc; sockaddr_x dag; socklen_t daglen; char sdag[1024]; char IP[MAX_XID_SIZE]; // lookup the xia service daglen = sizeof(dag); if (XgetDAGbyName(name, &dag, &daglen) < 0) die(-1, "unable to locate: %s\n", name); // create a socket, and listen for incoming connections if ((sock = Xsocket(AF_XIA, SOCK_STREAM, 0)) < 0) die(-1, "Unable to create the listening socket\n"); if (Xconnect(sock, (struct sockaddr*)&dag, daglen) < 0) { Xclose(sock); die(-1, "Unable to bind to the dag: %s\n", dag); } rc = XreadLocalHostAddr(sock, my_ad, MAX_XID_SIZE, my_hid, MAX_XID_SIZE, IP, MAX_XID_SIZE); if (rc < 0) { Xclose(sock); die(-1, "Unable to read local address.\n"); } else{ warn("My AD: %s, My HID: %s\n", my_ad, my_hid); } // save the AD and HID for later. This seems hacky // we need to find a better way to deal with this Graph g(&dag); strncpy(sdag, g.dag_string().c_str(), sizeof(sdag)); // say("sdag = %s\n",sdag); char *ads = strstr(sdag,"AD:"); char *hids = strstr(sdag,"HID:"); // i = sscanf(ads,"%s",s_ad ); // i = sscanf(hids,"%s", s_hid); if(sscanf(ads,"%s",s_ad ) < 1 || strncmp(s_ad,"AD:", 3) !=0){ die(-1, "Unable to extract AD."); } if(sscanf(hids,"%s", s_hid) < 1 || strncmp(s_hid,"HID:", 4) !=0 ){ die(-1, "Unable to extract AD."); } warn("Service AD: %s, Service HID: %s\n", s_ad, s_hid); return sock; }
// XreadLocalHostAddr ********************************************************* TEST(XreadLocalHostAddr, ValidParameters) { char ad[XID_LEN], hid[XID_LEN], fid[XID_LEN]; int sock = Xsocket(AF_XIA, XSOCK_STREAM, 0); int rc = XreadLocalHostAddr(sock, ad, XID_LEN, hid, XID_LEN, fid, XID_LEN); ASSERT_EQ(0, rc); EXPECT_STRNE(ad, ""); EXPECT_STRNE(hid, ""); EXPECT_STRNE(fid, ""); EXPECT_EQ(0, strncmp(ad, "AD:", 3)); EXPECT_EQ(0, strncmp(hid, "HID:", 4)); EXPECT_EQ(0, strncmp(fid, "IP:", 3)); }
virtual void SetUp() { sockaddr_x sa; int sock = Xsocket(AF_XIA, SOCK_STREAM, 0); XreadLocalHostAddr(sock, ad, XID_LEN, hid, XID_LEN, fid, XID_LEN); sprintf(fdag, FULL_DAG, ad, hid, SID); Graph gf(fdag); gf.fill_sockaddr(&sa); XregisterName(FULL_NAME, &sa); sprintf(hdag, HOST_DAG, ad, hid); Graph gh(hdag); gh.fill_sockaddr(&sa); XregisterName(HOST_NAME, &sa); Xclose(sock); nad = new Node(ad); nhid = new Node(hid); nsid = new Node(SID); ai = NULL; }
TEST(XreadLocalHostAddr, InvalidSocket) { char ad[XID_LEN], hid[XID_LEN], fid[XID_LEN]; int rc = XreadLocalHostAddr(0, ad, XID_LEN, hid, XID_LEN, fid, XID_LEN); ASSERT_EQ(-1, rc); }