Changeset 1613 for pjproject/trunk/pjlib/src/pj/sock_common.c
- Timestamp:
- Dec 3, 2007 2:33:39 PM (17 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
pjproject/trunk/pjlib/src/pj/sock_common.c
r1608 r1613 60 60 61 61 /* 62 * Print socket address. 63 */ 64 PJ_DEF(char*) pj_sockaddr_print( const pj_sockaddr_t *addr, 65 char *buf, int size, 66 unsigned flags) 67 { 68 enum { 69 WITH_PORT = 1, 70 WITH_BRACKETS = 2 71 }; 72 73 char txt[PJ_INET6_ADDRSTRLEN]; 74 char port[32]; 75 const pj_addr_hdr *h = (const pj_addr_hdr*)addr; 76 char *bquote, *equote; 77 pj_status_t status; 78 79 status = pj_inet_ntop(h->sa_family, pj_sockaddr_get_addr(addr), 80 txt, sizeof(txt)); 81 if (status != PJ_SUCCESS) 82 return ""; 83 84 if (h->sa_family != PJ_AF_INET6 || (flags & WITH_BRACKETS)==0) { 85 bquote = ""; equote = ""; 86 } else { 87 bquote = "["; equote = "]"; 88 } 89 90 if (flags & WITH_PORT) { 91 pj_ansi_snprintf(port, sizeof(port), ":%d", 92 pj_sockaddr_get_port(addr)); 93 } else { 94 port[0] = '\0'; 95 } 96 97 pj_ansi_snprintf(buf, size, "%s%s%s%s", 98 bquote, txt, equote, port); 99 100 return buf; 101 } 102 103 /* 62 104 * Set the IP address of an IP socket address from string address, 63 105 * with resolving the host if necessary. The string address may be in a … … 185 227 186 228 /* 229 * Compare two socket addresses. 230 */ 231 PJ_DEF(int) pj_sockaddr_cmp( const pj_sockaddr_t *addr1, 232 const pj_sockaddr_t *addr2) 233 { 234 const pj_sockaddr *a1 = (const pj_sockaddr*) addr1; 235 const pj_sockaddr *a2 = (const pj_sockaddr*) addr2; 236 int port1, port2; 237 int result; 238 239 /* Compare address family */ 240 if (a1->addr.sa_family < a2->addr.sa_family) 241 return -1; 242 else if (a1->addr.sa_family > a2->addr.sa_family) 243 return 1; 244 245 /* Compare addresses */ 246 result = pj_memcmp(pj_sockaddr_get_addr(a1), 247 pj_sockaddr_get_addr(a2), 248 pj_sockaddr_get_addr_len(a1)); 249 if (result != 0) 250 return result; 251 252 /* Compare port number */ 253 port1 = pj_sockaddr_get_port(a1); 254 port2 = pj_sockaddr_get_port(a2); 255 256 if (port1 < port2) 257 return -1; 258 else if (port1 > port2) 259 return 1; 260 261 /* TODO: 262 * Do we need to compare flow label and scope id in IPv6? 263 */ 264 265 /* Looks equal */ 266 return 0; 267 } 268 269 /* 187 270 * Get first IP address associated with the hostname. 188 271 */ … … 278 361 279 362 /* 363 * Copy only the address part (sin_addr/sin6_addr) of a socket address. 364 */ 365 PJ_DEF(void) pj_sockaddr_copy_addr( pj_sockaddr *dst, 366 const pj_sockaddr *src) 367 { 368 pj_memcpy(pj_sockaddr_get_addr(dst), 369 pj_sockaddr_get_addr(src), 370 pj_sockaddr_get_addr_len(src)); 371 } 372 373 /* 280 374 * Set port number of pj_sockaddr_in 281 375 */
Note: See TracChangeset
for help on using the changeset viewer.