| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
#include "test.h" |
| 20 |
#include <pjlib.h> |
| 21 |
|
| 22 |
#define THIS_FILE "util.c" |
| 23 |
|
| 24 |
void app_perror(const char *msg, pj_status_t rc) |
| 25 |
{ |
| 26 |
char errbuf[PJ_ERR_MSG_SIZE]; |
| 27 |
|
| 28 |
PJ_CHECK_STACK(); |
| 29 |
|
| 30 |
pj_strerror(rc, errbuf, sizeof(errbuf)); |
| 31 |
PJ_LOG(3,("test", "%s: [pj_status_t=%d] %s", msg, rc, errbuf)); |
| 32 |
} |
| 33 |
|
| 34 |
#define SERVER 0 |
| 35 |
#define CLIENT 1 |
| 36 |
|
| 37 |
pj_status_t app_socket(int family, int type, int proto, int port, |
| 38 |
pj_sock_t *ptr_sock) |
| 39 |
{ |
| 40 |
pj_sockaddr_in addr; |
| 41 |
pj_sock_t sock; |
| 42 |
pj_status_t rc; |
| 43 |
|
| 44 |
rc = pj_sock_socket(family, type, proto, &sock); |
| 45 |
if (rc != PJ_SUCCESS) |
| 46 |
return rc; |
| 47 |
|
| 48 |
pj_bzero(&addr, sizeof(addr)); |
| 49 |
addr.sin_family = (pj_uint16_t)family; |
| 50 |
addr.sin_port = (short)(port!=-1 ? pj_htons((pj_uint16_t)port) : 0); |
| 51 |
rc = pj_sock_bind(sock, &addr, sizeof(addr)); |
| 52 |
if (rc != PJ_SUCCESS) { |
| 53 |
pj_sock_close(sock); |
| 54 |
return rc; |
| 55 |
} |
| 56 |
|
| 57 |
#if PJ_HAS_TCP |
| 58 |
if (type == pj_SOCK_STREAM()) { |
| 59 |
rc = pj_sock_listen(sock, 5); |
| 60 |
if (rc != PJ_SUCCESS) |
| 61 |
return rc; |
| 62 |
} |
| 63 |
#endif |
| 64 |
|
| 65 |
*ptr_sock = sock; |
| 66 |
return PJ_SUCCESS; |
| 67 |
} |
| 68 |
|
| 69 |
pj_status_t app_socketpair(int family, int type, int protocol, |
| 70 |
pj_sock_t *serverfd, pj_sock_t *clientfd) |
| 71 |
{ |
| 72 |
pj_status_t status; |
| 73 |
pj_sock_t sv[2]; |
| 74 |
|
| 75 |
status = pj_sock_socketpair(family, type, protocol, sv); |
| 76 |
if (status != PJ_SUCCESS) { |
| 77 |
PJ_PERROR(1, (THIS_FILE, status, "socketpair error")); |
| 78 |
return status; |
| 79 |
} |
| 80 |
|
| 81 |
*serverfd = sv[0]; |
| 82 |
*clientfd = sv[1]; |
| 83 |
return PJ_SUCCESS; |
| 84 |
} |