Example #1
0
/*
** where it all happens
*/
int main(int argc, char **argv)
{
	srand(time(NULL));
	getConfig(argc, argv);

	say ("\n%s (%s): started\n", TITLE, VERSION);

    if(!(sdag = XgetDAGbyName(DGRAM_NAME)))
		die(-1, "Unable to lookup name: %s\n", DGRAM_NAME);

	if (threads == 1)
		// just do it
		mainLoop(NULL);
	else {
		pthread_t *clients = (pthread_t*)malloc(threads * sizeof(pthread_t));

		if (!clients)
			die(-5, "Unable to allocate threads\n");

		for (int i = 0; i < threads; i++) {
			pthread_create(&clients[i], NULL, mainLoop, NULL);
		}
		for (int i = 0; i < threads; i++) {
			pthread_join(clients[i], NULL);
		}

		free(clients);
	}

	free(sdag);
	return 0;
}
Example #2
0
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;
}
Example #3
0
TEST(XgetDAGbyName, ExtraLen)
{
	sockaddr_x sa;
	socklen_t len = sizeof(sa) + 10;
	Graph g(TEST_DAG);
	g.fill_sockaddr(&sa);
	XregisterName(TEST_NAME, &sa);
	memset(&sa, 0, sizeof(sa));
	EXPECT_EQ(0, XgetDAGbyName(TEST_NAME, &sa, &len));
	EXPECT_EQ(len, sizeof(sa));
	Graph g1(&sa);
	// TEST_DAG was in form of AD:... HID:... SID:...
	EXPECT_EQ(3, g1.num_nodes());
}
Example #4
0
int main(int argc, char *argv[])
{
    int sock, n;
    size_t dlen;
    char reply[128];
    char buffer[2048],theirDAG[1024];    

    //Open socket
    sock=Xsocket(XSOCK_DGRAM);
    if (sock < 0) 
	error("Opening socket");
	
    //Name query to the name server
    char * sname = (char*) malloc(snprintf(NULL, 0, "%s", SNAME) + 1);
    sprintf(sname, "%s", SNAME);      
    char * dag = XgetDAGbyName(sname);

    while(1)
    {
	printf("\nPlease enter the message (0 to exit): ");
	bzero(buffer,2048);
	fgets(buffer,2048,stdin);
	if (buffer[0]=='0'&&strlen(buffer)==2)
	    break;
	    
	//Use Xconnect() with Xsend()
	//Xsend(sock,buffer,strlen(buffer),0);
	
	//Or use Xsendto()

	Xsendto(sock,buffer,strlen(buffer),0,dag,strlen(dag)+1);
	printf("Sent\n");


	//Process reply from server
	dlen =  sizeof(theirDAG);
	n = Xrecvfrom(sock,reply,128,0,theirDAG,&dlen);
	//n = Xrecv(sock,reply,128,0);
	if (n < 0) 
	    error("recvfrom");
	//printf("Received a datagram from:%s\n",theirDAG);
	write(1,reply,n);
	printf("\n");
    }

    Xclose(sock);
    return 0;
}
Example #5
0
TEST(XgetDAGbyName, BadName)
{
	sockaddr_x sa;
	socklen_t len = sizeof(sa);
	EXPECT_EQ(-1, XgetDAGbyName(BAD_NAME, &sa, &len));
}
Example #6
0
TEST(XgetDAGbyName, ShortLen)
{
	sockaddr_x sa;
	socklen_t len = 10;
	EXPECT_EQ(-1, XgetDAGbyName(TEST_NAME, &sa, &len));
}
Example #7
0
TEST(XgetDAGbyName, NullLen)
{
	sockaddr_x sa;
	EXPECT_EQ(-1, XgetDAGbyName(TEST_NAME, &sa, NULL));
}
Example #8
0
TEST(XgetDAGbyName, NullSockaddr)
{
	socklen_t len = sizeof(sockaddr_x);
	EXPECT_EQ(-1, XgetDAGbyName(TEST_NAME, NULL, &len));
}
Example #9
0
TEST(XgetDAGbyName, NoName)
{
	sockaddr_x sa;
	socklen_t len = sizeof(sa);
	EXPECT_EQ(-1, XgetDAGbyName("", &sa, &len));
}
Example #10
0
int main(int argc, char **argv)
{
	int sock, chunkSock;
	int offset;
	sockaddr_x dag;
	socklen_t daglen;
	char sdag[1024];
	char *p;
	const char *fin;
	const char *fout;
	char cmd[512];
	char reply[512];
	int status = 0;

	say ("\n%s (%s): started\n", TITLE, VERSION);

	if (argc != 3)
		die(-1, "usage: cftp <source file> <dest file>\n");

	fin = argv[1];
	fout = argv[2];

    // 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);
	}

	// 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));
	ad = strstr(sdag, "AD:");
	p = strchr(ad, ' ');
	*p = 0;
	hid = p + 1;
	hid = strstr(hid, "HID:");
	p = strchr(hid, ' ');
	*p = 0;

	// send the file request
	sprintf(cmd, "get %s",  fin);
	sendCmd(sock, cmd);

	// get back number of chunks in the file
	getReply(sock, reply, sizeof(reply));

	int count = atoi(&reply[4]);

	if ((chunkSock = Xsocket(AF_XIA, XSOCK_CHUNK, 0)) < 0)
		die(-1, "unable to create chunk socket\n");

	FILE *f = fopen(fout, "w");

	offset = 0;
	while (offset < count) {
		int num = NUM_CHUNKS;
		if (count - offset < num)
			num = count - offset;

		// tell the server we want a list of <num> cids starting at location <offset>
		sprintf(cmd, "block %d:%d", offset, num);
		sendCmd(sock, cmd);

		getReply(sock, reply, sizeof(reply));
		offset += NUM_CHUNKS;

		if (getFileData(chunkSock, f, &reply[4]) < 0) {
			status= -1;
			break;
		}
	}
	
	fclose(f);

	if (status < 0) {
		unlink(fin);
	}

	say("shutting down\n");
	sendCmd(sock, "done");
	Xclose(sock);
	Xclose(chunkSock);
	return status;
}