92
Build Dynamic DNS Server from Scratch in C (Part 1) SITCON 2017

Build Dynamic DNS server from scratch in C (Part1)

Embed Size (px)

Citation preview

Page 1: Build Dynamic DNS server from scratch in C (Part1)

Build Dynamic DNS Server from Scratch in C (Part 1)

SITCON 2017

Page 2: Build Dynamic DNS server from scratch in C (Part1)

yenWu(吳彥寬)

● NCKU CSIE (Master, Now)● NCKU Mathematics(Bachelor)● C● Robotic● RTOS● Microkernel

2

Page 3: Build Dynamic DNS server from scratch in C (Part1)

DDNS● Project Link: https://github.com/yenWu/DDNS● DDNS = Dynamic DNS● In order to figure out what's DNS and how it work, I would Implement

Dynamic DNS server in User-level.

3

Page 4: Build Dynamic DNS server from scratch in C (Part1)

Outline

1. Story Time2. Introduction3. Prerequisites4. Aims5. Cautions

4

Page 5: Build Dynamic DNS server from scratch in C (Part1)

Story Time

5

Page 6: Build Dynamic DNS server from scratch in C (Part1)

Story Time

Project “Build your own DNS Server in FreeBSD”

6

Page 7: Build Dynamic DNS server from scratch in C (Part1)

Story Time

Can I build one from scratch to be my final project?

7

Page 8: Build Dynamic DNS server from scratch in C (Part1)

Story Time

Sure. But you should try more difficult one, “Dynamic DNS Server”.

8

Page 9: Build Dynamic DNS server from scratch in C (Part1)

Story Time

OK. That’s better than building one.

9

Page 10: Build Dynamic DNS server from scratch in C (Part1)

Story Time

OK. That’s better than building one.

I took three days long to fix the lowercase bug when building AMP.

10

Page 12: Build Dynamic DNS server from scratch in C (Part1)

An Episode!

12

Page 13: Build Dynamic DNS server from scratch in C (Part1)

An Episode!I said that I want to build a TCP/IP Stack at first time ...

13

Page 14: Build Dynamic DNS server from scratch in C (Part1)

Build TCP/IP Stack from Scratch in C (Part 1)

SITCON 2017

Page 15: Build Dynamic DNS server from scratch in C (Part1)

Most frequently asked questions and comments● Why do you try to build from scratch? I already have BIND(FreeBSD)!● Your project doesn’t more better than BIND. Why do you still do that?

15

Page 16: Build Dynamic DNS server from scratch in C (Part1)

Most frequently asked questions and comments● Why do you try to build from scratch? I already have BIND(FreeBSD)!

○ Writing one is the best pratice for me to understand the concept of DNS.

● Your project doesn’t more better than BIND. Why do you still do that?

16

Page 17: Build Dynamic DNS server from scratch in C (Part1)

Most frequently asked questions and comments● Why do you try to build from scratch? I already have BIND(FreeBSD)!

○ Writing one is the best pratice for me to understand the concept of DNS.

● Your project doesn’t more better than BIND. Why do you still do that?○ If you have wrote one, you can have more idea to improve it.

17

Page 18: Build Dynamic DNS server from scratch in C (Part1)

Introduction

18

Page 20: Build Dynamic DNS server from scratch in C (Part1)

Internet

● The Internet maintains two principal namespaces

○ Domain name hierarchy

○ Internet Protocol (IP) address spaces

20

Page 21: Build Dynamic DNS server from scratch in C (Part1)

What is DNS server?

21

Page 22: Build Dynamic DNS server from scratch in C (Part1)

What is DNS server?

22

Page 23: Build Dynamic DNS server from scratch in C (Part1)

What is DNS server?● Hierarchical decentralized naming system● Distributed database● A little similar to Phonebook● Translate readily memorized domain name to IP address● DNS client v.s. DNS server● Two category of the DNS server

○ Recursive DNS server○ Iterative DNS server

23

Page 24: Build Dynamic DNS server from scratch in C (Part1)

Hierarchy of DNS servers

“www.yahoo.com”

24

Page 25: Build Dynamic DNS server from scratch in C (Part1)

Hierarchy of DNS servers

“www.yahoo.com.”

25

Page 26: Build Dynamic DNS server from scratch in C (Part1)

Hierarchy of DNS servers

“www.yahoo.com.”

26

Page 27: Build Dynamic DNS server from scratch in C (Part1)

Hierarchy of DNS servers

“www.yahoo.com.”

27

Page 28: Build Dynamic DNS server from scratch in C (Part1)

Let’s take it deeplyThat’s all I knew before I started this.

28

Page 29: Build Dynamic DNS server from scratch in C (Part1)

What is DNS server?● DNS client v.s. DNS server

○ DNS Server ⊇ { Resolver, Database, Cache}○ DNS Client ⊇ { Resolver}

● Unlike a phonebook, DNS can be quickly updated, allowing a service's location on the network to change without affecting the end users, who continue to use the same host name.

● Responds with answers to queries against its database.● Port 53

29

Page 30: Build Dynamic DNS server from scratch in C (Part1)

What is DNS server?● The most common types of records

○ Start of Authority (SOA)○ IP addresses (A and AAAA)○ SMTP mail exchangers (MX)○ Name servers (NS)○ Pointers for reverse DNS lookups (PTR)○ Domain name aliases (CNAME)

● Different reaction depend on the type of record

30

Page 31: Build Dynamic DNS server from scratch in C (Part1)

Glance

31

Page 32: Build Dynamic DNS server from scratch in C (Part1)

Resolver

A Resolver maps a name to an address and vice versa.

Query

Response

Resolver Name Server

32

Page 33: Build Dynamic DNS server from scratch in C (Part1)

Iterative Resolution

client

edu

2

root

3

com

4

NS: google.com

5

iterative request“What is the IP address of www.google.com?”

ncku

1

iterative response“The IP address of www.google.com is 216.239.37.99.”

6

33

Page 34: Build Dynamic DNS server from scratch in C (Part1)

Iterative Resolution

client

edu

2

root

3

com

4

NS: google.com

5

iterative request“What is the IP address of www.google.com?”

ncku

1

iterative response“The IP address of www.google.com is 216.239.37.99.”

6

Iterative Query

34

Page 35: Build Dynamic DNS server from scratch in C (Part1)

Recursive Resolution

client

edu

2

root

3

com

4

google

5

recursive request“What is the IP address of www.google.com?”

ncku

1

recursive response“The IP address of www.google.com is 216.239.37.99.”

6

35

Page 36: Build Dynamic DNS server from scratch in C (Part1)

Recursive Resolution

client

edu

2

root

3

com

4

google

5

recursive request“What is the IP address of www.google.com?”

ncku

1

recursive response“The IP address of www.google.com is 216.239.37.99.”

6

Recursive Query

36

Page 37: Build Dynamic DNS server from scratch in C (Part1)

● Why hierarchy?

Questions

37

Page 38: Build Dynamic DNS server from scratch in C (Part1)

● Why hierarchy?○ More efficient than heterarchy.○ Update could be immediately showed.

Questions

38

Page 39: Build Dynamic DNS server from scratch in C (Part1)

● Why hierarchy?○ More efficient than heterarchy.○ Update could be immediately showed.

● Does it always waste a lot of time on process of lookup?

Questions

39

Page 40: Build Dynamic DNS server from scratch in C (Part1)

● Why hierarchy?○ More efficient than heterarchy.○ Update could be immediately showed.

● Does it always waste a lot of time on process of lookup?○ Right! So caching!

Questions

40

Page 41: Build Dynamic DNS server from scratch in C (Part1)

● Why hierarchy?○ More efficient than heterarchy.○ Update could be immediately showed.

● Does it always waste a lot of time on process of lookup?○ Right! So caching!

● Is it true that react immediately when we update the informaition(RR)?

Questions

41

Page 42: Build Dynamic DNS server from scratch in C (Part1)

● Why hierarchy?○ More efficient than heterarchy.○ Update could be immediately showed.

● Does it always waste a lot of time on process of lookup?○ Right! So caching!

● Is it true that react immediately when we update the informaition(RR)?○ Probably not … because of the cache.

Questions

42

Page 43: Build Dynamic DNS server from scratch in C (Part1)

● Why hierarchy?○ More efficient than heterarchy.○ Update could be immediately showed.

● Does it always waste a lot of time on process of lookup?○ Right! So caching!

● Is it true that react immediately when we update the informaition(RR)?○ Probably not … because of the cache.

● Is it easy to update the information(RR)?

Questions

43

Page 44: Build Dynamic DNS server from scratch in C (Part1)

● Why hierarchy?○ More efficient than heterarchy.○ Update could be immediately showed.

● Does it always waste a lot of time on process of lookup?○ Right! So caching!

● Is it true that react immediately when we update the informaition(RR)?○ Probably not … because of the cache.

● Is it easy to update the information(RR)?○ Maybe not. In RFC 1035, we would load zone and startup file into database at local.

Questions

44

Page 45: Build Dynamic DNS server from scratch in C (Part1)

● Why hierarchy?○ More efficient than heterarchy.○ Update could be immediately showed.

● Does it always waste a lot of time on process of lookup?○ Right! So caching!

● Is it true that react immediately when we update the informaition(RR)?○ Probably not … because of the cache.

● Is it easy to update the information(RR)?○ Maybe not. In RFC 1035, we would load zone and startup file into database at local.

● Can we update the information on remote DNS Server?

Questions

45

Page 46: Build Dynamic DNS server from scratch in C (Part1)

● Why hierarchy?○ More efficient than heterarchy.○ Update could be immediately showed.

● Does it always waste a lot of time on process of lookup?○ Right! So caching!

● Is it true that react immediately when we update the informaition(RR)?○ Probably not … because of the cache.

● Is it easy to update the information(RR)?○ Maybe not. In RFC 1035, we would load zone and startup file into database at local.

● Can we update the information on remote DNS Server?○ No. We need another mechanism to do that. In RFC 2136, we could name DNS to DDNS with

remote updating mechanism.

● That’s why we need a Dynamic DNS Server.

Questions

46

Page 47: Build Dynamic DNS server from scratch in C (Part1)

Process flow

47

Page 48: Build Dynamic DNS server from scratch in C (Part1)

48

Client

DHCP Server

Primary DNS ServerZone File

IP Address?

IP Address Update

Dynamic DNS

Page 49: Build Dynamic DNS server from scratch in C (Part1)

Fine ! We know the concept of DNS.

Can we start it ?

49

Page 50: Build Dynamic DNS server from scratch in C (Part1)

Prerequisites

50

Page 51: Build Dynamic DNS server from scratch in C (Part1)

IPheader

UDPheader DNS message

IPheader

TCPheader DNS message

2-byteDNS msg.

length

● DNS messages are encapsulated in UDP by default.● If the resolver expects the response to exceed 512 bytes, the

resolver encapsulates the query in TCP instead.● If a request is sent over UDP and the response is longer than 512

bytes, the server sends the first 512 bytes of the response using UDP and sets the TC (truncated) flag. The resolver then re-sends the query using TCP.

max. 512 bytes

no limit (up to max. TCP payload size)

51

Transport

Page 52: Build Dynamic DNS server from scratch in C (Part1)

DNS Protocol

52

Page 53: Build Dynamic DNS server from scratch in C (Part1)

Message Format● Header Section● Question Section● RR Section

53

Page 54: Build Dynamic DNS server from scratch in C (Part1)

Header Section

● ID := identification of DNS transaction● FLAG● QDCOUNT := questions record count● ANCOUNT := answer record count● NSCOUNT := authority record count● ARCOUNT := additional record count

54

Page 55: Build Dynamic DNS server from scratch in C (Part1)

Flag● QR● Opcode● RCODE

55

Page 56: Build Dynamic DNS server from scratch in C (Part1)

Flag

Query 0

Respond 1

Query 0000

OpCode Retired

0001

Status 0010

reserved 0011

Notify 0100

Update 0101

56

Page 57: Build Dynamic DNS server from scratch in C (Part1)

Flag NoError No Error 0000

FormErr Format Error 0001

ServFail Server Failure 0010

NXDomain Non-Existent Domain 0011

NotImp Not Implemented 0100

Refused Query Refused 0101

YXDomain Name Exists when it should not 0110

YXRRSet RR Set Exists when it should not 0111

NXRRSet RR Set that should exist does not 1000

NotAuth Server Not Authoritative for zone 1001

NotZone Name not contained in zone 101057

Page 58: Build Dynamic DNS server from scratch in C (Part1)

Message Formate● Header Section● Question Section● RR Section

58

Page 59: Build Dynamic DNS server from scratch in C (Part1)

Message Formate● Header Section● Question Section● RR Section

59

Page 60: Build Dynamic DNS server from scratch in C (Part1)

RR Section● The most common Type

○ Start of Authority (SOA)○ IP addresses (A and AAAA)○ SMTP mail exchangers (MX)○ Name servers (NS)○ Pointers for reverse DNS lookups (PTR)○ Domain name aliases (CNAME)

60

Page 61: Build Dynamic DNS server from scratch in C (Part1)

RR Section● The most common TYPE

○ Start of Authority (SOA)○ IP addresses (A and AAAA)○ SMTP mail exchangers (MX)○ Name servers (NS)○ Pointers for reverse DNS lookups (PTR)○ Domain name aliases (CNAME)

● RDATA depends on TYPE

61

Page 62: Build Dynamic DNS server from scratch in C (Part1)

RR Section

IN Internet 0x0001

CH Chaos 0x0003

HS Hesiod 0x0004

QCLASS Only

Any 0x00FF

62

Page 63: Build Dynamic DNS server from scratch in C (Part1)

Aims of this Project

63

Page 64: Build Dynamic DNS server from scratch in C (Part1)

Aims1. Efficiency2. Uniform Protocol Access Interface3. Consistency with RFC standard4. Pluggable Database and DNS Complement5. Benchmark6. Education

64

Page 65: Build Dynamic DNS server from scratch in C (Part1)

Efficiency

Is it good enough ?

65

Page 66: Build Dynamic DNS server from scratch in C (Part1)

EfficiencyIn order to send message rapidly, we should maintain continous memory buffer!

66

Page 67: Build Dynamic DNS server from scratch in C (Part1)

Uniform

Protocol

Access

Interface

● MACRO● Hard to debug

67

Page 68: Build Dynamic DNS server from scratch in C (Part1)

Uniform Protocol Access Interface

68

Page 69: Build Dynamic DNS server from scratch in C (Part1)

Consistency with RFC standard

69

Page 70: Build Dynamic DNS server from scratch in C (Part1)

Pluggable Database and DNS Complement

70

Page 71: Build Dynamic DNS server from scratch in C (Part1)

Pluggable Database and DNS Complement

Object-Orien Programming in C !?

71

Page 72: Build Dynamic DNS server from scratch in C (Part1)

Education

● A pioneer have shared all of the pitfalls he met at protocol level.● Provide uniform protocol access interface.● Easy to change implementation.

72

Page 73: Build Dynamic DNS server from scratch in C (Part1)

Benchmark

● Follow the assignment(phonebook) of Embedded System Course in NCKU

● Mission: You need to optimize the building and searching time on phonebook.

● Assignment Link: https://hackmd.io/s/rJYD4UPKe

73

Page 74: Build Dynamic DNS server from scratch in C (Part1)

Phonebook

● clear● gnuplot

74

Page 75: Build Dynamic DNS server from scratch in C (Part1)

Great ! It time to work!

75

Page 76: Build Dynamic DNS server from scratch in C (Part1)

Cautions

76

Page 77: Build Dynamic DNS server from scratch in C (Part1)

Pitfalls!!!

77

Page 78: Build Dynamic DNS server from scratch in C (Part1)

Notice !!!

● If you start to build it, maybe you would be trapped in this three pitfalls: ○ Domain Name Format in Message Format○ Message Compression○ Endian Transfer

78

Page 79: Build Dynamic DNS server from scratch in C (Part1)

Domain Name in Message Format

class of network (1 = Internet)

12 5 2 1 Address – IPv4

Name Server (authoritative) Canonical Name (alias) Pointer – reverse lookup

15 Mail Exchange 28 Address - IPv6 252 Zone Transfer

PTR CNAME

NS A

MX AAAA AXFR

sent in query;repeated in response

w w w . g o o g l e . c o m

Query Name

Query Type Query Class

16 bit 16 bit

Variable Length

79

Page 80: Build Dynamic DNS server from scratch in C (Part1)

Domain Name in Message Format

class of network (1 = Internet)

12 5 2 1 Address – IPv4

Name Server (authoritative) Canonical Name (alias) Pointer – reverse lookup

15 Mail Exchange 28 Address - IPv6 252 Zone Transfer

PTR CNAME

NS A

MX AAAA AXFR

sent in query;repeated in response

w w w . g o o g l e . c o m .

Query Name

Query Type Query Class

16 bit 16 bit

Variable Length

80

Page 81: Build Dynamic DNS server from scratch in C (Part1)

Domain Name in Message Format

class of network (1 = Internet)

12 5 2 1 Address – IPv4

Name Server (authoritative) Canonical Name (alias) Pointer – reverse lookup

15 Mail Exchange 28 Address - IPv6 252 Zone Transfer

PTR CNAME

NS A

MX AAAA AXFR

sent in query;repeated in response

3 w w w 6 g o o g l e 3 c o m 0

count

Query Name

Query Type Query Class

16 bit 16 bit

Variable Length

81

Page 82: Build Dynamic DNS server from scratch in C (Part1)

Que

stio

nSe

ctio

nA

nsw

erSe

ctio

n

3 w w w 6 g o o g l e 3 c o m 0

1 1 000 ... 1 1 0 0

2 bit 30 bit

16 bit

Query Name

Query Type Query Class

Header

Domain Name

16 bit 16 bit

Variable Length

Variable Length

12 byte

C0 0C

PointerCompression flag

Compression

82

Page 83: Build Dynamic DNS server from scratch in C (Part1)

Que

stio

nSe

ctio

nA

nsw

erSe

ctio

n

3 w w w 6 g o o g l e 3 c o m 0

1 1 000 ... 1 1 0 0

2 bit 14 bit

16 bit

Query Name

Query Type Query Class

Header

Domain Name

C0 0C

PointerCompression flag

Compression 11

Reserve 10

Reserve 01

Normal 00

Compression

83

Page 84: Build Dynamic DNS server from scratch in C (Part1)

Que

stio

nSe

ctio

nA

nsw

erSe

ctio

n

3 w w w 6 g o o g l e 3 c o m 0

1 1 000 ... 1 1 0 0

2 bit 30 bit

16 bit

Query Name

Query Type Query Class

Header

Domain Name

C0 0C

PointerCompression flag

Compression 11

Reserve 10

Reserve 01

Normal 00

Compression

84

Page 85: Build Dynamic DNS server from scratch in C (Part1)

The difficult one for Endian transfer

0 0 0 1 0 1 0 1 0 1 0 1 0 1 0 0

QR Opcode AA TC RD RA Z AD CD RCODE

85

Page 86: Build Dynamic DNS server from scratch in C (Part1)

As I've said …………………………………..

0 0 0 1 0 1 0 1 0 1 0 1 0 1 0 0

QR Opcode AA TC RD RA Z AD CD RCODE

1 0 1 0 0 1 0 0 0 1 0 0 0 0 1 0

86

Page 87: Build Dynamic DNS server from scratch in C (Part1)

struct in C

● make sure continuous memory space

87

Page 89: Build Dynamic DNS server from scratch in C (Part1)

Demo - Resolver

89

Page 90: Build Dynamic DNS server from scratch in C (Part1)

Project would continous when I have free time (... graduated paper)

90

Page 91: Build Dynamic DNS server from scratch in C (Part1)

Contact me● Github: yenWu● LinkedIn: Yen-Kwan Wu● NCKU CSIE WIKI: yenWu● E-Mail: [email protected]

91

Page 92: Build Dynamic DNS server from scratch in C (Part1)

Q & A

92