Exemplo n.º 1
0
int main(void)
{
    unsigned char buffer[1024]; // buffer per ricezione
    unsigned long ip_address;
    unsigned short port_number;
    unsigned int num; // variabile per numero da generare
    int n;
    struct in_addr add;
    char *ip_address_string;

// inizializzazione socket con porta UDP numero 23365
    if (UDP_init(23365) < 0)
    {
        printf("Errore inizializzazione socket!\r\n");
        return -1;
    }
    printf("Servizio attivo…\r\n");
    while (1)
    {
        if ((n = UDP_receive( &ip_address, &port_number, buffer, sizeof(buffer))) > 0)
        {
            // ricezione di un datagram e verifica del messaggio
            buffer[n] = '\0'; // terminatore di stringa
            if (strcmp((char*)buffer, "REQ") == 0)
            {
                // richiesta di generazione di un nuovo numero
                num = sequence();
                UDP_send(ip_address, port_number, (void*)&num, sizeof(unsigned int));
                printf("…inviato numero %u.\r\n", num);
                //soluzione

                add.s_addr=htonl( ip_address );

                ip_address_string=inet_ntoa(add);
                printf("... all'indirizzo %s \r\n", ip_address_string);
            }
        }
    }
    UDP_close();
    return 0;
}
Exemplo n.º 2
0
int EventListen_local(char * bridgeaddr, char * bridgeport, char *resultbuffer) {
	//unsigned char bridgeaddr[20], gatewayaddr[20], bridgeport[20];
	// function of the code is not cut in stone.
	//unsigned char *dataPtr;
	int ports[MAX_VPS], fd_max, running = 1, len;
	fd_set fdset;
	//ports[1] = 10000;
	//ports[2] = 9009;
	//char * ipaddr_s, *ipport_s;
	char remote_ip[INET6_ADDRSTRLEN];
	//gvps_id_to_ip(device_id, gatewayaddr, bridgeaddr, bridgeport);
	//ipaddr_s = getIPADRformVPS(device_id);
	//ipport_s = getIPPortformVPS(device_id);

	ports[1] = atoi(bridgeport);

	printf("Starting udplisten on %d\n", ports[1]);
	printf("Expecting something from IP: %s\n", bridgeaddr);

	fd_max = UDP_init( ports, &fdset);

	while (running) {
		len = get_ip_UDP_listen(&fdset, fd_max, (unsigned char *) resultbuffer, remote_ip);

		if (!(unsigned char) strcmp(remote_ip, bridgeaddr)) {
			printf("we got %s from right IP: %s %s\n", resultbuffer, remote_ip,
					bridgeport);

			return len;
		} else {
			printf("we got %s from wrong IP: %s %s\n", resultbuffer, remote_ip,
					bridgeport);
		}
	}
	return 0;
}
Exemplo n.º 3
0
int main_imanager_reply(void *request){
	int i, j;
	void *buf;
//	int index;
	int sockfd;	
	int *node;
	unsigned int src_node;
	unsigned int sm_num = 0;
	unsigned int offset;
	unsigned int *send_node;
	struct sm_header *now;
	struct sockaddr_in server_addr;
	src_node = ((struct request_header*)request)->src_node;
	buf = mem_malloc(sizeof(struct imain_reply) + g_group.sm_table->item_num*200);
	((struct imain_reply*)buf)->req.msg_type = MSG_IMAIN_REPLY;
	((struct imain_reply*)buf)->req.seq_number = ((struct request_header*)request)->src_seq_number;
	offset = sizeof(struct imain_reply);
	for(i=0; i<g_group.sm_table->table_size; i++){
		now = (struct sm_header*)g_group.sm_table->row[i];
		while(now!=NULL){
			sm_num++;
			//max 200 byte of one mutex 24+32+4*32 = 184
			((struct main_sm_info*)(buf+offset))->hash_id = now->hash.id;
			((struct main_sm_info*)(buf+offset))->name_len = strlen(now->hash.name);
			((struct main_sm_info*)(buf+offset))->home_node = now->home_node;
			((struct main_sm_info*)(buf+offset))->size = now->size;
			((struct main_sm_info*)(buf+offset))->count = now->count;
			((struct main_sm_info*)(buf+offset))->users_count = now->users->use;
			offset += sizeof(struct main_sm_info);
			memcpy(buf+offset, now->hash.name, strlen(now->hash.name));
			offset += strlen(now->hash.name);
			//node id in the table
			for(j=0;j<now->users->table_size;j++){
				node = now->users->row[j];
				if(*node != -1){
					*((unsigned int*)(buf+offset)) = *node;
					offset += sizeof(unsigned int);
				}			
			}
			
			now = (struct sm_header*)now->hash.next;
		}
	}
	((struct imain_reply*)buf)->sm_num = sm_num;
	//send mutex info & wait new mutex ready
	sendRecv(src_node, buf, offset, buf, sizeof(struct request_header));
	g_group.coordinator.main_id = ((struct request_header*)request)->src_node;
	//send to other node the
	((struct new_manager_req*)buf)->req.msg_type = MSG_NEWMAIN_MANAGER;
	((struct new_manager_req*)buf)->new_manager = src_node;
	for(i=0;i<g_group.node_table->table_size;i++){
		send_node = tableGetRow(g_group.node_table, i);
		if(*send_node != -1 && *send_node != g_group.node_id && *send_node != src_node)
			sendTo(*send_node, buf, sizeof(struct new_manager_req));
	}
	//send to demon
	((struct main_s2d_req*)buf)->msg_type = MSG_NEWMAIN_MANAGER;
	((struct main_s2d_req*)buf)->app_len = strlen(g_group.app_name);
	((struct main_s2d_req*)buf)->group_len = strlen(g_group.group_name);
	((struct main_s2d_req*)buf)->ip = ((struct node*)g_group.node_table->row[src_node])->ip;
	((struct main_s2d_req*)buf)->port = ((struct node*)g_group.node_table->row[src_node])->port;	
	((struct main_s2d_req*)buf)->id = src_node;
	memcpy( buf+sizeof(struct main_s2d_req), g_group.app_name, ((struct main_s2d_req*)buf)->app_len);
	memcpy( buf+sizeof(struct main_s2d_req)+strlen(g_group.app_name), g_group.group_name, ((struct main_s2d_req*)buf)->group_len);   
	bzero(&server_addr, sizeof(server_addr));
	sockfd = UDP_init(g_system_conf.broadcast_ip, g_system_conf.demon_port, &server_addr);
	UDP_setBroadcast(sockfd);
	UDP_setTTL(sockfd, 1);
	sendto(sockfd, buf, sizeof(struct main_s2d_req)+((struct main_s2d_req*)buf)->app_len+((struct main_s2d_req*)buf)->group_len,
		0, (struct sockaddr*)&server_addr, sizeof(server_addr));

	mem_free(buf);
	return 1;	

}