Benny Prijono | f996213 | 2006-05-16 13:20:00 +0000 | [diff] [blame] | 1 | /* $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 */ |
| 45 | static pjsip_endpoint *sip_endpt; |
| 46 | |
| 47 | |
| 48 | /* Callback to handle incoming requests. */ |
| 49 | static pj_bool_t on_rx_request( pjsip_rx_data *rdata ) |
| 50 | { |
| 51 | /* Respond (statelessly) all incoming requests (except ACK!) |
| 52 | * with 501 (Not Implemented) |
| 53 | */ |
| 54 | if (rdata->msg_info.msg->line.req.method.id != PJSIP_ACK_METHOD) { |
| 55 | pjsip_endpt_respond_stateless( sip_endpt, rdata, |
| 56 | PJSIP_SC_NOT_IMPLEMENTED, NULL, |
| 57 | NULL, NULL); |
| 58 | } |
| 59 | return PJ_TRUE; |
| 60 | } |
| 61 | |
| 62 | |
| 63 | |
| 64 | /* |
| 65 | * main() |
| 66 | * |
| 67 | */ |
| 68 | int main() |
| 69 | { |
| 70 | pj_caching_pool cp; |
| 71 | pjsip_module mod_app = |
| 72 | { |
| 73 | NULL, NULL, /* prev, next. */ |
| 74 | { "mod-app", 7 }, /* Name. */ |
| 75 | -1, /* Id */ |
| 76 | PJSIP_MOD_PRIORITY_APPLICATION, /* Priority */ |
| 77 | NULL, /* load() */ |
| 78 | NULL, /* start() */ |
| 79 | NULL, /* stop() */ |
| 80 | NULL, /* unload() */ |
| 81 | &on_rx_request, /* on_rx_request() */ |
| 82 | NULL, /* on_rx_response() */ |
| 83 | NULL, /* on_tx_request. */ |
| 84 | NULL, /* on_tx_response() */ |
| 85 | NULL, /* on_tsx_state() */ |
| 86 | }; |
| 87 | |
| 88 | |
| 89 | pj_status_t status; |
| 90 | |
| 91 | /* Must init PJLIB first: */ |
| 92 | status = pj_init(); |
| 93 | PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1); |
| 94 | |
| 95 | |
| 96 | /* Then init PJLIB-UTIL: */ |
| 97 | status = pjlib_util_init(); |
| 98 | PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1); |
| 99 | |
| 100 | /* Must create a pool factory before we can allocate any memory. */ |
| 101 | pj_caching_pool_init(&cp, &pj_pool_factory_default_policy, 0); |
| 102 | |
| 103 | |
| 104 | /* Create global endpoint: */ |
| 105 | { |
Benny Prijono | f996213 | 2006-05-16 13:20:00 +0000 | [diff] [blame] | 106 | /* Endpoint MUST be assigned a globally unique name. |
Benny Prijono | 1ec70b3 | 2006-06-20 15:39:07 +0000 | [diff] [blame] | 107 | * Ideally we should put hostname or public IP address, but |
| 108 | * we'll just use an arbitrary name here. |
Benny Prijono | f996213 | 2006-05-16 13:20:00 +0000 | [diff] [blame] | 109 | */ |
| 110 | |
Benny Prijono | f996213 | 2006-05-16 13:20:00 +0000 | [diff] [blame] | 111 | /* Create the endpoint: */ |
Benny Prijono | 1ec70b3 | 2006-06-20 15:39:07 +0000 | [diff] [blame] | 112 | status = pjsip_endpt_create(&cp.factory, "sipstateless", |
Benny Prijono | f996213 | 2006-05-16 13:20:00 +0000 | [diff] [blame] | 113 | &sip_endpt); |
| 114 | PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1); |
| 115 | } |
| 116 | |
| 117 | /* |
| 118 | * Add UDP transport, with hard-coded port |
| 119 | */ |
| 120 | #ifdef HAS_UDP_TRANSPORT |
| 121 | { |
| 122 | pj_sockaddr_in addr; |
| 123 | |
| 124 | addr.sin_family = PJ_AF_INET; |
| 125 | addr.sin_addr.s_addr = 0; |
| 126 | addr.sin_port = pj_htons(5060); |
| 127 | |
| 128 | status = pjsip_udp_transport_start( sip_endpt, &addr, NULL, 1, NULL); |
| 129 | if (status != PJ_SUCCESS) { |
| 130 | PJ_LOG(3,(THIS_FILE, |
| 131 | "Error starting UDP transport (port in use?)")); |
| 132 | return 1; |
| 133 | } |
| 134 | } |
| 135 | #endif |
| 136 | |
| 137 | /* |
| 138 | * Register our module to receive incoming requests. |
| 139 | */ |
| 140 | status = pjsip_endpt_register_module( sip_endpt, &mod_app); |
| 141 | PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1); |
| 142 | |
| 143 | |
| 144 | /* Done. Loop forever to handle incoming events. */ |
| 145 | PJ_LOG(3,(THIS_FILE, "Press Ctrl-C to quit..")); |
| 146 | |
| 147 | for (;;) { |
| 148 | pjsip_endpt_handle_events(sip_endpt, NULL); |
| 149 | } |
| 150 | } |