#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 135 of file common.h.
|
Constructor. Constructor. Definition at line 183 of file common.cpp. 00183 :sockfd_(0) 00184 { 00185 }
|
|
cast a socket address to normal address format Function to interpreate hostname and port from the SocketAddress Definition at line 246 of file common.cpp. Referenced by ReceivingPort::receivePacket(). 00247 { 00248 addr->setHostname(inet_ntoa(address->sin_addr)); 00249 addr->setPort(ntohs(address->sin_port)); 00250 }
|
|
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 208 of file common.cpp. References Address::getHostname(), and Address::getPort(). Referenced by ReceivingPort::init(), SendingPort::init(), and SendingPort::sendPacket(). 00209 { 00210 char *hostname; 00211 int port; 00212 unsigned int tmp; 00213 struct hostent *hp; 00214 00215 hostname = addr->getHostname(); 00216 port = addr->getPort(); 00217 00218 address->sin_family = AF_INET; 00219 address->sin_port = htons((short)port); 00220 00221 if (strcmp(hostname, "") == 0) { 00222 address->sin_addr.s_addr = htonl(INADDR_ANY); 00223 } 00224 else { 00225 //tmp = inet_addr(hostname); // If an IP address is given 00226 tmp = inet_aton(hostname, &(address->sin_addr)); 00227 //if(tmp != (unsigned long) INADDR_NONE){ 00228 // address->sin_addr.s_addr = tmp; 00229 //} 00230 if (tmp == 0) 00231 { // if a hostname is passed, call DNS 00232 if ((hp = gethostbyname(hostname)) == NULL) { 00233 herror("gethostbyname"); 00234 throw "Error in Resolving hostname!" ; 00235 } 00236 memcpy((char *)&address->sin_addr, (char *)hp->h_addr, hp->h_length); 00237 } 00238 } 00239 return (sockaddr*)address; 00240 }
|