parse_event.c

Go to the documentation of this file.
00001 /*
00002  * $Id: parse_event.c 4789 2008-09-01 11:32:36Z klaus_darilion $
00003  *
00004  * Copyright (C) 2001-2003 FhG Fokus
00005  *
00006  * This file is part of Kamailio, a free SIP server.
00007  *
00008  * Kamailio is free software; you can redistribute it and/or modify
00009  * it under the terms of the GNU General Public License as published by
00010  * the Free Software Foundation; either version 2 of the License, or
00011  * (at your option) any later version
00012  *
00013  * Kamailio is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016  * GNU General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU General Public License 
00019  * along with this program; if not, write to the Free Software 
00020  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00021  */
00022 
00023 /*!
00024  * \file
00025  * \brief Event header field body parser
00026  *
00027  * The parser was written for Presence Agent module only.
00028  * it recognize presence package only, no sub-packages, no parameters
00029  * It should be replaced by a more generic parser if sub-packages or
00030  * parameters should be parsed too.
00031  * \ingroup parser
00032  */
00033 
00034 #include "parse_event.h"
00035 #include "../mem/mem.h"    /* pkg_malloc, pkg_free */
00036 #include "../dprint.h"
00037 #include <string.h>        /* memset */
00038 #include "../trim.h"       /* trim_leading */
00039 #include <stdio.h>         /* printf */
00040 #include "../ut.h"
00041 
00042 
00043 #define PRES_STR "presence"
00044 #define PRES_STR_LEN 8
00045 
00046 #define PRES_WINFO_STR "presence.winfo"
00047 #define PRES_WINFO_STR_LEN 14
00048 
00049 #define PRES_XCAP_CHANGE_STR "xcap-change"
00050 #define PRES_XCAP_CHANGE_STR_LEN 11
00051 
00052 #define PRES_SIP_PROFILE_STR "sip-profile"
00053 #define PRES_SIP_PROFILE_STR_LEN 11
00054 
00055 #define MWI_STR "message-summary"
00056 #define MWI_STR_LEN 15
00057 
00058 #define DIALOG_STR "dialog"
00059 #define DIALOG_STR_LEN 6
00060 
00061 #define DIALOG_SLA_STR "dialog;sla"
00062 #define DIALOG_SLA_STR_LEN 10
00063 
00064 static inline char* skip_token(char* _b, int _l)
00065 {
00066    int i = 0;
00067 
00068    for(i = 0; i < _l; i++) {
00069       switch(_b[i]) {
00070       case ' ':
00071       case '\r':
00072       case '\n':
00073       case '\t':
00074       case ';':
00075          return _b + i;
00076       }
00077    }
00078 
00079    return _b + _l;
00080 }
00081 
00082 
00083 int event_parser(char* _s, int _l, event_t* _e)
00084 {
00085    str tmp;
00086    char* end;
00087    char buf[128];
00088    param_hooks_t phooks;
00089 
00090    tmp.s = _s;
00091    tmp.len = _l;
00092 
00093    trim_leading(&tmp);
00094 
00095    if (tmp.len == 0) {
00096       LM_ERR("empty body\n");
00097       return -1;
00098    }
00099 
00100    _e->text.s = tmp.s;
00101 
00102    end = skip_token(tmp.s, tmp.len);
00103 
00104    _e->text.len = end - tmp.s;
00105 
00106    strncpy(buf, tmp.s, tmp.len);
00107    buf[tmp.len] = 0;
00108 
00109    if ((_e->text.len == PRES_STR_LEN) && 
00110        !strncasecmp(PRES_STR, tmp.s, _e->text.len)) {
00111       _e->parsed = EVENT_PRESENCE;
00112    } else if ((_e->text.len == PRES_XCAP_CHANGE_STR_LEN) && 
00113          !strncasecmp(PRES_XCAP_CHANGE_STR, tmp.s, _e->text.len)) {
00114       _e->parsed = EVENT_XCAP_CHANGE;
00115    } else if ((_e->text.len == PRES_WINFO_STR_LEN) && 
00116          !strncasecmp(PRES_WINFO_STR, tmp.s, _e->text.len)) {
00117       _e->parsed = EVENT_PRESENCE_WINFO;
00118    } else if ((_e->text.len == PRES_SIP_PROFILE_STR_LEN) && 
00119          !strncasecmp(PRES_SIP_PROFILE_STR, tmp.s, _e->text.len)) {
00120       _e->parsed = EVENT_SIP_PROFILE;
00121    } else if ((_e->text.len == DIALOG_STR_LEN) && 
00122          !strncasecmp(DIALOG_STR, tmp.s, _e->text.len)) {
00123       _e->parsed = EVENT_DIALOG;
00124    } else if ((_e->text.len == MWI_STR_LEN) && 
00125          !strncasecmp(MWI_STR, tmp.s, _e->text.len)) {
00126       _e->parsed = EVENT_MWI;
00127    } else {
00128       _e->parsed = EVENT_OTHER;
00129    }
00130    
00131 
00132    if( (*end)== ';')
00133    {
00134       str params_str;
00135       params_str.s= end+1;
00136       end= skip_token(params_str.s, tmp.len- _e->text.len- 1);
00137       
00138       params_str.len= end- params_str.s;
00139 
00140       if (parse_params(&params_str, CLASS_ANY, &phooks, &_e->params)<0)
00141          return -1;
00142       
00143       if(_e->parsed == EVENT_DIALOG && _e->params!= NULL && _e->params->next== NULL&&
00144             _e->params->name.len== 3 && strncmp(_e->params->name.s, "sla", 3)== 0 )
00145       {
00146          _e->parsed = EVENT_DIALOG_SLA;
00147       }
00148 
00149    }
00150    else
00151       _e->params= NULL;
00152 
00153    return 0;
00154 }
00155 
00156 
00157 /*!
00158  * Parse Event header field body
00159  */
00160 int parse_event(struct hdr_field* _h)
00161 {
00162    event_t* e;
00163 
00164    if (_h->parsed != 0) {
00165       return 0;
00166    }
00167 
00168    e = (event_t*)pkg_malloc(sizeof(event_t));
00169    if (e == 0) {
00170       LM_ERR("no pkg memory left\n");
00171       return -1;
00172    }
00173 
00174    memset(e, 0, sizeof(event_t));
00175 
00176    if (event_parser(_h->body.s, _h->body.len, e) < 0) {
00177       LM_ERR("event_parser failed\n");
00178       pkg_free(e);
00179       return -2;
00180    }
00181 
00182    _h->parsed = (void*)e;
00183    return 0;
00184 }
00185 
00186 
00187 /*!
00188  * Free all memory
00189  */
00190 void free_event(event_t** _e)
00191 {
00192    if (*_e)
00193    {  
00194       if((*_e)->params)
00195          free_params((*_e)->params);
00196       pkg_free(*_e);
00197    }  
00198    *_e = 0;
00199 }
00200 
00201 
00202 /*!
00203  * Print structure, for debugging only
00204  */
00205 void print_event(event_t* _e)
00206 {
00207    printf("===Event===\n");
00208    printf("text  : \'%.*s\'\n", _e->text.len, ZSW(_e->text.s));
00209    printf("parsed: %s\n", 
00210           (_e->parsed == EVENT_PRESENCE) ? ("EVENT_PRESENCE") : ("EVENT_OTHER"));
00211    printf("===/Event===\n");
00212 }

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