aboutsummaryrefslogtreecommitdiff
path: root/j1demo/firmware/dhcp.fs
blob: 971e567ed4d9a204821d548c6a334283c43f803b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
( DHCP: Dynamic Host Configuration Protocol  JCB 13:13 08/24/10)
module[ dhcp"

\  Since DHCP alarm is only used when there is no lease, it is
\  safe to use the ip-subnetmask for the same purpose.

ip-subnetmask constant dhcp-alarm

: dhcp-xid
    ip-router 2@
;

: dhcp-xid!
    ip-router 2!
;

: dhcp-option  \  ( ... n code -- )
    mac-pkt-c,
    dup mac-pkt-c,
    0do
        mac-pkt-c,
    loop
;

: dhcp-common   \  ( messagetype -- )
    d# 67 d# 68
    d# 0 invert dup
    d# 0 dup
    d# 0               \  broadcast ethaddr
                            ( dst-port src-port dst-ip src-ip *ethaddr -- )
    udp-header
    h# 0101 h# 0600 mac-pkt-2,
    dhcp-xid mac-pkt-2,
    d# 10 mac-pkt-,0
    net-my-mac mac-pkt-3,
    d# 101 mac-pkt-,0          \  d# 5 + d# 96 zeroes

    h# 6382 h# 5363
    mac-pkt-2,

    \  DHCP option 53: DHCP Discover
                            \  messagetype
    d# 1 d# 53                    \  messagetype 1 53
    dhcp-option

    \  DHCP option 50: 192.168.1.100 requested

    \  DHCP option 55: Parameter Request List: 
    \  Request Subnet Mask (1), Router (3),
    \  Domain Name Server (6)
    d# 1 d# 3 d# 6   d# 3 d# 55 dhcp-option
;

: dhcp-wrapup
    \  Finish options
    h# ff mac-pkt-c,
    \ mac-wrptr @ d# 1 and 
    d# 1 if    \ XXX
        h# ff mac-pkt-c,
    then

    udp-wrapup
    mac-send
;

\  memory layout is little-endian

: macc@++ ( c-addr -- c-addr+1 c )
    dup 1+ swap macc@ ;

: dhcp-field    \  ( match -- ptr/0 )
    OFFSET_DHCP_OPTIONS d# 4 + mac-inoffset 
                            \  match ptr
    begin
        macc@++             \  match ptr code
        dup h# ff <>
    while                   \  match ptr code
        d# 2 pick =                  
        if
            nip             \  ptr
            exit
        then                \  match ptr
        macc@++ +           \  match ptr'
    repeat
    \  fail - return false
    2drop false
;

: dhcp-yiaddr
    d# 2 OFFSET_DHCP_YIADDR mac-inoffset mac@n
;

: dhcp-field4
    dhcp-field d# 1 +
    macc@++ swap macc@++ swap macc@++ swap macc@
                            ( a b c d )
    swap d# 8 lshift or -rot
    swap d# 8 lshift or
    swap
;

build-debug? [IF]
: .pad ( ip. c-addr u -- )  d# 14 typepad ip-pretty cr ;

: dhcp-status
    ip-addr 2@ s" IP" .pad
    ip-router 2@ s" router" .pad
    ip-subnetmask 2@ s" subnetmask" .pad
;
[ELSE]
: dhcp-status ;
[THEN]

: lease-setalarm
    d# 0 >r
    begin
        2dup d# 63. d>
    while
        d2/ r> 1+ >r
    repeat
    r>
    hex4 space hex8 cr
;

: dhcp-wait-offer
    h# 11 ip-isproto
    OFFSET_UDP_SOURCEPORT packet@ d# 67 = and
    OFFSET_UDP_DESTPORT packet@ d# 68 = and
    d# 2 OFFSET_DHCP_XID mac-inoffset mac@n dhcp-xid d= and
    if
        snap
        d# 53 dhcp-field ?dup
        snap
        if 
            d# 1 + macc@ 
            snap
            dup d# 2 =
            if
                \ [char] % emit
                d# 3 dhcp-common

                \  option 50: request IP
                h# 3204
                dhcp-yiaddr
                mac-pkt-3,

                \  Option 54: server
                h# 3604
                d# 54 dhcp-field4
                mac-pkt-3,

                dhcp-wrapup
            then
            d# 5 =
            if
                \ clrwdt
                \ [char] & emit

                dhcp-yiaddr ip-addr 2!
                d# 1 dhcp-field4 ip-subnetmask 2!
                \  For the router and DNS server, send out ARP requests right now.  This
                \  reduces start-up time.
                d# 3 dhcp-field4 2dup ip-router 2! arp-lookup drop 
                d# 6 dhcp-field4 2dup ip-dns    2! arp-lookup drop 
                \  Option 51: lease time
                s" expires in " type
                d# 51 dhcp-field4 swap d. cr
            then
        then
        snap
    then
;

: dhcp-discover d# 1 dhcp-common dhcp-wrapup ;

]module