1
/* $Id: util.c 3553 2011-05-05 06:14:19Z nanang $ */
3
* Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
4
* Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
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.
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.
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
23
#define THIS_FILE "util.c"
25
void app_perror(const char *msg, pj_status_t rc)
27
char errbuf[PJ_ERR_MSG_SIZE];
31
pj_strerror(rc, errbuf, sizeof(errbuf));
32
PJ_LOG(3,("test", "%s: [pj_status_t=%d] %s", msg, rc, errbuf));
38
pj_status_t app_socket(int family, int type, int proto, int port,
45
rc = pj_sock_socket(family, type, proto, &sock);
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));
57
if (type == pj_SOCK_STREAM()) {
58
rc = pj_sock_listen(sock, 5);
68
pj_status_t app_socketpair(int family, int type, int protocol,
69
pj_sock_t *serverfd, pj_sock_t *clientfd)
72
static unsigned short port = 11000;
78
/* Create both sockets. */
80
rc = pj_sock_socket(family, type, protocol, &sock[i]);
81
if (rc != PJ_SUCCESS) {
83
pj_sock_close(sock[0]);
89
pj_bzero(&addr, sizeof(addr));
90
addr.sin_family = pj_AF_INET();
92
addr.sin_port = pj_htons(port++);
93
rc = pj_sock_bind(sock[SERVER], &addr, sizeof(addr));
101
/* For TCP, listen the socket. */
103
if (type == pj_SOCK_STREAM()) {
104
rc = pj_sock_listen(sock[SERVER], PJ_SOMAXCONN);
105
if (rc != PJ_SUCCESS)
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)
116
/* For TCP, must accept(), and get the new socket. */
118
if (type == pj_SOCK_STREAM()) {
121
rc = pj_sock_accept(sock[SERVER], &newserver, NULL, NULL);
122
if (rc != PJ_SUCCESS)
125
/* Replace server socket with new socket. */
126
pj_sock_close(sock[SERVER]);
127
sock[SERVER] = newserver;
131
*serverfd = sock[SERVER];
132
*clientfd = sock[CLIENT];
138
pj_sock_close(sock[i]);