blob: 2f46f8238ed09361ff45aebc42e7762aaf46d9b4 [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
Patrick Williams59486672022-12-08 06:23:47 -060018function UpdateNeighbor() {
19 local netdev="$1"
20 local service="$2"
21 local object="$3"
William A. Kennington III7d6fa422021-02-08 17:04:02 -080022
Patrick Williams59486672022-12-08 06:23:47 -060023 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
William A. Kennington III7d6fa422021-02-08 17:04:02 -080040}
41
Patrick Williams59486672022-12-08 06:23:47 -060042function UpdateNeighbors() {
43 local netdev="$1"
William A. Kennington III7d6fa422021-02-08 17:04:02 -080044
Patrick Williams59486672022-12-08 06:23:47 -060045 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
William A. Kennington III7d6fa422021-02-08 17:04:02 -080051}
52
Patrick Williams59486672022-12-08 06:23:47 -060053function Main() {
54 set -o nounset
55 set -o errexit
56 set -o pipefail
William A. Kennington III7d6fa422021-02-08 17:04:02 -080057
Patrick Williams59486672022-12-08 06:23:47 -060058 InitTerm
59 UpdateNeighbors "$@"
William A. Kennington III7d6fa422021-02-08 17:04:02 -080060}
61
62return 0 2>/dev/null
63Main "$@"