blob: 8a6c72f486d170c8a04ec23b292cf049a945a56c [file] [log] [blame]
William A. Kennington III7d6fa422021-02-08 17:04:02 -08001#!/bin/bash
Brandon Kimdab96f12021-02-18 11:21:37 -08002# Copyright 2021 Google LLC
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15
William A. Kennington III7d6fa422021-02-08 17:04:02 -080016source "$(dirname "${BASH_SOURCE[0]}")"/ncsid_lib.sh
17
18UpdateNeighbor() {
19 local netdev="$1"
20 local service="$2"
21 local object="$3"
22
23 eval "$(GetNeighbor "$service" "$object" | JSONToVars)" || return $?
24 local new_mac
25 if ! new_mac="$(DetermineNeighbor "$netdev" "$IPAddress")"; then
26 echo "Faild to find $IPAddress" >&2
27 return 10
28 fi
29 new_mac=$(normalize_mac "$new_mac") || return
30 if [ "$new_mac" != "$(normalize_mac "$MACAddress")" ]; then
31 echo "Updating $IPAddress: $MACAddress -> $new_mac" >&2
32 SuppressTerm
33 local rc=0
34 DeleteObject "$service" "$object" && \
35 AddNeighbor "$service" "$netdev" "$IPAddress" "$new_mac" || \
36 rc=$?
37 UnsuppressTerm
38 return $rc
39 fi
40}
41
42UpdateNeighbors() {
43 local netdev="$1"
44
45 local entry
46 while read entry; do
47 eval "$(echo "$entry" | JSONToVars)"
48 RunInterruptibleBg UpdateNeighbor "$netdev" "$service" "$object"
49 done < <(GetNeighborObjects "$netdev" 2>/dev/null)
50 WaitInterruptibleBg
51}
52
53Main() {
54 set -o nounset
55 set -o errexit
56 set -o pipefail
57
58 InitTerm
59 UpdateNeighbors "$@"
60}
61
62return 0 2>/dev/null
63Main "$@"