int main(){ char ip[50]; generate_ip(ip); printf("%s\n", ip); return 0; }
void run(void *) {//this keeps the program alive //debug_->debug("%u --- counter = %d\n", radio_->id(), counter); if (mode==1) {//if you are not in the network if (counter>=0) {//if the counter is positive counter++; if (counter==4) {//if the counter is 4 then you can come in the network debug_->debug("%u:: i'm going in!!\n", radio_->id()); send_hello_message();//send hello message mode=0;//change your mode, now you are in the network } else if (counter!=-1) {//if counter is not -1 send participation message (it will not be ofcourse) debug_->debug("(%u - %s - %s)sending participation message %i\n",radio_->id(),my_ip,my_host,counter); send_participate_message(); } } else {//else if (counter==-1)//if counter is -1, well.. we fix this in other position doesnot need here.. anyway. { debug_->debug("oops must resolve it!!!\n"); } generate_ip(); } timer_->set_timer<Zeroconf, &Zeroconf::run>( 5000, this, 0 );//recall run function after 5000miliseconds } else if (mode==0) {//if you are in the network... timer_->set_timer<Zeroconf, &Zeroconf::run>( 1000, this, 0 );//recall run function after 5000 miliseconds //here we will add code for searching for services if needed, and advertise new services if any. } }
void handle_participate_message(zeroconf_msg& message) { msg_data m_data; memcpy(&m_data, message.payload(), message.payload_size() ); debug_->debug("receive participate(%u) from (%s)\n",radio_->id(),(char *)message.source()); if (mode==0)//if node is in the network {//if you receive an other message if (!strcmp((char *)message.destination(),my_ip) || !strcmp(m_data.dest_host,my_host)) {//you are the destination //debug_->debug("%s got a message wow!:)(%s)\n",my_ip,message.source_ip); if (!strcmp((char *)message.source(),"0.0.0.0")) //someone is trying to connect with my ip {//if the sender is one whos trying to come in debug_->debug("found an outsider, i must reply to him\n"); set_node_message_id(my_MACC,++msgID); zeroconf_msg reply; memset(&reply,0,sizeof(reply)); reply.set_type(TYPE_LEN * sizeof(char), (Os::Radio::block_data_t *) &("participate")); reply.set_source(IP_LEN * sizeof(char),(Os::Radio::block_data_t *) &my_ip); reply.set_destination(IP_LEN * sizeof(char),(Os::Radio::block_data_t *) &("ANY")); reply.set_source_mac(MAC_LEN * sizeof(char),(Os::Radio::block_data_t *) &my_MACC ); reply.set_msg_id(msgID); msg_data reply_data; strcpy(reply_data.source_host,my_host); reply.set_payload(sizeof(msg_data),(Os::Radio::block_data_t *) &reply_data); radio_->send( Os::Radio::BROADCAST_ADDRESS, sizeof(zeroconf_msg), (Os::Radio::block_data_t *)&reply); } } else {//else forward the message //strcpy(message.not_ip,my_ip); radio_->send( Os::Radio::BROADCAST_ADDRESS, sizeof(zeroconf_msg), (Os::Radio::block_data_t *)&message); } } else if (mode == 1) {//if node is not in the network debug_->debug("eimai o 0:m_data.source_host=%s\n",m_data.source_host); if (!strcmp((char *)message.source(),my_ip)) {//if you receive message from someone with your ip counter=0; debug_->debug("(%u)WOW someone has my ip.... i must change it?\n",radio_->id()); generate_ip();//generate new ip } if (!strcmp(m_data.source_host,my_host)) {//if you receive message from someone with your hostname debug_->debug("%u === my_host = %s & source_host = %s \n", radio_->id(), my_host, m_data.source_host); counter=0; //generate_hostname();//generate new hostname debug_->debug("(%u)WOW someone has my hostname.... i must change it?\n",radio_->id()); resolve_hostname(my_host, trial_num); trial_num = 1; } //someone is claiming the hostname i want at the same time if (!strcmp(m_data.dest_host,my_host) && strcmp((char *)message.source_mac(),my_MACC)) { counter=0; resolve_hostname(my_host, trial_num); } if (!strcmp((char *)message.destination(),my_ip) && strcmp((char *)message.source_mac(),my_MACC)) { counter=0; generate_ip(); } } }