cmUdpPort.h/c:Added cmUdpLocalAddr(). Replaced cmUdpHostNameXXX() w/ cmUdpHostName().

This commit is contained in:
kpl 2013-04-27 12:13:38 -07:00
parent f4caf7f522
commit 875fce5f9d
2 changed files with 27 additions and 18 deletions

View File

@ -42,7 +42,9 @@ typedef struct
unsigned recvCnt; unsigned recvCnt;
unsigned queCbCnt; unsigned queCbCnt;
unsigned errCnt; unsigned errCnt;
struct sockaddr_in sockaddr;
cmChar_t ntopBuf[ INET_ADDRSTRLEN+1 ]; // use INET6_ADDRSTRLEN for IPv6 cmChar_t ntopBuf[ INET_ADDRSTRLEN+1 ]; // use INET6_ADDRSTRLEN for IPv6
cmChar_t hnameBuf[ HOST_NAME_MAX+1 ];
} cmUdp_t; } cmUdp_t;
cmUdpH_t cmUdpNullHandle = cmSTATIC_NULL_HANDLE; cmUdpH_t cmUdpNullHandle = cmSTATIC_NULL_HANDLE;
@ -186,8 +188,6 @@ cmUdpRC_t cmUdpInit(
{ {
cmUdpRC_t rc; cmUdpRC_t rc;
struct sockaddr_in addr;
cmUdp_t* p = _cmUdpHandleToPtr(h); cmUdp_t* p = _cmUdpHandleToPtr(h);
if((rc = _cmUdpFinal(p)) != kOkUdpRC ) if((rc = _cmUdpFinal(p)) != kOkUdpRC )
@ -200,11 +200,11 @@ cmUdpRC_t cmUdpInit(
return cmErrSysMsg(&p->err, kSockCreateFailUdpRC, errno, "Socket create failed." ); return cmErrSysMsg(&p->err, kSockCreateFailUdpRC, errno, "Socket create failed." );
// create the local address // create the local address
if((rc = _cmUdpInitAddr(p, NULL, port, &addr )) != kOkUdpRC ) if((rc = _cmUdpInitAddr(p, NULL, port, &p->sockaddr )) != kOkUdpRC )
goto errLabel; goto errLabel;
// bind the socket to a local address/port // bind the socket to a local address/port
if( (bind( p->sockH, (struct sockaddr*)&addr, sizeof(addr))) == cmUdp_SYS_ERR ) if( (bind( p->sockH, (struct sockaddr*)&p->sockaddr, sizeof(p->sockaddr))) == cmUdp_SYS_ERR )
{ {
rc = cmErrSysMsg(&p->err,kSockBindFailUdpRC,errno,"Socket bind failed." ); rc = cmErrSysMsg(&p->err,kSockBindFailUdpRC,errno,"Socket bind failed." );
goto errLabel; goto errLabel;
@ -287,6 +287,13 @@ cmUdpRC_t cmUdpFinal( cmUdpH_t h )
bool cmUdpIsValid( cmUdpH_t h ) bool cmUdpIsValid( cmUdpH_t h )
{ return h.h != NULL; } { return h.h != NULL; }
const struct sockaddr_in* cmUdpLocalAddr( cmUdpH_t h )
{
cmUdp_t* p = _cmUdpHandleToPtr(h);
return &p->sockaddr;
}
cmUdpRC_t cmUdpConnect( cmUdpH_t h, const char* remoteAddr, cmUdpPort_t remotePort ) cmUdpRC_t cmUdpConnect( cmUdpH_t h, const char* remoteAddr, cmUdpPort_t remotePort )
{ {
@ -563,19 +570,18 @@ const cmChar_t* cmUdpAddrToString( cmUdpH_t h, const struct sockaddr_in* addr )
return p->ntopBuf; return p->ntopBuf;
} }
unsigned cmUdpHostNameMaxCharCount() const cmChar_t* cmUdpHostName( cmUdpH_t h )
{ return HOST_NAME_MAX+1; }
cmUdpRC_t cmUdpHostName( cmChar_t* buf, unsigned bufByteCnt )
{ {
if( bufByteCnt > 0 ) cmUdp_t* p = _cmUdpHandleToPtr(h);
buf[0] = 0;
if( bufByteCnt < cmUdpHostNameMaxCharCount() ) _cmUdpClear_errno();
return kBufTooSmallUdpRC;
if( gethostname(buf,bufByteCnt-1) != 0 ) if( gethostname(p->hnameBuf,HOST_NAME_MAX) != 0 )
return kHostNameFailUdpRC; {
cmErrSysMsg(&p->err,kHostNameFailUdpRC,errno, "gethostname() failed." );
return NULL;
}
return kOkUdpRC; p->hnameBuf[HOST_NAME_MAX] = 0;
return p->hnameBuf;
} }

View File

@ -62,6 +62,8 @@ extern "C" {
cmUdpRC_t cmUdpFinal( cmUdpH_t h ); cmUdpRC_t cmUdpFinal( cmUdpH_t h );
bool cmUdpIsValid( cmUdpH_t h ); bool cmUdpIsValid( cmUdpH_t h );
const struct sockaddr_in* cmUdpLocalAddr( cmUdpH_t h );
// Set a destination address for this socket. Once a destination address is set // Set a destination address for this socket. Once a destination address is set
// the caller may use cmUdpSend() to communicate with the specified remote socket // the caller may use cmUdpSend() to communicate with the specified remote socket
// without having to specify an destination address on each call. // without having to specify an destination address on each call.
@ -120,8 +122,9 @@ extern "C" {
const cmChar_t* cmUdpAddrToString( cmUdpH_t h, const struct sockaddr_in* addr ); const cmChar_t* cmUdpAddrToString( cmUdpH_t h, const struct sockaddr_in* addr );
unsigned cmUdpHostNameMaxCharCount(); const cmChar_t* cmUdpHostName( cmUdpH_t h );
cmUdpRC_t cmUdpHostName( cmChar_t* buf, unsigned bufByteCnt );
#ifdef __cplusplus #ifdef __cplusplus
} }