William A. Kennington III | 7d6fa42 | 2021-02-08 17:04:02 -0800 | [diff] [blame] | 1 | #!/bin/bash |
Brandon Kim | dab96f1 | 2021-02-18 11:21:37 -0800 | [diff] [blame] | 2 | # 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 III | 7d6fa42 | 2021-02-08 17:04:02 -0800 | [diff] [blame] | 16 | source "$(dirname "${BASH_SOURCE[0]}")"/ncsid_lib.sh |
| 17 | |
Patrick Williams | 5948667 | 2022-12-08 06:23:47 -0600 | [diff] [blame] | 18 | function UpdateNeighbor() { |
| 19 | local netdev="$1" |
| 20 | local service="$2" |
| 21 | local object="$3" |
William A. Kennington III | 7d6fa42 | 2021-02-08 17:04:02 -0800 | [diff] [blame] | 22 | |
Patrick Williams | 5948667 | 2022-12-08 06:23:47 -0600 | [diff] [blame] | 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 |
William A. Kennington III | 7d6fa42 | 2021-02-08 17:04:02 -0800 | [diff] [blame] | 40 | } |
| 41 | |
Patrick Williams | 5948667 | 2022-12-08 06:23:47 -0600 | [diff] [blame] | 42 | function UpdateNeighbors() { |
| 43 | local netdev="$1" |
William A. Kennington III | 7d6fa42 | 2021-02-08 17:04:02 -0800 | [diff] [blame] | 44 | |
Patrick Williams | 5948667 | 2022-12-08 06:23:47 -0600 | [diff] [blame] | 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 |
William A. Kennington III | 7d6fa42 | 2021-02-08 17:04:02 -0800 | [diff] [blame] | 51 | } |
| 52 | |
Patrick Williams | 5948667 | 2022-12-08 06:23:47 -0600 | [diff] [blame] | 53 | function Main() { |
| 54 | set -o nounset |
| 55 | set -o errexit |
| 56 | set -o pipefail |
William A. Kennington III | 7d6fa42 | 2021-02-08 17:04:02 -0800 | [diff] [blame] | 57 | |
Patrick Williams | 5948667 | 2022-12-08 06:23:47 -0600 | [diff] [blame] | 58 | InitTerm |
| 59 | UpdateNeighbors "$@" |
William A. Kennington III | 7d6fa42 | 2021-02-08 17:04:02 -0800 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | return 0 2>/dev/null |
| 63 | Main "$@" |