summaryrefslogtreecommitdiffstats
path: root/ship/lib/retiolum
blob: 2a5c73384c8eb73665f17008d3cf27c155256091 (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
#!/bin/sh
# retiolum host functions
#@include core
tinc_path=${tinc_path:-/etc/tinc}
netname=${netname:-retiolum}
hosts_dir=${hosts_dir:-$tinc_path/$netname/hosts}

check_free_v4(){
  myipv4=${1-10.243.0.-1}
  v4num=${myipv4##*.}
  printf "Retard check: "
  if [ "$v4num" -gt 0 -a "$v4num" -lt "256" ];
  then 
    info "No retard detected\n"
    cd $hosts_dir
    info "Check if ip is still free: "
    for i in `ls -1`; do
      if grep -q -e $myipv4\$ $i ;then
        warn "Host IP already taken by $i! "
        return 1
      fi
    done
    info "Passed\n"
    return 0
  else
    error "you are made of stupid. bailing out\n" 
    return 1
  fi
  cd - >/dev/null
}

find_active_nodes(){
  # TODO this function currently only supports a single address for a host
  cd $hosts_dir
  # posix grep does not support [[:space:]]
  set +f
  for name in `
    grep '^[  ]*Address[  ]*=' * |
      cut -d: -f1 | sort | uniq
  `; do
    if eval "`sed -n '
        s/[   ]\+//g
        s/^\(Address\|Port\)=\(.*\)/\1="\${\1+\$\1\n}\2"/p
      ' $name`"; then
      port=${Port-655}
      for host in $Address; do
        if nc -zw 2 $host $port 2>/dev/null; then
          echo "$name [('$host', $port)]"
        fi &
      done
      wait
    fi &
  done
  wait
  cd - >/dev/null
}
find_supernodes(){
  cd $hosts_dir
  set +f
  for name in `
    grep '^[  ]*Address[  ]*=' * |
      cut -d: -f1 | sort | uniq
  `; do
    if eval "`sed -n '
        s/[   ]\+//g
        s/^\(Address\|Port\)=\(.*\)/\1="\${\1+\$\1\n}\2"/p
      ' $name`"; then
      port=${Port-655}
      for host in $Address; do
        if nc -zw 2 $host $port 2>/dev/null; then
          echo "$name [('$host', $port)]"
        fi &
      done
      wait
    fi &
  done
  wait
  cd - >/dev/null
}