⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 isup.c

📁 asterisk1.4.6版本下 7#信令驱动 源码
💻 C
📖 第 1 页 / 共 2 页
字号:
/* isup.c - ISUP stuff.
 *
 * Copyright (C) 2005-2006, Sifira A/S.
 *
 * Author: Kristian Nielsen <kn@sifira.dk>
 *         Anders Baekgaard <ab@sifira.dk>
 *         Anders Baekgaard <ab@dicea.dk>
 *
 * This file is part of chan_ss7.
 *
 * chan_ss7 is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * chan_ss7 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with chan_ss7; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

#include <stdarg.h>
#include <string.h>
#include <stdio.h>
#include <sys/time.h>

#include "asterisk/logger.h"


#include <netinet/in.h>
#include "config.h"
#include "isup.h"
#include "mtp.h"


/* Generic ISUP message parameter decoding.
   First two parameters are the raw bytes of the SIF field and the length of it.
   Then comes quadruples of (PARAMETER, SIZE, DECODER, DATA) for the mandatory
   fixed part, followed by a single `0'.
   Then comes triples of (PARAMETER, DECODER, DATA) for the mandatory variable
   part, followed by a single `0'.
   Finally triples of (PARAMETER, DECODER, DATA) for the optional part, followed
   by a single `0'. */
int param_decode(unsigned char *buf, int buflen, ...) {
  va_list args;
  struct {
    enum isup_parameter_code param_type;
    decoder_t decoder;
    void *decoder_data;
  } opt_decoders[100];
  int num_opt_decoders;
  enum isup_parameter_code type;
  int i, j;
  int res;

  va_start(args, buflen);
  i = 0;

  /* First do the mandatory fixed part. */
  while((type = va_arg(args, typeof(type))) != 0) {
    int param_len = va_arg(args, int);
    decoder_t decoder = va_arg(args, decoder_t);
    void *data = va_arg(args, void *);

    if(i + param_len > buflen) {
      ast_log(LOG_NOTICE, "Short ISUP message for parameter type %d, "
              "len %d < %d.\n", type, buflen, i + param_len);
      return 0;
    }

    if(decoder != NULL) {
      res = (*decoder)(&(buf[i]), param_len, data);
      if(!res) {
        return res;
      }
    }

    i += param_len;
  }

  /* Next do the mandatory variable part. */
  while((type = va_arg(args, typeof(type))) != 0) {
    decoder_t decoder = va_arg(args, decoder_t);
    void *data = va_arg(args, void *);
    int param_start, param_len;

    if(i >= buflen) {
      ast_log(LOG_NOTICE, "Short ISUP message for parameter type %d, "
              "len %d < %d.\n", type, buflen, i + 1);
      return 0;
    }
    param_start = i + buf[i];
    if(i >= buflen) {
      ast_log(LOG_NOTICE, "Short ISUP message for parameter type %d, "
              "len %d < %d.\n", type, buflen, i + 1);
      return 0;
    }
    param_len = buf[param_start++];
    if(param_start + param_len > buflen) {
      ast_log(LOG_NOTICE, "Short ISUP message for parameter type %d, "
              "len %d < %d.\n", type, buflen, param_start + param_len);
      return 0;
    }
    if(decoder != NULL) {
      res = (*decoder)(&(buf[param_start]), param_len, data);
      if(!res) {
        return res;
      }
    }

    i++;
  }

  /* Finally do the optional part. First build a list of all decoders. */
  for(j = 0; (type = va_arg(args, typeof(type))) != 0; j++) {
    if(j >= sizeof(opt_decoders)/sizeof(opt_decoders[0])) {
      ast_log(LOG_ERROR, "Fatal: too many decoders.\n");
      return 0;
    }
    opt_decoders[j].param_type = type;
    opt_decoders[j].decoder = va_arg(args, decoder_t);
    opt_decoders[j].decoder_data = va_arg(args, void *);
  }
  va_end(args);
  num_opt_decoders = j;

  if(num_opt_decoders == 0) {
    /* There are no optional parameters needed, so we are done. */
    return 1;
  }

  /* Find the start of the optional part. */
  if(i >= buflen) {
    ast_log(LOG_NOTICE, "Short ISUP message for optional part, len %d < %d.\n",
            buflen, i + 1);
    return 0;
  }
  if(buf[i] == 0) {
    /* No optional parameters are present in the message. */
    return 1;
  }
  i = i + buf[i];

  /* Loop over each parameter in the optional section. */
  for(;;) {
    enum isup_parameter_code type;
    int param_len;

    if(i + 1 > buflen) {
      ast_log(LOG_NOTICE, "Short ISUP message for optional part, len %d < %d.\n",
              buflen, i + 1);
      return 0;
    }
    type = buf[i];
    if(type == 0) {
      /* End of optional parameters. */
      return 1;
    }
    if(i + 2 > buflen) {
      ast_log(LOG_NOTICE, "Short ISUP message for optional parameter type %d, "
              "len %d < %d.\n", type, buflen, i + 2);
      return 0;
    }
    param_len = buf[i + 1];
    if(i + 2 + param_len > buflen) {
      ast_log(LOG_NOTICE, "Short ISUP message for optional parameter type %d, "
              "len %d < %d.\n", type, buflen, i + 2 + param_len);
      return 0;
    }

    /* Call any matching decoder. */
    for(j = 0; j < num_opt_decoders; j++) {
      if(opt_decoders[j].param_type == type) {
        if(opt_decoders[j].decoder != NULL) {
          res = (*(opt_decoders[j].decoder))(&(buf[i + 2]), param_len, opt_decoders[j].decoder_data);
          if(!res) {
            return res;
          }
        }
        break;
      }
    }
    i = i + 2 +param_len;
  }
}

/* Decode parameter 0x29, "optional backward call indicators" (Q.763 (3.37)). */
static int decode_optional_backward_call_indicators(unsigned char *p, int len, void *data) {
  int *event_info_ptr = data;

  if(len < 1) {
    ast_log(LOG_NOTICE, "Short parameter 'optional backward call indicator', len %d < 1.\n", len);
    return 0;
  }
  *event_info_ptr = p[0] & 0xf;
  return 1;
}

/* Decode parameter 0x24, "event information" (Q.763 (3.21)). */
static int decode_event_info(unsigned char *p, int len, void *data) {
  int *event_info_ptr = data;

  if(len < 1) {
    ast_log(LOG_NOTICE, "Short parameter 'event information', len %d < 1.\n", len);
    return 0;
  }
  *event_info_ptr = p[0] & 0x7f;
  return 1;
}

/* Decode parameter 0x12 "cause indicators" (Q.763 (see Q.850 for values). */
static int decode_rel_cause(unsigned char *p, int len, void *data) {
  int *cause_ptr = data;

  if(len < 2) {
    ast_log(LOG_NOTICE, "Short parameter 'cause indicators', len %d < 2.\n", len);
    return 0;
  }
  *cause_ptr = p[1] & 0x7f;
  return 1;
}

/* Decode parameter 0x22 "suspend resume indicators" (Q.763 (3.52). */
static int decode_suspend_resume(unsigned char *p, int len, void *data) {
  int *indicator = data;

  if(len < 1) {
    ast_log(LOG_NOTICE, "Short parameter 'suspend/resume indicators', len %d < 1.\n", len);
    return 0;
  }
  *indicator = p[0];
  return 1;
}

/* Decode parameter 0x11, "backwards call indicators" (Q.763 (3.5)). */
static int decode_backwards_ind(unsigned char *p, int len, void *data) {
  struct isup_backwards_call_ind *ind_ptr = data;

  if(len < 2) {
    ast_log(LOG_NOTICE, "Short parameter 'cause indicators', len %d < 2.\n", len);
    return 0;
  }
  ind_ptr->called_party_status = (p[0] >> 2) & 0x3;
  ind_ptr->charge_indicator = p[0] & 0x3;
  return 1;
}

/* Decode parameter 0x16 "range and status". */
static int decode_range_and_status(unsigned char *p, int len, void *data) {
  struct isup_range_and_status *parm = data;
  int status_len;

  if(len < 1) {
    ast_log(LOG_NOTICE, "Short parameter 'range and status', len %d < 1.\n", len);
    return 0;
  }
  parm->range = p[0];

  if(parm->range == 0) {
    ast_log(LOG_NOTICE, "Invalid range 0 (must be >= 1) in range and status.\n");
    return 0;
  }

  status_len = ((parm->range + 1) + 7)/8;
  if(len < 1 + status_len) {
    ast_log(LOG_NOTICE, "Short parameter 'range and status', len %d < %d.\n",
            len, 1 + status_len);
    return 0;
  }

  memcpy(parm->status, &p[1], status_len);
  return 1;
}

/* Decode parameter 0x16 "range and status", in the variant used in the GRS
   message type where status is missing. */
static int decode_range_no_status(unsigned char *p, int len, void *data) {
  int *range_ptr = data;

  if(len < 1) {
    ast_log(LOG_NOTICE, "Short parameter 'range and no status', len %d < 1.\n", len);
    return 0;
  }
  *range_ptr = p[0];
  return 1;
}

/* Decode parameter 0x15 "circuit group supervision message type indicator"
   (Q.763 (3.13)). */
static int decode_cgsmti(unsigned char *p, int len, void *data) {
  int *cgsmti_ptr = data;
  int cgsmti;

  if(len < 1) {
    ast_log(LOG_NOTICE, "Short parameter 'circuit group supervision message "
            "type indicator', len %d < 1.\n", len);
    return 0;
  }
  cgsmti = p[0] & 0x3;
  if(cgsmti != 0 && cgsmti != 1) {
    ast_log(LOG_NOTICE, "Unimplemented 'circuit group supervision message "
            "type indicator' value %d.\n", cgsmti);
    return 0;
  }
  *cgsmti_ptr = cgsmti;
  return 1;
}

/* Decode parameter 0x6 "nature of connection indicators" (Q.763 (3.35)).
   For now, only decodes the "continuity check required" part. */
static int decode_noci_contcheck(unsigned char *p, int len, void *data) {
  struct iam *iam = data;

  if(len < 1) {
    ast_log(LOG_NOTICE, "Short parameter 'nature of connection indicators', "
            "len %d < 1.\n", len);
    return 0;
  }
  iam->contcheck   = ((p[0] >> 2) & 0x3) == 0x1;
  iam->echocontrol =  (p[0] >> 4) & 0x1;
  return 1;
}


static int decode_transmission_medium(unsigned char *p, int len, void *data) {
  struct iam *iam = data;

  if(len < 1) {
    ast_log(LOG_NOTICE, "Short parameter 'Transmission medium requirement', "
            "len %d < 1.\n", len);
    return 0;
  }
  iam->trans_medium = p[0];
  return 1;
}


/* Decode parameter 0x13 "redirection information" (Q.763 (3.45)). */
static int decode_redir_inf(unsigned char *p, int len, void *data) {
  struct isup_redir_info *redir_inf_ptr = data;

  if(len < 1) {
    ast_log(LOG_NOTICE, "Short parameter 'redirection information', "
            "len %d < 1.\n", len);
    return 0;
  }

  redir_inf_ptr->is_redirect = 1;
  if(len >= 2) {
    redir_inf_ptr->reason = (p[1] >> 4) & 0xf;
  } else {
    redir_inf_ptr->reason = 0;
  }

  return 1;
}

static void clear_isup_phonenum(struct isup_phonenum *num) {
  num->present = 0;
  num->restricted = 0;
  num->complete = 0;
  memset(num->num, 0, sizeof(num->num));
}

static int decode_isup_sni(unsigned char *p, int len, void *data) {
  static char digits[] = "0123456789ABCDE.";
  struct isup_phonenum *n = data;
  int i, j;
  int num_dig;

  if(len < 2) {
    ast_log(LOG_NOTICE, "Short parameter for ISUP phone number, len %d < 2.\n",
            len);
    return 0;
  }

  /* Two digits per byte, but only one digit in last byte if odd number of
     digits. */
  num_dig = (len-1)*2 - (p[0] & 0x80 ? 1 : 0);

  i = 0;
  /* Handle international number. */

  if(num_dig > PHONENUM_MAX) {
    ast_log(LOG_NOTICE, "Too many digits in phone number %d > %d, truncated.\n",
            num_dig, PHONENUM_MAX);
    num_dig = PHONENUM_MAX;
  }

  /* TODO: if p[0]=128 -> only first digit! */
  j = 1;
  while(i < num_dig) {
    int dig = p[j] & 0x0f;
    if(dig == 0xf) {
      n->complete = 1;
      break;
    }
    n->num[i++] = digits[dig];
    if(i < num_dig) {
      int dig = (p[j++] >> 4) & 0xf;
      if(dig == 0xf) {
        n->complete = 1;
        break;
      }
      n->num[i++] = digits[dig];
    }
  }
  n->num[i] = '\0';

  return 1;
}

/* Decode ISUP phonenum parameters:
     0x4 "called party number" (Q.763 (3.9))
     0xa "calling party number" (Q.763 (3.10))
     0xb "redirecting number" (Q.763 (3.44)) */
int decode_isup_phonenum(int with_presentation_restrict, unsigned char *p, int len, void *data) {
  static char digits[] = "0123456789ABCDE.";
  struct isup_phonenum *n = data;
  int i, j;
  int num_dig;
  int nature_of_adr_ind;

  if(len < 2) {
    ast_log(LOG_NOTICE, "Short parameter for ISUP phone number, len %d < 2.\n",
            len);
    return 0;
  }

  if(with_presentation_restrict) {
    switch((p[1] >> 2) & 0x3) {
      case 0:                   /* Presentation allowed */
        n->present = 1;
        n->restricted = 0;
        n->complete = 0;
        break;

      case 1:                   /* Presentation restricted */
        n->present = 1;
        n->restricted = 1;
        n->complete = 0;
        break;

      case 2:                   /* Address not available */
        n->present = 0;
        n->restricted = 0;
        n->complete = 1;
        break;

      case 3:                   /* Reserved */
        ast_log(LOG_NOTICE, "Found presentation restrict type 0x3, assuming "
                "not restricted and not complete.\n");
        n->present = 1;
        n->restricted = 0;
        n->complete = 0;
        break;

      default:
        ast_log(LOG_ERROR, "This cannot happen!?!.\n");
    }
  } else {
    n->present = 1;
    n->restricted = 0;
    n->complete = 0;
  }

  memset(n->num, 0, sizeof(n->num));
  if(len == 2) {
    ast_log(LOG_DEBUG, "No digits in phone number.\n");
    return 1;
  }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -