00001 #ifndef COMMON_H 00002 #define COMMON_H 00003 00004 #define MAX_HOSTNAME_LENGTH 256 00005 #define MAC_ADDR_LENGTH 6 00006 #define MAX_HEADER_SIZE 256 00007 #define DEFAULT_SEND_PORT 3000 00008 #define DEFAULT_RECV_PORT 4000 00009 #define MAXBUFLENGTH 10000 00010 #define MTU_SIZE 1500 00011 00012 #include <string.h> 00013 #include <sys/socket.h> 00014 #include <sys/types.h> 00015 #include <unistd.h> 00016 #include <netinet/in.h> 00017 #include <netdb.h> 00018 #include <arpa/inet.h> 00019 #include <cstdlib> 00020 #include <ctime> 00021 00022 #ifndef INADDR_NONE 00023 # define INADDR_NONE (-1) 00024 #endif 00025 00026 00028 00032 class PacketHdr{ 00033 00034 public: 00035 PacketHdr(); 00039 inline void init(){memset(info_,0,MAX_HEADER_SIZE );} 00043 short getShortIntegerInfo(int position); 00047 int getIntegerInfo(int position); 00051 inline char* accessInfo(){return info_;} 00055 inline int getSize() { return length_; } 00059 void setIntegerInfo(int a, int position); 00063 void setShortIntegerInfo(short b, int position); 00067 inline void setOctet(unsigned char c, int position){ *(info_+position)= c; length_++;} 00071 inline void setHeaderSize(int len){ length_ =len; } 00075 inline unsigned char getOctet(int position){ return info_[position];} 00076 00077 protected : 00078 unsigned char* info_; 00079 int length_; 00080 }; 00081 00082 00084 00089 class Packet { 00090 00091 public: 00095 static const int DEFAULT_PAYLOAD_SIZE = 512; 00096 Packet(); 00097 Packet(int buffer_length); 00098 int fillPayload(int size, char *inputstream); 00102 inline char* getPayload(){ return payload_;} 00103 void setPayloadSize(int size); 00107 inline int getBufferSize(){return length_;} 00111 inline int getPayloadSize(){ return size_;} 00115 inline int getHeaderSize(){ return header_->getSize();} 00119 inline PacketHdr* accessHeader() { return header_;} 00120 void extractHeader (char * streambuf); 00121 int makePacket ( char* streambuf); 00122 00123 protected: 00124 int size_; 00125 int length_; 00126 char* payload_; 00127 PacketHdr * header_; 00128 }; 00129 00131 00138 class Address 00139 { 00140 00141 public: 00143 Address(); 00145 Address(const char* hostname, short port); 00146 00151 inline bool isSet() { return (hostname_[0] != '\0' && port_ >= 0); } 00155 inline void setPort(const short port){ port_ = port; } 00157 inline short getPort(){ return port_; } 00159 00162 inline void setHostname(const char* hostname) { 00163 if (hostname == NULL) hostname_[0] = '\0'; else strcpy(hostname_, hostname); } 00165 inline char* getHostname() {return hostname_;} 00167 inline unsigned char* getHWAddr() { return macaddr_;} 00168 void setHWAddr( unsigned char* hwaddr); 00169 void setHWAddrFromColonFormat(const char* colon_seperated_macaddr); 00170 char * convertHWAddrToColonFormat(); 00172 Address *clone(); 00176 inline bool isSame(Address* addr) 00177 { 00178 if ( port_== addr->getPort()) return false; 00179 if ( strcmp( hostname_, addr->getHostname() ) == 0 ) return true; 00180 return false; 00181 } 00182 bool isSameMACAddr(Address* addr); 00183 00184 protected: 00185 char hostname_[MAX_HOSTNAME_LENGTH]; 00186 short port_; 00187 char * ipaddr_; 00188 unsigned char macaddr_[MAC_ADDR_LENGTH]; 00189 00190 }; 00191 00193 00204 class Port 00205 { 00206 public: 00208 Port(); 00210 virtual ~Port(){} 00214 virtual void init()=0; 00216 void setAddress(Address *addr); 00218 void setRemoteAddress( Address *daddr); 00220 inline Address *getRemoteAddr(){return &itsaddr_;} 00222 inline void closePort(){close(sockfd_);} 00223 protected: 00225 struct sockaddr* setSockAddress(Address *addr, struct sockaddr_in *address); 00227 void decodeSockAddress ( Address *addr, struct sockaddr_in *address); 00229 inline void setHostname(const char* hostname){ myaddr_.setHostname(hostname);} 00231 inline void setPort(const short port){ myaddr_.setPort(port);} 00233 inline void setRemoteHostname(const char* hostname){itsaddr_.setHostname(hostname);} 00235 inline void setRemotePort(const short port){itsaddr_.setPort(port); } 00237 inline int getSock(){ return sockfd_;} 00238 00239 protected: 00240 Address myaddr_; 00241 Address itsaddr_; 00242 struct sockaddr_in mySockAddress_; 00243 struct sockaddr_in dstSockAddress_; 00244 int sockfd_; 00245 }; 00246 00248 00253 class SendingPort : public Port 00254 { 00255 public: 00256 SendingPort(); 00258 SendingPort(char* hostname, short port); 00260 virtual ~SendingPort(){} 00264 void init(); 00270 void sendPacket(Packet *pkt); 00274 inline void setBroadcast(){bcastflag_ = 1;} 00278 inline void setBroadcastOff(){bcastflag_ = 0;} 00279 00280 protected: 00282 00285 int bcastflag_; 00289 char *sendingbuf_; 00290 }; 00291 00293 00301 class ReceivingPort : public Port 00302 { 00303 public: 00304 ReceivingPort(); 00305 virtual ~ReceivingPort(){} 00306 void init(); 00308 Packet* receivePacket(); 00309 00310 protected: 00314 Packet *pkt_; 00315 char *tmpBuffer_; 00316 }; 00317 00319 00322 class LossyReceivingPort: public ReceivingPort 00323 { 00324 public: 00325 LossyReceivingPort(float lossyratio); 00326 ~LossyReceivingPort(){} 00327 Packet* receivePacket(); 00328 protected: 00329 float loss_ratio_; 00330 }; 00331 00332 #endif 00333 00334