From 6074e052f08e3c6362900db37736f0f739ab8406 Mon Sep 17 00:00:00 2001 From: kevin Date: Fri, 12 Apr 2013 13:07:50 -0700 Subject: [PATCH] cmUdpPort.h/c:Added cmUdpAddrToString(). --- cmUdpPort.c | 17 +++++++++++++++++ cmUdpPort.h | 3 +++ 2 files changed, 20 insertions(+) diff --git a/cmUdpPort.c b/cmUdpPort.c index c8a155e..a9a04fc 100644 --- a/cmUdpPort.c +++ b/cmUdpPort.c @@ -42,6 +42,7 @@ typedef struct unsigned recvCnt; unsigned queCbCnt; unsigned errCnt; + cmChar_t ntopBuf[ INET_ADDRSTRLEN+1 ]; // use INET6_ADDRSTRLEN for IPv6 } cmUdp_t; cmUdpH_t cmUdpNullHandle = cmSTATIC_NULL_HANDLE; @@ -545,3 +546,19 @@ cmUdpRC_t cmUdpInitAddr( cmUdpH_t h, const char* addrStr, cmUdpPort_t portNumber cmUdp_t* p = _cmUdpHandleToPtr(h); return _cmUdpInitAddr(p,addrStr,portNumber,retAddrPtr); } + +const cmChar_t* cmUdpAddrToString( cmUdpH_t h, const struct sockaddr_in* addr ) +{ + cmUdp_t* p = _cmUdpHandleToPtr(h); + + _cmUdpClear_errno(); + + if( inet_ntop(AF_INET, addr, p->ntopBuf, INET_ADDRSTRLEN) == NULL) + { + cmErrSysMsg(&p->err,kNtoPFailUdpRC,errno, "Network address to string conversion failed." ); + return NULL; + } + + p->ntopBuf[INET_ADDRSTRLEN]=0; + return p->ntopBuf; +} diff --git a/cmUdpPort.h b/cmUdpPort.h index 43a0536..33c2dca 100644 --- a/cmUdpPort.h +++ b/cmUdpPort.h @@ -19,6 +19,7 @@ extern "C" { kSockRecvFailUdpRC, kSockSelectFailUdpRC, kPtoNFailUdpRC, + kNtoPFailUdpRC, kNotConnectedUdpRC, kThreadFailUdpRC, kQueueFailUdpRC, @@ -115,6 +116,8 @@ extern "C" { // Prepare a struct sockadddr_in for use with cmUdpSendTo() cmUdpRC_t cmUdpInitAddr( cmUdpH_t h, const char* addrStr, cmUdpPort_t portNumber, struct sockaddr_in* retAddrPtr ); + const cmChar_t* cmUdpAddrToString( cmUdpH_t h, const struct sockaddr_in* addr ); + #ifdef __cplusplus } #endif