Port Class Reference

Port class abstacts functions of communication interfaces. More...

#include <common.h>

Inheritance diagram for Port:

ReceivingPort SendingPort LossyReceivingPort List of all members.

Public Member Functions

 Port ()
 Constructor.
virtual ~Port ()
 Deconstructor.
virtual void init ()=0
void setAddress (Address *addr)
 set the port's own address
void setRemoteAddress (Address *daddr)
 set the address of the port at the other end of communication link
AddressgetRemoteAddr ()
 get the address of the port at the other end of communication link
void closePort ()
 close the port

Protected Member Functions

sockaddr * setSockAddress (Address *addr, struct sockaddr_in *address)
 cast an Address to socket address format
void decodeSockAddress (Address *addr, struct sockaddr_in *address)
 cast a socket address to normal address format
void setHostname (const char *hostname)
 set hostname of local address
void setPort (const short port)
 set port of local address
void setRemoteHostname (const char *hostname)
 set hostname of remote address
void setRemotePort (const short port)
 set port no of a remote address
int getSock ()
 get the socket file descriptor

Protected Attributes

Address myaddr_
 The default address of mine.
Address itsaddr_
 The default address of the other end of communication link.
sockaddr_in mySockAddress_
 IN UDP. this will be INADDR_ANY, not really my IP addr.
sockaddr_in dstSockAddress_
 for every outgoing packet of a connection, the destination address.
int sockfd_
 socket file descriptor

Detailed Description

Port class abstacts functions of communication interfaces.

Port is an abstract class for the interface to send/receive a packet, whether UDP, TCP Socket, or IP raw socket. The common funcitons for a port are defiend here:

Port is also associated with a pair of address. One Port send to one Address only, no matter the address is unicast or broadcast.

Definition at line 204 of file common.h.


Constructor & Destructor Documentation

Port::Port  ) 
 

Constructor.

Constructor.

Definition at line 261 of file common.cpp.

00261           :sockfd_(0)
00262 {
00263 }


Member Function Documentation

void Port::decodeSockAddress Address addr,
struct sockaddr_in *  address
[protected]
 

cast a socket address to normal address format

Function to interpreate hostname and port from the SocketAddress

Definition at line 323 of file common.cpp.

References Address::setHostname(), and Address::setPort().

Referenced by ReceivingPort::receivePacket().

00324 {
00325   addr->setHostname(inet_ntoa(address->sin_addr));
00326   addr->setPort(ntohs(address->sin_port));  
00327 }

virtual void Port::init  )  [pure virtual]
 

Function to initialize the port

Implemented in SendingPort, and ReceivingPort.

struct sockaddr * Port::setSockAddress Address addr,
struct sockaddr_in *  address
[protected]
 

cast an Address to socket address format

Fill sockaddr_in 'address' structure with information taken from 'addr' and return it cast to a 'struct sockaddr'. It handles following situations:

  • if hostname is given as empty "", then INADDR_ANY is used in return
  • if an IP address is given, then address could be set directly
  • if a hostname is given, call gethostbyname() to find the ip address of the hostname from DNS

Definition at line 285 of file common.cpp.

References Address::getHostname(), and Address::getPort().

Referenced by SendingPort::sendPacket().

00286 {
00287   char *hostname;
00288   int port;
00289   unsigned int tmp;
00290   struct hostent *hp;
00291 
00292   hostname = addr->getHostname();
00293   port = addr->getPort();
00294 
00295   address->sin_family = AF_INET;
00296   address->sin_port   = htons((short)port);
00297       
00298   if (strcmp(hostname, "") == 0) {
00299     address->sin_addr.s_addr = htonl(INADDR_ANY);  
00300   } 
00301   else {
00302     //tmp = inet_addr(hostname);  // If an IP address is given
00303     tmp = inet_aton(hostname, &(address->sin_addr));
00304     //if(tmp != (unsigned long) INADDR_NONE){    
00305     //  address->sin_addr.s_addr = tmp;  
00306     //}
00307     if (tmp == 0) 
00308     {  // if a hostname is passed, call DNS
00309       if ((hp = gethostbyname(hostname)) == NULL) {
00310         herror("gethostbyname");
00311         throw "Error in Resolving hostname!" ;                           
00312       }
00313       memcpy((char *)&address->sin_addr, (char *)hp->h_addr, hp->h_length);
00314     }
00315   }
00316   return (sockaddr*)address;
00317 }


The documentation for this class was generated from the following files:
Generated on Fri Feb 9 12:49:37 2007 for Common_Classes_Projects_ECE544 by  doxygen 1.4.6