#ifndef __DDCA_H__ #define __DDCA_H__ /*standard headers*/ #include #include #include #include #include #include /*custom made headers*/ #include #include #include /*networking headers*/ #define __USE_BSD /* use bsd'ish ip header */ #include /* these headers are for a Linux system, but */ #include /* the names on other systems are easy to guess.. */ #include #include #define __FAVOR_BSD /* use bsd'ish tcp header */ #include #include #include #include #include //#include //_syscall2(int, add_gateway_node, unsigned int, arg1, int, arg2); //axg----start---- #define JOIN_SIGNAL SIGRTMIN // signal for joint timer #define ADOPT_SIGNAL SIGRTMAX // signal for adopt timer #define HELLO_SIGNAL SIGRTMIN + 1 // signal for hello timer const int JOIN_EXPIRE_SEC = 2L; // to add the amount in sec for timer expiration const int JOIN_EXPIRE_NSEC = 0L; // amount in usec for timer expiration const int ADOPT_EXPIRE_SEC = 3L; // for adopt timer const int ADOPT_EXPIRE_NSEC = 0L; // in usec for adopt timer const int HELLO_EXPIRE_SEC = 5L; // for hello timer const int HELLO_EXPIRE_NSEC = 0L; const int SLEEP_TIME = 10; // specifies the time to go to sleep const int ALPHA_THRESHOLD = 1; // have to redefine later, this is a % const int T_THRESHOLD = 5; // have to redefine later //axg----end---- struct ipheader { unsigned char ip_hl:4, ip_v:4; /* this means that each member is 4 bits */ unsigned char ip_tos; unsigned short int ip_len; unsigned short int ip_id; unsigned short int ip_off; unsigned char ip_ttl; unsigned char ip_p; unsigned short int ip_sum; unsigned int ip_src; unsigned int ip_dst; }; /* total ip header length: 20 bytes (=160 bits) */ struct icmpheader { unsigned char icmp_type; unsigned char icmp_code; unsigned short int icmp_cksum; /* The following data structures are ICMP type specific */ unsigned short int icmp_id; unsigned short int icmp_seq; }; /* total icmp header length: 8 bytes (=64 bits) */ struct udpheader { unsigned short int uh_sport; unsigned short int uh_dport; unsigned short int uh_len; unsigned short int uh_check; }; /* total udp header length: 8 bytes (=64 bits) */ struct tcpheader { unsigned short int th_sport; unsigned short int th_dport; unsigned int th_seq; unsigned int th_ack; unsigned char th_x2:4, th_off:4; unsigned char th_flags; unsigned short int th_win; unsigned short int th_sum; unsigned short int th_urp; }; /* total tcp header length: 20 bytes (=160 bits) */ //the ddca header struct ddca_header { unsigned char ver; //version unsigned char proto; //protocol, actually ddca or hybrid routing? unsigned char ttl; unsigned char reserved;//dont know what to put here so better pad unsigned int seq; //sequence number unsigned int nid; unsigned int cid; unsigned long csum; //checksum unsigned int dest_nid; //added on 16 July unsigned int dest_cid; }; //contains information only for cluster formation struct ddca_body { unsigned long msg_type; unsigned int nid; unsigned long alpha; unsigned long t; }; class DDCA { private: int sockfd; unsigned int ip_addr; int clusterSize; char datagram_snd[4096];//use only to send data char datagram_rcv[4096];//use only to rcv data struct ip * iph_rcv; struct ip * iph_snd; unsigned int myNid; //our ip address unsigned int myCid; //parents ip address unsigned int rcvNid; //he who sent it's ip address unsigned int rcvCid; //he who sent's parent int joinFlag; //our join flag int retryFlag; //maybe we want to retry int adoptFlag; //our adopt flag //to manage the selection of the best parent BOOL stateChangeFlag; struct ddca_body old_parent; //begin -- axg //STATE myState; // my present state NODE_STATUS nodeStatus; timer_t joinTimer, adoptTimer, helloTimer; // timers struct sigevent joinEvent, adoptEvent, helloEvent; // events associated struct itimerspec joinSpec, adoptSpec, helloSpec; // structure to initialize struct sigaction sa; // used for registering handlers sigset_t set; // set of signals siginfo_t info; // structure with info about the timers //end -- axg //we need a router to communicate with //it is the router to which all our packets marked as routing packets //go to finally class ROUTER *router; public: int alpha, t; STATE myState; // making it public to access in timers DDCA() { retryFlag = UNSET; joinFlag = UNSET; adoptFlag = UNSET; clusterSize = -1; alpha = 0; t = 0; myState = INACTIVE; //some initialization stuff right here sockfd = -1; // cout <<"initialized all stuff in constructor" << endl; stateChangeFlag = FALSE; } //initialization stuff void initDDCA(); void setRouter(class ROUTER *rt) { router = rt; } //timer management void initTimer (TIMER); void setTimer (TIMER); void cancelTimer (TIMER); void deleteTimer (TIMER); //RAW sockets etc void initRAW(); //void buildPacket(struct PKT); //DDCA core functions unsigned long joinCluster(); //performs actions required to join best feasible cluster void unicastMsg(char *msg, int size, int ip); //unicast message to some guy void broadcastMsg(char *msg, int size); //broadcast a message to whosoever can listen to it void broadcastMsg_u(char *msg, int size, unsigned int to); //broadcast a message to whosoever can listen to it void broadcastMsgRT(char *msg, int size); //broadcast a message to whosoever can listen to it char * recvPacket(char *buff); char * getPacketFrmDDCA() { return datagram_rcv; } //void buildPacket(char *msg, int size); char *buildDDCAPacket(int size); //state management functions //functions to maintain the connection (socket stuff) void enterInactive(); void parentState (EVENT); void unclusteredState (EVENT); void childState (EVENT); void orphanState (EVENT); // function to send HELLO messages int send_hello (); // functions to change the status of node from node -> gw and gw -> node int setGateway (); int unsetGateway (); //other essential stuff int isClusterConnected(); //do we still have reachability to any node in the cluster unsigned long selectBestParent(struct ddca_body *new_parent); int isParentReachable(); //if parent not reachable but others are, we have a cluster partition and not cluster disconnection int ddcaMain(); //main ddca loop, goes around in infinite loop waiting for events to occur // void cleanSelect (sigset_t); // trying to aviod goto void initThreshold (); // initialize the key //for test purposes int limitedBcast(); //general purpose functions unsigned int getNid(){return myNid;} unsigned int getCid(){return myCid;} void add_to_cluster() {clusterSize++;} void delete_from_cluster() {clusterSize--;} }; #endif /*__DDCA_H__*/