blob: d81c518f2d4e753ef95476f4f4e02f7f32a3577d [file] [log] [blame]
Benny Prijonof9962132006-05-16 13:20:00 +00001/* $Id$ */
2/*
3 * Copyright (C) 2003-2006 Benny Prijono <benny@prijono.org>
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
20/**
21 * sipcore.c
22 *
23 * This program is only used to measure the footprint of the SIP core.
24 * When UDP transport is included (with HAS_UDP_TRANSPORT macro), the
25 * executable will respond any incoming requests with 501 (Not Implemented)
26 * response statelessly.
27 */
28
29
30/* Include all headers. */
31#include <pjsip.h>
32#include <pjlib-util.h>
33#include <pjlib.h>
34
35
36/* If this macro is set, UDP transport will be initialized at port 5060 */
37#define HAS_UDP_TRANSPORT
38
39
40/* Log identification */
41#define THIS_FILE "sipstateless.c"
42
43
44/* Global SIP endpoint */
45static pjsip_endpoint *sip_endpt;
46
Benny Prijono0deef102007-02-17 00:12:15 +000047/* What response code to be sent (default is 501/Not Implemented) */
48static int code = PJSIP_SC_NOT_IMPLEMENTED;
49
Benny Prijonof9962132006-05-16 13:20:00 +000050
51/* Callback to handle incoming requests. */
52static pj_bool_t on_rx_request( pjsip_rx_data *rdata )
53{
54 /* Respond (statelessly) all incoming requests (except ACK!)
55 * with 501 (Not Implemented)
56 */
57 if (rdata->msg_info.msg->line.req.method.id != PJSIP_ACK_METHOD) {
58 pjsip_endpt_respond_stateless( sip_endpt, rdata,
Benny Prijono0deef102007-02-17 00:12:15 +000059 code, NULL,
Benny Prijonof9962132006-05-16 13:20:00 +000060 NULL, NULL);
61 }
62 return PJ_TRUE;
63}
64
65
66
67/*
68 * main()
69 *
70 */
Benny Prijono0deef102007-02-17 00:12:15 +000071int main(int argc, char *argv[])
Benny Prijonof9962132006-05-16 13:20:00 +000072{
73 pj_caching_pool cp;
74 pjsip_module mod_app =
75 {
76 NULL, NULL, /* prev, next. */
77 { "mod-app", 7 }, /* Name. */
78 -1, /* Id */
79 PJSIP_MOD_PRIORITY_APPLICATION, /* Priority */
80 NULL, /* load() */
81 NULL, /* start() */
82 NULL, /* stop() */
83 NULL, /* unload() */
84 &on_rx_request, /* on_rx_request() */
85 NULL, /* on_rx_response() */
86 NULL, /* on_tx_request. */
87 NULL, /* on_tx_response() */
88 NULL, /* on_tsx_state() */
89 };
90
91
92 pj_status_t status;
93
Benny Prijono0deef102007-02-17 00:12:15 +000094 if (argc == 2)
95 code = atoi(argv[1]);
96
Benny Prijonof9962132006-05-16 13:20:00 +000097 /* Must init PJLIB first: */
98 status = pj_init();
99 PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
100
101
102 /* Then init PJLIB-UTIL: */
103 status = pjlib_util_init();
104 PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
105
106 /* Must create a pool factory before we can allocate any memory. */
107 pj_caching_pool_init(&cp, &pj_pool_factory_default_policy, 0);
108
109
110 /* Create global endpoint: */
111 {
Benny Prijonof9962132006-05-16 13:20:00 +0000112 /* Endpoint MUST be assigned a globally unique name.
Benny Prijono1ec70b32006-06-20 15:39:07 +0000113 * Ideally we should put hostname or public IP address, but
114 * we'll just use an arbitrary name here.
Benny Prijonof9962132006-05-16 13:20:00 +0000115 */
116
Benny Prijonof9962132006-05-16 13:20:00 +0000117 /* Create the endpoint: */
Benny Prijono1ec70b32006-06-20 15:39:07 +0000118 status = pjsip_endpt_create(&cp.factory, "sipstateless",
Benny Prijonof9962132006-05-16 13:20:00 +0000119 &sip_endpt);
120 PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
121 }
122
123 /*
124 * Add UDP transport, with hard-coded port
125 */
126#ifdef HAS_UDP_TRANSPORT
127 {
128 pj_sockaddr_in addr;
129
130 addr.sin_family = PJ_AF_INET;
131 addr.sin_addr.s_addr = 0;
132 addr.sin_port = pj_htons(5060);
133
134 status = pjsip_udp_transport_start( sip_endpt, &addr, NULL, 1, NULL);
135 if (status != PJ_SUCCESS) {
136 PJ_LOG(3,(THIS_FILE,
137 "Error starting UDP transport (port in use?)"));
138 return 1;
139 }
140 }
141#endif
142
143 /*
144 * Register our module to receive incoming requests.
145 */
146 status = pjsip_endpt_register_module( sip_endpt, &mod_app);
147 PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
148
149
150 /* Done. Loop forever to handle incoming events. */
151 PJ_LOG(3,(THIS_FILE, "Press Ctrl-C to quit.."));
152
153 for (;;) {
154 pjsip_endpt_handle_events(sip_endpt, NULL);
155 }
156}