#include <common.h>
Inheritance diagram for Port:
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 | |
Address * | getRemoteAddr () |
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 |
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. Constructor. Definition at line 261 of file common.cpp. 00261 :sockfd_(0) 00262 { 00263 }
|
|
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 }
|
|
Function to initialize the port Implemented in SendingPort, and ReceivingPort. |
|
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:
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 }
|