Skip to content

Commit

Permalink
feat: add support for IPv6
Browse files Browse the repository at this point in the history
  • Loading branch information
coderbyheart committed Mar 1, 2024
1 parent e4c6029 commit 22a281b
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 17 deletions.
34 changes: 26 additions & 8 deletions .github/workflows/live-test.yaml
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
name: Live test

on:
workflow_dispatch:
schedule:
- cron: "0 0 * * *"
push:
paths:
- ".github/workflows/live-test.yaml"
workflow_dispatch:
schedule:
- cron: "0 0 * * *"
push:
paths:
- ".github/workflows/live-test.yaml"

jobs:
test:
test-ipv4:
runs-on: ubuntu-latest

steps:
Expand All @@ -22,4 +22,22 @@ jobs:

- name: Run test client
working-directory: client
run: go run client.go -address coap.nordicsemi.academy
run: go run client.go -address coap.nordicsemi.academy

test-ipv6:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4

- name: Setup Go
uses: actions/setup-go@v4
with:
go-version: 1.21.6

- name: Set up WARP
uses: fscarmen/[email protected]

- name: Run test client
working-directory: client
run: go run client.go -address coap.nordicsemi.academy -udp6
1 change: 0 additions & 1 deletion .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,3 @@ jobs:
run: |
docker tag academy-coap ${{vars.REGISTRY_LOGIN_SERVER}}/coap |
docker push ${{vars.REGISTRY_LOGIN_SERVER}}/coap
25 changes: 21 additions & 4 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"flag"
"fmt"
"log"
"net"
"time"

"github.com/plgd-dev/go-coap/v3/udp"
Expand All @@ -25,15 +26,31 @@ func check(e error) {

func main() {
address := flag.String("address", "localhost",
"The UDP Server listen address, e.g. `localhost` or `0.0.0.0`.")
"The UDP Server listen address, e.g. `localhost`.")
password := flag.String("password", "connect:anything",
"The password to use for the PSK in dTLS.")
udp6 := flag.Bool("udp6", false, "Whether to use IPv6")
flag.Parse()

ctx, cancel := context.WithTimeout(context.Background(), time.Duration(10)*time.Second)
defer cancel()

udpAddr := fmt.Sprintf("%s:%d", *address, 5688)
var ip []net.IP
if *udp6 {
ip6, err := net.DefaultResolver.LookupIP(context.Background(), "ip6", *address)
if err != nil {
log.Fatal("Failed to resolve IPv6 address: ", err)
}
ip = ip6
} else {
ip4, err := net.DefaultResolver.LookupIP(context.Background(), "ip4", *address)
if err != nil {
log.Fatal("Failed to resolve IPv4 address: ", err)
}
ip = ip4
}

udpAddr := fmt.Sprintf("%s:%d", ip[0].String(), 5688)
log.Printf("UDP Server listening on: %s\n", udpAddr)
co, err := udp.Dial(udpAddr)
check(err)
Expand All @@ -44,7 +61,7 @@ func main() {
readId1, err := internal.TestGetCustom(co, ctx, fmt.Sprintf("/%s", id1))
check(err)
if string(readId1) != string(id1) {
log.Fatal(fmt.Sprintf("Read value %s is not equal written value %s.", id1, readId1))
log.Fatalf("Read value %s is not equal written value %s.", id1, readId1)
}

dtlsAddr := fmt.Sprintf("%s:%d", *address, 5689)
Expand All @@ -66,6 +83,6 @@ func main() {
readId2, err := internal.TestGetCustom(codTLS, ctx, fmt.Sprintf("/%s", id2))
check(err)
if string(readId2) != string(id2) {
log.Fatal(fmt.Sprintf("Read value %s is not equal written value %s.", id2, readId2))
log.Fatalf("Read value %s is not equal written value %s.", id2, readId2)
}
}
14 changes: 10 additions & 4 deletions server/start.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,15 @@ if [ -f $FILE ]; then
export STORAGE_CONNECTION_STRING=$(cat /run/secrets/STORAGE_CONNECTION_STRING)
fi

# we need to figure out the IP of the public interface here
IP=`ip addr show eth0 | grep "inet\b" | awk '{print $2}' | cut -d/ -f1`
# Enumerate all network interfaces and get their assigned IP addresses
interfaces=$(ip -o addr show | awk '{print $2}' | cut -d':' -f1)

for interface in $interfaces; do
IP=$(ip -o addr show $interface | awk '{print $4}' | cut -d'/' -f1)
echo "Interface: $interface, Assigned IP: $IP"

nohup /home/coap/coap-server -address "${IP}" -password "connect:anything" -dTLS &
nohup /home/coap/coap-server -address "${IP}" &
done

nohup /home/coap/coap-server -address "${IP}" -password "connect:anything" -dTLS &
nohup /home/coap/coap-server -address "${IP}" &
sleep infinity

0 comments on commit 22a281b

Please sign in to comment.