3ÆÄÆ®-11

Similar documents
3ÆÄÆ®-14

bn2019_2

02 C h a p t e r Java

Microsoft PowerPoint - 04-UDP Programming.ppt

rmi_박준용_final.PDF

SMB_ICMP_UDP(huichang).PDF

비긴쿡-자바 00앞부속

<C0CCBCBCBFB52DC1A4B4EBBFF82DBCAEBBE7B3EDB9AE2D D382E687770>

PowerPoint 프레젠테이션

Microsoft PowerPoint - 09-EDU-15-채팅 프로그래밍

PowerPoint 프레젠테이션

JMF3_심빈구.PDF

Microsoft PowerPoint - CSharp-15-채팅

Microsoft PowerPoint - Supplement-03-TCP Programming.ppt [호환 모드]

Microsoft PowerPoint - 03-TCP Programming.ppt

01_피부과Part-01

Analytics > Log & Crash Search > Unity ios SDK [Deprecated] Log & Crash Unity ios SDK. TOAST SDK. Log & Crash Unity SDK Log & Crash Search. Log & Cras

자바-11장N'1-502

mytalk

hd1300_k_v1r2_Final_.PDF

11 템플릿적용 - Java Program Performance Tuning (김명호기술이사)

PowerPoint 프레젠테이션

시스코 무선랜 설치운영 매뉴얼(AP1200s_v1.1)

컴퓨터네트워크

1217 WebTrafMon II

01....b

¾Ë·¹¸£±âÁöħ¼�1-ÃÖÁ¾

00목차

(291)본문7

2007백서-001-특집

신림프로그래머_클린코드.key

자바로

컴퓨터네트워크

À©µµ³×Æ®¿÷ÇÁ·Î±×·¡¹Ö4Àå_ÃÖÁ¾

OOP 소개

untitled

12-file.key

T100MD+

03장.스택.key


<4D F736F F F696E74202D20C1A63235C0E520B3D7C6AEBFF6C5A920C7C1B7CEB1D7B7A1B9D628B0ADC0C729205BC8A3C8AF20B8F0B5E55D>

Chap12

Chapter11OSPF

05-class.key

1998년~1999년의 일기

초보자를 위한 C# 21일 완성

MPLAB C18 C

Index

PowerPoint 프레젠테이션

Part Part

PART

£01¦4Àå-2

½ºÅ丮ÅÚ¸µ3_³»Áö

272*406OSAKAÃÖÁ¾-¼öÁ¤b64ٽÚ

UDP Flooding Attack 공격과 방어

Java

Something that can be seen, touched or otherwise sensed

KARAAUTO_4¿ù.qxd-ÀÌÆå.ps, page Normalize

Subnet Address Internet Network G Network Network class B networ

TCP.IP.ppt

텀블러514

The Pocket Guide to TCP/IP Sockets: C Version

13ÀåÃß°¡ºÐ

Network seminar.key

Microsoft Word - KPMC-400,401 SW 사용 설명서

#KM560


......

untitled

쉽게 풀어쓴 C 프로그래밍

chapter4

PWR PWR HDD HDD USB USB Quick Network Setup Guide xdsl/cable Modem PC DVR 1~3 1.. DVR DVR IP xdsl Cable xdsl Cable PC PC DDNS (


1. GigE Camera Interface를 위한 최소 PC 사양 CPU : Intel Core 2 Duo, 2.4GHz이상 RAM : 2GB 이상 LANcard : Intel PRO/1000xT 이상 VGA : PCI x 16, VRAM DDR2 RAM 256MB

The Pocket Guide to TCP/IP Sockets: C Version

11강-힙정렬.ppt

第 1 節 組 織 11 第 1 章 檢 察 의 組 織 人 事 制 度 등 第 1 項 大 檢 察 廳 第 1 節 組 대검찰청은 대법원에 대응하여 수도인 서울에 위치 한다(검찰청법 제2조,제3조,대검찰청의 위치와 각급 검찰청의명칭및위치에관한규정 제2조). 대검찰청에 검찰총장,대

2014밝고고운동요부르기-수정3

2005프로그램표지

ilist.add(new Integer(1))과 같이 사용하지 않고 ilist.add(1)과 같이 사용한 것은 자바 5.0에 추가된 기본 자료형과 해당 객체 자료 형과의 오토박싱/언박싱 기능을 사용한 것으로 오토박싱이란 자바 컴파일러가 객체를 요구하는 곳에 기본 자료형

NoSQL

제11장 프로세스와 쓰레드

5장.key

자바 프로그래밍

untitled

Mobile Service > IAP > Android SDK [ ] IAP SDK TOAST SDK. IAP SDK. Android Studio IDE Android SDK Version (API Level 10). Name Reference V

PowerPoint Presentation

untitled


K&R2 Reference Manual 번역본

Microsoft Word - ZIO-AP1500N-Manual.doc

untitled

01-OOPConcepts(2).PDF

SRC PLUS 제어기 MANUAL

강의10

USB USB DV25 DV25 REC SRN-475S REC SRN-475S LAN POWER LAN POWER Quick Network Setup Guide xdsl/cable Modem PC DVR 1~3 1.. DVR DVR IP xdsl Cable xdsl C

thesis

PowerPoint Presentation

SYN flooding

歯JavaExceptionHandling.PDF

Transcription:

Chapter 11 Chapter 12 Chapter 13 Chapter 14 Chapter 15 Chapter 16 Chapter 17 C # N e t w o r k P r o g r a m m i n g

Part 3 _ chapter 11 ICMP >>> 430

Chapter 11 _ 1 431

Part 3 _ 432

Chapter 11 _ N o t e 433

Part 3 _ 2 434

Chapter 11 _ Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Raw, ProtocolType.Icmp); IPEndPoint iep = new IPEndPoint(IPAddress.Parse("192.168.1.2"), 0); sock.sendto(packet, iep); W a r ning 435

Part 3 _ W a r ning 3 436

Chapter 11 _ class ICMP public byte Type; public byte Code; public UInt16 Checksum; public int MessageSize; public byte[] Message = new byte[1024]; public ICMP() ICMP packet = new ICMP(); packet.type = 0x08; packet.code = 0x00; packet.checksum = 0; Buffer.BlockCopy(BitConverter.GetBytes((short)1), 0, packet.message, 0, 2); Buffer.BlockCopy(BitConverter.GetBytes((short)1), 0, packet.message, 2, 2); byte[] data = Encoding.ASCII.GetBytes("test packet"); 437

Part 3 _ Buffer.BlockCopy(data, 0, packet.message, 4, data.length); packet.messagesize = data.length + 4; public ICMP(byte[] data, int size) Type = data[20]; Code = data[21]; Checksum = BitConverter.ToUInt16(data, 22); MessageSize = size - 24; Buffer.BlockCopy(data, 24, Message, 0, MessageSize); int recv = ReceiveFrom(data, ref ep); ICMP response = new ICMP(data, recv); Console.WriteLine("Received ICMP packet:"); Console.WriteLine(" Type 0", response.type); Console.WriteLine(" Code: 0", response.code); Int16 Identifier = BitConverter.ToInt16(response.Message, 0); Int16 Sequence = BitConverter.ToInt16(response.Message, 2); Console.WriteLine(" Identifier: 0", Identifier); Console.WriteLine(" Sequence: 0", Sequence); stringdata = Encoding.ASCII.GetString(response.Message, 4, response.messagesize - 4); Console.WriteLine(" data: 0", stringdata); 438

Chapter 11 _ public byte[] getbytes() byte[] data = new byte[messagesize + 9]; Buffer.BlockCopy(BitConverter.GetBytes(Type), 0, data, 0, 1); Buffer.BlockCopy(BitConverter.GetBytes(Code), 0, data, 1, 1); Buffer.BlockCopy(BitConverter.GetBytes(Checksum), 0, data, 2, 2); Buffer.BlockCopy(Message, 0, data, 4, MessageSize); return data; IPEndPoint iep = new IPEndPoint(IPAddress.Parse("192.168.1.2"), 0); sock.sendto(packet.getbytes(), iep); 439

Part 3 _ T i p L i Sting public UInt16 getchecksum() UInt32 chcksm = 0; byte[] data = getbytes(); int packetsize = MessageSize + 8; int index = 0; while ( index < packetsize) chcksm += Convert.ToUInt32(BitConverter.ToUInt16(data, index)); index += 2; chcksm = (chcksm >> 16) + (chcksm & 0xffff); chcksm += (chcksm >> 16); return (UInt16)(~chcksm); 440

Chapter 11 _ packet.checksum = 0; packet.checksum = packet.getchecksum(); N o t e L i Sting using System; using System.Net; using System.Text; class ICMP public byte Type; public byte Code; public UInt16 Checksum; public int MessageSize; public byte[] Message = new byte[1024]; public ICMP() 441

Part 3 _ public ICMP(byte[] data, int size) Type = data[20]; Code = data[21]; Checksum = BitConverter.ToUInt16(data, 22); MessageSize = size - 24; Buffer.BlockCopy(data, 24, Message, 0, MessageSize); public byte[] getbytes() byte[] data = new byte[messagesize + 9]; Buffer.BlockCopy(BitConverter.GetBytes(Type), 0, data, 0, 1); Buffer.BlockCopy(BitConverter.GetBytes(Code), 0, data, 1, 1); Buffer.BlockCopy(BitConverter.GetBytes(Checksum), 0, data, 2, 2); Buffer.BlockCopy(Message, 0, data, 4, MessageSize); return data; public UInt16 getchecksum() UInt32 chcksm = 0; byte[] data = getbytes(); int packetsize = MessageSize + 8; int index = 0; while ( index < packetsize) chcksm += Convert.ToUInt32(BitConverter.ToUInt16(data, index)); index += 2; chcksm = (chcksm >> 16) + (chcksm & 0xffff); chcksm += (chcksm >> 16); return (UInt16)(~chcksm); 442

Chapter 11 _ 4 Echo Request Type = 8 Code = 0 Checksum Identifier = 1 Sequence = 1 Message = test packet Server Type = 0Code = 0 Checksum Identifier = 1 Sequence = 1 L i Sting using System; using System.Net; using System.Net.Sockets; using System.Text; class SimplePing 443

Part 3 _ public static void Main (string[] argv) byte[] data = new byte[1024]; int recv; Socket host = new Socket(AddressFamily.InterNetwork, SocketType.Raw, ProtocolType.Icmp); IPEndPoint iep = new IPEndPoint(IPAddress.Parse(argv[0]), 0); EndPoint ep = (EndPoint)iep; ICMP packet = new ICMP(); packet.type = 0x08; packet.code = 0x00; packet.checksum = 0; Buffer.BlockCopy( BitConverter.GetBytes((short)1), 0, packet.message, 0, 2); Buffer.BlockCopy( BitConverter.GetBytes((short)1), 0, packet.message, 2, 2); data = Encoding.ASCII.GetBytes("test packet"); Buffer.BlockCopy(data, 0, packet.message, 4, data.length); packet.messagesize = data.length + 4; int packetsize = packet.messagesize + 4; UInt16 chcksum = packet.getchecksum(); packet.checksum = chcksum; host.setsocketoption(socketoptionlevel.socket, SocketOptionName.ReceiveTimeout, 3000); host.sendto(packet.getbytes(), packetsize, SocketFlags.None, iep); try data = new byte[1024]; recv = host.receivefrom(data, ref ep); catch (SocketException) Console.WriteLine("No response from remote host"); return; ICMP response = new ICMP(data, recv); Console.WriteLine("response from: 0", ep.tostring()); Console.WriteLine(" Type 0", response.type); Console.WriteLine(" Code: 0", response.code); int Identifier = BitConverter.ToInt16(response.Message, 0); int Sequence = BitConverter.ToInt16(response.Message, 2); Console.WriteLine(" Identifier: 0", Identifier); 444

Chapter 11 _ Console.WriteLine(" Sequence: 0", Sequence); string stringdata = Encoding.ASCII.GetString(response.Message, 4, response.messagesize - 4); Console.WriteLine(" data: 0", stringdata); host.close(); csc SimplePing.cs ICMP.cs C:\>SimplePing 127.0.0.1 445

Part 3 _ response from: 127.0.0.1:0 Type 0 Code: 0 Identifier: 1 Sequence: 1 data: test packet C:\> C:\>SimplePing 192.168.1.111 No response from remote host C:\> C:\>SimplePing 192.168.1.2 response from: 192.168.1.2:0 Type 0 Code: 0 Identifier: 1 Sequence: 1 data: test packet C:\> W a r ning 446

Chapter 11 _ 5 IPHostEntry iphe = Dns.Resolve(hostbox.Text); IPEndPoint iep = new IPEndPoint(iphe.AddressList[0], 0); 447

Part 3 _ L i Sting using System; using System.Drawing; using System.Net; using System.Net.Sockets; using System.Text; using System.Threading; using System.Windows.Forms; class AdvPing : Form private static int pingstart, pingstop, elapsedtime; private static TextBox hostbox, databox; private static ListBox results; private static Thread pinger; private static Socket sock; public AdvPing() Text = "Advanced Ping Program"; Size = new Size(400, 380); Label label1 = new Label(); label1.parent = this; label1.text = "Enter host to ping:"; label1.autosize = true; label1.location = new Point(10, 30); hostbox = new TextBox(); hostbox.parent = this; hostbox.size = new Size(200, 2 * Font.Height); hostbox.location = new Point(10, 55); results = new ListBox(); results.parent = this; results.location = new Point(10, 85); results.size = new Size(360, 18 * Font.Height); Label label2 = new Label(); label2.parent = this; label2.text = "Packet data:"; label2.autosize = true; label2.location = new Point(10, 330); 448

Chapter 11 _ databox = new TextBox(); databox.parent = this; databox.text = "test packet"; databox.size = new Size(200, 2 * Font.Height); databox.location = new Point(80, 325); Button sendit = new Button(); sendit.parent = this; sendit.text = "Start"; sendit.location = new Point(220,52); sendit.size = new Size(5 * Font.Height, 2 * Font.Height); sendit.click += new EventHandler(ButtonSendOnClick); Button stopit = new Button(); stopit.parent = this; stopit.text = "Stop"; stopit.location = new Point(295,52); stopit.size = new Size(5 * Font.Height, 2 * Font.Height); stopit.click += new EventHandler(ButtonStopOnClick); Button closeit = new Button(); closeit.parent = this; closeit.text = "Close"; closeit.location = new Point(300, 320); closeit.size = new Size(5 * Font.Height, 2 * Font.Height); closeit.click += new EventHandler(ButtonCloseOnClick); sock = new Socket(AddressFamily.InterNetwork, SocketType.Raw, ProtocolType.Icmp); sock.setsocketoption(socketoptionlevel.socket, SocketOptionName.ReceiveTimeout, 3000); void ButtonSendOnClick(object obj, EventArgs ea) pinger = new Thread(new ThreadStart(sendPing)); pinger.isbackground = true; pinger.start(); void ButtonStopOnClick(object obj, EventArgs ea) pinger.abort(); results.items.add("ping stopped"); 449

Part 3 _ void ButtonCloseOnClick(object obj, EventArgs ea) sock.close(); Close(); void sendping() IPHostEntry iphe = Dns.Resolve(hostbox.Text); IPEndPoint iep = new IPEndPoint(iphe.AddressList[0], 0); EndPoint ep = (EndPoint)iep; ICMP packet = new ICMP(); int recv, i = 1; packet.type = 0x08; packet.code = 0x00; Buffer.BlockCopy(BitConverter.GetBytes(1), 0, packet.message, 0, 2); byte[] data = Encoding.ASCII.GetBytes(databox.Text); Buffer.BlockCopy(data, 0, packet.message, 4, data.length); packet.messagesize = data.length + 4; int packetsize = packet.messagesize + 4; results.items.add("pinging " + hostbox.text); while(true) packet.checksum = 0; Buffer.BlockCopy(BitConverter.GetBytes(i), 0, packet.message, 2, 2 UInt16 chcksum = packet.getchecksum(); packet.checksum = chcksum; pingstart = Environment.TickCount; sock.sendto(packet.getbytes(), packetsize, SocketFlags.None, iep); try data = new byte[1024]; recv = sock.receivefrom(data, ref ep); pingstop = Environment.TickCount; elapsedtime = pingstop - pingstart; results.items.add("reply from: " + ep.tostring() + ", seq: " + i + ", time = " + elapsedtime + "ms"); catch (SocketException) results.items.add("no reply from host"); 450

Chapter 11 _ i++; Thread.Sleep(3000); public static void Main() Application.Run(new AdvPing()); sock = new Socket(AddressFamily.InterNetwork, SocketType.Raw, ProtocolType.Icmp); sock.setsocketoption(socketoptionlevel.socket, SocketOptionName.ReceiveTimeout, 3000); csc /t:winexe AdvPing.cs ICMP.cs 451

Part 3 _ 6 452

Chapter 11 _ L i Sting using System; using System.Net; using System.Net.Sockets; using System.Text; class TraceRoute public static void Main (string[] argv) byte[] data = new byte[1024]; int recv, timestart, timestop; Socket host = new Socket(AddressFamily.InterNetwork, SocketType.Raw, ProtocolType.Icmp); IPHostEntry iphe = Dns.Resolve(argv[0]); IPEndPoint iep = new IPEndPoint(iphe.AddressList[0], 0); EndPoint ep = (EndPoint)iep; ICMP packet = new ICMP(); packet.type = 0x08; packet.code = 0x00; packet.checksum = 0; Buffer.BlockCopy(BitConverter.GetBytes(1), 0, packet.message, 0, 2); Buffer.BlockCopy(BitConverter.GetBytes(1), 0, packet.message, 2, 2); data = Encoding.ASCII.GetBytes("test packet"); Buffer.BlockCopy(data, 0, packet.message, 4, data.length); packet.messagesize = data.length + 4; int packetsize = packet.messagesize + 4; 453

Part 3 _ UInt16 chcksum = packet.getcchecksum(); packet.checksum = chcksum; host.setsocketoption(socketoptionlevel.socket, SocketOptionName.ReceiveTimeout, 3000); int badcount = 0; for (int i = 1; i < 50; i++) host.setsocketoption(socketoptionlevel.ip, SocketOptionName.IpTimeToLive, i); timestart = Environment.TickCount; host.sendto(packet.getbytes(), packetsize, SocketFlags.None, iep); try data = new byte[1024]; recv = host.receivefrom(data, ref ep); timestop = Environment.TickCount; ICMP response = new ICMP(data, recv); if (response.type == 11) Console.WriteLine("hop 0: response from 1, 2ms", i, ep.tostring(), timestop-timestart); if (response.type == 0) Console.WriteLine("0 reached in 1 hops, 2ms.", ep.tostring(), i, timestop-timestart); break; badcount = 0; catch (SocketException) Console.WriteLine("hop 0: No response from remote host", i); badcount++; if (badcount == 5) Console.WriteLine("Unable to contact remote host"); break; host.close(); 454

Chapter 11 _ for (int i = 1; i < 50; i++) host.setsocketoption(socketoptionlevel.ip, SocketOptionName.IpTimeToLive, i); timestart = Environment.TickCount; host.sendto(packet.getbytes(), packetsize, SocketFlags.None, iep); csc TraceRoute.cs ICMP.cs C:\>TraceRoute www.cisco.com hop 1: No response from remote host hop 2: response from 206.148.207.106:0, 220ms hop 3: response from 65.123.106.113:0, 140ms 455

Part 3 _ hop 4: response from 205.171.20.125:0, 141ms hop 5: response from 205.171.20.142:0, 140ms hop 6: response from 205.171.1.162:0, 130ms hop 7: response from 144.232.26.54:0, 130ms hop 8: response from 144.232.8.117:0, 191ms hop 9: response from 144.232.3.138:0, 190ms hop 10: response from 144.228.44.14:0, 190ms hop 11: response from 128.107.239.89:0, 190ms hop 12: response from 128.107.239.102:0, 191ms 198.133.219.25:0 reached in 13 hops, 180ms. C:\> 7 456

Chapter 11 _ ICMP response = new ICMP(data, recv); long answer = BitConverter.ToUInt32(response.Data, 4); IPAddress netmask = new IPAddress(answer); L i Sting using System; using System.Net; using System.Net.Sockets; using System.Text; class FindMask public static void Main () byte[] data = new byte[1024]; int recv; Socket host = new Socket(AddressFamily.InterNetwork, 457

Part 3 _ SocketType.Raw, ProtocolType.Icmp); IPEndPoint iep = new IPEndPoint(IPAddress.Broadcast, 0); EndPoint ep = (EndPoint)iep; ICMP packet = new ICMP(); packet.type = 0x11; packet.code = 0x00; packet.checksum = 0; Buffer.BlockCopy(BitConverter.GetBytes(1), 0, packet.message, 0, 2); Buffer.BlockCopy(BitConverter.GetBytes(1), 0, packet.message, 2, 2); Buffer.BlockCopy(BitConverter.GetBytes(0), 0, packet.message, 4, 4); packet.messagesize = 8; int packetsize = packet.messagesize + 4; UInt16 chksm = packet.getchecksum(); packet.checksum = chksm; host.setsocketoption(socketoptionlevel.socket, SocketOptionName.ReceiveTimeout, 3000); host.setsocketoption(socketoptionlevel.socket, SocketOptionName.Broadcast, 1); host.sendto(packet.getbytes(), packetsize, SocketFlags.None, iep); try data = new byte[1024]; recv = host.receivefrom(data, ref ep); catch (SocketException) Console.WriteLine("Unable to determine subnet mask for this subnet"); return; ICMP response = new ICMP(data, recv); Console.WriteLine("Received an ICMP type 0 packet", response.type); long answer = BitConverter.ToUInt32(response.Message, 4); IPAddress netmask = new IPAddress(answer); Console.WriteLine("The subnet mask for this subnet is: 0", netmask.tostring()); 458

Chapter 11 _ csc FindMask.cs ICMP.cs host.setsocketoption(socketoptionlevel.socket, SocketOptionName.ReceiveTimeout, 3000); host.setsocketoption(socketoptionlevel.socket, SocketOptionName.Broadcast, 1); C:\>FindMask Received an ICMP type 18 packet The subnet mask for this subnet is: 255.255.252.0 C:\> 459

Part 3 _ L i Sting C:\>windump -X icmp windump listening on\device\packet_el90x1 13:21:49.576074 192.168.1.32 > 255.255.255.255: icmp: address mask request 0x0000 4500 0020 b355 0000 8001 6c55 c0a8 0120 E...U...lU... 0x0010 ffff ffff 1100 ecff 0100 0100 0000 0000... 13:21:49.576203 192.168.1.149 > 192.168.1.32: icmp: address mask is 0xfffffc00 0x0000 4500 0020 6103 0000 ff01 23ff c0a8 0195 E...a...#... 0x0010 c0a8 0120 1200 effe 0100 0100 ffff fc00... 0x0020 0000 0000 0000 0000 0000 0000 0000... 13:21:49.576236 192.168.1.143 > 192.168.1.32: icmp: address mask is 0xfffffc00 0x0000 4500 0020 8db8 0000 ff01 f74f c0a8 018f E...O... 0x0010 c0a8 0120 1200 effe 0100 0100 ffff fc00... 0x0020 0000 0000 0000 0000 0000 0000 0000... 13:21:49.576320 192.168.1.5 > 192.168.1.32: icmp: address mask is 0xfffffc00 0x0000 4500 0020 de96 0000 3c01 6afc c0a8 0105 E...<.j.... 0x0010 c0a8 0120 1200 effe 0100 0100 ffff fc00... 0x0020 0000 0000 0000 0000 0000 0000 0000 0000... 0x0030 0000 0000 0000... 408 packets received by filter 0 packets dropped by kernel C:\> 460

Chapter 11 _ 8 461