public Pdu(int Type) { header=new PDUHeader(); header.P = 0x32; header.type = (byte) Type; pduHeaderLen = (Type == 2 || Type == 3) ? 12 : 10; Param = new List<byte>(); Data = new List<byte>(); UData = new List<byte>(); }
private Tuple <PDUHeader, byte[], AuthData> ReadPDU(int frag_count) { byte[] buffer = ReadFragment(_max_recv_fragment); RpcUtils.DumpBuffer(true, $"{GetType().Name} Receive Buffer - Fragment {frag_count}", buffer); MemoryStream stm = new MemoryStream(buffer); BinaryReader reader = new BinaryReader(stm); PDUHeader header = PDUHeader.Read(reader); NdrUnmarshalBuffer.CheckDataRepresentation(header.DataRep); AuthData auth_data = new AuthData(); int auth_trailing_length = header.AuthLength > 0 ? header.AuthLength + AuthData.PDU_AUTH_DATA_HEADER_SIZE : 0; byte[] data = reader.ReadAllBytes(header.FragmentLength - PDUHeader.PDU_HEADER_SIZE - auth_trailing_length); if (auth_trailing_length > 0) { stm.Seek(header.FragmentLength - auth_trailing_length, SeekOrigin.Begin); auth_data = AuthData.Read(reader, header.AuthLength); } return(Tuple.Create(header, data, auth_data)); }
public Pdu(byte[] recievedPdu) { int Type = recievedPdu[1]; pduHeaderLen = (Type == 2 || Type == 3) ? 12 : 10; byte[] array=new byte[pduHeaderLen]; Array.Copy(recievedPdu, 0, array, 0, pduHeaderLen); header = EndianessMarshaler.BytesToStruct<PDUHeader>(array); Param = new List<byte>(); array = new byte[header.plen]; Array.Copy(recievedPdu, pduHeaderLen, array, 0, header.plen); Param.AddRange(array); Data = new List<byte>(); array = new byte[header.dlen]; Array.Copy(recievedPdu, pduHeaderLen + header.plen, array, 0, header.dlen); Data.AddRange(array); UData = new List<byte>(); }
public Pdu(byte[] recievedPdu) { int Type = recievedPdu[1]; pduHeaderLen = (Type == 2 || Type == 3) ? 12 : 10; byte[] array=new byte[pduHeaderLen]; Array.Copy(recievedPdu, 0, array, 0, pduHeaderLen); header = EndianessMarshaler.BytesToStruct<PDUHeader>(array); Param = new List<byte>(); array = new byte[header.plen]; Array.Copy(recievedPdu, pduHeaderLen, array, 0, header.plen); Param.AddRange(array); Data = new List<byte>(); array = new byte[header.dlen]; header.dlen = (ushort)Math.Min(header.dlen, recievedPdu.Length - pduHeaderLen - header.plen);//for OLD PLC: recievedPdu.Length = 400, header.dlen = 448 Array.Copy(recievedPdu, pduHeaderLen + header.plen, array, 0, header.dlen); Data.AddRange(array); UData = new List<byte>(); initUData(); }
static void Main(string[] args) { //Common.Logging.LogManager.Adapter = new Common.Logging.Simple.ConsoleOutLoggerFactoryAdapter(); var encSrv = new SmppEncodingService(); var hexBytes = "000000dd0000000500000000019182410001013334363439323836383039000501657669636572746961000400000000000000008569643a323533303932393134353232363637333732207375623a30303120646c7672643a303031207375626d697420646174653a3133303932393136353220646f6e6520646174653a3133303932393136353220737461743a44454c49565244206572723a3030303020746578743a1b3c657669534d531b3e0a534d532064652050727565042300030300000427000102001e001332353330393239313435323236363733373200"; var packet = StringToByteArray(hexBytes); var bodyBytes = packet.Skip(16).ToArray(); var pdu = PDU.CreatePDU(PDUHeader.Parse(new ByteBuffer(packet), encSrv), encSrv); pdu.SetBodyData(new ByteBuffer(bodyBytes)); var receiptedMessageId = pdu.GetOptionalParamString(JamaaTech.Smpp.Net.Lib.Protocol.Tlv.Tag.receipted_message_id); //Assert.AreEqual("253092914522667372", pdu.ReceiptedMessageId); Trace.WriteLine("Start"); //Trace.Listeners.Add(new ConsoleTraceListener()); smppConfig = GetSmppConfiguration(); //SMPPEncodingUtil.UCS2Encoding = Encoding.UTF8; var client = CreateSmppClient(smppConfig); client.Start(); // must wait until connected before start sending while (client.ConnectionState != SmppConnectionState.Connected) { Thread.Sleep(100); } var input = ""; do { Console.Write("Enter Dest: "); input = Console.ReadLine(); if ("q".Equals(input, StringComparison.InvariantCultureIgnoreCase)) { break; } var dest = input; Console.Write("Enter Msg: "); var msgTxt = Console.ReadLine(); if (string.IsNullOrEmpty(msgTxt)) { msgTxt = @"السلام عليكم ورحمة الله وبركاته هذه رسالة عربية متعددة الاسطر"; } TextMessage msg = new TextMessage(); msg.DestinationAddress = dest; //Receipient number msg.SourceAddress = smppConfig.SourceAddress; //Originating number //msg.Text = "Hello, this is my test message!"; msg.Text = msgTxt; msg.RegisterDeliveryNotification = true; //I want delivery notification for this message msg.UserMessageReference = Guid.NewGuid().ToString(); Console.WriteLine($"msg.UserMessageReference: {msg.UserMessageReference}"); Trace.WriteLine($"msg.UserMessageReference: {msg.UserMessageReference}"); //client.SendMessage(msg); client.BeginSendMessage(msg, SendMessageCompleteCallback, client); Console.ReadLine(); } while (true); }
private PDU WaitPDU() { PDUHeader header = null; PDU pdu = null; byte[] bodyBytes = null; byte[] headerBytes = null; //-- try { headerBytes = ReadHeaderBytes(); } catch (TcpIpException tcpIp_ex_1) { if (vTraceSwitch.TraceInfo) { Trace.WriteLine(string.Format( "200010:TCP/IP Exception encountered while reading pdu header bytes:{0};", tcpIp_ex_1.Message)); } HandleException(tcpIp_ex_1); throw; } //-- header = PDUHeader.Parse(new ByteBuffer(headerBytes)); try { pdu = PDU.CreatePDU(header); } catch (InvalidPDUCommandException inv_ex) { ByteBuffer iBuffer = new ByteBuffer((int)header.CommandLength); iBuffer.Append(header.GetBytes()); if (header.CommandLength > 16) { try { iBuffer.Append(ReadBodyBytes((int)header.CommandLength - 16)); } catch (TcpIpException tcpIp_ex_3) { HandleException(tcpIp_ex_3); } } if (vTraceSwitch.TraceWarning) { Trace.WriteLine(string.Format( "200011:Invalid PDU command type:{0};", iBuffer.DumpString())); } RaisePduErrorEvent(inv_ex, iBuffer.ToBytes(), header, null); throw; } //-- try { bodyBytes = ReadBodyBytes((int)header.CommandLength - 16); } catch (TcpIpException tpcIp_ex_2) { if (vTraceSwitch.TraceInfo) { Trace.WriteLine(string.Format( "200012:TCP/IP Exception encountered while reading pdu body bytes:{0};", tpcIp_ex_2.Message)); } HandleException(tpcIp_ex_2); throw; } //-- try { pdu.SetBodyData(new ByteBuffer(bodyBytes)); } catch (PDUException pdu_ex) { ByteBuffer pBuffer = new ByteBuffer((int)header.CommandLength); pBuffer.Append(headerBytes); pBuffer.Append(bodyBytes); RaisePduErrorEvent(pdu_ex, pBuffer.ToBytes(), header, pdu); if (vTraceSwitch.TraceWarning) { Trace.WriteLine(string.Format( "200013:Malformed PDU body received:{0} {1};", pBuffer.DumpString(), pdu_ex.Message)); } throw; } return(pdu); }
private PDUBase SendReceivePDU(PDUBase send_pdu) { try { CallId++; PDUHeader pdu_header = new PDUHeader() { MajorVersion = PDUHeader.RPC_VERSION_MAJOR, MinorVersion = PDUHeader.RPC_VERSION_MINOR, DataRep = _data_rep, CallId = CallId, Type = send_pdu.PDUType }; List <byte[]> fragments = send_pdu.DoFragment(_max_send_fragment - PDUHeader.PDU_HEADER_SIZE); for (int i = 0; i < fragments.Count; ++i) { pdu_header.Flags = send_pdu.GetFlags(); if (i == 0) { pdu_header.Flags |= PDUFlags.FirstFrag; } if (i == fragments.Count - 1) { pdu_header.Flags |= PDUFlags.LastFrag; } pdu_header.FragmentLength = (ushort)(fragments[i].Length + PDUHeader.PDU_HEADER_SIZE); MemoryStream send_stm = new MemoryStream(); BinaryWriter writer = new BinaryWriter(send_stm); pdu_header.Write(writer); writer.Write(fragments[i]); byte[] fragment = send_stm.ToArray(); string name = fragments.Count == 1 ? "RPC Named Pipe Send Buffer" : $"RPC Named Pipe Send Buffer - Fragment {i}"; RpcUtils.DumpBuffer(true, name, fragment); if (_pipe.Write(fragment) != fragment.Length) { throw new RpcTransportException("Failed to write out PDU buffer."); } } MemoryStream recv_stm = new MemoryStream(); PDUHeader curr_header = new PDUHeader(); int frag_count = 0; while ((curr_header.Flags & PDUFlags.LastFrag) == 0) { var pdu = ReadPDU(frag_count++); curr_header = pdu.Item1; if (curr_header.CallId != CallId) { throw new RpcTransportException("Mismatching call ID."); } recv_stm.Write(pdu.Item2, 0, pdu.Item2.Length); } return(CheckFault(curr_header.ToPDU(recv_stm.ToArray()))); } catch (EndOfStreamException) { throw new RpcTransportException("End of stream."); } }
public PDUErrorEventArgs(PDUException exception, byte[] byteDump, PDUHeader header, PDU pdu) : this(exception, byteDump, header) { vPdu = pdu; }
public PDUErrorEventArgs(PDUException exception, byte[] byteDump, PDUHeader header) { vException = exception; vByteDump = byteDump; vHeader = header; }
private byte[] SendReceiveRequestPDU(int proc_num, Guid objuuid, byte[] stub_data, RpcTransportSecurityContext security_context) { try { CallId++; PDURequest request_pdu = new PDURequest() { OpNum = (short)proc_num, ObjectUUID = objuuid }; int max_fragment = _max_send_fragment - request_pdu.HeaderLength; int auth_data_length = 0; if (_auth_data_required) { auth_data_length = security_context.AuthDataLength; max_fragment -= (auth_data_length + AuthData.PDU_AUTH_DATA_HEADER_SIZE); max_fragment &= ~0xF; } List <byte[]> fragments = PDURequest.DoFragment(stub_data, max_fragment); for (int i = 0; i < fragments.Count; ++i) { PDUHeader pdu_header = new PDUHeader() { MajorVersion = PDUHeader.RPC_VERSION_MAJOR, MinorVersion = PDUHeader.RPC_VERSION_MINOR, DataRep = _data_rep, CallId = CallId, Type = PDUType.Request }; if (i == 0) { pdu_header.Flags |= PDUFlags.FirstFrag; } if (i == fragments.Count - 1) { pdu_header.Flags |= PDUFlags.LastFrag; } byte[] stub_fragment = fragments[i]; byte[] auth_data = new byte[0]; byte[] header = request_pdu.ToArray(pdu_header, stub_fragment.Length, 0); if (_auth_data_required) { int auth_data_padding = 0; int auth_trailing_size = (header.Length + stub_fragment.Length + AuthData.PDU_AUTH_DATA_HEADER_SIZE) & 0xF; if (auth_trailing_size != 0) { auth_data_padding = 16 - auth_trailing_size; Array.Resize(ref stub_fragment, stub_fragment.Length + auth_data_padding); } header = request_pdu.ToArray(pdu_header, stub_fragment.Length + AuthData.PDU_AUTH_DATA_HEADER_SIZE, auth_data_length); auth_data = security_context.ProtectPDU(header, ref stub_fragment, auth_data_padding, _send_sequence_no); } MemoryStream send_stm = new MemoryStream(); BinaryWriter writer = new BinaryWriter(send_stm); writer.Write(header); writer.Write(stub_fragment); writer.Write(auth_data); byte[] fragment = send_stm.ToArray(); string name = fragments.Count == 1 ? $"{GetType().Name} Send Buffer" : $"{GetType().Name} Send Buffer - Fragment {i}"; RpcUtils.DumpBuffer(true, name, fragment); if (!WriteFragment(fragment)) { throw new RpcTransportException("Failed to write out PDU buffer."); } _send_sequence_no++; } MemoryStream recv_stm = new MemoryStream(); PDUHeader curr_header = new PDUHeader(); int frag_count = 0; while ((curr_header.Flags & PDUFlags.LastFrag) == 0) { var pdu = ReadPDU(frag_count++); curr_header = pdu.Item1; AuthData auth_data = pdu.Item3; if (curr_header.CallId != CallId) { throw new RpcTransportException($"Mismatching call ID - {curr_header.CallId} should be {CallId}."); } if (auth_data.ContextId != security_context.ContextId) { security_context = GetContext(auth_data.ContextId); } var recv_pdu = CheckFault(curr_header.ToPDU(pdu.Item2)); if (recv_pdu is PDUResponse resp_pdu) { byte[] resp_stub_data = _auth_data_required ? security_context.UnprotectPDU(resp_pdu.ToArray(curr_header), resp_pdu.StubData, auth_data, _recv_sequence_no) : resp_pdu.StubData; _recv_sequence_no++; recv_stm.Write(resp_stub_data, 0, resp_stub_data.Length); } else { throw new RpcTransportException($"Unexpected {recv_pdu.PDUType} PDU from server."); } } return(recv_stm.ToArray()); } catch (EndOfStreamException) { throw new RpcTransportException("End of stream."); } }
private Tuple <PDUBase, AuthData> SendReceivePDU(int call_id, PDUBase send_pdu, byte[] auth_data, bool receive_pdu, RpcTransportSecurityContext security_context) { try { int trailing_auth_length = auth_data.Length > 0 ? auth_data.Length + AuthData.PDU_AUTH_DATA_HEADER_SIZE : 0; PDUHeader pdu_header = new PDUHeader() { MajorVersion = PDUHeader.RPC_VERSION_MAJOR, MinorVersion = PDUHeader.RPC_VERSION_MINOR, DataRep = _data_rep, CallId = CallId, Type = send_pdu.PDUType, Flags = PDUFlags.LastFrag | PDUFlags.FirstFrag, AuthLength = checked ((ushort)auth_data.Length) }; byte[] pdu_data = send_pdu.ToArray(); int pdu_data_length = pdu_data.Length + PDUHeader.PDU_HEADER_SIZE; int auth_padding = 0; if (auth_data.Length > 0 && (pdu_data_length & 15) != 0 && send_pdu.PDUType != PDUType.Auth3) { auth_padding = 16 - (pdu_data_length & 15); } pdu_header.FragmentLength = checked ((ushort)(pdu_data.Length + PDUHeader.PDU_HEADER_SIZE + trailing_auth_length + auth_padding)); MemoryStream send_stm = new MemoryStream(); BinaryWriter writer = new BinaryWriter(send_stm); pdu_header.Write(writer); writer.Write(pdu_data); if (auth_data.Length > 0) { writer.Write(new byte[auth_padding]); new AuthData(security_context.TransportSecurity.AuthenticationType, security_context.TransportSecurity.AuthenticationLevel, auth_padding, security_context.ContextId, auth_data).Write(writer, auth_padding); } byte[] fragment = send_stm.ToArray(); RpcUtils.DumpBuffer(true, $"{GetType().Name} Send Buffer", fragment); if (!WriteFragment(fragment)) { throw new RpcTransportException("Failed to write out PDU buffer."); } _send_sequence_no++; if (!receive_pdu) { return(null); } var pdu = ReadPDU(0); var curr_header = pdu.Item1; if (!curr_header.Flags.HasFlagAllSet(PDUFlags.LastFrag | PDUFlags.FirstFrag)) { throw new RpcTransportException($"Invalid PDU flags {curr_header.Flags}."); } if (curr_header.CallId != call_id) { throw new RpcTransportException($"Mismatching call ID - {curr_header.CallId} should be {call_id}."); } if (pdu.Item3.ContextId != security_context.ContextId) { throw new RpcTransportException($"Mismatching context ID - {pdu.Item3.ContextId} should be {security_context.ContextId}."); } _recv_sequence_no++; return(Tuple.Create(CheckFault(curr_header.ToPDU(pdu.Item2)), pdu.Item3)); } catch (EndOfStreamException) { throw new RpcTransportException("End of stream."); } }
static void Main(string[] args) { Common.Logging.LogManager.Adapter = new Common.Logging.Simple.DebugLoggerFactoryAdapter(); var encSrv = new SmppEncodingService(); var hexBytes = "000000dd0000000500000000019182410001013334363439323836383039000501657669636572746961000400000000000000008569643a323533303932393134353232363637333732207375623a30303120646c7672643a303031207375626d697420646174653a3133303932393136353220646f6e6520646174653a3133303932393136353220737461743a44454c49565244206572723a3030303020746578743a1b3c657669534d531b3e0a534d532064652050727565042300030300000427000102001e001332353330393239313435323236363733373200"; var packet = StringToByteArray(hexBytes); var bodyBytes = packet.Skip(16).ToArray(); var pdu = PDU.CreatePDU(PDUHeader.Parse(new ByteBuffer(packet), encSrv), encSrv); pdu.SetBodyData(new ByteBuffer(bodyBytes)); var receiptedMessageId = pdu.GetOptionalParamString(JamaaTech.Smpp.Net.Lib.Protocol.Tlv.Tag.receipted_message_id); //Assert.AreEqual("253092914522667372", pdu.ReceiptedMessageId); _Log.Info("Start"); //Trace.Listeners.Add(new ConsoleTraceListener()); smppConfig = GetSmppConfiguration(); //SMPPEncodingUtil.UCS2Encoding = Encoding.UTF8; client = CreateSmppClient(smppConfig); client.Start(); // must wait until connected before start sending while (client.ConnectionState != SmppConnectionState.Connected) { Thread.Sleep(100); } // Accept command input bool bQuit = false; do { // Hit Enter in the terminal once the binds are up to see this prompt Console.WriteLine("Commands"); Console.WriteLine("send 123456 hello"); Console.WriteLine("quit"); Console.WriteLine(""); Console.Write("\n#>"); string command = Console.ReadLine(); if (command.Length == 0) { continue; } switch (command.Split(' ')[0].ToString()) { case "quit": case "exit": case "q": bQuit = true; break; default: ProcessCommand(command); break; } if (bQuit) { break; } } while (true); if (client != null) { client.Dispose(); } }