2

I have written a program about ICMP protcol,which implements the PING command.Though everything runs well,i still have some questions.

  1. There are two variables.One is "ep" which is an object of EndPoint class and the other is "iep" which is an Object of IPEndPoint class.There are something strange happened.After casting the "iep" to "ep",the value of "ep" is changed.For example,if "iep" is "180.97.33.107" and after casting "iep" to "ep",the value of "ep" will be "192.168.1.1".

  2. The question about function GetHostEntry() is that if the input is domain name, the program works successfully,while if the input is IP address,the program will throw an exception that host can not be found.


Here is the code.

 private void but_simping_Click(object sender, EventArgs e)
            {
                try
                {
                    if (txt_dstip.Text == "")
                    {
                        MessageBox.Show("IP地址不能为空!");
                        return;
                    }

                    byte[] msgData = new byte[1024];
                    int receive;

                    Socket host = new Socket(AddressFamily.InterNetwork, SocketType.Raw, ProtocolType.Icmp);

                    IPHostEntry iphentry = Dns.GetHostEntry(txt_dstip.Text.Trim());
                    IPEndPoint iep = new IPEndPoint(iphentry.AddressList[0], 0);



                    EndPoint ep = (EndPoint)iep;



                    ICMP icmp = new ICMP();


                    icmp.packType = 0x08;
                    icmp.packCode = 0x00;
                    icmp.CheckSum = 0;


                    Buffer.BlockCopy(BitConverter.GetBytes((short)1), 0, icmp.Msg, 0, 2);
                    Buffer.BlockCopy(BitConverter.GetBytes((short)1), 0, icmp.Msg, 2, 2);
                    msgData = Encoding.ASCII.GetBytes("This is a test packet");
                    Buffer.BlockCopy(msgData, 0, icmp.Msg, 4, msgData.Length);


                    icmp.msgSize = msgData.Length + 4;
                    int pktsize = icmp.msgSize + 4;


                    UInt16 checksum = icmp.getCheckSum();
                    icmp.CheckSum = checksum;


                    host.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, 3000);


                    host.SendTo(icmp.getBytes(), pktsize, SocketFlags.None, iep);

                    msgData = new byte[1024];


                    receive = host.ReceiveFrom(msgData, ref ep);
                    ShowICMPMsg(msgData, receive, iep);
                    host.Close();
                }
                catch (SocketException ee)
                {
                    MessageBox.Show(ee.Message.ToString());
                    return;
                }
                finally
                {
                }


            }

PS:i'm not a native english speaker.If there're some mistakes,just point it out,thanks.

Dean Ding
  • 21
  • 3
  • That's really strange, because I've just created simple console app to check your 2 questions, and all works as expected. Have you tried debugging it? What ip addresses do you have on each stage? – Ilya Luzyanin Aug 17 '14 at 15:20
  • Thanks,i have tried debugging it.But i still can't find the problems.Could you share your code with me?Maybe it will be a great help. – Dean Ding Aug 18 '14 at 03:45
  • It's nothing special really. I don't have that app right now, but I've created dotnetfiddle with most of its code - https://dotnetfiddle.net/IFXxnP. As you can see, cast is done correctly. – Ilya Luzyanin Aug 18 '14 at 05:06
  • I think i find where is wrong.Before you call the function ReceiveFrom(),everthing is correct,and after that the value of "ep" has changed to my local address.So this phenonemon is something about the ReceiveFrom() function and the keyword "ref". – Dean Ding Aug 18 '14 at 06:44
  • Oh, I didn't know that issue happens further in code. Yes, it's very likely that `ep` is changed in ReceiveFrom, since it "captures the remote host endpoint from which the data was sent" (http://msdn.microsoft.com/en-us/library/wdfskwcy(v=vs.110).aspx) – Ilya Luzyanin Aug 18 '14 at 07:17
  • How about the second question?I solve it by calling GetHostAddress() function.Using Wireshark ,i find that GetHostEntry() will send NBNS(NetBIOS Name Service) package to the host which i want to connect with,while the GetHostAddress() will send ICMP package directly.The host never respond the NBNS protocol. – Dean Ding Aug 18 '14 at 08:35
  • I'm not an expert in ICMP, so I can only recommend you to read this article: http://msdn.microsoft.com/en-us/library/ms143998(v=vs.110).aspx, Remarks section. It contains the sequence of actions that are performed when IP address is passed as a parameter, and possible errors that might occur. Maybe this can lead you to some solution. – Ilya Luzyanin Aug 18 '14 at 08:58

0 Answers0