Main Page | Class Hierarchy | Class List | File List | Class Members

common.h

00001 #ifndef COMMON_H
00002 #define COMMON_H
00003 
00004 #define MAX_HOSTNAME_LENGTH 256
00005 #define MAC_ADDR_LENGTH 6
00006 
00007 #define DEFAULT_SEND_PORT 3000
00008 #define DEFAULT_RECV_PORT 4000
00009 #define MAXBUFLENGTH 10000
00010 
00011 #include <string.h>
00012 #include <sys/socket.h>
00013 #include <sys/types.h>
00014 #include <unistd.h>
00015 #include <netinet/in.h>
00016 #include <netdb.h>
00017 #include <arpa/inet.h>
00018 
00019 #ifndef INADDR_NONE
00020 #  define INADDR_NONE (-1)
00021 #endif
00022 
00024 
00029 class Packet {
00030  
00031 public:
00035   static const int DEFAULT_PAYLOAD_SIZE = 512;
00036   Packet();
00037   Packet(int buffer_length);
00038   int fillPayload(int size, char *inputstream);  
00042   inline char* getPayload(){ return payload_;} 
00043   void setPayloadSize(int size);
00047   inline int getBufferSize(){return length_;}
00051   inline int getPayloadSize(){ return size_;}
00052  
00053  
00054 private:               
00055       int size_;  
00056       int length_; 
00057       char* payload_; 
00058 
00059 };
00060 
00062 
00069 class Address
00070 {
00071   
00072  public:
00074   Address();
00076   Address(const char* hostname, short port);
00077    
00082   inline bool isSet() { return (hostname_[0] != '\0' && port_ >= 0); }
00086   inline void setPort(const short port){ port_ = port; }
00088   inline short getPort(){ return port_; }
00090 
00093   inline void setHostname(const char* hostname) { 
00094     if (hostname == NULL) hostname_[0] = '\0'; else strcpy(hostname_, hostname); }
00096   inline char* getHostname() {return hostname_;} 
00098   inline unsigned char* getHWAddr() { return macaddr_;} 
00099   void setHWAddr(unsigned char* hwaddr); 
00100   void setHWAddrFromColonFormat(const char* colon_seperated_macaddr);
00101   char * convertHWAddrToColonFormat();
00103   Address *clone();
00107   inline bool isSame(Address* addr)
00108   {
00109     if ( port_==  addr->getPort()) return false;
00110     if ( strcmp( hostname_, addr->getHostname() ) == 0 ) return true;
00111     return false;
00112   } 
00113   bool isSameMACAddr(Address* addr);
00114 
00115 private:
00116   char   hostname_[MAX_HOSTNAME_LENGTH]; 
00117   short port_;  
00118   char * ipaddr_;  
00119   unsigned char   macaddr_[MAC_ADDR_LENGTH]; 
00120   
00121 };
00122 
00124 
00135 class Port
00136 {
00137  public:
00139   Port();
00141   virtual ~Port(){}
00145   virtual void init()=0;
00147   void setAddress(Address *addr);
00149   void setRemoteAddress( Address *daddr);
00151   inline Address *getRemoteAddr(){return &itsaddr_;}
00153   inline void closePort(){close(sockfd_);}
00154  protected:
00156   struct sockaddr* setSockAddress(Address *addr, struct sockaddr_in *address);
00158   void decodeSockAddress ( Address *addr, struct sockaddr_in *address);
00160   inline void setHostname(const char* hostname){ myaddr_.setHostname(hostname);}
00162   inline void setPort(const short port){ myaddr_.setPort(port);}
00164   inline void setRemoteHostname(const char* hostname){itsaddr_.setHostname(hostname);}
00166   inline void setRemotePort(const short port){itsaddr_.setPort(port); }
00168   inline int getSock(){ return sockfd_;}
00169  
00170  protected:
00171   Address myaddr_;  
00172   Address itsaddr_; 
00173   struct sockaddr_in mySockAddress_;  
00174   struct sockaddr_in dstSockAddress_;  
00175   int sockfd_; 
00176 };
00177 
00179 
00184 class SendingPort : public Port
00185 {
00186  public:
00187   SendingPort();
00189   SendingPort(char* hostname, short port);
00191   virtual ~SendingPort(){}
00195   void init();
00201   void sendPacket(Packet *pkt);
00205   inline void setBroadcast(){bcastflag_ = 1;}
00209   inline void setBroadcastOff(){bcastflag_ = 0;}
00210 
00211  protected:
00213 
00216   int bcastflag_; 
00217 };
00218 
00220 
00228 class ReceivingPort : public Port
00229 {
00230  public:
00231   ReceivingPort();
00232   virtual ~ReceivingPort(){}
00233   void init();
00235   Packet* receivePacket(); 
00236   
00237  protected:
00241    Packet *pkt_;   
00242    char *tmpBuffer_;  
00243 };
00244 
00245 
00246 #endif
00247 
00248 

Generated on Thu Jan 26 17:08:01 2006 for Common_Classes_Projects_ECE544 by  doxygen 1.4.2