ece 550d - duke electrical and computer...
TRANSCRIPT
ECE 550DFundamentals of Computer Systems and Engineering
Fall 2017
Networking Basics
Prof. John BoardDuke University
Slides are derived from work byProfs. Tyler Bletsch and Andrew Hilton (Duke)
2
Networking
• How do computers communicate?• Two (and only two) computers connected by a direct wire?
• Relatively straight forward: move bits across wire• Internet?
• Many computers, some attached to the same “wire”• All around the world• With other communications going on…• And un-reliable links• And tons of different systems, media, protocols…
• Pretty complicated, so how could we possibly manage it?Abstraction
(oh right, the answer to like…everything)
3
7-layer OSI model
• 7 layer networking stack• (Theoretically) can change out any layer at a time
1 Physical Layer
2 Data-link Layer
3 Network Layer
4 Transport Layer
5 Session Layer
6 Presentation Layer
7 Application Layer
4
Layer 1: Physical Layer
• Defines how our physical media work – wire, fiber, RF• Pin layout• Voltages (or laser pulse description or radio frequency usage)• Timing Requirements
• Examples:• Cat 5 Cable (“Ethernet Cable”) – physical wire• Timing of laser pulses over optical fiber• Wireless radio signal specifications
• Lots of “EE” content, abstracted away from most users and programmers
5
Layer 2: Data-link Layer
• How to move bits across the wires in a meaningful way• Communication between two computers on same physical network• May include some error checking
• Example (but just one of many): Ethernet• Data transmitted in frames• Frame has:
• Pre-amble: used to detect collisions (since >1 machine might try to communicate at once)
• Header: source and destination MAC address• Payload: actual data• CRC check: detect corrupt data
• Carrier Sense, Multiple Access, Collision Detect (CSMACD)• Carrier Sense: listen for if anyone else transmitting• Multiple Access: can wire up many computers to it• Collision Detect: two transmissions at once? Detect and retry random
interval later
6
MAC Address?
• Media Access Control Address• One of two (well, 3 with IPv6) addresses associated with each device
on the modern internet, the other being the IP address(es)• MAC address is unique to each device, more specifically unique to each
network adapter (so wired and wireless are two different MAC addresses) – set forever when device is manufactured (*)
• Used for Layer 2 communication
• ifconfig command
• (*) well, usually…
7
CSMACD
• Ethernet uses CSMACD for multiple systems on a network• Other options, but we won’t go into them• Detection of collisions?
• Pre-amble is fixed pattern• Network card senses medium while transmitting• Mismatch with expected? Collision
• Collision happens?• Exponential backoff• Pick random number of time units• Retry• Fail again? Pick random number from 2x as big a range
• Analogy: crowded dinner party• Try to talk. Someone else talking? Wait. Try again. Fail again? Wait
longer
8
Abstraction: Joys and Limitations
• Joys of abstraction:• Can build an Ethernet card without any info about higher layers• Will work with all of them
• Limitations:• 7-layer model’s abstraction not perfect• Ethernet protocol imposes max limit on cable length
• E.g., layer 2 constrains layer 1• This arises from the need to detect collisions before finishing
sending
9
Midterm Histogram 2017
100 * 90 ** 80 *** 70 ** 6099 89 * 79 ** 69 * <60 ****98 * 88 * 78 ** 68 *97 *** 87 **** 77 ** 67 *96 * 86 * 76 * 66 *95 ** 85 **** 75 * 65 *94 **** 84 *** 74 * 64 *93 * 83 **** 73 6392 * 82 ** 72 *** 6291 *****
***81 * 71 * 61
10
7-layer OSI model
• Reminder where we are so farCat 5 Cable
Ethernet
1 Physical Layer
2 Data-link Layer
3 Network Layer
4 Transport Layer
5 Session Layer
6 Presentation Layer
7 Application Layer
11
Our messages so far
• Header says what network layer protocol the pay load is
Preamble Header Payload CRC
12
Layer 3: The Network Layer
• Layer 2 lets computers on same network talk, using just MAC addresses
• Layer 3 lets computers talk across networks• Addressing
• How do we specify what computer to talk to?• Routing
• How do we get from here to there?
• Example: IP protocol• IPv4 and IPv6: pretty similar in most core regards• Best effort delivery• Addressing (IP addresses)• Routing• Analogy: Mailing a letter
13
IP addresses
• IPv4 addresses: 32-bit numbers• Could write as decimal or hex...but that’s not how it’s done• Instead write as four separate bytes (each expressed in decimal),
separated by dots: dotted decimal notation• IPv4 address: 152.3.34.5• As four hex bytes: 98, 03, 22, 05• As a 32-bit hex value: 98032205• 32-bit binary value: 10011000000000110010001000000101
• IPv6: 128-bit addresses• More bits needed to address more separate things on the internet• Been around for a long time, seeing very slow adoption• Addresses represented as eight two-byte groups in hex separated by
colons, for example 3001:0db3:0000:0032:0000:3a2e:0330:7384• We’ll use IPv4 to explain for now
14
IP
• Computer 1 wants to send data to Computer 2• For now, assume it knows IP address (we’ll see DNS later)• Has direct connection to its ISP… but then what?
• The internet is a big place after all..
Computer 166.220.152.32
Computer274.125.130.105
The InternetISP 2
74.125.130.1
ISP 166.220.152.1
15
Let’s zoom in on ISP 1
• ISP has connections to a handful of other places• Generally very high bandwidth connections• Will send your data (packet) to one of these, but which one?
Computer 166.220.152.32
ISP 166.220.152.1
Other Cust A 66.220.152.33
Other Cust B 66.220.152.34
ISP 498.139.183.1
ISP 5
ISP 6129.42.60.1
16
IP Routing
• IP addresses are hierarchical• May not know how to find 74.125.130.105…• But know which way to go to get to 74.______• Move one step closer• Within 74 network, know how to find 74.125• Then 74.125.130• Then find 74.125.130.105
• Analogy:• How do I get to 2200 Mission College Blvd, Santa Clara, CA?
17
IP Routing
• Analogy:• How do I get to 2200 Mission College Blvd, Santa Clara, CA?
• I have no idea, but I can get you to I-40 West• Then you can ask someone else when you get to CA
• Once in CA, you ask someone else:• “I only know its north of here, so take the 5 North and ask
someone else”
• Etc..• This works because our physical addresses are hierarchical:
Country, State, City, Street, Number
18
Routing Basics
• Routing is done with tables• CIDR notation: 40.1.0.0/24
• Match first 24 bits of 40.1.0.0, ignore remaining 8 bits• Find match, entry tells what link to send out on• Example
• 40.0.0.0/8 => Link 0 (match first 8 bits, ignore remaining 24)• 50.1.0.0/16 => Link 1• 50.3.27.0/24 => Link 3 • 50.2.0.0/16 => Link 2• 50.3.42.0/24 => Link 1
19
Routing: More Complex
• Approach one: Static Routing• Enter all routes• Let system run• Hope nothing goes down• Works fine for small networks
• Reality:• Network links/systems go down• Often multiple paths to same place
• Changing traffic patterns = changing fastest route
20
Distance Vector Protocols
• Routers• Know distances to immediate neighbors• Compute distance vector
• How far to any destination from all known info• Transmit distance vectors to neighbors
• Discover better (shorter) route? Update table• Now know more info, so repeat process
21
Distance Vector Routing
A
C
Dz
Dest Cost RtA ∞ —B ∞ —C ∞ —D ∞ —
v
Dest Cost RtA 2 AB ∞ —C 2 CD ∞ —
x
Dest Cost RtA ∞ —B 2 BC ∞ —D ∞ —
w
Dest Cost RtA ∞ —B ∞ —C ∞ —D ∞ —
1
B
t
Dest Cost RtA ∞ —B 1 BC ∞ —D 3 D
2
3
3
2
4
3
2
1
2
2
22
Distance Vector Routing
A
C
Dz
Dest Cost RtA ∞ —B ∞ —C ∞ —D ∞ —
v
Dest Cost RtA 2 AB ∞ —C 2 CD ∞ —
x
Dest Cost RtA ∞ —B 2 BC ∞ —D ∞ —
w
Dest Cost RtA ∞ —B ∞ —C ∞ —D ∞ —
1
B
t
Dest Cost RtA ∞ —B 1 BC ∞ —D 3 D
2
3
3
2
4
3
2
1
2
2
2+1 via v2+3 via x2+1 via v
23
Distance Vector Routing
A
C
Dz
Dest Cost RtA ∞ —B ∞ —C ∞ —D ∞ —
v
Dest Cost RtA 2 AB ∞ —C 2 CD ∞ —
x
Dest Cost RtA ∞ —B 2 BC ∞ —D ∞ —
w
Dest Cost RtA ∞ —B ∞ —C ∞ —D ∞ —
1
B
t
Dest Cost RtA ∞ —B 1 BC ∞ —D 3 D
2
3
3
2
4
3
2
1
2
2
no improvements
24
Distance Vector Routing
A
C
Dz
Dest Cost RtA 4 vB 3 tC 4 vD 5 t
v
Dest Cost RtA 2 AB ∞ —C 2 CD ∞ —
x
Dest Cost RtA ∞ —B 2 BC ∞ —D 6 t
w
Dest Cost RtA 3 vB 5 xC 3 vD ∞ —
1
B
t
Dest Cost RtA ∞ —B 1 BC ∞ —D 3 D
2
3
3
2
4
3
2
1
2
2
25
Distance Vector Routing
A
C
Dz
Dest Cost RtA 4 vB 3 tC 4 vwD 5 t
v
Dest Cost RtA 2 AB 5 zC 2 CD 7 z
x
Dest Cost RtA 6 wB 2 BC 6 wD 6 t
w
Dest Cost RtA 3 vB 5 xC 3 vD 9 x
1
B
t
Dest Cost RtA 6 zB 1 BC 6 zD 3 D
2
3
3
2
4
3
2
1
2
2
26
Distance Vector Routing
A
C
Dz
Dest Cost RtA 4 vB 3 tC 4 vwD 5 t
v
Dest Cost RtA 2 AB 5 zC 2 CD 7 z
x
Dest Cost RtA 6 wB 2 BC 6 wD 6 t
w
Dest Cost RtA 3 vB 5 xC 3 vD 8 v
1
B
t
Dest Cost RtA 6 zB 1 BC 6 zD 3 D
2
3
3
2
4
3
2
1
2
2
27
Distance Vector Routing
A
C
Dz
Dest Cost RtA 4 vB 3 tC 4 vwD 5 t
v
Dest Cost RtA 2 AB 5 zC 2 CD 7 z
x
Dest Cost RtA 6 wB 2 BC 6 wD 6 t
w
Dest Cost RtA 3 vB 5 xC 3 vD 8 v
1
B
t
Dest Cost RtA 6 zB 1 BC 6 zD 3 D
2
3
3
2
4
3
2
1
2
2
Now everything is stable…but what if…
28
Distance Vector Routing
A
C
Dz
Dest Cost RtA 4 vB 3 tC 4 vD 5 t
v
Dest Cost RtA 2 AB 5 zC 2 CD 7 z
x
Dest Cost RtA 6 wB 2 BC 6 wD 6 t
w
Dest Cost RtA 3 vB 5 xC 3 vD 8 v
1
B
t
Dest Cost RtA 6 zB 1 BC 6 zD 3 D
2
3
3
2
4
3
2
1
2
2
Router z fails?
29
Distance Vector Routing
A
C
Dz
Dest Cost RtA 4 vB 3 tC 4 vD 5 t
v
Dest Cost RtA 2 AB 6 wC 2 CD 9 w
x
Dest Cost RtA 6 wB 2 BC 6 wD 6 t
w
Dest Cost RtA 3 vB 5 xC 3 vD 8 v
1
B
t
Dest Cost RtA 9 xB 1 BC 9 xD 3 D
2
3
3
2
4
3
2
1
2
2
Temporary problem:v <->w routing loop!
30
Distance Vector Routing
A
C
Dz
Dest Cost RtA 4 vB 3 tC 4 vD 5 t
v
Dest Cost RtA 2 AB 6 wC 2 CD 9 w
x
Dest Cost RtA 6 wB 2 BC 6 wD 6 t
w
Dest Cost RtA 3 vB 5 xC 3 vD 9 x
1
B
t
Dest Cost RtA 9 xB 1 BC 9 xD 3 D
2
3
3
2
4
3
2
1
2
2
Fixed it!(v has stale cost still)
31
Distance Vector Routing
A
C
Dz
Dest Cost RtA 4 vB 3 tC 4 vD 5 t
v
Dest Cost RtA 2 AB 6 wC 2 CD 10 w
x
Dest Cost RtA 6 wB 2 BC 6 wD 6 t
w
Dest Cost RtA 3 vB 5 xC 3 vD 9 x
1
B
t
Dest Cost RtA 9 xB 1 BC 9 xD 3 D
2
3
3
2
4
3
2
1
2
2
32
Distance Vector Routing
A
C
Dz
Dest Cost RtA 4 vB 3 tC 4 vD 5 t
v
Dest Cost RtA 2 AB 6 wC 2 CD 10 w
x
Dest Cost RtA 6 wB 2 BC 6 wD 6 t
w
Dest Cost RtA 3 vB 5 xC 3 vD 9 x
1
B
t
Dest Cost RtA 9 xB 1 BC 9 xD 3 D
2
3
3
2
4
3
2
1
2
2
Stable again.If z comes back, we’ll rediscover better routes
33
Distance Vector Routing
A
C
Dz
Dest Cost RtA 4 vB 3 tC 4 vD 5 t
v
Dest Cost RtA 2 AB 6 wC 2 CD 10 w
x
Dest Cost RtA 6 wB 2 BC 6 wD 6 t
w
Dest Cost RtA 3 vB 5 xC 3 vD 9 x
1
B
t
Dest Cost RtA 9 xB 1 BC 9 xD 3 D
2
3
3
2
4
3
2
1
2
2
But what if v fails?
34
Distance Vector Routing
A
C
Dz
Dest Cost RtA 4 vB 3 tC 4 vD 5 t
v
Dest Cost RtA 2 AB 6 wC 2 CD 10 w
x
Dest Cost RtA 6 wB 2 BC 6 wD 6 t
w
Dest Cost RtA 3 vB 5 xC 3 vD 9 x
1
B
t
Dest Cost RtA 9 xB 1 BC 9 xD 3 D
2
3
3
2
4
3
2
1
2
2
v is down…but x advertises routes to A and C (cost 6)
35
Distance Vector Routing
A
C
Dz
Dest Cost RtA 4 vB 3 tC 4 vD 5 t
v
Dest Cost RtA 2 AB 6 wC 2 CD 10 w
x
Dest Cost RtA 6 wB 2 BC 6 wD 6 t
w
Dest Cost RtA 9 xB 5 xC 9 xD 9 x
1
B
t
Dest Cost RtA 9 xB 1 BC 9 xD 3 D
2
3
3
2
4
3
2
1
2
2
update w’s tableNow x has new info
36
Distance Vector Routing
A
C
Dz
Dest Cost RtA 4 vB 3 tC 4 vD 5 t
v
Dest Cost RtA 2 AB 6 wC 2 CD 10 w
x
Dest Cost RtA 9 wB 2 BC 9 tD 6 t
w
Dest Cost RtA 9 xB 5 xC 9 xD 9 x
1
B
t
Dest Cost RtA 9 xB 1 BC 9 xD 3 D
2
3
3
2
4
3
2
1
2
2
Update x’s table..w and t will update to 12(routing through x)
37
Count-to-infinity
• Algorithm will slowly “count to infinity”• Actually: count to max value it holds• Then throw away the route, concluding there is no path there
• Packets sent in meanwhile? • IP: Time To Live (TTL)• Starts at fixed value (e.g., 255)• Decremented every time the packet is forwarded• Packet dropped when TTL == 0
• Traceroute: uses TTL fields to probe to different distances• Uses ICMP protocol to get response on TTL
• Note: many fancier routing schemes, we aren’t covering
38
Distance Vector Routing
x
Dest Cost RtA 9 wB 2 BC 9 tD 6 t
w
Dest Cost RtA 9 x—B 5 xC 9 xD 9 x
3
Obvious optimization:- If x gets a route from w, it should not advertise that route back to w- Called “split-horizon”- Helps stabilize faster, but does not solve the problem
(may still count to infinity)
39
Link State Protocols
• Another option: link state protocols• Send info about direct connections to all routers• All routers build global pictures of network• Run graph algorithms to find shortest paths
• E.g., Dijkstra’s shortest path algorithm• Global information is nice, but…
• Complex for very large systems• How many routers on the internet?• Do they all exchange all their info and run Dijkstra’s?
• Of course not..• So… what do we do?
39
Use Abstraction… (and hierarchy)
40
• Divide internet up into Autonomous Systems (ASes)• Each AS can advertise routes to other ASes• Routing internal to AS is hidden from outside world
• Can be Link State, Distance Vector, other…• We won’t go into too many details
40
Border Gateway Protocol
AS 1
AS 4
AS 2AS 3
41
The IPv6 problem
• IPv4 developed in late 1970’s, deployed in early 1980’s• Computers were big and expensive; there was one network computer
for the entire School of Engineering then (I ran it!).• PCs had just been invented but it was silly to think they would ever be
on the internet, which was for serious work• So 2^32 addresses was an approximation of infinity• Initial tenants (most in US) got huge allocations – MIT alone got
multiple /8 networks.• (Global allocations, MIT sale)
42
7-layer OSI model
• IP: hierarchical addresses + best effort deliveryCat 5 Cable
Ethernet
1 Physical Layer
2 Data-link Layer
3 Network Layer
4 Transport Layer
5 Session Layer
6 Presentation Layer
7 Application Layer
IP
43
Our messages so far
• IP Header has• Src IP• Dest IP• Payload type (what protocol)• Other info
• Side note: IP does fragmentation to fit within frame size• We aren’t covering that
Preamble Header Header CRCPayload
44
Layer 4: Transport Layer
• Reliability (if used)• Acknowledgements of data receipt• Retries of failed data for individual packets – does not guarantee
delivered in order for instance!• Flow Control
• Restrict rate of data sending• Multiplexing/De-multiplexing data
• E.g., Ports: identify which program some data is for• Keep data streams separate
45
Layer 5: Session Layer
• Concept of “a connection”• Establish/terminate• (OSI includes a variety of obscure features not often used)
• TCP: combines these two layers together• Sets up/terminates sessions• Has sequence numbering for packets• Acknowledges (ACKs) packets that are received• Establishes flow control (responds to congestion by throttling sending)
46
TCP
• We’ll draw diagram with computers on each side• Time goes down• Three messages above (TCP’s “3 way handshake”)
9987: SYN, ACK(1045)
1045: SYN
1046: ACK(9987)
47
TCP
• To open a new connection:• 1 computer sends SYN (“Hi, lets talk”)
• All messages have sequence numbers including SYN• First sequence number of a new connection is random• TCP sequence numbers by byte
9987: SYN, ACK(1045)
1045: SYN
1046: ACK(9987)
48
TCP
• Other computer• ACKs (Acknowledges) the message (says what sequence # it ACKs)• Also sends SYN “Hey sure, lets talk”
9987: SYN, ACK(1045)
1045: SYN
1046: ACK(9987)
49
TCP
• First computer then ACKs this SYN• And probably sends data along with the ACK
• TCP control info (SYN, ACK, FIN): bits in TCP header• Packets can have multiple control bits on + carry data
9987: SYN, ACK(1045)
1045: SYN
1046: ACK(9987)
50
TCP: Normal operation
• Data going right in blue• ACKS coming left in green
• note: ACK #ed by expected next data• Sliding window (flow control)
• Limit amount of un-ACKed data at a time
200025003000
35004000
4500
ACK(2500)ACK(3000)ACK(3500)
51
TCP: Re-ordered Data
• Data may get re-ordered in network• One packet takes one route, another takes another
• TCP: no problem• Sender re-orders data properly• Sends ACK for as much data as it has
5000
ACK(6000)
5500
52
TCP: Lost data
• Data may also get lost in the network• E.g., router is backlogged, can’t handle it has to drop from queue
• TCP will re-send un-ACKed data after a timeout
6000
ACK(6500)
6000
53
TCP: Duplicate Data
• Receiver may get duplicate data• 7000-7499 gets lost• But 7500-7599 arrives• Then sender re-sends both: no ACK for either (why not?)• No problem: receiver drops duplicate (can tell: sequence #s)
7000
ACK(8000)
7500
7000
7500
54
TCP: Closing connection
• Connection closed with FIN message• Receiver ACKs
• Other side may close (with FIN) [typical]• Or remain open: can still send data• Side that closed cannot send, but should receive/ACK• FIN/ACK may be one message
9000 FIN
ACK(9016)
101090 FIN
ACK(101106)
55
TCP: Closing connection
• What if ACK for FIN gets lost?• FIN gets retried… but other side expects connection is closed?
• TCP has a state to handle this• Connection expected to be closed, but resources/state still held• Times out if no activity (assumes ACK got through if no retry)
9000 FIN
ACK(9016)
101090 FINACK(101106)
101090 FINACK(101106)
56
Flow control: Sliding Window
• Problem:• Congestion -> dropped packets• Dropped packets -> Retries• Retries = duplicates of data -> More congestion
• Vicious cycle…
• TCP implements flow control with a sliding window• Limitation of amount of un-ACKed data out at a time• Retry required? Shrink window
• Assumes congestion, tries to avoid it• No retries in a while? Grow window back
• Maybe it cleared up?
57
7-layer OSI model
• TCP: It’s the coolest thing since memory got sliced into pages!Cat 5 Cable
Ethernet
1 Physical Layer
2 Data-link Layer
3 Network Layer
4 Transport Layer
5 Session Layer
6 Presentation Layer
7 Application Layer
IP
TCP
(TCP)
58
Our messages so far
• TCP header has• Source/Dest port• Sequence numbers• Control bits (SYN/ACK/FIN)• Check sum over data• Other stuff
Preamble Header Header CRCHeader Payload
59
Layer 6: Presentation Layer
• Responsible for data formats• Examples
• Character encoding schemes• Serialization of objects
• We’re not really going to talk about it much
60
Layer 7: Application Layer
• Protocol specific to how applications want to communicate• Examples:
• HTTP(S)• (S)FTP• SSH• SMTP• IMAP• …
• Again, not going into this much…
61
7-layer OSI model
• Flexibility Example: Wired vs Wireless• Change out two layers, rest stay the same
Cat 5 Cable
Ethernet
IP
TCP
(TCP)
-
HTTP
Wireless Radio
802.11
IP
TCP
(TCP)
-
HTTP
62
7-layer OSI model
• Flexibility Example: A different application on top of bothCat 5 Cable
Ethernet
IP
TCP
(TCP)
-
SMTP
Wireless Radio
802.11
IP
TCP
(TCP)
-
SMTP
63
How to find out IP addresses?
• I don’t know about you, but whenever I want to visit Google, I just type “172.217.2.206” into my browser
• Not really• People don’t memorize IP addresses – need convenient way to
translate human-readable names to IP addresses• This is the domain name system (DNS)• Hierarchical servers with hierarchical database:
Root DNS Servers
com DNS servers org DNS servers edu DNS servers
poly.eduDNS servers
umass.eduDNS serversyahoo.com
DNS serversamazon.comDNS servers
pbs.orgDNS servers
Figure from DNS – Domain Name System, CS234, UC Irvine.
64
requesting hostcis.poly.edu
gaia.cs.umass.edu
root DNS server
local DNS serverdns.poly.edu
1
2
45
6
authoritative DNS serverdns.cs.umass.edu
7
8
TLD DNS server
3
DNS name resolution example
Recursive query:• Resolves any query by
consulting servers that are authoritative for the query
• To do so, servers may recursively query other servers higher up in the hierarchy
Adapted from DNS – Domain Name System, CS234, UC Irvine.
65
Additional DNS facts
• DNS contains multiple kinds of records:• Name -> IP translations (“A” records)• IP -> Name translations (“PTR” records)• Name -> Name translations (“CNAME” records)• more
• DNS clients and servers can cache results• The local Duke DNS server definitely has “google.com” cached
• Second-level domains (“duke.edu”, “google.com”, etc.) are registered, usually by paying a fee, with a registrar (e.g. “easydns.com”) who interacts with the top-level domain authority (Verisign for .com, Educause for .edu, etc.)
• Sub-domains can be created administratively by the owner of a domain (e.g. “whatever.duke.edu” can be made by the “duke.edu” admin).
66
Wireshark demo
67
Network programming
• Coding networking code in…• Java: Look in java.net, start with Socket• C:
• socket()• connect()• accept()• bind() • listen()
68
Example C sockets clientDoes a simple HTTP request of google.com
// adapted from simple-client.c by Sean Walton and Macmillan Publishers// adapted by Tyler Bletsch for Duke University
// This program will request http://google.com/ and print the first 1024 bytes of the response// Note: for simplicity, the google IP address is hard coded
#include <stdlib.h>#include <stdio.h>#include <unistd.h> // needed for close#include <string.h> // needed for bzero#include <sys/socket.h> // needed for socket calls#include <resolv.h> // needed for socket type#include <arpa/inet.h> // needed for inet_aton
#define PORT 80#define SERVER_ADDR "172.217.1.14" // ^ this is google.com#define MAXBUF 1024
char request[] = "GET / HTTP/1.0\r\n\r\n";int request_len = sizeof(request)-1;
#define die(s) { perror(s); exit(1); }
int main() {int sockfd;struct sockaddr_in dest;char buffer[MAXBUF];
/*---Open socket for streaming---*/if ( (sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0 )
die("socket");
/*---Initialize server address/port struct---*/bzero(&dest, sizeof(dest));dest.sin_family = AF_INET;dest.sin_port = htons(PORT);if ( inet_aton(SERVER_ADDR, &dest.sin_addr) == 0 )
die(SERVER_ADDR);
/*---Connect to server---*/if (connect(sockfd, (struct sockaddr*)&dest, sizeof(dest)) != 0)
die("connect");
/*---Send request---*/send(sockfd, request, request_len, 0);
/*---Get and print response---*/bzero(buffer, MAXBUF);recv(sockfd, buffer, sizeof(buffer), 0);printf("%s", buffer);
/*---Clean up---*/close(sockfd);return 0;
}
69
Test run
70
Summary
• Networking Overview• 7-layer model• Emphasis on IP (Layer 3) and TCP (Layers 4 and 5)
• Not comprehensive, but…• You are now at least conversant enough to discuss the OSI stack at
parties