Debian Codegraph @

Xpjproject/pjlib/src/pjlib-test/util.c

raw symbols SCIP
1 /*
2 * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
3 * Copyright (C) 2003-2008 Benny Prijono <[email protected]>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
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 }