Browse Source

cmUdpPort.h/c:Added cmUdpAddrToString().

master
kevin 11 years ago
parent
commit
6074e052f0
2 changed files with 20 additions and 0 deletions
  1. 17
    0
      cmUdpPort.c
  2. 3
    0
      cmUdpPort.h

+ 17
- 0
cmUdpPort.c View File

42
   unsigned        recvCnt;
42
   unsigned        recvCnt;
43
   unsigned        queCbCnt;
43
   unsigned        queCbCnt;
44
   unsigned        errCnt;
44
   unsigned        errCnt;
45
+  cmChar_t        ntopBuf[ INET_ADDRSTRLEN+1 ]; // use INET6_ADDRSTRLEN for IPv6
45
 } cmUdp_t;
46
 } cmUdp_t;
46
 
47
 
47
 cmUdpH_t cmUdpNullHandle = cmSTATIC_NULL_HANDLE;
48
 cmUdpH_t cmUdpNullHandle = cmSTATIC_NULL_HANDLE;
545
   cmUdp_t* p = _cmUdpHandleToPtr(h);
546
   cmUdp_t* p = _cmUdpHandleToPtr(h);
546
   return _cmUdpInitAddr(p,addrStr,portNumber,retAddrPtr);
547
   return _cmUdpInitAddr(p,addrStr,portNumber,retAddrPtr);
547
 }
548
 }
549
+
550
+const cmChar_t* cmUdpAddrToString( cmUdpH_t h, const struct sockaddr_in* addr )
551
+{
552
+  cmUdp_t* p = _cmUdpHandleToPtr(h);
553
+
554
+  _cmUdpClear_errno();
555
+  
556
+  if( inet_ntop(AF_INET, addr,  p->ntopBuf, INET_ADDRSTRLEN) == NULL)
557
+  {
558
+    cmErrSysMsg(&p->err,kNtoPFailUdpRC,errno, "Network address to string conversion failed." );
559
+    return NULL;
560
+  }
561
+  
562
+  p->ntopBuf[INET_ADDRSTRLEN]=0;
563
+  return p->ntopBuf;
564
+}

+ 3
- 0
cmUdpPort.h View File

19
     kSockRecvFailUdpRC,
19
     kSockRecvFailUdpRC,
20
     kSockSelectFailUdpRC,
20
     kSockSelectFailUdpRC,
21
     kPtoNFailUdpRC,
21
     kPtoNFailUdpRC,
22
+    kNtoPFailUdpRC,
22
     kNotConnectedUdpRC,
23
     kNotConnectedUdpRC,
23
     kThreadFailUdpRC,
24
     kThreadFailUdpRC,
24
     kQueueFailUdpRC,
25
     kQueueFailUdpRC,
115
   // Prepare a struct sockadddr_in for use with cmUdpSendTo()
116
   // Prepare a struct sockadddr_in for use with cmUdpSendTo()
116
   cmUdpRC_t cmUdpInitAddr( cmUdpH_t h, const char* addrStr, cmUdpPort_t portNumber, struct sockaddr_in* retAddrPtr );
117
   cmUdpRC_t cmUdpInitAddr( cmUdpH_t h, const char* addrStr, cmUdpPort_t portNumber, struct sockaddr_in* retAddrPtr );
117
 
118
 
119
+  const cmChar_t*  cmUdpAddrToString( cmUdpH_t h, const struct sockaddr_in* addr );
120
+
118
 #ifdef __cplusplus
121
 #ifdef __cplusplus
119
 }
122
 }
120
 #endif
123
 #endif

Loading…
Cancel
Save