Benny Prijono | f996213 | 2006-05-16 13:20:00 +0000 | [diff] [blame] | 1 | /* $Id$ */ |
| 2 | /* |
Benny Prijono | a771a51 | 2007-02-19 01:13:53 +0000 | [diff] [blame] | 3 | * Copyright (C) 2003-2007 Benny Prijono <benny@prijono.org> |
Benny Prijono | f996213 | 2006-05-16 13:20:00 +0000 | [diff] [blame] | 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 | * |
Benny Prijono | a2914f2 | 2007-02-18 01:50:01 +0000 | [diff] [blame] | 23 | * 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 Prijono | f996213 | 2006-05-16 13:20:00 +0000 | [diff] [blame] | 26 | */ |
| 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 Prijono | a2914f2 | 2007-02-18 01:50:01 +0000 | [diff] [blame] | 38 | /* If this macro is set, TCP transport will be initialized at port 5060 */ |
Benny Prijono | 3569c0d | 2007-04-06 10:29:20 +0000 | [diff] [blame] | 39 | #define HAS_TCP_TRANSPORT (1 && PJ_HAS_TCP) |
Benny Prijono | f996213 | 2006-05-16 13:20:00 +0000 | [diff] [blame] | 40 | |
| 41 | /* Log identification */ |
| 42 | #define THIS_FILE "sipstateless.c" |
| 43 | |
| 44 | |
| 45 | /* Global SIP endpoint */ |
| 46 | static pjsip_endpoint *sip_endpt; |
| 47 | |
Benny Prijono | 0deef10 | 2007-02-17 00:12:15 +0000 | [diff] [blame] | 48 | /* What response code to be sent (default is 501/Not Implemented) */ |
| 49 | static int code = PJSIP_SC_NOT_IMPLEMENTED; |
| 50 | |
Benny Prijono | f996213 | 2006-05-16 13:20:00 +0000 | [diff] [blame] | 51 | |
| 52 | /* Callback to handle incoming requests. */ |
| 53 | static 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 Prijono | 0deef10 | 2007-02-17 00:12:15 +0000 | [diff] [blame] | 60 | code, NULL, |
Benny Prijono | f996213 | 2006-05-16 13:20:00 +0000 | [diff] [blame] | 61 | NULL, NULL); |
| 62 | } |
| 63 | return PJ_TRUE; |
| 64 | } |
| 65 | |
| 66 | |
| 67 | |
| 68 | /* |
| 69 | * main() |
| 70 | * |
| 71 | */ |
Benny Prijono | 0deef10 | 2007-02-17 00:12:15 +0000 | [diff] [blame] | 72 | int main(int argc, char *argv[]) |
Benny Prijono | f996213 | 2006-05-16 13:20:00 +0000 | [diff] [blame] | 73 | { |
| 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 Prijono | 0deef10 | 2007-02-17 00:12:15 +0000 | [diff] [blame] | 95 | if (argc == 2) |
| 96 | code = atoi(argv[1]); |
| 97 | |
Benny Prijono | f996213 | 2006-05-16 13:20:00 +0000 | [diff] [blame] | 98 | /* 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 Prijono | f996213 | 2006-05-16 13:20:00 +0000 | [diff] [blame] | 113 | /* Endpoint MUST be assigned a globally unique name. |
Benny Prijono | 1ec70b3 | 2006-06-20 15:39:07 +0000 | [diff] [blame] | 114 | * Ideally we should put hostname or public IP address, but |
| 115 | * we'll just use an arbitrary name here. |
Benny Prijono | f996213 | 2006-05-16 13:20:00 +0000 | [diff] [blame] | 116 | */ |
| 117 | |
Benny Prijono | f996213 | 2006-05-16 13:20:00 +0000 | [diff] [blame] | 118 | /* Create the endpoint: */ |
Benny Prijono | 1ec70b3 | 2006-06-20 15:39:07 +0000 | [diff] [blame] | 119 | status = pjsip_endpt_create(&cp.factory, "sipstateless", |
Benny Prijono | f996213 | 2006-05-16 13:20:00 +0000 | [diff] [blame] | 120 | &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 | |
Benny Prijono | 8ab968f | 2007-07-20 08:08:30 +0000 | [diff] [blame] | 131 | addr.sin_family = pj_AF_INET(); |
Benny Prijono | f996213 | 2006-05-16 13:20:00 +0000 | [diff] [blame] | 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 Prijono | 3569c0d | 2007-04-06 10:29:20 +0000 | [diff] [blame] | 144 | #if HAS_TCP_TRANSPORT |
Benny Prijono | a2914f2 | 2007-02-18 01:50:01 +0000 | [diff] [blame] | 145 | /* |
| 146 | * Add UDP transport, with hard-coded port |
| 147 | */ |
| 148 | { |
| 149 | pj_sockaddr_in addr; |
| 150 | |
Benny Prijono | 8ab968f | 2007-07-20 08:08:30 +0000 | [diff] [blame] | 151 | addr.sin_family = pj_AF_INET(); |
Benny Prijono | a2914f2 | 2007-02-18 01:50:01 +0000 | [diff] [blame] | 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 Prijono | f996213 | 2006-05-16 13:20:00 +0000 | [diff] [blame] | 164 | /* |
| 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 | } |