#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 214 of file common.h.
|
Constructor. Constructor. Definition at line 263 of file common.cpp. 00263 :sockfd_(0) 00264 { 00265 }
|
|
cast a socket address to normal address format Function to interpreate hostname and port from the SocketAddress Definition at line 325 of file common.cpp. Referenced by ReceivingPort::receivePacket(). 00326 { 00327 addr->setHostname(inet_ntoa(address->sin_addr)); 00328 addr->setPort(ntohs(address->sin_port)); 00329 }
|
|
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 287 of file common.cpp. References Address::getHostname(), and Address::getPort(). Referenced by ReceivingPort::init(), SendingPort::init(), and SendingPort::sendPacket(). 00288 { 00289 char *hostname; 00290 int port; 00291 unsigned int tmp; 00292 struct hostent *hp; 00293 00294 hostname = addr->getHostname(); 00295 port = addr->getPort(); 00296 00297 address->sin_family = AF_INET; 00298 address->sin_port = htons((short)port); 00299 00300 if (strcmp(hostname, "") == 0) { 00301 address->sin_addr.s_addr = htonl(INADDR_ANY); 00302 } 00303 else { 00304 //tmp = inet_addr(hostname); // If an IP address is given 00305 tmp = inet_aton(hostname, &(address->sin_addr)); 00306 //if(tmp != (unsigned long) INADDR_NONE){ 00307 // address->sin_addr.s_addr = tmp; 00308 //} 00309 if (tmp == 0) 00310 { // if a hostname is passed, call DNS 00311 if ((hp = gethostbyname(hostname)) == NULL) { 00312 herror("gethostbyname"); 00313 throw "Error in Resolving hostname!" ; 00314 } 00315 memcpy((char *)&address->sin_addr, (char *)hp->h_addr, hp->h_length); 00316 } 00317 } 00318 return (sockaddr*)address; 00319 }
|