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

📄 pdu.c

📁 GSM猫管理程序
💻 C
📖 第 1 页 / 共 4 页
字号:
                *time = 0;              }              else if (atoi(time) > 23 || atoi(time +3) > 59 || atoi(time +6) > 59)              {                // Not a fatal error (?)                //pdu_error(err_str, 0, Src_Pointer -full_pdu, 6, "Invalid value(s) in time of SMSC Timestamp: \"%s\"", time);                //*time = 0;                add_warning(warning_headers, "Invalid value(s) in time of SMSC Timestamp.");              }              if (!(*err_str))              {                Src_Pointer += 6;                // Time zone is not used but bytes are checked:                if (octet2bin_check(Src_Pointer) < 0)                  pdu_error(err_str, 0, Src_Pointer -full_pdu, 2,                            "Invalid character(s) in Time Zone of SMSC Time Stamp: \"%.2s\"", Src_Pointer);                else                  Src_Pointer += 2;              }            }          }          if (!(*err_str))          {            if (strlen(Src_Pointer) < 14)              pdu_error(err_str, 0, Src_Pointer -full_pdu, 14, "While trying to read Discharge Timestamp: %s", err_too_short);            else            {              // get Discharge timestamp              sprintf(temp,"%c%c-%c%c-%c%c %c%c:%c%c:%c%c",Src_Pointer[1],Src_Pointer[0],Src_Pointer[3],Src_Pointer[2],Src_Pointer[5],Src_Pointer[4],Src_Pointer[7],Src_Pointer[6],Src_Pointer[9],Src_Pointer[8],Src_Pointer[11],Src_Pointer[10]);              if (!isdigit(temp[0]) || !isdigit(temp[1]) || !isdigit(temp[3]) || !isdigit(temp[4]) || !isdigit(temp[6]) || !isdigit(temp[7]) ||                   !isdigit(temp[9]) || !isdigit(temp[10]) || !isdigit(temp[12]) || !isdigit(temp[13]) || !isdigit(temp[15]) || !isdigit(temp[16]))                pdu_error(err_str, 0, Src_Pointer -full_pdu, 12, "Invalid character(s) in Discharge Timestamp: \"%s\"", temp);              else if (atoi(temp +3) > 12 || atoi(temp +6) > 31 || atoi(temp +9) > 24 || atoi(temp +12) > 59 || atoi(temp +16) > 59)              {                // Not a fatal error (?)                //pdu_error(err_str, 0, Src_Pointer -full_pdu, 12, "Invalid value(s) in Discharge Timestamp: \"%s\"", temp);                add_warning(warning_headers, "Invalid values(s) in Discharge Timestamp.");              }              if (!(*err_str))              {                Src_Pointer += 12;                // Time zone is not used but bytes are checked:                if (octet2bin_check(Src_Pointer) < 0)                  pdu_error(err_str, 0, Src_Pointer -full_pdu, 2,                            "Invalid character(s) in Time Zone of Discharge Time Stamp: \"%.2s\"", Src_Pointer);                else                  Src_Pointer += 2;              }            }            if (!(*err_str))            {              sprintf(strchr(result, 0), "Discharge_timestamp: %s", temp);              if (strlen(Src_Pointer) < 2)                pdu_error(err_str, 0, Src_Pointer -full_pdu, 2, "While trying to read Status octet: %s", err_too_short);              else              {                // get Status                if ((status = octet2bin_check(Src_Pointer)) < 0)                  pdu_error(err_str, 0, Src_Pointer -full_pdu, 2, "Invalid Status octet: \"%.2s\"", Src_Pointer);                else                {                  switch (status)                  {                    case 0: p = "Ok,short message received by the SME"; break;                    case 1: p = "Ok,short message forwarded by the SC to the SME but the SC is unable to confirm delivery"; break;                    case 2: p = "Ok,short message replaced by the SC"; break;                    case 32: p = "Still trying,congestion"; break;                    case 33: p = "Still trying,SME busy"; break;                    case 34: p = "Still trying,no response sendr SME"; break;                    case 35: p = "Still trying,service rejected"; break;                    case 36: p = "Still trying,quality of service not available"; break;                    case 37: p = "Still trying,error in SME"; break;                    case 64: p = "Error,remote procedure error"; break;                    case 65: p = "Error,incompatible destination"; break;                    case 66: p = "Error,connection rejected by SME"; break;                    case 67: p = "Error,not obtainable"; break;                    case 68: p = "Error,quality of service not available"; break;                    case 69: p = "Error,no interworking available"; break;                    case 70: p = "Error,SM validity period expired"; break;                    case 71: p = "Error,SM deleted by originating SME"; break;                    case 72: p = "Error,SM deleted by SC administration"; break;                    case 73: p = "Error,SM does not exist"; break;                    case 96: p = "Error,congestion"; break;                    case 97: p = "Error,SME busy"; break;                    case 98: p = "Error,no response sendr SME"; break;                    case 99: p = "Error,service rejected"; break;                    case 100: p = "Error,quality of service not available"; break;                    case 101: p = "Error,error in SME"; break;                    default: p = "unknown";                  }                  sprintf(strchr(result, 0), "\n%s %i,%s", SR_Status, status, p);                }              }            }          }        }      }    }  }  return strlen(result);}// Splits a PDU string into the parts // Input: // pdu is the pdu string// mode can be old or new and selects the pdu version// Output:// alphabet indicates the character set of the message.// sendr Sender// date and time Date/Time-stamp// message is the message text or binary message// smsc that sent this message// with_udh return the udh flag of the message// is_statusreport is 1 if this was a status report// is_unsupported_pdu is 1 if this pdu was not supported// Returns the length of the message int splitpdu(char *pdu, char *mode, int *alphabet, char *sendr, char *date, char *time, char *message,             char *smsc, int *with_udh, char *a_udh_data, char *a_udh_type, int *is_statusreport,             int *is_unsupported_pdu, char *from_toa, int *report, int *replace, char *warning_headers){  int Length;  int Type;  char* Pointer;  int result = 0;  char *err_str = NULL;  char *save_err_str = NULL;  char *try_mode = mode;  int try_count;  int i;  int message_length;  int expected_length;  char tmp_udh_data[SIZE_UDH_DATA] = {};  char tmp_udh_type[SIZE_UDH_TYPE] = {};  char *udh_data;  char *udh_type;  udh_data = (a_udh_data)? a_udh_data : tmp_udh_data;  udh_type = (a_udh_type)? a_udh_type : tmp_udh_type;  // Patch for Wavecom SR memory reading:  if (strncmp(pdu, "000000FF00", 10) == 0)  {    strcpy(pdu, pdu +8);    while (strlen(pdu) < 52)      strcat(pdu, "00");  }  // ------------------------------------  for (try_count = 0; try_count < 2; try_count++)  {    if (try_count)    {      if (strcmp(mode, "new") == 0)        try_mode = "old";      else        try_mode = "new";    }    message_length = 0;    expected_length = 0;    sendr[0]=0;    date[0]=0;    time[0]=0;    message[0]=0;    smsc[0]=0;     *alphabet=0;    *with_udh=0;    *udh_data = 0;    *udh_type = 0;    *is_statusreport = 0;    *is_unsupported_pdu = 0;    from_toa[0] = 0;    *report = 0;    *replace = -1;    if (warning_headers)      *warning_headers = 0;#ifdef DEBUGMSG  printf("!! splitpdu(pdu=%s, mode=%s, ...)\n",pdu,mode);#endif    Pointer=pdu;    if (strlen(Pointer) < 2)      pdu_error(&err_str, 0, Pointer -pdu, 2, "While trying to read first octet: %s", err_too_short);    else    {      if (strcmp(try_mode, "new") == 0)      {        if ((Length = octet2bin_check(Pointer)) < 0)          pdu_error(&err_str, 0, Pointer -pdu, 2, "While reading first octet: %s", err_pdu_content);        else        {          // smsc number is not mandatory          if (Length == 0)            Pointer += 2;          else          {            // Address type and at least one octet is expected:            if (Length < 2 || Length > MAX_SMSC_ADDRESS_LENGTH)              pdu_error(&err_str, 0, Pointer -pdu, 2, "Invalid sender SMSC address length: \"%.2s\"", Pointer);            else            {              Length = Length *2 -2;              // No padding because the given value is number of octets.              if (strlen(Pointer) < Length +4)                pdu_error(&err_str, 0, Pointer -pdu, Length +4, "While trying to read sender SMSC address (length %i): %s",                          Length, err_too_short);              else              {                Pointer += 2;                i = octet2bin_check(Pointer);                if (i < 0)                  pdu_error(&err_str, 0, Pointer -pdu, 2, "Invalid sender SMSC address type: \"%.2s\"", Pointer);                else if (i < 0x80)                  pdu_error(&err_str, 0, Pointer -pdu, 2, "Missing bit 7 in sender SMSC address type: \"%.2s\"", Pointer);                else                {                  Pointer += 2;                  strncpy(smsc, Pointer, Length);                  smsc[Length] = 0;                  swapchars(smsc);                  // Does any SMSC use alphanumeric number?                  if ((i & 112) == 80)                  {                    // There can be only hex digits from the original PDU.                    // The number itself is wrong in this case.                    for (i = 0; smsc[i]; i++)                      if (!isXdigit(smsc[i]))                      {                        pdu_error(&err_str, 0, Pointer -pdu, Length, "Invalid character(s) in alphanumeric SMSC address: \"%s\"", smsc);                        *smsc = 0;                        break;                      }                  }                  else                  {                    // Last character is allowed as F (and dropped) but all other non-numeric will produce an error:                    if (smsc[Length -1] == 'F')                      smsc[Length -1] = 0;                    for (i = 0; smsc[i]; i++)                      if (!isdigit(smsc[i]))                      {                        // Not a fatal error (?)                        //pdu_error(&err_str, 0, Pointer -pdu, Length, "Invalid character(s) in numeric SMSC address: \"%s\"", smsc);                        //*smsc = 0;                        add_warning(warning_headers, "Invalid character(s) in numeric SMSC address");                        break;                      }                  }                  if (!err_str)                    Pointer += Length;                }              }            }          }        }      }      if (!err_str)      {        if (strlen(Pointer) < 2)          pdu_error(&err_str, 0, Pointer -pdu, 2, "While trying to read First octet of the SMS-DELIVER PDU: %s", err_too_short);        else        {          if ((i = octet2bin_check(Pointer)) < 0)            pdu_error(&err_str, 0, Pointer -pdu, 2, "While reading First octet of the SMS-DELIVER PDU: %s", err_pdu_content);          else          {            // Unused bits 3 and 4 should be zero, failure with this produces a warning:            if (i & 0x18)              add_warning(warning_headers, "Unused bits 3 and 4 are used in the first octet of the SMS-DELIVER PDU.");            if (i & 0x40) // Is UDH bit set?              *with_udh = 1;            if (i & 0x20) // Is status report going to be returned to the SME?              *report = 1;            Type = i & 3;            if (Type == 0) // SMS Deliver            {              Pointer += 2;              result = split_type_0(pdu, Pointer, alphabet, sendr, date, time, message, &message_length, &expected_length,                                    *with_udh, udh_data, udh_type, from_toa, replace, &err_str, warning_headers);              if (err_str && *udh_type)                pdu_error(&err_str, "", -1, 0, "Message has Udh_type: %s", udh_type);              // If a decoding fails, the reason is invalid or missing characters               // in the PDU. Can also be too high TP-UDL value.               // Decoders are modified to return partially decoded strings with an              // additional length variables. Binary messages are not shown in the report.              if (*alphabet != 1 && err_str && message_length > 0)              {                char ascii[MAXTEXT];                char title[101];                if (*alphabet <= 0)                  i = gsm2iso(message, message_length, ascii, sizeof(ascii));                else                {                  memcpy(ascii, message, message_length);                  ascii[message_length] = 0;                  i = decode_ucs2(ascii, message_length);                  expected_length /= 2;                }                if (i > 0)                {                  sprintf(title, "Partial content of text (%i characters, expected %i):\n", i, expected_length);                  pdu_error(&err_str, title, -1, 0, "%s", ascii);                }              }            }            else if (Type == 2) // Status Report            {              Pointer += 2;              result = split_type_2(pdu, Pointer, sendr, date, time, message, from_toa, &err_str, warning_headers);              *is_statusreport=1;            }            else if (Type == 1) // Sent message            {              pdu_error(&err_str, "", -1, 0, "%s%.2s%s", "The PDU data (", Pointer,                        ") says that this is a sent message. Can only decode received messages.");              *is_unsupported_pdu = 1;            }            else            {              pdu_error(&err_str, "", -1, 0, "%s%.2s%s%i%s", "The PDU data (", Pointer, ") says that the message format is ",                        Type, " which is not supported. Cannot decode.");              *is_unsupported_pdu = 1;            }          }        }      }    }    if (!err_str)      try_count++; // All ok, no more tries required.    else    {      *alphabet = 0;      *with_udh = 0;      // Possible udh_data is now incorrect:      *udh_data = 0;      *udh_type = 0;      *is_statusreport = 0;      if (try_count == 0)      {        // First try. Save the result and try again with another PDU mode.        if ((save_err_str = (char *)malloc(strlen(err_str) +1)))          strcpy(save_err_str, err_str);      }      else      {        // Second try. Nothing more to do. Return report and some information.        char *n_mode = "new (with CSA)";        char *o_mode = "old (without CSA)";        *message = 0;        if (save_err_str)          sprintf(message, "First tried with PDU mode %s:\n%s\nNext ", (*mode == 'n')? n_mode : o_mode, save_err_str);        sprintf(strchr(message, 0), "tried with PDU mode %s:\n%s\n", (*mode == 'n')? o_mode : n_mode, err_str);        sprintf(strchr(message, 0), "No success. This PDU cannot be decoded. There is something wrong.\n");        if (!(*is_unsupported_pdu))          strcat(message, "\nIf you are unsure, confused or angry, please view the GSM 03.40\n"                          "(ETSI TS 100 901) and related documents for details of correct\n"                          "PDU format. You can also get some help via the Support Website.\n");        *is_unsupported_pdu = 1;      }      result = strlen(message);      free(err_str);      err_str = NULL;    }  }  if (save_err_str)    free(save_err_str);  return result;}

⌨️ 快捷键说明

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