 
 : socket ( -- socket ) (socket) 1st ;
 
-: init-socket ( -- ) f 5353 <inet4> <datagram> 0 (socket) as-mutate ;
+: init-socket-on-port ( port -- )
+  f swap <inet4> <datagram> 0 (socket) as-mutate ;
+
+: init-socket ( -- ) 53 init-socket-on-port ;
 
 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
 


: set-upstream-server ( ip -- ) 0 (upstream-server) as-mutate ;

! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

: query->answer/cache ( query -- rrs/NX/f )
    {
      { [ dup type>> CNAME = ] [ cache-get* ] }
      {
        [ dup clone CNAME >>type cache-get* vector? ]
        [
          dup clone CNAME >>type cache-get* 1st       ! query rr/cname
          dup rdata>>                                 ! query rr/cname cname
          >r swap clone r>                            ! rr/cname query cname
          >>name                                      ! rr/cname query
          query->answer/cache                         ! rr/cname rrs/NX/f
            {
              { [ dup vector? ] [ clone push-on ] }
              { [ dup NX = ]    [ nip ] }
              { [ dup f = ]     [ nip ] }
            }
          cond
        ]
      }
      { [ t ] [ cache-get* ] }
    }
  cond ;

! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!


  message->ba                                 ! addr-spec byte-array
  swap                                        ! byte-array addr-spec
  socket send
  loop ;

! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

: start ( -- ) init-socket init-upstream-server loop ;

MAIN: start