options.c

Go to the documentation of this file.
00001 /*
00002  * $Id: options.c 5603 2009-02-12 19:42:49Z miconda $
00003  *
00004  * Options Reply Module
00005  *
00006  * Copyright (C) 2001-2003 FhG Fokus
00007  *
00008  * This file is part of Kamailio, a free SIP server.
00009  *
00010  * Kamailio is free software; you can redistribute it and/or modify
00011  * it under the terms of the GNU General Public License as published by
00012  * the Free Software Foundation; either version 2 of the License, or
00013  * (at your option) any later version
00014  *
00015  * Kamailio is distributed in the hope that it will be useful,
00016  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018  * GNU General Public License for more details.
00019  *
00020  * You should have received a copy of the GNU General Public License
00021  * along with this program; if not, write to the Free Software
00022  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00023  *
00024  * History:
00025  * -------
00026  * 2003-11-11: build_lump_rpl() removed, add_lump_rpl() has flags (bogdan)
00027  */
00028 
00029 /*!
00030  * \file siputils/options.c
00031  * \brief Options reply modules
00032  * \ingroup options
00033  * Module: \ref options
00034  */
00035 
00036 /*!
00037  * \defgroup options OPTIONS :: Reply to OPTION pokes
00038  */
00039 
00040 #ifdef EXTRA_DEBUG
00041 #include <stdlib.h>   /* required by abort() */
00042 #endif
00043 #include "options.h"
00044 #include "../../ut.h"
00045 #include "../../mem/mem.h"
00046 #include "../../data_lump_rpl.h"
00047 #include "../../parser/parse_uri.h"
00048 
00049 static str opt_200_rpl = str_init("OK");
00050 static str opt_500_rpl = str_init("Server internal error");
00051 
00052 
00053 int opt_reply(struct sip_msg* _msg, char* _foo, char* _bar) {
00054    str rpl_hf;
00055    int offset = 0;
00056 
00057    /* check if it is called for an OPTIONS request */
00058    if (_msg->REQ_METHOD!=METHOD_OPTIONS) {
00059       LM_ERR("called for non-OPTIONS request\n");
00060       return -1;
00061    }
00062    if(_msg->parsed_uri_ok==0 && parse_sip_msg_uri(_msg)<0)
00063    {
00064       LM_ERR("ERROR while parsing the R-URI\n");
00065       return -1;
00066    }
00067    /* FIXME: should we additionally check if ruri == server addresses ?! */
00068    if (_msg->parsed_uri.user.len != 0) {
00069       LM_ERR("ruri contains username\n");
00070       return -1;
00071    }
00072 
00073    /* calculate the length and allocated the mem */
00074    rpl_hf.len = ACPT_STR_LEN + ACPT_ENC_STR_LEN + ACPT_LAN_STR_LEN + 
00075          SUPT_STR_LEN + 4*HF_SEP_STR_LEN + opt_accept.len + opt_accept_enc.len
00076          + opt_accept_lang.len + opt_supported.len;
00077    rpl_hf.s = (char*)pkg_malloc(rpl_hf.len);
00078    if (!rpl_hf.s) {
00079       LM_CRIT("out of pkg memory\n");
00080       goto error;
00081    }
00082 
00083    /* create the header fields */
00084    memcpy(rpl_hf.s, ACPT_STR, ACPT_STR_LEN);
00085    offset = ACPT_STR_LEN;
00086    memcpy(rpl_hf.s + offset, opt_accept.s, opt_accept.len);
00087    offset += opt_accept.len;
00088    memcpy(rpl_hf.s + offset, HF_SEP_STR, HF_SEP_STR_LEN);
00089    offset += HF_SEP_STR_LEN;
00090    memcpy(rpl_hf.s + offset, ACPT_ENC_STR, ACPT_ENC_STR_LEN);
00091    offset += ACPT_ENC_STR_LEN;
00092    memcpy(rpl_hf.s + offset, opt_accept_enc.s, opt_accept_enc.len);
00093    offset += opt_accept_enc.len;
00094    memcpy(rpl_hf.s + offset, HF_SEP_STR, HF_SEP_STR_LEN);
00095    offset += HF_SEP_STR_LEN;
00096    memcpy(rpl_hf.s + offset, ACPT_LAN_STR, ACPT_LAN_STR_LEN);
00097    offset += ACPT_LAN_STR_LEN;
00098    memcpy(rpl_hf.s + offset, opt_accept_lang.s, opt_accept_lang.len);
00099    offset += opt_accept_lang.len;
00100    memcpy(rpl_hf.s + offset, HF_SEP_STR, HF_SEP_STR_LEN);
00101    offset += HF_SEP_STR_LEN;
00102    memcpy(rpl_hf.s + offset, SUPT_STR, SUPT_STR_LEN);
00103    offset += SUPT_STR_LEN;
00104    memcpy(rpl_hf.s + offset, opt_supported.s, opt_supported.len);
00105    offset += opt_supported.len;
00106    memcpy(rpl_hf.s + offset, HF_SEP_STR, HF_SEP_STR_LEN);
00107 
00108 #ifdef EXTRA_DEBUG
00109    offset += HF_SEP_STR_LEN;
00110    if (offset != rpl_hf.len) {
00111       LM_CRIT("headerlength (%i) != offset (%i)\n", rpl_hf.len, offset);
00112       abort();
00113    }
00114 #endif
00115 
00116 
00117    if (add_lump_rpl( _msg, rpl_hf.s, rpl_hf.len,
00118    LUMP_RPL_HDR|LUMP_RPL_NODUP)!=0) {
00119       if (opt_slb.send_reply(_msg, 200, &opt_200_rpl) == -1) {
00120          LM_ERR("failed to send 200 via send_reply\n");
00121          return -1;
00122       }
00123       else
00124          return 0;
00125    } else {
00126       pkg_free(rpl_hf.s);
00127       LM_ERR("add_lump_rpl failed\n");
00128    }
00129 
00130 error:
00131    if (opt_slb.send_reply(_msg, 500, &opt_500_rpl) == -1) {
00132       LM_ERR("failed to send 500 via send_reply\n");
00133       return -1;
00134    }
00135    else
00136       return 0;
00137 }
00138 

Generated on Thu May 24 00:00:28 2012 for Kamailio - The Open Source SIP Server by  doxygen 1.5.6