@@ -2,13 +2,14 @@ import { Device, DeviceType } from "./device";
22import { ViewGraph } from "../graphs/viewgraph" ;
33import PcImage from "../../assets/pc.svg" ;
44import { Position } from "../common" ;
5- import { IpAddress } from "../../packets/ip" ;
5+ import { IpAddress , IPv4Packet } from "../../packets/ip" ;
66import { createDropdown , DeviceInfo , RightBar } from "../../graphics/right_bar" ;
77import { ProgramInfo } from "../../graphics/renderables/device_info" ;
8- import { sendPacket } from "../packet" ;
8+ import { Packet , sendPacket } from "../packet" ;
99import { Ticker } from "pixi.js" ;
1010import { DeviceId } from "../graphs/datagraph" ;
1111import { Layer } from "./layer" ;
12+ import { EchoRequest } from "../../packets/icmp" ;
1213import { isHost , RunningProgram } from "../graphs/datagraph" ;
1314
1415const DEFAULT_ECHO_DELAY = 250 ; // ms
@@ -48,6 +49,13 @@ export class Host extends Device {
4849 return DeviceType . Host ;
4950 }
5051
52+ receivePacket ( packet : Packet ) : DeviceId | null {
53+ if ( this . ip . equals ( packet . rawPacket . destinationAddress ) ) {
54+ this . handlePacket ( packet ) ;
55+ }
56+ return null ;
57+ }
58+
5159 getProgramList ( ) {
5260 const adjacentDevices = this . viewgraph
5361 . getDeviceIds ( )
@@ -64,7 +72,7 @@ export class Host extends Device {
6472 // TODO: extract into classes
6573 const programList : ProgramInfo [ ] = [
6674 {
67- name : "Send ICMP echo " ,
75+ name : "Send ping " ,
6876 inputs : [ dropdownContainer ] ,
6977 start : ( ) => this . sendSingleEcho ( destination . value ) ,
7078 } ,
@@ -104,27 +112,51 @@ export class Host extends Device {
104112
105113 private sendSingleEcho ( id : string ) {
106114 const dst = parseInt ( id ) ;
107- sendPacket ( this . viewgraph , "ICMP" , this . id , dst ) ;
115+ const dstDevice = this . viewgraph . getDevice ( dst ) ;
116+ if ( dstDevice ) {
117+ const echoRequest = new EchoRequest ( 0 ) ;
118+ const ipPacket = new IPv4Packet ( this . ip , dstDevice . ip , echoRequest ) ;
119+ sendPacket (
120+ this . viewgraph ,
121+ ipPacket ,
122+ echoRequest . getPacketType ( ) ,
123+ this . id ,
124+ dst ,
125+ ) ;
126+ }
108127 }
109128
110129 private startNewEchoServer ( id : string ) {
111130 this . addRunningProgram ( { name : "Echo server" , inputs : [ id ] } ) ;
112131 this . startEchoServer ( id ) ;
113132 }
114133
134+ // TODO: Receive ip address instead of id?
115135 private startEchoServer ( id : string ) {
116136 const dst = parseInt ( id ) ;
117- let progress = 0 ;
118- const send = ( ticker : Ticker ) => {
119- const delay = DEFAULT_ECHO_DELAY ;
120- progress += ticker . deltaMS ;
121- if ( progress < delay ) {
122- return ;
123- }
124- sendPacket ( this . viewgraph , "ICMP" , this . id , dst ) ;
125- progress -= delay ;
126- } ;
127- this . startProgram ( send ) ;
137+ const dstDevice = this . viewgraph . getDevice ( dst ) ;
138+ // If ip address received instead of id, device may not exist.
139+ if ( dstDevice ) {
140+ let progress = 0 ;
141+ const echoRequest = new EchoRequest ( 0 ) ;
142+ const ipPacket = new IPv4Packet ( this . ip , dstDevice . ip , echoRequest ) ;
143+ const send = ( ticker : Ticker ) => {
144+ const delay = DEFAULT_ECHO_DELAY ;
145+ progress += ticker . deltaMS ;
146+ if ( progress < delay ) {
147+ return ;
148+ }
149+ sendPacket (
150+ this . viewgraph ,
151+ ipPacket ,
152+ echoRequest . getPacketType ( ) ,
153+ this . id ,
154+ dst ,
155+ ) ;
156+ progress -= delay ;
157+ } ;
158+ this . startProgram ( send ) ;
159+ }
128160 }
129161
130162 private startProgram ( tick : ( ticker : Ticker ) => void ) : Pid {
0 commit comments