Skip to content

Commit

Permalink
Unified error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Kioubit committed Feb 27, 2023
1 parent 4b240e1 commit 8cafc52
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 24 deletions.
12 changes: 6 additions & 6 deletions pndp/flow.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func NewResponder(iface string, filter []*net.IPNet, autosenseInterface string)
fmt.Println("WARNING: You should use a whitelist for the responder unless you really know what you are doing")
}
if !isValidNetworkInterface(iface, autosenseInterface) {
showError("No such network interface")
showFatalError("No such network interface")
}

var s sync.WaitGroup
Expand Down Expand Up @@ -79,7 +79,7 @@ func (obj *ResponderObj) start() {
stopInterfaceMon()
}

//Stop a running Responder instance
// Stop a running Responder instance
// Returns false on error
func (obj *ResponderObj) Stop() bool {
close(obj.stopChan)
Expand All @@ -103,7 +103,7 @@ func (obj *ResponderObj) Stop() bool {
func NewProxy(iface1 string, iface2 string, filter []*net.IPNet, autosenseInterface string) *ProxyObj {

if !isValidNetworkInterface(iface1, iface2, autosenseInterface) {
showError("No such network interface")
showFatalError("No such network interface")
}

var s sync.WaitGroup
Expand Down Expand Up @@ -166,7 +166,7 @@ func (obj *ProxyObj) start() {
stopInterfaceMon()
}

//Stop a running Proxy instance
// Stop a running Proxy instance
// Returns false on error
func (obj *ProxyObj) Stop() bool {
close(obj.stopChan)
Expand All @@ -190,7 +190,7 @@ func ParseFilter(f string) []*net.IPNet {
for i, n := range s {
_, cidr, err := net.ParseCIDR(n)
if err != nil {
showError("filter:", err.Error())
showFatalError("filter:", err.Error())
}
result[i] = cidr
}
Expand Down Expand Up @@ -224,7 +224,7 @@ func isValidNetworkInterface(iface ...string) bool {
return true
}

func showError(error ...string) {
func showFatalError(error ...string) {
fmt.Printf("Error: ")
for _, err := range error {
fmt.Printf(err + " ")
Expand Down
10 changes: 5 additions & 5 deletions pndp/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,14 @@ func setPromisc(fd int, iface string, enable bool, withInterfaceFlags bool) {
if withInterfaceFlags {
tFD, err := syscall.Socket(syscall.AF_INET6, syscall.SOCK_DGRAM, 0)
if err != nil {
panic(err)
showFatalError(err.Error())
}

var ifl iflags
copy(ifl.name[:], []byte(iface))
_, _, ep := syscall.Syscall(syscall.SYS_IOCTL, uintptr(tFD), syscall.SIOCGIFFLAGS, uintptr(unsafe.Pointer(&ifl)))
if ep != 0 {
panic(ep)
showFatalError(ep.Error())
}

if enable {
Expand All @@ -63,7 +63,7 @@ func setPromisc(fd int, iface string, enable bool, withInterfaceFlags bool) {

_, _, ep = syscall.Syscall(syscall.SYS_IOCTL, uintptr(tFD), syscall.SIOCSIFFLAGS, uintptr(unsafe.Pointer(&ifl)))
if ep != 0 {
panic(ep)
showFatalError(ep.Error())
}

_ = syscall.Close(tFD)
Expand All @@ -73,7 +73,7 @@ func setPromisc(fd int, iface string, enable bool, withInterfaceFlags bool) {
// -------------------------- Socket Options ---------------------------
iFace, err := net.InterfaceByName(iface)
if err != nil {
panic(err.Error())
showFatalError(err.Error())
}

mReq := unix.PacketMreq{
Expand All @@ -90,7 +90,7 @@ func setPromisc(fd int, iface string, enable bool, withInterfaceFlags bool) {

err = unix.SetsockoptPacketMreq(fd, unix.SOL_PACKET, opt, &mReq)
if err != nil {
panic(err)
showFatalError(err.Error())
}
// ---------------------------------------------------------------------
}
Expand Down
8 changes: 4 additions & 4 deletions pndp/interfaceMon.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func startInterfaceMon() {
s = make(chan interface{})
err := getInterfaceUpdates(u, s)
if err != nil {
panic(err.Error())
showFatalError(err.Error())
}
go getUpdates()
}
Expand Down Expand Up @@ -79,7 +79,7 @@ func getUpdates() {

type monInterface struct {
addCount int
sourceIP []byte //TODO ULA and GUA
sourceIP []byte //TODO ULA
networks []*net.IPNet
iface *net.Interface
autosense bool
Expand All @@ -99,7 +99,7 @@ func addInterfaceToMon(iface string, autosense bool) {

niface, err := net.InterfaceByName(iface)
if err != nil {
panic(err.Error())
showFatalError(err.Error())
}

for i := range monInterfaceList {
Expand Down Expand Up @@ -131,7 +131,7 @@ func removeInterfaceFromMon(iface string) {
defer monMutex.Unlock()
niface, err := net.InterfaceByName(iface)
if err != nil {
panic(err.Error())
showFatalError(err.Error())
}
for i := range monInterfaceList {
if monInterfaceList[i].iface.Name == niface.Name {
Expand Down
10 changes: 5 additions & 5 deletions pndp/listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func listen(iface string, responder chan *ndpRequest, requestType ndpType, stopW

niface, err := net.InterfaceByName(iface)
if err != nil {
panic(err.Error())
showFatalError(err.Error())
}
tiface := &syscall.SockaddrLinklayer{
Protocol: htons16(syscall.ETH_P_IPV6),
Expand All @@ -43,12 +43,12 @@ func listen(iface string, responder chan *ndpRequest, requestType ndpType, stopW
}

if len([]byte(iface)) > syscall.IFNAMSIZ {
panic("Interface size larger then maximum allowed by the kernel")
showFatalError("Interface size larger then maximum allowed by the kernel")
}

err = syscall.Bind(fd, tiface)
if err != nil {
panic(err.Error())
showFatalError(err.Error())
}

setPromisc(fd, iface, true, false)
Expand Down Expand Up @@ -83,14 +83,14 @@ func listen(iface string, responder chan *ndpRequest, requestType ndpType, stopW

err = f.ApplyTo(fd)
if err != nil {
panic(err.Error())
showFatalError(err.Error())
}

for {
buf := make([]byte, 86)
numRead, err := syscall.Read(fd, buf)
if err != nil {
panic(err)
showFatalError(err.Error())
}
if numRead < 78 {
if GlobalDebug {
Expand Down
8 changes: 4 additions & 4 deletions pndp/responder.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func respond(iface string, requests chan *ndpRequest, respondType ndpType, ndpQu
var err error
autoiface, err = net.InterfaceByName(autoSense)
if err != nil {
panic(err)
showFatalError(err.Error())
}
}

Expand All @@ -26,19 +26,19 @@ func respond(iface string, requests chan *ndpRequest, respondType ndpType, ndpQu

fd, err := syscall.Socket(syscall.AF_INET6, syscall.SOCK_RAW, syscall.IPPROTO_RAW)
if err != nil {
panic(err)
showFatalError(err.Error())
}
defer func(fd int) {
_ = syscall.Close(fd)
}(fd)
err = syscall.BindToDevice(fd, iface)
if err != nil {
panic(err)
showFatalError(err.Error())
}

respondIface, err := net.InterfaceByName(iface)
if err != nil {
panic(err.Error())
showFatalError(err.Error())
}

//var result = selectSourceIP(respondIface)
Expand Down

0 comments on commit 8cafc52

Please sign in to comment.