
 
 
#ifndef AST_MODULE
#define AST_MODULE
#endif
#include "asterisk.h"
#include <pjsip.h>
#include <pjsip_ua.h>
#include <pjlib.h>
 
#include "asterisk/res_pjsip.h"
#include "asterisk/res_pjsip_session.h"
#include "asterisk/res_hep.h"
#include "asterisk/module.h"
#include "asterisk/netsock2.h"
#include <pjsip.h>
#include <pjmedia.h>
#include <pjmedia-codec.h>
#include <pjsip_ua.h>
#include <pjsip_simple.h>
#include <pjlib-util.h>

// Author: Ondrej Melichar , zasuvny modul pro PJSIP v ramci diplomove prace VUT FEKT

/*** MODULEINFO
        <depend>pjproject</depend>
        <depend>res_pjsip</depend>
        <support_level>core</support_level>
 ***/
 

 
static pj_bool_t analyza_paketu(pjsip_rx_data *rdata);
 
static pj_bool_t analyza_paketu(pjsip_rx_data *rdata)
{
    
 
    // Detekce Missing Required Headers
    pjsip_uri *uri_from = NULL;
    pjsip_uri *uri_to = NULL;
    char from_buffer[1024];
    char to_buffer[1024];
 
 
    ast_log(LOG_NOTICE," Jedna se o metodu %.*s \n", (int)rdata->msg_info.msg->line.req.method.name.slen, rdata->msg_info.msg->line.req.method.name.ptr);
    
    uri_from = pjsip_uri_get_uri(rdata->msg_info.from->uri);
    uri_to = pjsip_uri_get_uri(rdata->msg_info.to->uri);
   
   // Nutno pouzit "slen", jinak neopravneny pristup do pameti!
   
    pjsip_uri_print(PJSIP_URI_IN_FROMTO_HDR, uri_from, from_buffer, sizeof(from_buffer)-1);
    pjsip_uri_print(PJSIP_URI_IN_FROMTO_HDR, uri_to, to_buffer, sizeof(to_buffer)-1);
   
    if(from_buffer!=NULL)
    {
        ast_log(LOG_NOTICE,"Parametr From: ma hodnotu %s \n",from_buffer);
            if(to_buffer!=NULL)
            {
            ast_log(LOG_NOTICE,"Parametr To: ma hodnotu %s \n",to_buffer);
                if(((int)rdata->msg_info.cid->id.slen)!=0)
                {
                    ast_log(LOG_NOTICE,"Parametr Call-ID: ma hodnotu %.*s \n",(int)rdata->msg_info.cid->id.slen, rdata->msg_info.cid->id.ptr);
                }
                else
                {
                ast_log(LOG_NOTICE,"Zprava neobsahuje parametr Call-ID!");
                }
            }
            else
            {
                ast_log(LOG_NOTICE,"Zprava neobsahuje parametr To!");
            }
    }
    else
    {
        ast_log(LOG_NOTICE,"Zprava neobsahuje parametr From!");
   } 
 
   

    // Detekce RFC 2543 INVITE
    if(((int)rdata->msg_info.via->branch_param.slen)!=0)
    {
   
    ast_log(LOG_NOTICE,"Parametr Branch ma hodnotu %.*s \n",(int)rdata->msg_info.via->branch_param.slen, rdata->msg_info.via->branch_param.ptr);
        if(((int)rdata->msg_info.from->tag.slen))
        {
            ast_log(LOG_NOTICE,"Parametr Tag ma hodnotu %.*s \n",(int)rdata->msg_info.from->tag.slen , rdata->msg_info.from->tag.ptr);
                       
        }
        else
        {
            ast_log(LOG_NOTICE,"Chybi parametr Tag! Zprava dle RFC 2543. \n");
        }
   
    }
    else
    {
        ast_log(LOG_NOTICE,"Chybi parametr Branch! Zprava dle RFC 2543. \n");
        if(((int)rdata->msg_info.from->tag.slen))
        {
            ast_log(LOG_NOTICE,"Parametr Tag ma hodnotu %.*s \n",(int)rdata->msg_info.from->tag.slen , rdata->msg_info.from->tag.ptr);
                       
        }
        else
        {
            ast_log(LOG_NOTICE,"Chybi parametr Tag! Zprava dle RFC 2543. \n");
        }
    }
   
    //Detekce NCL 
   
   if(!(rdata->msg_info.clen))
            {
            ast_log(LOG_NOTICE,"Chybi parametr Content-Length! Zprava dle RFC 2543. \n");	
			}
   
   else
            {
                ast_log(LOG_NOTICE,"Parametr Content-Length ma hodnotu %d \n",rdata->msg_info.clen->len);
            } 
      
      
            
   
        return PJ_SUCCESS;
   }
   
 
   // nazev modulu, priorita, funkce provedena pri prijmu a odesilani sip zpravy
static pjsip_module res_modul_xmelic20 = {
    .name = { "Zasuvny modul pro ustrednu Asterisk - stack PJSIP - Ondrej Melichar - v4", 72 },
    .priority = PJSIP_MOD_PRIORITY_TRANSPORT_LAYER - 2,
    .on_rx_request = analyza_paketu,
    .on_rx_response = analyza_paketu,
 
};
 
 
static int load_module(void)
{
   
    ast_log(AST_LOG_WARNING, "Zasuvny modul byl uspesne spusten! \n");
    ast_sip_register_service(&res_modul_xmelic20);
    return AST_MODULE_LOAD_SUCCESS;
}
 
static int unload_module(void)
{
    ast_sip_unregister_service(&res_modul_xmelic20);
    return 0;
}
 //registrace modulu pri nahrati, priorita modulu, pozadavky pro spusteni
AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "Zasuvny modul pro ustrednu Asterisk - stack PJSIP - Ondrej Melichar - v4",
 	.requires = "res_pjsip",
    .load = load_module,
    .unload = unload_module,
 	.load_pri = PJSIP_MOD_PRIORITY_TRANSPORT_LAYER  - 2,
);
