Tristan Matthews | 0a329cc | 2013-07-17 13:20:14 -0400 | [diff] [blame] | 1 | /* $Id: util.c 3553 2011-05-05 06:14:19Z nanang $ */ |
| 2 | /* |
| 3 | * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com) |
| 4 | * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 2 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program; if not, write to the Free Software |
| 18 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 19 | */ |
| 20 | #include "test.h" |
| 21 | #include <pjlib.h> |
| 22 | |
| 23 | #define THIS_FILE "util.c" |
| 24 | |
| 25 | void app_perror(const char *msg, pj_status_t rc) |
| 26 | { |
| 27 | char errbuf[PJ_ERR_MSG_SIZE]; |
| 28 | |
| 29 | PJ_CHECK_STACK(); |
| 30 | |
| 31 | pj_strerror(rc, errbuf, sizeof(errbuf)); |
| 32 | PJ_LOG(3,("test", "%s: [pj_status_t=%d] %s", msg, rc, errbuf)); |
| 33 | } |
| 34 | |
| 35 | #define SERVER 0 |
| 36 | #define CLIENT 1 |
| 37 | |
| 38 | pj_status_t app_socket(int family, int type, int proto, int port, |
| 39 | pj_sock_t *ptr_sock) |
| 40 | { |
| 41 | pj_sockaddr_in addr; |
| 42 | pj_sock_t sock; |
| 43 | pj_status_t rc; |
| 44 | |
| 45 | rc = pj_sock_socket(family, type, proto, &sock); |
| 46 | if (rc != PJ_SUCCESS) |
| 47 | return rc; |
| 48 | |
| 49 | pj_bzero(&addr, sizeof(addr)); |
| 50 | addr.sin_family = (pj_uint16_t)family; |
| 51 | addr.sin_port = (short)(port!=-1 ? pj_htons((pj_uint16_t)port) : 0); |
| 52 | rc = pj_sock_bind(sock, &addr, sizeof(addr)); |
| 53 | if (rc != PJ_SUCCESS) |
| 54 | return rc; |
| 55 | |
| 56 | #if PJ_HAS_TCP |
| 57 | if (type == pj_SOCK_STREAM()) { |
| 58 | rc = pj_sock_listen(sock, 5); |
| 59 | if (rc != PJ_SUCCESS) |
| 60 | return rc; |
| 61 | } |
| 62 | #endif |
| 63 | |
| 64 | *ptr_sock = sock; |
| 65 | return PJ_SUCCESS; |
| 66 | } |
| 67 | |
| 68 | pj_status_t app_socketpair(int family, int type, int protocol, |
| 69 | pj_sock_t *serverfd, pj_sock_t *clientfd) |
| 70 | { |
| 71 | int i; |
| 72 | static unsigned short port = 11000; |
| 73 | pj_sockaddr_in addr; |
| 74 | pj_str_t s; |
| 75 | pj_status_t rc = 0; |
| 76 | pj_sock_t sock[2]; |
| 77 | |
| 78 | /* Create both sockets. */ |
| 79 | for (i=0; i<2; ++i) { |
| 80 | rc = pj_sock_socket(family, type, protocol, &sock[i]); |
| 81 | if (rc != PJ_SUCCESS) { |
| 82 | if (i==1) |
| 83 | pj_sock_close(sock[0]); |
| 84 | return rc; |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | /* Retry bind */ |
| 89 | pj_bzero(&addr, sizeof(addr)); |
| 90 | addr.sin_family = pj_AF_INET(); |
| 91 | for (i=0; i<5; ++i) { |
| 92 | addr.sin_port = pj_htons(port++); |
| 93 | rc = pj_sock_bind(sock[SERVER], &addr, sizeof(addr)); |
| 94 | if (rc == PJ_SUCCESS) |
| 95 | break; |
| 96 | } |
| 97 | |
| 98 | if (rc != PJ_SUCCESS) |
| 99 | goto on_error; |
| 100 | |
| 101 | /* For TCP, listen the socket. */ |
| 102 | #if PJ_HAS_TCP |
| 103 | if (type == pj_SOCK_STREAM()) { |
| 104 | rc = pj_sock_listen(sock[SERVER], PJ_SOMAXCONN); |
| 105 | if (rc != PJ_SUCCESS) |
| 106 | goto on_error; |
| 107 | } |
| 108 | #endif |
| 109 | |
| 110 | /* Connect client socket. */ |
| 111 | addr.sin_addr = pj_inet_addr(pj_cstr(&s, "127.0.0.1")); |
| 112 | rc = pj_sock_connect(sock[CLIENT], &addr, sizeof(addr)); |
| 113 | if (rc != PJ_SUCCESS) |
| 114 | goto on_error; |
| 115 | |
| 116 | /* For TCP, must accept(), and get the new socket. */ |
| 117 | #if PJ_HAS_TCP |
| 118 | if (type == pj_SOCK_STREAM()) { |
| 119 | pj_sock_t newserver; |
| 120 | |
| 121 | rc = pj_sock_accept(sock[SERVER], &newserver, NULL, NULL); |
| 122 | if (rc != PJ_SUCCESS) |
| 123 | goto on_error; |
| 124 | |
| 125 | /* Replace server socket with new socket. */ |
| 126 | pj_sock_close(sock[SERVER]); |
| 127 | sock[SERVER] = newserver; |
| 128 | } |
| 129 | #endif |
| 130 | |
| 131 | *serverfd = sock[SERVER]; |
| 132 | *clientfd = sock[CLIENT]; |
| 133 | |
| 134 | return rc; |
| 135 | |
| 136 | on_error: |
| 137 | for (i=0; i<2; ++i) |
| 138 | pj_sock_close(sock[i]); |
| 139 | return rc; |
| 140 | } |