aboutsummaryrefslogtreecommitdiffstats
path: root/ctrl/facemgr/examples/run-bonjour.sh
blob: cc1c28e272fa7993d3250b8ec7e69052f1c79901 (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
177
178
#!/bin/bash

# Notes for MacOS:
#
#  - Browse all discoverable services
#    dns-sd -B _services._dns-sd._udp local.
#
#  - Browse all hICN services
#    dns-sd -B _hicn._udp local.
#
#  - Lookup for specific options
#    dns-sd -L "hicn node" _hicn._udp local.
#
#  - Lookup addresses
#    dns-sd -G v4v6 adreena.local.
#
# NOTE: trailing dot '.' is optional

set -e

PORT=9695

#-------------------------------------------------------------------------------

FN_AVAHI_CFG_SRC=$SCRIPT_PATH/etc_avahi_services_hicn.service
FN_AVAHI_CFG=/etc/avahi/services/hicn.service

# https://unix.stackexchange.com/questions/265149/why-is-set-o-errexit-breaking-this-read-heredoc-expression
! read -r -d '' TPL_AVAHI_CFG <<-EOF
<?xml version="1.0" standalone='no'?>
<!DOCTYPE service-group SYSTEM "avahi-service.dtd">
<service-group>
  <name>hicn node</name>
  <service>
    <type>_hicn._udp</type>
    <port>$PORT</port>
  </service>
</service-group>
EOF

#-------------------------------------------------------------------------------

# Reliably determine script's full path
SCRIPT_PATH="$( cd "$(dirname "$0")" ; pwd -P )"

# https://unix.stackexchange.com/questions/325594/script-a-test-for-installed-debian-package-error-handling
function pkg_is_installed()
{
    PKG="$1"
    LISTF=$(mktemp)
    dpkg-query -W -f '${Package} ${State}\n' >$LISTF
    grep "^${PKG} " $LISTF >/dev/null
    GREP_RC=$?
    rm $LISTF

    # for even moar strict error handling
    test $GREP_RC == 0 -o $GREP_RC == 1

    return $GREP_RC
}

# https://stackoverflow.com/questions/3466166/how-to-check-if-running-in-cygwin-mac-or-linux
function detect_os()
{
    unameOut="$(uname -s)"
    case "${unameOut}" in
        Linux*)     machine=linux;;
        Darwin*)    machine=mac;;
        CYGWIN*)    machine=cygwin;;
        MINGW*)     machine=mingw;;
        *)          machine=unknown;;
    esac
    echo ${machine}
}

function ensure_pkg_is_installed()
{
    PKG="$1"
    pkg_is_installed $PKG && return
    sudo apt install $PKG
}

function ensure_file_installed()
{
    SRC=$1
    DST=$2

    # Test whether destination exists and is up to date
    [ -s $DST ] && cmp -s $SRC $DST && return

    sudo cp $SRC $DST
}

function ensure_file_template()
{
    DST=$1
    TPL=$2

    echo "$TPL" | sudo tee $DST >/dev/null
}

function is_function()
{
    [ "$(type -t $1)" == "function" ]
}

function os_function()
{
    FUN=$1
    shift
    ARGS=$@

    OS=$(detect_os)
    if ! is_function ${FUN}_${OS}; then
        echo "Platform $OS not supported for $FUN [${FUN}_${OS}]"
        exit -1
    fi
    ${FUN}_${OS} $ARGS
}

#-------------------------------------------------------------------------------

# NOTE: debian only
function run_bonjour_server_linux()
{
    ensure_pkg_is_installed avahi-daemon
    #ensure_file_installed $FN_AVAHI_CFG_SRC $FN_AVAHI_CFG
    ensure_file_template  $FN_AVAHI_CFG "$TPL_AVAHI_CFG"
    sudo service avahi-daemon restart
    echo >&2, "Bonjour is now served through avahi"
}

function run_bonjour_server_mac()
{
    dns-sd -R hicn _hicn._tcp local $PORT
    # Proxy mode -P
}

function run_bonjour_client_linux()
{
    avahi-browse -ptr _hicn._udp
}

function run_bonjour_client_mac()
{
    dns-sd -B _hicn._udp local

}

# XXX function run_bonjour_proxy_linux() { }

function run_bonjour_proxy_mac()
{
    if [[ $# != 2 ]]; then
        echo "Usage: $0 proxy IP_ADDRESS"
        exit -1
    fi
    IP=$1
    dns-sd -P hicn _hicn._udp local $PORT hicn.local $IP path=/
}

#-------------------------------------------------------------------------------

case $1 in
    client)
        os_function run_bonjour_client
        ;;
    server)
        os_function run_bonjour_server
        ;;
    proxy)
        os_function run_bonjour_proxy $@
        ;;
    *)
        echo "$0 [client|server]"
        exit -1
        ;;
esac