SMPP Server Development in Python

49 views Asked by At

I am working on the development of SMPP Server in which I am facing some issues. The issue is with the receiving of submit sm pdu. When I receive submit_sm from one IP it is parsing every data accurately, but at the same time when I received even same data from other IP, it parses everything accurately but at the last 2 fields. it is shows message length 0 and short message null which raw data print clearly shows that the received pdu as message at the end. My parsing fumction is given below

def parse_submit_sm_pdu(received_data): if len(received_data) < 16: print("Error: Insufficient data to unpack the header.") return

# Unpack the header
command_length, command_id, command_status, sequence_number = struct.unpack('>IIII', received_data[:16])

# Extract body
body = received_data[16:]

# Extracting strings with null padding
service_type_start = 0
service_type_end = service_type_start + body[service_type_start:].find(b'\x00')
service_type = body[service_type_start:service_type_end].decode('ascii').ljust(6, '\x00')

source_addr_ton, source_addr_npi = struct.unpack('BB', body[service_type_end: service_type_end + 2])

source_addr_start = service_type_end + 2
source_addr_end = source_addr_start + body[source_addr_start:].find(b'\x00')
source_addr = body[source_addr_start:source_addr_end].decode('ascii').ljust(21, '\x00')

dest_addr_ton, dest_addr_npi = struct.unpack('BB', body[source_addr_end: source_addr_end + 2])

destination_addr_start = source_addr_end + 2
destination_addr_end = destination_addr_start + body[destination_addr_start:].find(b'\x00')
destination_addr = body[destination_addr_start:destination_addr_end].decode('ascii').ljust(21, '\x00')

esm_class, protocol_id, priority_flag = struct.unpack('BBB', body[destination_addr_end: destination_addr_end + 3])

schedule_delivery_time_start = destination_addr_end + 3
schedule_delivery_time_end = schedule_delivery_time_start + body[schedule_delivery_time_start:].find(b'\x00')
schedule_delivery_time = body[schedule_delivery_time_start:schedule_delivery_time_end].decode('ascii').ljust(17, '\x00')

validity_period_start = schedule_delivery_time_end + 1
validity_period_end = validity_period_start + body[validity_period_start:].find(b'\x00')
validity_period = body[validity_period_start:validity_period_end].decode('ascii').ljust(17, '\x00')

registered_delivery, replace_if_present_flag, data_coding, sm_default_msg_id, sm_length = struct.unpack('BBBBB', body[validity_period_end: validity_period_end + 5])

# Adjusted the handling of sm_length to correctly extract its value
# sm_length_start = validity_period_end + 5
# sm_length = body[sm_length_start]


sm_length_start = validity_period_end + 5
sm_length = struct.unpack('B', body[sm_length_start:sm_length_start + 1])[0]

short_message_start = sm_length_start + 1
short_message_end = short_message_start + sm_length
short_message = body[short_message_start:short_message_end].decode('ascii')


# short_message_start = sm_length_start + 1
# short_message_end = short_message_start + sm_length
# short_message = body[short_message_start:short_message_end].decode('ascii').ljust(254, '\x00')

print("Received submit sm pdu............")
print(f"  Command Length: {command_length}")
print(f"  Command ID: {format(command_id, '#010x')}")
print(f"  Command Status: {command_status}")
print(f"  Sequence Number: {sequence_number}")
print(f"  Service Type: {service_type}")
print(f"  Source Addr TON: {source_addr_ton}")
print(f"  Source Addr NPI: {source_addr_npi}")
print(f"  Source Addr: {source_addr}")
print(f"  Dest Addr TON: {dest_addr_ton}")
print(f"  Dest Addr NPI: {dest_addr_npi}")
print(f"  Destination Addr: {destination_addr}")
print(f"  ESM Class: {esm_class}")
print(f"  Protocol ID: {protocol_id}")
print(f"  Priority Flag: {priority_flag}")
print(f"  Schedule Delivery Time: {schedule_delivery_time}")
print(f"  Validity Period: {validity_period}")
print(f"  Registered Delivery: {registered_delivery}")
print(f"  Replace If Present Flag: {replace_if_present_flag}")
print(f"  Data Coding: {data_coding}")
print(f"  SM Default Msg ID: {sm_default_msg_id}")
print(f"  SM Length: {sm_length}")
print(f"  Short Message: {short_message}")

Here the issue is at smlength and short mesage while receiving data from one IP. I have set the bytes ans type of each accordinng to smpp version 3.4 document specification.

I want my parsing function which works on all the IP's.

0

There are 0 answers