VNET VIRTUAL NETWORK USING THREADS (vn_ is used at the beginning of each function name to prevent conflicts with student modules) Students use 3 main functions to access the "link layer": int vn_SystemInit(char *ConfigFile) Opens and reads the configuration file For each host If the host is on this SubNet: Configures an "interface" with its vnet address and the subnet address, netmask, port#, cross traffic, and reliability for the Subnet Sets up the "arp cache" for this Subnet: cross references the vnet name & address with the IP name & address for each host(interface) on the Subnet Mallocs and initializes the IP queue Creates a thread for each interface-- to wait for packets and queue them in the IP queue If the configuration file has a trace entry (TRACE is enabled), the tracefile is opened and a pcap_file header is written returns 0 for success set an error code and returns a non-zero integer if an error occurs (see vnet_error.h) int vn_SendPkt(char *pkt, int pktsize, struct in_addr NextHop, int iface) Checks to see that the specified interface is configured If this is a broadcast address, the packet is sent to each host on the specified subnet (*ex. for BROADCAST to subnet 10.10.1.0, NextHop=10.10.1.255) Else uses the "arp cache" to look up "physical address" (IP address = physical address) of the NextHop Appends an "ethernet" header to the packet Calls vn_Corrupt(char *Mbuf, int PacSize) to randomly corrupt & lose packets depending on the reliability specified in the configuration file If the packet has not been randomly chosen to be lost, the packet may be delayed a random number of milliseconds (if cross traffic is > 0) and then sent on to the NextHop If TRACE is enabled, a pcap_pkthdr and the packet are written to the tracefile returns 0 for success returns PEERNOTFOUND for an invalid address or interface sets an error code and returns a non-zero integer if another error occurs (see vnet_error.h) *for broadcast, NextHop = the broadcast address of the subnet you wish to broadcast a packet on(you can only broadcast on your own subnet) int vn_RecvPkt(char *buf, int size) Returns a packet from the IP queue. Blocks until receiving a signal that a packet has been placed in the queue Copies "size" characters into "buf" provided by the calling function returns the number of characters actually received *packets are dropped if the IP queue is full The following 4 functions are provided and can be used as needed: int vn_gethostname(char *myname, int len) copies this host's vnetname into the supplied buffer, myname, of size len returns 0 for success returns GETHOSTNAME on error ex. int retcode, len; char myname[NAMESIZE]; retcode = vn_gethostname(myname, NAMESIZE); struct in_addr vn_gethostbyname(char *vnetname) returns the vnet address as a struct in_addr, for the vnet name pointed to by the char pointer, vnetname returns an address > 0 on success returns an address equal 0 on error and sets loc_err to GETHOSTFAIL ex. struct in_addr toaddr; char vnetname[NAMESIZE] = "delta1"; toaddr = vn_gethostbyname(vnetname); int vn_gethostbyaddr(struct in_addr vnetaddr, char *vnetname, int len) matches the u_long int contained in the in_addr structure, vnetaddr, against vnet addresses stored in the "arp cache" a vnetname is copied into the array pointed to by the char pointer, vnetname, on a successful match returns GETHOSTFAIL on error ex. int retcode; char vnetname[NAMESIZE]; struct in_addr vnetaddr; retcode = (vnetaddr, vnetname, NAMESIZE); int vn_Stats(int *ifaces, Stats *buf) stores the number of interfaces for this host into ifaces and stores the statistics for all interfaces into the Stats structure passed to it by the calling function ex. int retcode, ifaces; Stats buf[MAXINTERFACES]; retcode = vn_Stats(&ifaces, buf); Error messages and codes are contained in vnet_error.c & vent_error.h. Study these as you may wish to extend the codes for "trickle-up" error reporting as you go from layer to layer. vnet_link.h file: #define is used for: PKTSIZE 1024 RECVQSIZE 50 SUBNETHOSTS 35 MAXINTERFACES 10 ADDRSIZE 16 NAMESIZE 32 (see also vnet_ip.h, netinet/ip.h, netinet/udp.h) Configuration File: (see subnet.config) For each subnet in the file: first line: net subnet netmask port# crosstraffic reliability where "net" is a literal indicating a new subnet subnet is the subnet's vnet address netmask is this subnet's netmask(used for forwarding) port is the subnet port number crosstraffic is an integer indicating the maximum delay (in milliseconds) that will be experienced by a packet as a result of simulated cross traffic reliability is a floating point number between 0.0 and 100.0 indicating the reliability of the network. This is the percent chance that any given packet will be successfully sent. successive lines: realname vnetname vnetaddr where realname is the host's actual name vnetname is the vnet name for this host vnetaddr is the vnet address for this host on this subnet (ex. cetus2e alpha1 10.10.5.4) (optional): default vnetrouter where "default" is a literal indicating a default routing entry vnetrouter is this subnet's default router used when no matching entry can be found in the routing table (ex. default 10.10.2.1) (optional): route subnet vnetrouter netmask where "route" is a literal indicating a subnet routing entry subnet is the vnet address of a distant subnet this router needs to know about but is not directly connected to vnetrouter is the nexthop to reach the distant subnet netmask is the netmask used for that distant subnet (ex. route 10.10.5.0 10.10.2.1 255.255.255.0) (optional): trace filename where "trace" is a literal indicating that all packets sent and received on this subnet should be written to a tracefile (filename appended with the host name) that can be read by tcpdump *this should be the last entry in the file the preceeding lines are repeated for each SubNet VNET Tutorial: -To use the sample executable type at the unix prompt: vnet demo.config (use demo.config because the vnet demo is a very early prototype and so uses a config file without the netmasks) -If you are at hydra4b, you should see the interface configuration displayed: INTERFACES: VnetIFace: 10.10.5.1 SubNet: 10.10.5.0 BroadCast: 10.10.5.255 Port: 5099 RecvSock: 6 SendSock: 5 CrossTraf: 65 Reliability: 85.000000 ARPCACHE vnetname: delta1 vnetaddr: 10.10.5.1 realname: hydra4b vnetport: 5099 vnetname: delta2 vnetaddr: 10.10.5.2 realname: hydra4c vnetport: 5099 vnetname: delta3 vnetaddr: 10.10.5.3 realname: hydra4d vnetport: 5099 SENDTO(VnetName or VnetAddress): delta3 -To send a message to delta3, type delta3 or 10.10.5.3 after the colon(above) and press enter. Then type your message (below) and press enter-- MESSAGE: hello from delta1 DESTINATION: 10.10.5.3 NEXTHOP: 10.10.5.3 Interface 0--PktsSent: 1, PktsRecv: 0, PktsDropped: 0 CorruptedPkts: 0, LostPkts: 0 -at hydra4d you should see your message and a display of network statistics. I am calling vn_Stats() right after every packet is sent and received just to show what is happening. SENDTO(VnetName or VnetAddress): RECEIVED 45 CHARACTER MSG: hello from delta1 Interface 0--PktsSent: 0, PktsRecv: 1, PktsDropped: 0 CorruptedPkts: 0, LostPkts: 0 -To send a broadcast to the network, enter the broadcast address as the destination: 10.10.5.255