Example #1
0
File: oapi.c Project: myroman/alpha
// Argument msg is not a null-terminated string
int msg_send(int callbackFd, char* destIpAddr, int destPort, const char* msg, int forceRediscovery) {
	if (callbackFd <= 0) {
		debug("API: callbackFd should be positive");
		return -1;
	}
	if (destIpAddr == NULL) {
		debug("API: destIpAddr can't be NULL");
		return -1;
	}
	if (destPort <= 0) {
		debug("API: destPort should be positive");
		return -1;
	}
	if (msg == NULL) {
		debug("API: msg can't be NULL");
		return -1;
	}

	//Building structure
	PayloadHdr ph;

	bzero(&ph, sizeof(ph));
	if (destPort == SRV_PORT_NUMBER) {
		ph.msgType = CLIENT_MSG_TYPE;
	} else {
		ph.msgType = SRV_MSG_TYPE;
	}
	ph.forceRediscovery = forceRediscovery;		
	if (strcmp(destIpAddr, "loc") == 0) {
		// for ODR this message means "Destination IP = local"
		ph.destIp = LOCAL_INET_IP; // inet_addr("0.1.2.3"); 
	} else {
		ph.destIp = inet_addr(destIpAddr);
	}	
	ph.destPort = destPort;
	//Initially
	int msgSpace = strlen(msg) + 1;
	bzero(ph.msg, msgSpace);
	strcpy(ph.msg, msg);

	printPayloadContents(&ph);

	// Packing and sending
	uint32_t bufLen = 0;
	void* packedBuf = packPayload(&ph, &bufLen);	
	
	SockAddrUn addr = createSockAddrUn(ODR_UNIX_PATH);
	printf("Sending packed buffer, length=%u...", bufLen);	
	int n;
	if ((n = sendto(callbackFd, packedBuf, bufLen, 0, (SA *)&addr, sizeof(addr))) == -1) {
		printFailed();
	} else{
		printOK();
		PayloadHdr pp;
		unpackPayload(packedBuf, &pp);
		printPayloadContents(&pp);
	}
	free(packedBuf);
	return 0;
}
Example #2
0
void testInsert(void) {
    printf("Running insert test\n");
    Heap heap = setupUnorderedHeap();
    makeMaxHeap(heap);
    deleteFromHeap(heap, 4);
    insertIntoHeap(heap, newNode(14));
    if (!isMaxHeap(heap) || heap->numNodes != 10) {
        printFailed("Not a max heap after deleting 4 and inserting 14");
        return;
    }

    deleteFromHeap(heap, 1);
    insertIntoHeap(heap, newNode(5));
    if (!isMaxHeap(heap) || heap->numNodes != 10) {
        printFailed("Not a max heap after reinserting 4");
        return;
    }
    printPassed();
}
Example #3
0
void testDelete(void) {
    printf("Running delete test\n");
    Heap heap = setupUnorderedHeap();
    makeMaxHeap(heap);
    deleteFromHeap(heap, 4);
    if (!isMaxHeap(heap) || heap->numNodes != 9) {
        printFailed("Not a max heap after deleting 4");
    }
    printPassed();
}
Example #4
0
void testNewHeap(void) {
    printf("Running newHeap test\n");
    Node nodes[10] = { newNode(1), newNode(2), newNode(3), newNode(4), newNode(5), newNode(6), newNode(7), newNode(8), newNode(9), newNode(10) };
    Heap heap = newHeap(10, nodes);
    Node *sortedNodes = getNodes(heap);
    for(int i = 0; i<10; i++) {
        int expected = i + 1;
        if(nodeAt(sortedNodes, i)->data != expected) {
            char message[50];
            sprintf(message, "expected data=%d, got data=%d", expected, (*(sortedNodes + i))->data);
            printFailed(message);
            return;
        }
    }
    printPassed();
}
Example #5
0
File: oapi.c Project: myroman/alpha
int msg_recv(int sockfd, char* msg, char* srcIpAddr, int* srcPort) {
	
	fd_set set;
	int maxfd;
	struct timeval tv;
	PayloadHdr ph;
	char* buf = malloc(ETHFR_MAXDATA_LEN);
	for(;;){
		tv.tv_sec = 10;
		tv.tv_usec = 0;
		FD_ZERO(&set);
		FD_SET(sockfd, &set);
		maxfd = sockfd+1;
		select(maxfd, &set, NULL, NULL, &tv);
		if(FD_ISSET(sockfd, &set)){
			// let's choose some smaller value than 1500 bytes.
			bzero(buf, ETHFR_MAXDATA_LEN);
			printf("Waiting for request...");
			int length = recvfrom(sockfd, buf, ETHFR_MAXDATA_LEN, 0, NULL, NULL);
			if (length == -1) { 
				printFailed();
				free(buf);
				return length;
			}
			printOK();
			debug("Got packed payload, length = %d", length);
			
			unpackPayload(buf, &ph);
			strcpy(srcIpAddr, printIPHuman(ph.srcIp));
			*srcPort = ph.srcPort;
			strcpy(msg, ph.msg);

			free(buf);
			return length;
		}
		free(buf);
		debug("Nothing read. Timeout.");
		return -1;//timeout
	}
}
Example #6
0
void testFindLargestChild() {
    printf("Running test: Find largest child\n");
    Heap heap = setupUnorderedHeap();
    int node;
    int expected;
    int actual;
    char buffer[59];

    // Test value 10
    node = 1;
    expected = NONE;
    actual = findLargestChild(heap,node);
    if (actual != expected) {
        sprintf(buffer, "findLargestChild: expected=%d, got=%d for node=%d",expected, actual, node);
        printFailed(buffer);
        return;
    }

    // Test value 7
    node = 3;
    expected = 7;
    actual = findLargestChild(heap,node);
    if (actual != expected) {
        sprintf(buffer, "findLargestChild: expected=%d, got=%d for node=%d",expected, actual, node);
        printFailed(buffer);
        return;
    }

    // Test value 9
    node = 7;
    expected = NONE;
    actual = findLargestChild(heap,node);
    if (actual != expected) {
        sprintf(buffer, "findLargestChild: expected=%d, got=%d for node=%d",expected, actual, node);
        printFailed(buffer);
        return;
    }

    // Test value 1
    node = 2;
    expected = 4;
    actual = findLargestChild(heap,node);
    if (actual != expected) {
        sprintf(buffer, "findLargestChild: expected=%d, got=%d for node=%d",expected, actual, node);
        printFailed(buffer);
        return;
    }

    // Test value 8
    node = 10;
    expected = NONE;
    actual = findLargestChild(heap,node);
    if (actual != expected) {
        sprintf(buffer, "findLargestChild: expected=%d, got=%d for node=%d",expected, actual, node);
        printFailed(buffer);
        return;
    }

    // Test value 8
    node = 9;
    expected = NONE;
    actual = findLargestChild(heap,node);
    if (actual != expected) {
        sprintf(buffer, "findLargestChild: expected=%d, got=%d for node=%d",expected, actual, node);
        printFailed(buffer);
        return;
    }

    // Test value 8
    node = 6;
    expected = NONE;
    actual = findLargestChild(heap,node);
    if (actual != expected) {
        sprintf(buffer, "findLargestChild: expected=%d, got=%d for node=%d",expected, actual, node);
        printFailed(buffer);
        return;
    }
    printPassed();
}
Example #7
0
File: tour.c Project: myroman/beta
void dispatch(int rtSocket, int pgSocket, int pfpSocket) {
	fd_set set;
	int maxfd;
	struct timeval tv;	
	void* buf = malloc(MAXLINE);
	SockAddrIn senderAddr;	
	int addrLen = sizeof(senderAddr),
	res = 0;

	debug("Gonna listen to rtsocket: %d", rtSocket);
	for(;;){
		if (endOfTour == 0) {
			tv.tv_sec = 20;
		} 
		else {
			tv.tv_sec = 5;			
		}
		
		tv.tv_usec = 0;
		FD_ZERO(&set);
		FD_SET(rtSocket, &set);
		FD_SET(pgSocket, &set);
		maxfd = max(rtSocket, pgSocket);
		if (mcastRecvSd > 0) {
			FD_SET(mcastRecvSd, &set);
			maxfd = max(maxfd, mcastRecvSd);
		}
		res = select(maxfd + 1, &set, NULL, NULL, &tv);
		if (res == 0) {
			if (endOfTour == 1) {
				printf("Auf Wiedersehen!\n");
				return;
			}
			debug("Nothing read. Timeout.");				
			continue;
		}
		
		// if received a rt
		if(FD_ISSET(rtSocket, &set)){
			// let's choose some smaller value than 1500 bytes.
			bzero(buf, MAXLINE);
			bzero(&senderAddr, addrLen);
		 	addrLen = sizeof(senderAddr);
			int length = recvfrom(rtSocket, buf, MAXLINE, 0, (SA* )&senderAddr, &addrLen);
			if (length == -1) { 
				printFailed();								
			}
			char* ipr = inet_ntoa(senderAddr.sin_addr);			
			debug("Got rt packet from %s, length = %d", ipr, length);									
			processRtResponse(buf, length, senderAddr, rtSocket);			
		}
		// if received a ping
		if (FD_ISSET(pgSocket, &set)){
			bzero(buf, MAXLINE);
			bzero(&senderAddr, addrLen);
		 	addrLen = sizeof(senderAddr);
			int length = recvfrom(pgSocket, buf, MAXLINE, 0, (SA* )&senderAddr, &addrLen);
			if (length == -1) { 
				printFailed();								
			}

			processPgResponse(buf, length, senderAddr, addrLen);
		}

		if (mcastRecvSd > 0 && FD_ISSET(mcastRecvSd, &set)){
			bzero(buf, MAXLINE);
			bzero(&senderAddr, addrLen);
		 	addrLen = sizeof(senderAddr);
			int length = recvfrom(mcastRecvSd, buf, MAXLINE, 0, (SA* )&senderAddr, &addrLen);
			if (length == -1) { 
				printFailed();								
			}
			processMulticastRecv(buf, length, senderAddr, addrLen);

		}	
	}
	free(buf);
}
Example #8
0
File: tour.c Project: myroman/beta
int sendRtMsgIntermediate(int sd, void * buf, ssize_t len){
	
	int status, *ip_flags;
	struct ip iphdr;
	uint8_t *data, *packet;
	struct icmp icmphdr;
	struct sockaddr_in *ipv4, sin;	
	void *tmp;

	// Allocate memory for various arrays.
	data = allocate_ustrmem (IP_MAXPACKET);
	packet = allocate_ustrmem (IP_MAXPACKET);
	ip_flags = allocate_intmem (4);
	//Unpack the tourdata 
	struct tourdata * unpack = (struct tourdata *)buf;

	subscribeToMulticast(unpack);

	//since we work and change index
	unpack->index = ntohl(unpack->index);
	printf("Index:%d, nodes in tour:%d\n", unpack->index, ntohl(unpack->nodes_in_tour));
	if((unpack->index + 1) == ntohl(unpack->nodes_in_tour)){
		//Tour has ended. Send multicast here to everyone and return actually 
		//you dont send anyting from here
		endOfTour = 1;		

		free (data);
		free (packet);
		free (ip_flags);
		return 0;		
	}			

	int itemsArrSizeBytes = ntohl(unpack->nodes_in_tour) * sizeof(struct in_addr);
	int datalen = sizeof(struct tourdata) + itemsArrSizeBytes;
	
	// IPv4 header

	// IPv4 header length (4 bits): Number of 32-bit words in header = 5
	iphdr.ip_hl = IP4_HDRLEN / sizeof (uint32_t);	
	iphdr.ip_v = 4;// Internet Protocol version (4 bits): IPv4	
	iphdr.ip_tos = 0;// Type of service (8 bits)	
	iphdr.ip_len = htons (IP4_HDRLEN + ICMP_HDRLEN + datalen);// Total length of datagram (16 bits): IP header + UDP header + datalen	
	iphdr.ip_id = htons (MY_IP_ID);// ID sequence number (16 bits): unused, since single datagram
debug("hey");
	// Flags, and Fragmentation offset (3, 13 bits): 0 since single datagram
	ip_flags[0] = 0;  
	ip_flags[1] = 0;// Do not fragment flag (1 bit)  
	ip_flags[2] = 0;// More fragments following flag (1 bit)  
	ip_flags[3] = 0;// Fragmentation offset (13 bits)

	iphdr.ip_off = htons ((ip_flags[0] << 15) + (ip_flags[1] << 14) + (ip_flags[2] << 13) +  ip_flags[3]);	
	iphdr.ip_ttl = 255;// Time-to-Live (8 bits): default to maximum value	
	iphdr.ip_p = RT_PROTO;// Transport layer protocol (8 bits): 1 for ICMP
debug("hey");
	void * ptr = buf + sizeof(struct tourdata);
	ptr = ptr + (sizeof(struct in_addr) * unpack->index); //current node-item
	iphdr.ip_src = *(struct in_addr *)ptr;
	
	unpack->index++;
	ptr = ptr + sizeof(struct in_addr); //advance the pointer to the next in_addr_t
	iphdr.ip_dst  = *(struct in_addr *)ptr;
	debug("hey. Src IP :%s\n", printIPHuman(iphdr.ip_src.s_addr));
	debug("hey. Dest IP :%s\n", printIPHuman(iphdr.ip_dst.s_addr));
	// IPv4 header checksum (16 bits): set to 0 when calculating checksum
	iphdr.ip_sum = 0;
	iphdr.ip_sum = checksum ((uint16_t *) &iphdr, IP4_HDRLEN);

	// ICMP header	
	icmphdr.icmp_type = 0;// Message Type (8 bits): echo request	
	icmphdr.icmp_code = 0;// Message Code (8 bits): echo request	
	icmphdr.icmp_id = htons (RT_ICMPID);// Identifier (16 bits): usually pid of sending process - pick a number	
	icmphdr.icmp_seq = htons (0);// Sequence Number (16 bits): starts at 0	
	icmphdr.icmp_cksum = 0;
	icmphdr.icmp_cksum = icmp4_checksum(icmphdr, data, datalen);

	// Copy tour packet to the new ICMP data payload
	unpack->index = htonl(unpack->index);
	memcpy(data, unpack, sizeof(struct tourdata));

	memcpy((void*)(data + sizeof(struct tourdata)), (void*)(buf + sizeof(struct tourdata)), itemsArrSizeBytes);	
debug("hey itemarrsizebytes %d", itemsArrSizeBytes);
	// Prepare packet.
	// First part is an IPv4 header.
	memcpy (packet, &iphdr, IP4_HDRLEN);
	// Next part of packet is upper layer protocol header.
debug("hey");
	memcpy ((packet + IP4_HDRLEN), &icmphdr, ICMP_HDRLEN);
	// Finally, add the ICMP data.
	debug("hey");
	memcpy (packet + IP4_HDRLEN + ICMP_HDRLEN, data, datalen);
	debug("hey, datalen=%d", datalen);
	memset (&sin, 0, sizeof (struct sockaddr_in));
	sin.sin_family = AF_INET;
	sin.sin_addr.s_addr = iphdr.ip_dst.s_addr;  
	debug("hey. Dest IP :%s\n", printIPHuman(ntohl(iphdr.ip_dst.s_addr)));
	printf("Gonna send...");
	// Send packet.
	if (sendto (sd, packet, IP4_HDRLEN + ICMP_HDRLEN + datalen, 0, (struct sockaddr *) &sin, sizeof (struct sockaddr)) < 0)  {
		printFailed();
		return 1;		
	}
	printOK();
	
	// Free allocated memory.
	free (data);
	free (packet);
	free (ip_flags);

	return 0;
}