blob: 53b1fc7c7624b8ee362e39eeffa0b24fe8941bee [file] [log] [blame]
Benny Prijonof9962132006-05-16 13:20:00 +00001/* $Id$ */
2/*
Benny Prijonoa771a512007-02-19 01:13:53 +00003 * Copyright (C) 2003-2007 Benny Prijono <benny@prijono.org>
Benny Prijonof9962132006-05-16 13:20:00 +00004 *
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 *
Benny Prijonoa2914f22007-02-18 01:50:01 +000023 * A simple program to respond any incoming requests (except ACK, of course!)
24 * with any status code (taken from command line argument, with the default
25 * is 501/Not Implemented).
Benny Prijonof9962132006-05-16 13:20:00 +000026 */
27
28
29/* Include all headers. */
30#include <pjsip.h>
31#include <pjlib-util.h>
32#include <pjlib.h>
33
34
35/* If this macro is set, UDP transport will be initialized at port 5060 */
36#define HAS_UDP_TRANSPORT
37
Benny Prijonoa2914f22007-02-18 01:50:01 +000038/* If this macro is set, TCP transport will be initialized at port 5060 */
Benny Prijono3569c0d2007-04-06 10:29:20 +000039#define HAS_TCP_TRANSPORT (1 && PJ_HAS_TCP)
Benny Prijonof9962132006-05-16 13:20:00 +000040
41/* Log identification */
42#define THIS_FILE "sipstateless.c"
43
44
45/* Global SIP endpoint */
46static pjsip_endpoint *sip_endpt;
47
Benny Prijono0deef102007-02-17 00:12:15 +000048/* What response code to be sent (default is 501/Not Implemented) */
49static int code = PJSIP_SC_NOT_IMPLEMENTED;
50
Benny Prijonof9962132006-05-16 13:20:00 +000051
52/* Callback to handle incoming requests. */
53static pj_bool_t on_rx_request( pjsip_rx_data *rdata )
54{
55 /* Respond (statelessly) all incoming requests (except ACK!)
56 * with 501 (Not Implemented)
57 */
58 if (rdata->msg_info.msg->line.req.method.id != PJSIP_ACK_METHOD) {
59 pjsip_endpt_respond_stateless( sip_endpt, rdata,
Benny Prijono0deef102007-02-17 00:12:15 +000060 code, NULL,
Benny Prijonof9962132006-05-16 13:20:00 +000061 NULL, NULL);
62 }
63 return PJ_TRUE;
64}
65
66
67
68/*
69 * main()
70 *
71 */
Benny Prijono0deef102007-02-17 00:12:15 +000072int main(int argc, char *argv[])
Benny Prijonof9962132006-05-16 13:20:00 +000073{
74 pj_caching_pool cp;
75 pjsip_module mod_app =
76 {
77 NULL, NULL, /* prev, next. */
78 { "mod-app", 7 }, /* Name. */
79 -1, /* Id */
80 PJSIP_MOD_PRIORITY_APPLICATION, /* Priority */
81 NULL, /* load() */
82 NULL, /* start() */
83 NULL, /* stop() */
84 NULL, /* unload() */
85 &on_rx_request, /* on_rx_request() */
86 NULL, /* on_rx_response() */
87 NULL, /* on_tx_request. */
88 NULL, /* on_tx_response() */
89 NULL, /* on_tsx_state() */
90 };
91
92
93 pj_status_t status;
94
Benny Prijono0deef102007-02-17 00:12:15 +000095 if (argc == 2)
96 code = atoi(argv[1]);
97
Benny Prijonof9962132006-05-16 13:20:00 +000098 /* Must init PJLIB first: */
99 status = pj_init();
100 PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
101
102
103 /* Then init PJLIB-UTIL: */
104 status = pjlib_util_init();
105 PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
106
107 /* Must create a pool factory before we can allocate any memory. */
108 pj_caching_pool_init(&cp, &pj_pool_factory_default_policy, 0);
109
110
111 /* Create global endpoint: */
112 {
Benny Prijonof9962132006-05-16 13:20:00 +0000113 /* Endpoint MUST be assigned a globally unique name.
Benny Prijono1ec70b32006-06-20 15:39:07 +0000114 * Ideally we should put hostname or public IP address, but
115 * we'll just use an arbitrary name here.
Benny Prijonof9962132006-05-16 13:20:00 +0000116 */
117
Benny Prijonof9962132006-05-16 13:20:00 +0000118 /* Create the endpoint: */
Benny Prijono1ec70b32006-06-20 15:39:07 +0000119 status = pjsip_endpt_create(&cp.factory, "sipstateless",
Benny Prijonof9962132006-05-16 13:20:00 +0000120 &sip_endpt);
121 PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
122 }
123
124 /*
125 * Add UDP transport, with hard-coded port
126 */
127#ifdef HAS_UDP_TRANSPORT
128 {
129 pj_sockaddr_in addr;
130
131 addr.sin_family = PJ_AF_INET;
132 addr.sin_addr.s_addr = 0;
133 addr.sin_port = pj_htons(5060);
134
135 status = pjsip_udp_transport_start( sip_endpt, &addr, NULL, 1, NULL);
136 if (status != PJ_SUCCESS) {
137 PJ_LOG(3,(THIS_FILE,
138 "Error starting UDP transport (port in use?)"));
139 return 1;
140 }
141 }
142#endif
143
Benny Prijono3569c0d2007-04-06 10:29:20 +0000144#if HAS_TCP_TRANSPORT
Benny Prijonoa2914f22007-02-18 01:50:01 +0000145 /*
146 * Add UDP transport, with hard-coded port
147 */
148 {
149 pj_sockaddr_in addr;
150
151 addr.sin_family = PJ_AF_INET;
152 addr.sin_addr.s_addr = 0;
153 addr.sin_port = pj_htons(5060);
154
155 status = pjsip_tcp_transport_start(sip_endpt, &addr, 1, NULL);
156 if (status != PJ_SUCCESS) {
157 PJ_LOG(3,(THIS_FILE,
158 "Error starting TCP transport (port in use?)"));
159 return 1;
160 }
161 }
162#endif
163
Benny Prijonof9962132006-05-16 13:20:00 +0000164 /*
165 * Register our module to receive incoming requests.
166 */
167 status = pjsip_endpt_register_module( sip_endpt, &mod_app);
168 PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
169
170
171 /* Done. Loop forever to handle incoming events. */
172 PJ_LOG(3,(THIS_FILE, "Press Ctrl-C to quit.."));
173
174 for (;;) {
175 pjsip_endpt_handle_events(sip_endpt, NULL);
176 }
177}