Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

prototype scion-socket #247

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Launch Client",
"type": "go",
"request": "launch",
"mode": "auto",
"program": "${workspaceFolder}/_examples/helloworld_socket/helloworld_socket.go",
"args": ["-remote" ,"1-151,10.151.0.30:4444"]

},
{
"name": "Launch Server",
"type": "go",
"request": "launch",
"mode": "auto",
"program": "${workspaceFolder}/_examples/helloworld_socket/helloworld_socket.go",
"args": ["-listen" ,"10.150.0.30:4444"]

}
]
}
1 change: 1 addition & 0 deletions _examples/go.mod
Original file line number Diff line number Diff line change
@@ -44,6 +44,7 @@ require (
github.com/quic-go/qtls-go1-20 v0.3.3 // indirect
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect
github.com/scionproto/scion v0.9.1 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/uber/jaeger-client-go v2.30.0+incompatible // indirect
github.com/uber/jaeger-lib v2.0.0+incompatible // indirect
go.uber.org/atomic v1.9.0 // indirect
2 changes: 2 additions & 0 deletions _examples/go.sum
Original file line number Diff line number Diff line change
@@ -264,6 +264,8 @@ github.com/scionproto/scion v0.9.1/go.mod h1:5oZGBv6ZI6gK5MgYlZ70FaNUXihxYLrRfol
github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo=
github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE=
github.com/sirupsen/logrus v1.6.0/go.mod h1:7uNnSEd1DgxDLC74fIahvMZmmYsHGZGEOFrfsX/uA88=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.1.1 h1:2vfRuCMp5sSVIDSqO8oNnWJq7mPa6KVP3iPIwFBuy8A=
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
Binary file added _examples/helloworld_socket/helloworld_socket
Binary file not shown.
118 changes: 118 additions & 0 deletions _examples/helloworld_socket/helloworld_socket.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
// Copyright 2018 ETH Zurich
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package main

import (
"context"
"errors"
"flag"
"fmt"
"net/netip"
"os"
"time"

"github.com/netsec-ethz/scion-apps/pkg/pan"
)

func main() {
var err error
// get local and remote addresses from program arguments:
var listen pan.IPPortValue
flag.Var(&listen, "listen", "[Server] local IP:port to listen on")
remoteAddr := flag.String("remote", "", "[Client] Remote (i.e. the server's) SCION Address (e.g. 17-ffaa:1:1,[127.0.0.1]:12345)")
count := flag.Uint("count", 1, "[Client] Number of messages to send")
flag.Parse()

if (listen.Get().Port() > 0) == (len(*remoteAddr) > 0) {
check(fmt.Errorf("either specify -listen for server or -remote for client"))
}

if listen.Get().Port() > 0 {
err = runServer(listen.Get())
check(err)
} else {
err = runClient(*remoteAddr, int(*count))
check(err)
}
}

func runServer(listen netip.AddrPort) error {
sock, err := pan.NewScionSocket(context.Background(), listen)

if err != nil {
return err
}
defer sock.Close()
fmt.Println(sock.LocalAddr())

buffer := make([]byte, 16*1024)
for {
n, from, err := sock.ReadFrom(buffer)
if err != nil {
return err
}
data := buffer[:n]
fmt.Printf("Received %s: %s\n", from, data)
msg := fmt.Sprintf("take it back! %s", time.Now().Format("15:04:05.0"))
n, err = sock.WriteTo([]byte(msg), from)
if err != nil {
return err
}
fmt.Printf("Wrote %d bytes.\n", n)
}
}

func runClient(address string, count int) error {
addr, err := pan.ResolveUDPAddr(context.TODO(), address)
if err != nil {
return err
}

sock, err := pan.NewScionSocket(context.Background(), netip.AddrPort{})
if err != nil {
return err
}
defer sock.Close()

for i := 0; i < count; i++ {
nBytes, err := sock.WriteTo([]byte(fmt.Sprintf("hello world %s", time.Now().Format("15:04:05.0"))), addr)
if err != nil {
return err
}
fmt.Printf("Wrote %d bytes.\n", nBytes)

buffer := make([]byte, 16*1024)
/* if err = conn.SetReadDeadline(time.Now().Add(1 * time.Second)); err != nil {
return err
} */
n, _, err := sock.ReadFrom(buffer)
if errors.Is(err, os.ErrDeadlineExceeded) {
continue
} else if err != nil {
return err
}
data := buffer[:n]
fmt.Printf("Received reply: %s\n", data)
}
return nil
}

// Check just ensures the error is nil, or complains and quits
func check(e error) {
if e != nil {
fmt.Fprintln(os.Stderr, "Fatal error:", e)
os.Exit(1)
}
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -61,6 +61,7 @@ require (
github.com/quic-go/qtls-go1-20 v0.3.3 // indirect
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect
github.com/smartystreets/assertions v1.2.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/uber/jaeger-client-go v2.30.0+incompatible // indirect
github.com/uber/jaeger-lib v2.0.0+incompatible // indirect
go.uber.org/atomic v1.9.0 // indirect
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -284,6 +284,8 @@ github.com/smartystreets/assertions v1.2.0 h1:42S6lae5dvLc7BrLu/0ugRtcFVjoJNMC/N
github.com/smartystreets/assertions v1.2.0/go.mod h1:tcbTF8ujkAEcZ8TElKY+i30BzYlVhC/LOxJk7iOWnoo=
github.com/smartystreets/goconvey v1.7.2 h1:9RBaZCeXEQ3UselpuwUQHltGVXvdwm6cv1hgR6gDIPg=
github.com/smartystreets/goconvey v1.7.2/go.mod h1:Vw0tHAZW6lzCRk3xgdin6fKYcG+G3Pg9vgXWeJpQFMM=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.5.0 h1:1zr/of2m5FGMsad5YfcqgdqdWrIhu+EBEJRhR1U7z/c=
Loading