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

📄 pdu.c

📁 GSM猫管理程序
💻 C
📖 第 1 页 / 共 4 页
字号:
    }    else      binary[octetcounter] = i;  }  if (octets -skip_octets >= 0)    binary[octets -skip_octets] = 0;  *data_length = octets -skip_octets;  return octets -skip_octets;}int explain_toa(char *dest, char *octet_char, int octet_int){  int result;  char *p;  if (octet_char)    result = octet2bin_check(octet_char);  else    result = octet_int;  if (result != -1)  {    switch ((result & 0x70) >> 4)    {      case 0: p = "unknown"; break;      case 1: p = "international"; break;      case 2: p = "national"; break;      case 3: p = "network specific"; break;      case 4: p = "subsciber"; break;      case 5: p = "alphanumeric"; break;      case 6: p = "abbreviated"; break;      case 7: p = "reserved"; break;    }    if (octet_char)      sprintf(dest, "%.2s %s", octet_char, p);    else      sprintf(dest, "%02X %s", octet_int, p);    switch (result & 0x0F)    {      case 0: p = "unknown"; break;      case 1: p = "ISDN/telephone"; break;      case 3: p = "data"; break;      case 4: p = "telex"; break;      case 8: p = "national"; break;      case 9: p = "private"; break;      case 10: p = "ERMES"; break;      default: p = "reserved"; break;    }    sprintf(strchr(dest, 0), ", %s", p);  }  return result;}// Subroutine for messages type 0 (SMS-Deliver)// Input:// Src_Pointer points to the PDU string// Output:// sendr Sender// date and time Date/Time-stamp// message the message text or binary data// returns length of messageint split_type_0(char *full_pdu, char* Src_Pointer, int* alphabet, char* sendr, char* date, char* time,                 char* message, int *message_length, int *expected_length, int with_udh, char* udh_data,                 char *udh_type, char *from_toa, int *replace, char **err_str, char *warning_headers){  int Length;  int padding;  char tmpsender[100];  int result = 0;  int i;  int errorpos;#ifdef DEBUGMSG  printf("!! split_type_0(Src_Pointer=%s, ...\n",Src_Pointer);#endif  // There should be at least address-length and address-type:  if (strlen(Src_Pointer) < 4)    pdu_error(err_str, 0, Src_Pointer -full_pdu, 4, "While trying to read address length and address type: %s", err_too_short);  else  {    Length = octet2bin_check(Src_Pointer);    if (Length < 1 || Length > MAX_ADDRESS_LENGTH)      pdu_error(err_str, 0, Src_Pointer -full_pdu, 2, "Invalid sender address length: \"%.2s\"", Src_Pointer);    else    {      padding=Length%2;      Src_Pointer+=2;      i = explain_toa(from_toa, Src_Pointer, 0);      if (i < 0)        pdu_error(err_str, 0, Src_Pointer -full_pdu, 2, "Invalid sender address type: \"%.2s\"", Src_Pointer);      else if (i < 0x80)        pdu_error(err_str, 0, Src_Pointer -full_pdu, 2, "Missing bit 7 in sender address type: \"%.2s\"", Src_Pointer);      else      {        Src_Pointer += 2;        if ((i & 112) == 80)        {  // Sender is alphanumeric          if (strlen(Src_Pointer) < Length +padding)            pdu_error(err_str, 0, Src_Pointer -full_pdu, Length +padding,                      "While trying to read sender address (alphanumeric, length %i): %s",                      Length +padding, err_too_short);          else          {            snprintf(tmpsender,Length+3+padding,"%02X%s",Length*4/7,Src_Pointer);            if (pdu2text0(tmpsender, sendr) < 0)              pdu_error(err_str, 0, Src_Pointer -full_pdu, Length +padding,                        "While reading alphanumeric sender address: %s", err_pdu_content);          }        }        else        {  // sender is numeric          if (strlen(Src_Pointer) < Length +padding)            pdu_error(err_str, 0, Src_Pointer -full_pdu, Length +padding,                      "While trying to read sender address (numeric, length %i): %s",                      Length +padding, err_too_short);          else          {            strncpy(sendr, Src_Pointer, Length +padding);            sendr[Length +padding] = 0;            swapchars(sendr);            i = Length +padding -1;            if (padding)            {              if (sendr[i] != 'F')                add_warning(warning_headers, "Length of numeric sender address is odd, but not terminated with 'F'.");              else                sendr[i] = 0;            }            else            {              if (sendr[i] == 'F')              {                add_warning(warning_headers, "Length of numeric sender address is even, but still was terminated with 'F'.");                sendr[i] = 0;              }            }            for (i = 0; sendr[i]; i++)              if (!isdigit(sendr[i]))              {                // Not a fatal error (?)                //pdu_error(err_str, 0, Src_Pointer -full_pdu, Length +padding, "Invalid character(s) in sender address: \"%s\"", sendr);                //*sendr = 0;                add_warning(warning_headers, "Invalid character(s) in sender address.");                break;              }          }        }      }    }    if (!(*err_str))    {      Src_Pointer += Length +padding;      // Next there should be:      // XX protocol identifier      // XX data encoding scheme      // XXXXXXXXXXXXXX time stamp, 7 octets      // XX length of user data      // ( XX... user data  )      if (strlen(Src_Pointer) < 20)        pdu_error(err_str, 0, Src_Pointer -full_pdu, 20, "While trying to read TP-PID, TP-DSC, TP-SCTS and TP-UDL: %s", err_too_short);      else      {        if ((i = octet2bin_check(Src_Pointer)) < 0)          pdu_error(err_str, 0, Src_Pointer -full_pdu, 2, "Invalid protocol identifier: \"%.2s\"", Src_Pointer);        else        {          if ((i & 0xF8) == 0x40)            *replace = (i & 0x07);          Src_Pointer += 2;          if ((i = octet2bin_check(Src_Pointer)) < 0)            pdu_error(err_str, 0, Src_Pointer -full_pdu, 2, "Invalid data encoding scheme: \"%.2s\"", Src_Pointer);          else          {            *alphabet = (i & 0x0C) >>2;            if (*alphabet == 3)              pdu_error(err_str, 0, Src_Pointer -full_pdu, 2, "Invalid alphabet in data encoding scheme: value 3 is not supported.");              // ...or should this be a warning? If so, GSM alphabet is then used as a default.            if (*alphabet == 0)              *alphabet = -1;            if (!(*err_str))            {              Src_Pointer += 2;              sprintf(date,"%c%c-%c%c-%c%c",Src_Pointer[1],Src_Pointer[0],Src_Pointer[3],Src_Pointer[2],Src_Pointer[5],Src_Pointer[4]);              if (!isdigit(date[0]) || !isdigit(date[1]) || !isdigit(date[3]) || !isdigit(date[4]) || !isdigit(date[6]) || !isdigit(date[7]))              {                pdu_error(err_str, 0, Src_Pointer -full_pdu, 6, "Invalid character(s) in date of Service Centre Time Stamp: \"%s\"", date);                *date = 0;              }              else if (atoi(date +3) > 12 || atoi(date +6) > 31)              {                // Not a fatal error (?)                //pdu_error(err_str, 0, Src_Pointer -full_pdu, 6, "Invalid value(s) in date of Service Centre Time Stamp: \"%s\"", date);                //*date = 0;                add_warning(warning_headers, "Invalid values(s) in date of Service Centre Time Stamp.");              }              Src_Pointer += 6;              sprintf(time,"%c%c:%c%c:%c%c",Src_Pointer[1],Src_Pointer[0],Src_Pointer[3],Src_Pointer[2],Src_Pointer[5],Src_Pointer[4]);              if (!isdigit(time[0]) || !isdigit(time[1]) || !isdigit(time[3]) || !isdigit(time[4]) || !isdigit(time[6]) || !isdigit(time[7]))              {                pdu_error(err_str, 0, Src_Pointer -full_pdu, 6, "Invalid character(s) in time of Service Centre Time Stamp: \"%s\"", time);                *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 Service Centre Time Stamp: \"%s\"", time);                //*time = 0;                add_warning(warning_headers, "Invalid values(s) in time of Service Centre Time Stamp.");              }              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 Service Centre Time Stamp: \"%.2s\"", Src_Pointer);                else                  Src_Pointer += 2;              }            }          }        }      }    }    if (!(*err_str))    {      // Src_Pointer now points to the User data length, which octet exists.      // TODO: Can udh-len be zero?      if (*alphabet <= 0)      {        if ((result = pdu2text(Src_Pointer, message, message_length, expected_length, with_udh, udh_data, udh_type, &errorpos)) < 0)          pdu_error(err_str, 0, Src_Pointer -full_pdu +errorpos, 0, "While reading TP-UD (GSM text): %s",                    (result == -1)? err_pdu_content : err_too_short);      }      else      {        // With binary messages udh is NOT taken from the PDU.        i = with_udh;        if (*alphabet == 1)          i = 0;        if ((result = pdu2binary(Src_Pointer, message, message_length, expected_length, i, udh_data, udh_type, &errorpos)) < 0)          pdu_error(err_str, 0, Src_Pointer -full_pdu +errorpos, 0, "While reading TP-UD (%s): %s",                    (*alphabet == 1)? "binary" : "UCS2 text",                    (result == -1)? err_pdu_content : err_too_short);      }    }  }  return result;}// Subroutine for messages type 2 (Status Report)// Input: // Src_Pointer points to the PDU string// Output:// sendr Sender// date and time Date/Time-stamp// result is the status value and text translationint split_type_2(char *full_pdu, char* Src_Pointer,char* sendr, char* date,char* time,char* result,                 char *from_toa, char **err_str, char *warning_headers){  int Length;  int padding;  int status;  char temp[32];  char tmpsender[100];  int messageid;  int i;  char *p;  const char SR_MessageId[] = "Message_id:"; // Fixed title inside the status report body.  const char SR_Status[] = "Status:"; // Fixed title inside the status report body.  strcat(result,"SMS STATUS REPORT\n");  // There should be at least message-id, address-length and address-type:  if (strlen(Src_Pointer) < 6)    pdu_error(err_str, 0, Src_Pointer -full_pdu, 6,              "While trying to read message id, address length and address type: %s", err_too_short);  else  {    // get message id    if ((messageid = octet2bin_check(Src_Pointer)) < 0)      pdu_error(err_str, 0, Src_Pointer -full_pdu, 2, "Invalid message id: \"%.2s\"", Src_Pointer);    else    {      sprintf(strchr(result, 0), "%s %i\n", SR_MessageId, messageid);      // get recipient address      Src_Pointer+=2;      Length = octet2bin_check(Src_Pointer);      if (Length < 1 || Length > MAX_ADDRESS_LENGTH)        pdu_error(err_str, 0, Src_Pointer -full_pdu, 2, "Invalid recipient address length: \"%.2s\"", Src_Pointer);      else      {        padding=Length%2;        Src_Pointer+=2;        i = explain_toa(from_toa, Src_Pointer, 0);        if (i < 0)          pdu_error(err_str, 0, Src_Pointer -full_pdu, 2, "Invalid recipient address type: \"%.2s\"", Src_Pointer);        else if (i < 0x80)          pdu_error(err_str, 0, Src_Pointer -full_pdu, 2, "Missing bit 7 in recipient address type: \"%.2s\"", Src_Pointer);        else        {          Src_Pointer += 2;          if ((i & 112) == 80)          {  // Sender is alphanumeric            if (strlen(Src_Pointer) < Length +padding)              pdu_error(err_str, 0, Src_Pointer -full_pdu, Length +padding,                        "While trying to read recipient address (alphanumeric, length %i): %s",                        Length +padding, err_too_short);            else            {              snprintf(tmpsender,Length+3+padding,"%02X%s",Length*4/7,Src_Pointer);              if (pdu2text0(tmpsender, sendr) < 0)                pdu_error(err_str, 0, Src_Pointer -full_pdu, Length +padding,                          "While reading alphanumeric recipient address: %s", err_pdu_content);            }          }          else          {  // sender is numeric            if (strlen(Src_Pointer) < Length +padding)              pdu_error(err_str, 0, Src_Pointer -full_pdu, Length +padding,                        "While trying to read recipient address (numeric, length %i): %s",                        Length +padding, err_too_short);            else            {              strncpy(sendr,Src_Pointer,Length+padding);              sendr[Length +padding] = 0;              swapchars(sendr);              i = Length +padding -1;              if (padding)              {                if (sendr[i] != 'F')                  add_warning(warning_headers, "Length of numeric recipient address is odd, but not terminated with 'F'.");                else                  sendr[i] = 0;              }              else              {                if (sendr[i] == 'F')                {                  add_warning(warning_headers, "Length of numeric recipient address is even, but still was terminated with 'F'.");                  sendr[i] = 0;                }              }              for (i = 0; sendr[i]; i++)                if (!isdigit(sendr[i]))                {                  // Not a fatal error (?)                  //pdu_error(err_str, 0, Src_Pointer -full_pdu, Length +padding, "Invalid character(s) in recipient address: \"%s\"", sendr);                  //*sendr = 0;                  add_warning(warning_headers, "Invalid character(s) in recipient address.");                  break;                }            }          }          if (!(*err_str))          {            Src_Pointer+=Length+padding;            if (strlen(Src_Pointer) < 14)              pdu_error(err_str, 0, Src_Pointer -full_pdu, 14, "While trying to read SMSC Timestamp: %s", err_too_short);            else            {              // get SMSC timestamp              sprintf(date,"%c%c-%c%c-%c%c",Src_Pointer[1],Src_Pointer[0],Src_Pointer[3],Src_Pointer[2],Src_Pointer[5],Src_Pointer[4]);              if (!isdigit(date[0]) || !isdigit(date[1]) || !isdigit(date[3]) || !isdigit(date[4]) || !isdigit(date[6]) || !isdigit(date[7]))              {                pdu_error(err_str, 0, Src_Pointer -full_pdu, 6, "Invalid character(s) in date of SMSC Timestamp: \"%s\"", date);                *date = 0;              }              else if (atoi(date +3) > 12 || atoi(date +6) > 31)              {                // Not a fatal error (?)                //pdu_error(err_str, 0, Src_Pointer -full_pdu, 6, "Invalid value(s) in date of SMSC Timestamp: \"%s\"", date);                //*date = 0;                add_warning(warning_headers, "Invalid value(s) in date of SMSC Timestamp.");              }              Src_Pointer += 6;              sprintf(time,"%c%c:%c%c:%c%c",Src_Pointer[1],Src_Pointer[0],Src_Pointer[3],Src_Pointer[2],Src_Pointer[5],Src_Pointer[4]);              if (!isdigit(time[0]) || !isdigit(time[1]) || !isdigit(time[3]) || !isdigit(time[4]) || !isdigit(time[6]) || !isdigit(time[7]))              {                pdu_error(err_str, 0, Src_Pointer -full_pdu, 6, "Invalid character(s) in time of SMSC Timestamp: \"%s\"", time);

⌨️ 快捷键说明

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