smpp34_unpack.c 7.92 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
/*
 * Copyright (C) 2006 Raul Tremsal
 * File  : smpp34_unpack.c
 * Author: Raul Tremsal <ultraismo@yahoo.com>
 *
 * This file is part of libsmpp34 (c-open-smpp3.4 library).
 *
 * The libsmpp34 library is free software; you can redistribute it and/or 
 * modify it under the terms of the GNU Lesser General Public License as 
 * published by the Free Software Foundation; either version 2.1 of the 
 * License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but 
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 
 * License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License 
 * along with this library; if not, write to the Free Software Foundation, 
 * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 
 *
 */
#include <stdio.h>
#include <string.h>
25
#include <stdlib.h>
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
#include <sys/types.h>
#include <netinet/in.h>

#include <stdint.h>

#include "smpp34.h"
#include "smpp34_structs.h"
#include "smpp34_params.h"

/* GLOBALS ********************************************************************/
/* EXTERN *********************************************************************/
extern int smpp34_errno;
extern char smpp34_strerror[2048];
extern char *ptrerror;

/* FUNCTIONS ******************************************************************/
int 
43
smpp34_unpack(uint32_t type, void* tt, const uint8_t *ptrBuf, int ptrLen)
44 45 46
{

    char dummy_b[SMALL_BUFF];
47 48
    const uint8_t *ini      = ptrBuf;
    const uint8_t *aux      = ptrBuf;
49 50 51 52 53 54 55 56 57 58 59 60 61 62
    int lenval = 0;
    int left = ptrLen;
    int lefterror = 0;

    memset(smpp34_strerror, 0, sizeof(smpp34_strerror));
    ptrerror = smpp34_strerror;
    lefterror = sizeof(smpp34_strerror);

#define instancia t1->

#define U32( inst, par, _str ){\
    lenval = sizeof( uint32_t );\
    if( lenval > left ){\
        PUTLOG("[%s:%08X(%s)]", par, inst par,\
63
                                      "Value length exceed buffer length");\
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
        return( -1 );\
    };\
    memcpy(&inst par, aux, lenval);\
    left -= lenval; aux += lenval;\
    inst par = ntohl( inst par );\
    _str(inst par, dummy_b);\
    if( strcmp("", dummy_b) == 0 ){\
        PUTLOG( "[%s:%08X(%s)]", par, inst par, "Invalid value");\
        return( -1 );\
    };\
    PUTLOG("[%s:%08X(%s)]", par, inst par, "OK");\
};

#define U16( inst, par, _str ){\
    lenval = sizeof( uint16_t );\
    if( lenval > left ){\
        PUTLOG("[%s:%04X(%s)]", par, inst par,\
81
                                      "Value length exceed buffer length");\
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
        return( -1 );\
    }\
    memcpy(&inst par, aux, lenval);\
    left -= lenval; aux += lenval;\
    inst par = ntohs( inst par );\
    _str(inst par, dummy_b);\
    if( strcmp("", dummy_b) == 0 ){\
        PUTLOG("[%s:%04X(%s)]", par, inst par, "Invalid value");\
        return( -1 );\
    };\
    PUTLOG("[%s:%04X(%s)]", par, inst par, "OK");\
};

#define U08( inst, par, _str ){\
    lenval = sizeof( uint8_t );\
    if( lenval > left ){\
        PUTLOG("[%s:%02X(%s)]", par, inst par,\
99
                                      "Value length exceed buffer length");\
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
        return( -1 );\
    };\
    memcpy(&inst par, aux, lenval);\
    left -= lenval; aux += lenval;\
    _str(inst par, dummy_b);\
    if( strcmp("", dummy_b) == 0 ){\
        PUTLOG( "[%s:%02X(%s)]", par, inst par, "Invalid value");\
        return( -1 );\
    };\
    PUTLOG("[%s:%02X(%s)]", par, inst par, "OK");\
};

/* NOTA: Importante, Los WARNINGs de los octetos, advierten de una 
 *       inconsistencia de largo en los octets/c_octets recibidos, segun se 
 *       decida el grado de restriccion estos pueden ser errores graves.
 *       La accion a tomar es truncar estos datos a los largos permitidos en la 
 *       especicacion, pero seguir parseando (saltando segun los largos 
 *       invalidos) los datos recibidos.
 */
#define O_C_OCTET( inst, par, sizeval ){\
    if( !(inst command_status) ){\
        C_OCTET( inst, par, sizeval );\
    } else {\
        PUTLOG("[%s:%s(%s)]", par, inst par, "OK");\
    };\
}


#define C_OCTET( inst, par, size ){\
    lenval = strlen( (char*) aux ) + 1;\
    if( lenval > left ){\
        PUTLOG("[len(%s):%d(%s)]", par, lenval, \
132
                                      "Value length exceed buffer length");\
133 134 135 136 137 138 139 140 141 142 143 144 145
        return( -1 );\
    };\
    if( lenval > size ){\
        PUTLOG("[%s:%s(%s)]", par, inst par, "Data length is invalid");\
        return( -1 );\
    } else {\
        /*snprintf(inst par, (lenval>size)?size:lenval, "%s", aux); */ \
        memcpy(inst par, aux, lenval);\
        left -= lenval; aux += lenval;\
        PUTLOG("[%s:%s(%s)]", par, inst par, "OK");\
    };\
}

146
#define OCTET8( inst, par, size, lenval ){\
147 148
    if( lenval > left ){\
        PUTLOG("[leng %s:%d(%s)]", par, lenval,\
149
                                      "Value length exceed buffer length");\
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
        return( -1 );\
    };\
    if( lenval > size ){\
        PUTLOG("[%s:%s(%s)]", par, "<bin>",\
                                      "Data length is invalid (truncate)");\
        return( -1 );\
    };\
    memcpy(&inst par, aux, (lenval>size)?size:lenval);\
    left -= lenval; aux += lenval;\
    PUTLOG("[%s:%s(%s)]", par, "<bin>", "OK");\
}

#define OCTET16( inst, par, size ){\
    uint16_t l_lenval = 0; /* VERIFICAR ESTO */\
    memcpy(&l_lenval, (inst par - sizeof(uint16_t)), sizeof(uint16_t));\
    if( l_lenval > left ){\
        PUTLOG("[leng %s:%d(%s)]", par, l_lenval,\
167
                                      "Value length exceed buffer length");\
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242
        return( -1 );\
    };\
    if( l_lenval > size ){\
        PUTLOG("[%s:%s(%s)]", par, "<bin>", "Data length is invalid");\
        return( -1 );\
    };\
    PUTLOG("[%s:%s(%s)]", par, "<bin>", "OK");\
    memcpy(&(inst par), aux, (l_lenval>size)?size:l_lenval);\
    left -= l_lenval; aux += l_lenval;\
}

#define TLV( inst, tlv3, do_tlv ){\
    tlv_t *aux_tlv = NULL;\
    while( (aux - ini) < t1->command_length ){\
        aux_tlv = (tlv_t *) malloc(sizeof( tlv_t ));\
        memset(aux_tlv, 0, sizeof(tlv_t));\
        do_tlv( aux_tlv );\
        aux_tlv->next = inst tlv3;\
        inst tlv3 = aux_tlv;\
    };\
};

#define UDAD( inst, udad3, do_udad ){\
    udad_t *aux_udad = NULL;\
    int c = 0;\
    while( c < t1->no_unsuccess ){\
        aux_udad = (udad_t *) malloc(sizeof( udad_t ));\
        memset(aux_udad, 0, sizeof(udad_t));\
        do_udad( aux_udad );\
        aux_udad->next = inst udad3;\
        inst udad3 = aux_udad;\
        c++;\
    };\
};

#define DAD( inst, dad3, do_dad ){\
    dad_t *aux_dad = NULL;\
    int c = 0;\
    while( c < t1->number_of_dests ){\
        aux_dad = (dad_t *) malloc(sizeof( dad_t ));\
        memset(aux_dad, 0, sizeof(dad_t));\
        do_dad( aux_dad );\
        aux_dad->next = inst dad3;\
        inst dad3 = aux_dad;\
        c++;\
    };\
};

#include "def_frame/alert_notification.tlv"
#include "def_frame/bind_receiver_resp.tlv"
#include "def_frame/bind_transceiver_resp.tlv"
#include "def_frame/bind_transmitter_resp.tlv"
#include "def_frame/data_sm.tlv"
#include "def_frame/data_sm_resp.tlv"
#include "def_frame/deliver_sm.tlv"
#include "def_frame/submit_multi_resp.udad"
#include "def_frame/submit_multi.dad"
#include "def_frame/submit_multi.tlv"
#include "def_frame/submit_sm.tlv"
#include "def_list/smpp34_protocol.def"

    { /* Hace algunas Verificaciones **************************************/
        uint32_t len_orig;
        memcpy(&len_orig, tt, sizeof(uint32_t));
        if( len_orig != (aux - ini) ){
            PUTLOG("[%s:(Error in PDU length %d!=%d)]",PDU,len_orig,(uint32_t)(aux-ini));
            return( -1 );
        };
    };

#include "def_frame/clean.frame"
    return( 0 );
};

int 
243
smpp34_unpack2(void* tt, const uint8_t *ptrBuf, int ptrLen)
244 245 246 247 248 249 250
{
    uint32_t cmdid;
    uint32_t tempo;
    memcpy(&tempo, ptrBuf + 4, sizeof(uint32_t)); /* get command_id PDU */
    cmdid = ntohl( tempo );
    return( smpp34_unpack(cmdid, tt, ptrBuf, ptrLen) );
};