Hex Artifact Content
Not logged in

Artifact d3abec98100bb982dea8fc457d3f1502900e7d19:


0000: 5c 20 4c 69 6e 75 78 20 73 70 65 63 69 66 69 63  \ Linux specific
0010: 20 6e 65 74 77 6f 72 6b 20 73 74 75 66 66 0a 0a   network stuff..
0020: 5c 20 43 6f 70 79 72 69 67 68 74 20 28 43 29 20  \ Copyright (C) 
0030: 32 30 31 36 20 20 20 42 65 72 6e 64 20 50 61 79  2016   Bernd Pay
0040: 73 61 6e 0a 0a 5c 20 54 68 69 73 20 70 72 6f 67  san..\ This prog
0050: 72 61 6d 20 69 73 20 66 72 65 65 20 73 6f 66 74  ram is free soft
0060: 77 61 72 65 3a 20 79 6f 75 20 63 61 6e 20 72 65  ware: you can re
0070: 64 69 73 74 72 69 62 75 74 65 20 69 74 20 61 6e  distribute it an
0080: 64 2f 6f 72 20 6d 6f 64 69 66 79 0a 5c 20 69 74  d/or modify.\ it
0090: 20 75 6e 64 65 72 20 74 68 65 20 74 65 72 6d 73   under the terms
00a0: 20 6f 66 20 74 68 65 20 47 4e 55 20 41 66 66 65   of the GNU Affe
00b0: 72 6f 20 47 65 6e 65 72 61 6c 20 50 75 62 6c 69  ro General Publi
00c0: 63 20 4c 69 63 65 6e 73 65 20 61 73 20 70 75 62  c License as pub
00d0: 6c 69 73 68 65 64 20 62 79 0a 5c 20 74 68 65 20  lished by.\ the 
00e0: 46 72 65 65 20 53 6f 66 74 77 61 72 65 20 46 6f  Free Software Fo
00f0: 75 6e 64 61 74 69 6f 6e 2c 20 65 69 74 68 65 72  undation, either
0100: 20 76 65 72 73 69 6f 6e 20 33 20 6f 66 20 74 68   version 3 of th
0110: 65 20 4c 69 63 65 6e 73 65 2c 20 6f 72 0a 5c 20  e License, or.\ 
0120: 28 61 74 20 79 6f 75 72 20 6f 70 74 69 6f 6e 29  (at your option)
0130: 20 61 6e 79 20 6c 61 74 65 72 20 76 65 72 73 69   any later versi
0140: 6f 6e 2e 0a 0a 5c 20 54 68 69 73 20 70 72 6f 67  on...\ This prog
0150: 72 61 6d 20 69 73 20 64 69 73 74 72 69 62 75 74  ram is distribut
0160: 65 64 20 69 6e 20 74 68 65 20 68 6f 70 65 20 74  ed in the hope t
0170: 68 61 74 20 69 74 20 77 69 6c 6c 20 62 65 20 75  hat it will be u
0180: 73 65 66 75 6c 2c 0a 5c 20 62 75 74 20 57 49 54  seful,.\ but WIT
0190: 48 4f 55 54 20 41 4e 59 20 57 41 52 52 41 4e 54  HOUT ANY WARRANT
01a0: 59 3b 20 77 69 74 68 6f 75 74 20 65 76 65 6e 20  Y; without even 
01b0: 74 68 65 20 69 6d 70 6c 69 65 64 20 77 61 72 72  the implied warr
01c0: 61 6e 74 79 20 6f 66 0a 5c 20 4d 45 52 43 48 41  anty of.\ MERCHA
01d0: 4e 54 41 42 49 4c 49 54 59 20 6f 72 20 46 49 54  NTABILITY or FIT
01e0: 4e 45 53 53 20 46 4f 52 20 41 20 50 41 52 54 49  NESS FOR A PARTI
01f0: 43 55 4c 41 52 20 50 55 52 50 4f 53 45 2e 20 20  CULAR PURPOSE.  
0200: 53 65 65 20 74 68 65 0a 5c 20 47 4e 55 20 41 66  See the.\ GNU Af
0210: 66 65 72 6f 20 47 65 6e 65 72 61 6c 20 50 75 62  fero General Pub
0220: 6c 69 63 20 4c 69 63 65 6e 73 65 20 66 6f 72 20  lic License for 
0230: 6d 6f 72 65 20 64 65 74 61 69 6c 73 2e 0a 0a 5c  more details...\
0240: 20 59 6f 75 20 73 68 6f 75 6c 64 20 68 61 76 65   You should have
0250: 20 72 65 63 65 69 76 65 64 20 61 20 63 6f 70 79   received a copy
0260: 20 6f 66 20 74 68 65 20 47 4e 55 20 41 66 66 65   of the GNU Affe
0270: 72 6f 20 47 65 6e 65 72 61 6c 20 50 75 62 6c 69  ro General Publi
0280: 63 20 4c 69 63 65 6e 73 65 0a 5c 20 61 6c 6f 6e  c License.\ alon
0290: 67 20 77 69 74 68 20 74 68 69 73 20 70 72 6f 67  g with this prog
02a0: 72 61 6d 2e 20 20 49 66 20 6e 6f 74 2c 20 73 65  ram.  If not, se
02b0: 65 20 3c 68 74 74 70 3a 2f 2f 77 77 77 2e 67 6e  e <http://www.gn
02c0: 75 2e 6f 72 67 2f 6c 69 63 65 6e 73 65 73 2f 3e  u.org/licenses/>
02d0: 2e 0a 0a 24 32 30 30 20 43 6f 6e 73 74 61 6e 74  ...$200 Constant
02e0: 20 6e 65 74 6c 69 6e 6b 2d 73 69 7a 65 23 0a 30   netlink-size#.0
02f0: 20 56 61 6c 75 65 20 6e 65 74 6c 69 6e 6b 2d 73   Value netlink-s
0300: 6f 63 6b 0a 73 6f 63 6b 61 64 64 72 5f 6e 6c 20  ock.sockaddr_nl 
0310: 62 75 66 66 65 72 3a 20 6e 65 74 6c 69 6e 6b 2d  buffer: netlink-
0320: 61 64 64 72 0a 6e 65 74 6c 69 6e 6b 2d 73 69 7a  addr.netlink-siz
0330: 65 23 20 62 75 66 66 65 72 3a 20 6e 65 74 6c 69  e# buffer: netli
0340: 6e 6b 2d 62 75 66 66 65 72 0a 0a 41 46 5f 4e 45  nk-buffer..AF_NE
0350: 54 4c 49 4e 4b 20 6e 65 74 6c 69 6e 6b 2d 61 64  TLINK netlink-ad
0360: 64 72 20 6e 6c 5f 66 61 6d 69 6c 79 20 77 21 0a  dr nl_family w!.
0370: 30 20 20 20 20 20 20 20 20 20 20 6e 65 74 6c 69  0          netli
0380: 6e 6b 2d 61 64 64 72 20 6e 6c 5f 70 61 64 20 77  nk-addr nl_pad w
0390: 21 0a 24 30 30 64 38 36 30 37 66 35 20 6e 65 74  !.$00d8607f5 net
03a0: 6c 69 6e 6b 2d 61 64 64 72 20 6e 6c 5f 67 72 6f  link-addr nl_gro
03b0: 75 70 73 20 6c 21 0a 0a 3a 20 70 72 65 70 2d 6e  ups l!..: prep-n
03c0: 65 74 6c 69 6e 6b 20 28 20 2d 2d 20 29 0a 20 20  etlink ( -- ).  
03d0: 20 20 65 70 69 70 65 72 20 40 20 66 69 6c 65 6e    epiper @ filen
03e0: 6f 20 50 4f 4c 4c 49 4e 20 20 70 6f 6c 6c 66 64  o POLLIN  pollfd
03f0: 73 20 66 64 73 21 2b 20 3e 72 0a 20 20 20 20 6e  s fds!+ >r.    n
0400: 65 74 6c 69 6e 6b 2d 73 6f 63 6b 20 50 4f 4c 4c  etlink-sock POLL
0410: 49 4e 20 20 72 3e 20 66 64 73 21 2b 0a 20 20 20  IN  r> fds!+.   
0420: 20 70 6f 6c 6c 66 64 73 20 2d 20 70 6f 6c 6c 66   pollfds - pollf
0430: 64 20 2f 20 74 6f 20 70 6f 6c 6c 66 64 23 20 3b  d / to pollfd# ;
0440: 0a 0a 3a 20 67 65 74 2d 6e 65 74 6c 69 6e 6b 20  ..: get-netlink 
0450: 28 20 2d 2d 20 29 0a 20 20 20 20 50 46 5f 4e 45  ( -- ).    PF_NE
0460: 54 4c 49 4e 4b 20 53 4f 43 4b 5f 44 47 52 41 4d  TLINK SOCK_DGRAM
0470: 20 4e 45 54 4c 49 4e 4b 5f 52 4f 55 54 45 20 73   NETLINK_ROUTE s
0480: 6f 63 6b 65 74 20 64 75 70 20 3f 69 6f 72 20 74  ocket dup ?ior t
0490: 6f 20 6e 65 74 6c 69 6e 6b 2d 73 6f 63 6b 0a 20  o netlink-sock. 
04a0: 20 20 20 67 65 74 70 69 64 20 20 20 20 20 5b 20     getpid     [ 
04b0: 6e 65 74 6c 69 6e 6b 2d 61 64 64 72 20 6e 6c 5f  netlink-addr nl_
04c0: 70 69 64 20 5d 4c 20 6c 21 0a 20 20 20 20 6e 65  pid ]L l!.    ne
04d0: 74 6c 69 6e 6b 2d 73 6f 63 6b 20 6e 65 74 6c 69  tlink-sock netli
04e0: 6e 6b 2d 61 64 64 72 20 73 6f 63 6b 61 64 64 72  nk-addr sockaddr
04f0: 5f 6e 6c 20 62 69 6e 64 20 3f 69 6f 72 0a 20 20  _nl bind ?ior.  
0500: 20 20 70 72 65 70 2d 6e 65 74 6c 69 6e 6b 20 3b    prep-netlink ;
0510: 0a 0a 3a 20 6e 65 74 6c 69 6e 6b 3f 20 28 20 2d  ..: netlink? ( -
0520: 2d 20 66 6c 61 67 20 29 0a 20 20 20 20 70 6f 6c  - flag ).    pol
0530: 6c 66 64 73 20 70 6f 6c 6c 66 64 23 20 3e 70 6f  lfds pollfd# >po
0540: 6c 6c 20 64 72 6f 70 20 72 65 61 64 2d 65 76 65  ll drop read-eve
0550: 6e 74 0a 20 20 20 20 70 6f 6c 6c 66 64 73 20 5b  nt.    pollfds [
0560: 20 70 6f 6c 6c 66 64 20 72 65 76 65 6e 74 73 20   pollfd revents 
0570: 5d 4c 20 2b 20 77 40 20 50 4f 4c 4c 49 4e 20 61  ]L + w@ POLLIN a
0580: 6e 64 20 3b 0a 0a 3a 20 77 61 69 74 2d 66 6f 72  nd ;..: wait-for
0590: 2d 6e 65 74 6c 69 6e 6b 20 28 20 2d 2d 20 29 0a  -netlink ( -- ).
05a0: 20 20 20 20 42 45 47 49 4e 20 20 6e 65 74 6c 69      BEGIN  netli
05b0: 6e 6b 3f 20 30 3d 20 57 48 49 4c 45 20 20 3f 65  nk? 0= WHILE  ?e
05c0: 76 65 6e 74 73 20 20 52 45 50 45 41 54 20 3b 0a  vents  REPEAT ;.
05d0: 0a 3a 20 72 65 61 64 2d 6e 65 74 6c 69 6e 6b 20  .: read-netlink 
05e0: 28 20 2d 2d 20 61 64 64 72 20 75 20 29 0a 20 20  ( -- addr u ).  
05f0: 20 20 6e 65 74 6c 69 6e 6b 2d 73 6f 63 6b 20 6e    netlink-sock n
0600: 65 74 6c 69 6e 6b 2d 62 75 66 66 65 72 20 6e 65  etlink-buffer ne
0610: 74 6c 69 6e 6b 2d 73 69 7a 65 23 20 4d 53 47 5f  tlink-size# MSG_
0620: 44 4f 4e 54 57 41 49 54 20 72 65 63 76 20 64 75  DONTWAIT recv du
0630: 70 20 3f 69 6f 72 2d 61 67 61 69 6e 0a 20 20 20  p ?ior-again.   
0640: 20 6e 65 74 6c 69 6e 6b 2d 62 75 66 66 65 72 20   netlink-buffer 
0650: 6e 65 74 6c 69 6e 6b 2d 62 75 66 66 65 72 20 6c  netlink-buffer l
0660: 40 20 72 6f 74 20 75 6d 69 6e 20 3b 0a 0a 3a 20  @ rot umin ;..: 
0670: 72 65 61 64 2d 6e 65 74 6c 69 6e 6b 3f 20 28 20  read-netlink? ( 
0680: 2d 2d 20 61 64 64 72 20 75 20 29 0a 20 20 20 20  -- addr u ).    
0690: 70 6f 6c 6c 2d 74 69 6d 65 6f 75 74 23 20 30 20  poll-timeout# 0 
06a0: 70 74 69 6d 65 6f 75 74 20 32 21 20 20 77 61 69  ptimeout 2!  wai
06b0: 74 2d 66 6f 72 2d 6e 65 74 6c 69 6e 6b 0a 20 20  t-for-netlink.  
06c0: 20 20 72 65 61 64 2d 6e 65 74 6c 69 6e 6b 20 3b    read-netlink ;
06d0: 0a 0a 3a 20 61 64 64 72 65 73 73 3f 20 28 20 61  ..: address? ( a
06e0: 64 64 72 20 75 20 2d 2d 20 66 6c 61 67 20 29 0a  ddr u -- flag ).
06f0: 20 20 20 20 30 3d 20 49 46 20 20 64 72 6f 70 20      0= IF  drop 
0700: 66 61 6c 73 65 20 20 45 58 49 54 20 20 54 48 45  false  EXIT  THE
0710: 4e 0a 20 20 20 20 6e 6c 6d 73 67 5f 74 79 70 65  N.    nlmsg_type
0720: 20 77 40 20 52 54 4d 5f 4e 45 57 4c 49 4e 4b 20   w@ RTM_NEWLINK 
0730: 5b 20 52 54 4d 5f 44 45 4c 41 44 44 52 20 31 2b  [ RTM_DELADDR 1+
0740: 20 5d 4c 20 77 69 74 68 69 6e 20 3b 0a 0a 5c 20   ]L within ;..\ 
0750: 64 65 62 75 67 67 69 6e 67 20 73 74 75 66 66 20  debugging stuff 
0760: 74 6f 20 73 65 65 20 77 68 61 74 20 6b 69 6e 64  to see what kind
0770: 20 6f 66 20 74 68 69 6e 67 73 20 61 72 65 20 67   of things are g
0780: 6f 69 6e 67 20 6f 6e 0a 0a 3a 20 2e 72 74 61 64  oing on..: .rtad
0790: 64 72 34 20 28 20 61 64 64 72 20 2d 2d 20 29 20  dr4 ( addr -- ) 
07a0: 24 43 20 2b 20 34 20 2e 69 70 34 61 20 32 64 72  $C + 4 .ip4a 2dr
07b0: 6f 70 20 3b 0a 3a 20 2e 72 74 61 64 64 72 36 20  op ;.: .rtaddr6 
07c0: 28 20 61 64 64 72 20 2d 2d 20 29 20 24 43 20 2b  ( addr -- ) $C +
07d0: 20 24 31 30 20 2e 69 70 36 61 20 32 64 72 6f 70   $10 .ip6a 2drop
07e0: 20 3b 0a 3a 20 2e 69 66 61 6d 2d 66 6c 61 67 73   ;.: .ifam-flags
07f0: 20 28 20 6e 20 2d 2d 20 29 0a 20 20 20 20 69 66   ( n -- ).    if
0800: 61 2d 66 24 20 62 6f 75 6e 64 73 20 44 4f 0a 09  a-f$ bounds DO..
0810: 64 75 70 20 31 20 61 6e 64 20 49 46 20 20 49 20  dup 1 and IF  I 
0820: 63 40 20 65 6d 69 74 20 20 54 48 45 4e 20 20 32  c@ emit  THEN  2
0830: 2f 0a 20 20 20 20 4c 4f 4f 50 20 20 64 72 6f 70  /.    LOOP  drop
0840: 20 3b 0a 3a 20 2e 69 66 61 6d 2d 61 64 64 72 20   ;.: .ifam-addr 
0850: 28 20 61 64 64 72 20 2d 2d 20 29 0a 20 20 20 20  ( addr -- ).    
0860: 63 61 73 65 20 20 64 75 70 20 69 66 61 6d 5f 66  case  dup ifam_f
0870: 61 6d 69 6c 79 20 63 40 0a 09 41 46 5f 49 4e 45  amily c@..AF_INE
0880: 54 20 20 6f 66 20 20 2e 72 74 61 64 64 72 34 20  T  of  .rtaddr4 
0890: 20 65 6e 64 6f 66 0a 09 41 46 5f 49 4e 45 54 36   endof..AF_INET6
08a0: 20 6f 66 20 20 2e 72 74 61 64 64 72 36 20 20 65   of  .rtaddr6  e
08b0: 6e 64 6f 66 0a 09 6e 69 70 20 65 6e 64 63 61 73  ndof..nip endcas
08c0: 65 20 3b 0a 3a 20 2e 72 74 6d 73 67 20 28 20 61  e ;.: .rtmsg ( a
08d0: 64 64 72 20 2d 2d 20 29 0a 20 20 20 20 63 61 73  ddr -- ).    cas
08e0: 65 20 6e 6c 6d 73 67 5f 74 79 70 65 20 77 40 0a  e nlmsg_type w@.
08f0: 09 52 54 4d 5f 4e 45 57 41 44 44 52 20 6f 66 20  .RTM_NEWADDR of 
0900: 2e 22 20 61 64 64 20 22 20 65 6e 64 6f 66 0a 09  ." add " endof..
0910: 52 54 4d 5f 44 45 4c 41 44 44 52 20 6f 66 20 2e  RTM_DELADDR of .
0920: 22 20 64 65 6c 20 22 20 65 6e 64 6f 66 0a 20 20  " del " endof.  
0930: 20 20 65 6e 64 63 61 73 65 20 3b 0a 3a 20 2e 72    endcase ;.: .r
0940: 74 61 64 64 72 20 28 20 61 64 64 72 20 75 20 2d  taddr ( addr u -
0950: 2d 20 29 20 64 72 6f 70 0a 20 20 20 20 64 75 70  - ) drop.    dup
0960: 20 2e 72 74 6d 73 67 20 20 6e 6c 6d 73 67 68 64   .rtmsg  nlmsghd
0970: 72 20 2b 0a 20 20 20 20 64 75 70 20 69 66 61 6d  r +.    dup ifam
0980: 5f 69 6e 64 65 78 20 6c 40 20 30 20 2e 72 20 2e  _index l@ 0 .r .
0990: 22 20 3a 20 22 0a 20 20 20 20 64 75 70 20 69 66  " : ".    dup if
09a0: 61 6d 5f 66 6c 61 67 73 20 63 40 20 2e 69 66 61  am_flags c@ .ifa
09b0: 6d 2d 66 6c 61 67 73 20 2e 69 66 61 6d 2d 61 64  m-flags .ifam-ad
09c0: 64 72 0a 20 20 20 20 63 72 20 3b 0a 3a 20 6e 65  dr.    cr ;.: ne
09d0: 74 6c 69 6e 6b 2d 74 65 73 74 20 28 20 2d 2d 20  tlink-test ( -- 
09e0: 29 0a 20 20 20 20 6e 65 74 6c 69 6e 6b 2d 73 6f  ).    netlink-so
09f0: 63 6b 20 30 3d 20 49 46 20 20 67 65 74 2d 6e 65  ck 0= IF  get-ne
0a00: 74 6c 69 6e 6b 20 20 54 48 45 4e 0a 20 20 20 20  tlink  THEN.    
0a10: 42 45 47 49 4e 20 20 6b 65 79 3f 20 30 3d 20 57  BEGIN  key? 0= W
0a20: 48 49 4c 45 0a 09 20 20 20 20 72 65 61 64 2d 6e  HILE..    read-n
0a30: 65 74 6c 69 6e 6b 0a 09 20 20 20 20 32 64 75 70  etlink..    2dup
0a40: 20 61 64 64 72 65 73 73 3f 20 49 46 20 2e 72 74   address? IF .rt
0a50: 61 64 64 72 20 32 30 20 6d 73 0a 09 09 67 6c 6f  addr 20 ms...glo
0a60: 62 61 6c 2d 69 70 34 20 2e 69 70 34 61 20 32 64  bal-ip4 .ip4a 2d
0a70: 72 6f 70 0a 09 09 67 6c 6f 62 61 6c 2d 69 70 36  rop...global-ip6
0a80: 20 2e 69 70 36 61 20 32 64 72 6f 70 20 63 72 0a   .ip6a 2drop cr.
0a90: 09 20 20 20 20 45 4c 53 45 20 32 64 72 6f 70 20  .    ELSE 2drop 
0aa0: 54 48 45 4e 0a 20 20 20 20 52 45 50 45 41 54 20  THEN.    REPEAT 
0ab0: 3b 0a 0a 5c 20 72 65 6e 61 74 20 68 61 6e 64 73  ;..\ renat hands
0ac0: 68 61 6c 65 0a 0a 30 20 56 61 6c 75 65 20 6e 65  hale..0 Value ne
0ad0: 74 6c 69 6e 6b 2d 74 61 73 6b 0a 56 61 72 69 61  tlink-task.Varia
0ae0: 62 6c 65 20 6e 65 74 6c 69 6e 6b 2d 64 6f 6e 65  ble netlink-done
0af0: 3f 20 20 20 6e 65 74 6c 69 6e 6b 2d 64 6f 6e 65  ?   netlink-done
0b00: 3f 20 6f 6e 0a 56 61 72 69 61 62 6c 65 20 6e 65  ? on.Variable ne
0b10: 74 6c 69 6e 6b 2d 61 67 61 69 6e 3f 20 20 6e 65  tlink-again?  ne
0b20: 74 6c 69 6e 6b 2d 61 67 61 69 6e 3f 20 6f 66 66  tlink-again? off
0b30: 0a 44 65 66 65 72 20 61 64 64 72 2d 63 68 61 6e  .Defer addr-chan
0b40: 67 65 64 20 27 20 6e 6f 6f 70 20 69 73 20 61 64  ged ' noop is ad
0b50: 64 72 2d 63 68 61 6e 67 65 64 0a 0a 65 76 65 6e  dr-changed..even
0b60: 74 3a 20 3a 3e 6e 65 74 6c 69 6e 6b 20 28 20 2d  t: :>netlink ( -
0b70: 2d 20 29 0a 20 20 20 20 6e 65 74 6c 69 6e 6b 2d  - ).    netlink-
0b80: 61 67 61 69 6e 3f 20 40 20 49 46 0a 09 20 6e 65  again? @ IF.. ne
0b90: 74 6c 69 6e 6b 2d 64 6f 6e 65 3f 20 6f 66 66 20  tlink-done? off 
0ba0: 6e 65 74 6c 69 6e 6b 2d 61 67 61 69 6e 3f 20 6f  netlink-again? o
0bb0: 66 66 20 23 30 2e 20 64 68 74 2d 62 65 61 63 6f  ff #0. dht-beaco
0bc0: 6e 0a 20 20 20 20 45 4c 53 45 20 20 6e 65 74 6c  n.    ELSE  netl
0bd0: 69 6e 6b 2d 64 6f 6e 65 3f 20 6f 6e 20 20 54 48  ink-done? on  TH
0be0: 45 4e 20 3b 0a 65 76 65 6e 74 3a 20 3a 3e 61 64  EN ;.event: :>ad
0bf0: 64 72 2d 63 68 61 6e 67 65 64 20 28 20 2d 2d 20  dr-changed ( -- 
0c00: 29 0a 20 20 20 20 61 64 64 72 2d 63 68 61 6e 67  ).    addr-chang
0c10: 65 64 20 3b 0a 3a 20 72 65 6e 61 74 2d 63 6f 6d  ed ;.: renat-com
0c20: 70 6c 65 74 65 20 28 20 2d 2d 20 29 0a 20 20 20  plete ( -- ).   
0c30: 20 3c 65 76 65 6e 74 20 3a 3e 6e 65 74 6c 69 6e   <event :>netlin
0c40: 6b 20 6e 65 74 6c 69 6e 6b 2d 74 61 73 6b 20 65  k netlink-task e
0c50: 76 65 6e 74 3e 20 3b 0a 0a 5c 20 6e 65 74 6c 69  vent> ;..\ netli
0c60: 6e 6b 20 77 61 74 63 68 64 6f 67 0a 0a 32 20 63  nk watchdog..2 c
0c70: 6f 6e 73 74 61 6e 74 20 6e 65 74 6c 69 6e 6b 2d  onstant netlink-
0c80: 77 61 69 74 23 0a 0a 3a 20 63 68 65 63 6b 2d 70  wait#..: check-p
0c90: 72 65 66 65 72 72 65 64 3f 20 28 20 2d 2d 20 66  referred? ( -- f
0ca0: 6c 61 67 20 29 0a 20 20 20 20 30 20 6d 79 2d 61  lag ).    0 my-a
0cb0: 64 64 72 5b 5d 20 24 5b 5d 20 40 20 3e 6f 0a 20  ddr[] $[] @ >o. 
0cc0: 20 20 20 67 6c 6f 62 61 6c 2d 69 70 36 20 32 64     global-ip6 2d
0cd0: 75 70 20 73 74 72 30 3f 20 7b 20 76 36 7a 20 7d  up str0? { v6z }
0ce0: 20 68 6f 73 74 3a 69 70 76 36 20 24 31 30 20 73   host:ipv6 $10 s
0cf0: 74 72 3d 20 3e 72 0a 20 20 20 20 67 6c 6f 62 61  tr= >r.    globa
0d00: 6c 2d 69 70 34 20 32 64 75 70 20 73 74 72 30 3f  l-ip4 2dup str0?
0d10: 20 7b 20 76 34 7a 20 7d 20 68 6f 73 74 3a 69 70   { v4z } host:ip
0d20: 76 34 20 20 20 34 20 73 74 72 3d 20 72 3e 20 61  v4   4 str= r> a
0d30: 6e 64 20 30 3d 0a 20 20 20 20 76 36 7a 20 76 34  nd 0=.    v6z v4
0d40: 7a 20 61 6e 64 20 30 3d 20 74 6f 20 63 6f 6e 6e  z and 0= to conn
0d50: 65 63 74 65 64 3f 0a 20 20 20 20 6f 3e 20 20 63  ected?.    o>  c
0d60: 6f 6e 6e 65 63 74 65 64 3f 20 61 6e 64 20 3b 0a  onnected? and ;.
0d70: 0a 3a 20 6e 65 77 2d 70 72 65 66 65 72 72 65 64  .: new-preferred
0d80: 3f 20 28 20 2d 2d 20 66 6c 61 67 20 29 0a 20 20  ? ( -- flag ).  
0d90: 20 20 6e 65 74 6c 69 6e 6b 2d 77 61 69 74 23 20    netlink-wait# 
0da0: 70 74 69 6d 65 6f 75 74 20 21 20 5c 20 33 73 20  ptimeout ! \ 3s 
0db0: 77 61 69 74 20 69 6e 20 74 6f 74 61 6c 0a 20 20  wait in total.  
0dc0: 20 20 42 45 47 49 4e 20 20 6e 65 74 6c 69 6e 6b    BEGIN  netlink
0dd0: 3f 20 57 48 49 4c 45 20 20 72 65 61 64 2d 6e 65  ? WHILE  read-ne
0de0: 74 6c 69 6e 6b 0a 09 6e 65 74 6c 69 6e 6b 28 20  tlink..netlink( 
0df0: 32 64 75 70 20 61 64 64 72 65 73 73 3f 20 49 46  2dup address? IF
0e00: 20 20 32 64 75 70 20 2e 72 74 61 64 64 72 20 54    2dup .rtaddr T
0e10: 48 45 4e 20 29 20 20 32 64 72 6f 70 0a 20 20 20  HEN )  2drop.   
0e20: 20 52 45 50 45 41 54 0a 20 20 20 20 63 68 65 63   REPEAT.    chec
0e30: 6b 2d 70 72 65 66 65 72 72 65 64 3f 20 3b 0a 3a  k-preferred? ;.:
0e40: 20 77 61 69 74 2d 66 6f 72 2d 61 64 64 72 65 73   wait-for-addres
0e50: 73 20 28 20 2d 2d 20 29 0a 20 20 20 20 42 45 47  s ( -- ).    BEG
0e60: 49 4e 20 20 72 65 61 64 2d 6e 65 74 6c 69 6e 6b  IN  read-netlink
0e70: 3f 0a 09 6e 65 74 6c 69 6e 6b 28 20 32 64 75 70  ?..netlink( 2dup
0e80: 20 61 64 64 72 65 73 73 3f 20 49 46 20 20 32 64   address? IF  2d
0e90: 75 70 20 2e 72 74 61 64 64 72 20 54 48 45 4e 20  up .rtaddr THEN 
0ea0: 29 0a 20 20 20 20 61 64 64 72 65 73 73 3f 20 63  ).    address? c
0eb0: 68 65 63 6b 2d 70 72 65 66 65 72 72 65 64 3f 20  heck-preferred? 
0ec0: 6f 72 20 20 55 4e 54 49 4c 20 3b 0a 3a 20 6e 65  or  UNTIL ;.: ne
0ed0: 74 6c 69 6e 6b 2d 6c 6f 6f 70 20 28 20 2d 2d 20  tlink-loop ( -- 
0ee0: 29 0a 20 20 20 20 6e 65 74 6c 69 6e 6b 2d 73 6f  ).    netlink-so
0ef0: 63 6b 20 30 3d 20 49 46 20 20 67 65 74 2d 6e 65  ck 0= IF  get-ne
0f00: 74 6c 69 6e 6b 20 20 54 48 45 4e 0a 20 20 20 20  tlink  THEN.    
0f10: 42 45 47 49 4e 0a 09 77 61 69 74 2d 66 6f 72 2d  BEGIN..wait-for-
0f20: 61 64 64 72 65 73 73 20 20 21 21 30 64 65 70 74  address  !!0dept
0f30: 68 21 21 0a 09 6e 65 77 2d 70 72 65 66 65 72 72  h!!..new-preferr
0f40: 65 64 3f 20 49 46 0a 09 20 20 20 20 6e 61 74 28  ed? IF..    nat(
0f50: 20 2e 22 20 6e 65 77 20 70 72 65 66 65 72 72 65   ." new preferre
0f60: 64 20 49 50 3a 20 22 20 29 0a 09 20 20 20 20 6e  d IP: " )..    n
0f70: 65 74 6c 69 6e 6b 2d 64 6f 6e 65 3f 20 40 20 49  etlink-done? @ I
0f80: 46 0a 09 09 6e 61 74 28 20 2e 22 20 64 68 74 2d  F...nat( ." dht-
0f90: 62 65 61 63 6f 6e 22 20 63 72 20 29 0a 09 09 6e  beacon" cr )...n
0fa0: 65 74 6c 69 6e 6b 2d 64 6f 6e 65 3f 20 6f 66 66  etlink-done? off
0fb0: 20 6e 65 74 6c 69 6e 6b 2d 61 67 61 69 6e 3f 20   netlink-again? 
0fc0: 6f 66 66 0a 09 09 62 65 61 63 6f 6e 73 2d 6e 6f  off...beacons-no
0fd0: 77 21 20 21 21 30 64 65 70 74 68 21 21 0a 09 20  w! !!0depth!!.. 
0fe0: 20 20 20 45 4c 53 45 0a 09 09 6e 61 74 28 20 2e     ELSE...nat( .
0ff0: 22 20 6e 65 74 6c 69 6e 6b 2d 61 67 61 69 6e 22  " netlink-again"
1000: 20 63 72 20 29 20 6e 65 74 6c 69 6e 6b 2d 61 67   cr ) netlink-ag
1010: 61 69 6e 3f 20 6f 6e 20 20 21 21 30 64 65 70 74  ain? on  !!0dept
1020: 68 21 21 0a 09 20 20 20 20 54 48 45 4e 0a 09 20  h!!..    THEN.. 
1030: 20 20 20 3c 65 76 65 6e 74 20 3a 3e 61 64 64 72     <event :>addr
1040: 2d 63 68 61 6e 67 65 64 20 5b 20 75 70 40 20 5d  -changed [ up@ ]
1050: 4c 20 65 76 65 6e 74 3e 0a 09 54 48 45 4e 0a 20  L event>..THEN. 
1060: 20 20 20 41 47 41 49 4e 20 3b 0a 3a 20 63 72 65     AGAIN ;.: cre
1070: 61 74 65 2d 6e 65 74 6c 69 6e 6b 2d 74 61 73 6b  ate-netlink-task
1080: 20 28 20 2d 2d 20 29 0a 20 20 20 20 5b 27 5d 20   ( -- ).    ['] 
1090: 6e 65 74 6c 69 6e 6b 2d 6c 6f 6f 70 20 31 20 6e  netlink-loop 1 n
10a0: 65 74 32 6f 2d 74 61 73 6b 20 74 6f 20 6e 65 74  et2o-task to net
10b0: 6c 69 6e 6b 2d 74 61 73 6b 20 3b 0a 0a 3a 6e 6f  link-task ;..:no
10c0: 6e 61 6d 65 20 64 65 66 65 72 73 20 69 6e 69 74  name defers init
10d0: 2d 72 65 73 74 20 63 72 65 61 74 65 2d 6e 65 74  -rest create-net
10e0: 6c 69 6e 6b 2d 74 61 73 6b 20 3b 20 69 73 20 69  link-task ; is i
10f0: 6e 69 74 2d 72 65 73 74 0a 0a 5c 5c 5c 0a 4c 6f  nit-rest..\\\.Lo
1100: 63 61 6c 20 56 61 72 69 61 62 6c 65 73 3a 0a 66  cal Variables:.f
1110: 6f 72 74 68 2d 6c 6f 63 61 6c 2d 77 6f 72 64 73  orth-local-words
1120: 3a 0a 20 20 20 20 28 0a 20 20 20 20 20 28 28 22  :.    (.     (("
1130: 6e 65 74 32 6f 3a 22 20 22 2b 6e 65 74 32 6f 3a  net2o:" "+net2o:
1140: 22 29 20 64 65 66 69 6e 69 74 69 6f 6e 2d 73 74  ") definition-st
1150: 61 72 74 65 72 20 28 66 6f 6e 74 2d 6c 6f 63 6b  arter (font-lock
1160: 2d 6b 65 79 77 6f 72 64 2d 66 61 63 65 20 2e 20  -keyword-face . 
1170: 31 29 0a 20 20 20 20 20 20 22 5b 20 5c 74 5c 6e  1).      "[ \t\n
1180: 5d 22 20 74 20 6e 61 6d 65 20 28 66 6f 6e 74 2d  ]" t name (font-
1190: 6c 6f 63 6b 2d 66 75 6e 63 74 69 6f 6e 2d 6e 61  lock-function-na
11a0: 6d 65 2d 66 61 63 65 20 2e 20 33 29 29 0a 20 20  me-face . 3)).  
11b0: 20 20 20 28 22 5b 61 2d 7a 30 2d 39 5d 2b 28 22     ("[a-z0-9]+("
11c0: 20 69 6d 6d 65 64 69 61 74 65 20 28 66 6f 6e 74   immediate (font
11d0: 2d 6c 6f 63 6b 2d 63 6f 6d 6d 65 6e 74 2d 66 61  -lock-comment-fa
11e0: 63 65 20 2e 20 31 29 0a 20 20 20 20 20 20 22 29  ce . 1).      ")
11f0: 22 20 6e 69 6c 20 63 6f 6d 6d 65 6e 74 20 28 66  " nil comment (f
1200: 6f 6e 74 2d 6c 6f 63 6b 2d 63 6f 6d 6d 65 6e 74  ont-lock-comment
1210: 2d 66 61 63 65 20 2e 20 31 29 29 0a 20 20 20 20  -face . 1)).    
1220: 29 0a 66 6f 72 74 68 2d 6c 6f 63 61 6c 2d 69 6e  ).forth-local-in
1230: 64 65 6e 74 2d 77 6f 72 64 73 3a 0a 20 20 20 20  dent-words:.    
1240: 28 0a 20 20 20 20 20 28 28 22 6e 65 74 32 6f 3a  (.     (("net2o:
1250: 22 20 22 2b 6e 65 74 32 6f 3a 22 29 20 28 30 20  " "+net2o:") (0 
1260: 2e 20 32 29 20 28 30 20 2e 20 32 29 20 6e 6f 6e  . 2) (0 . 2) non
1270: 2d 69 6d 6d 65 64 69 61 74 65 29 0a 20 20 20 20  -immediate).    
1280: 29 0a 45 6e 64 3a 0a 5b 54 48 45 4e 5d 0a        ).End:.[THEN].