Skip to content

Commit cef9074

Browse files
committed
added comments, handles startCapture error + better error handling
1 parent 56b24eb commit cef9074

File tree

1 file changed

+31
-31
lines changed

1 file changed

+31
-31
lines changed

ios/pcap/ipfinder.go

+31-31
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package pcap
22

33
import (
4-
// "fmt"
4+
"fmt"
55
"time"
66

77
"github.com/danielpaulus/go-ios/ios"
@@ -26,24 +26,22 @@ func (n PacketInfo) complete() bool {
2626
return (n.IPv6 != "" && n.Mac != "") || (n.IPv4 != "" && n.Mac != "")
2727
}
2828

29-
// FindIp reads pcap packets until one is found that matches the given MAC
29+
// FindIPByMac reads pcap packets until one is found that matches the given MAC
3030
// and contains an IP address. This won't work if the iOS device "automatic Wifi address" privacy
3131
// feature is enabled. The MAC needs to be static.
3232
func FindIPByMac(device ios.DeviceEntry) (NetworkInfo, error) {
3333
mac, err := ios.GetWifiMac(device)
34-
_ = mac
3534
if err != nil {
36-
return NetworkInfo{}, err
35+
return NetworkInfo{}, fmt.Errorf("FindIPMyMac: unable to get WiFi MAC Address: %w", err)
3736
}
3837

3938
// channel to receive PacketInfo
4039
c := make(chan PacketInfo)
4140
done := make(chan bool)
41+
captureError := make(chan error)
4242

4343
go func() {
44-
if err := startCapture(device, c, done); err != nil {
45-
log.Error(err)
46-
}
44+
captureError <- startCapture(device, c, done)
4745
}()
4846

4947
// captureDuration := time.Duration(10 * time.Second)
@@ -55,27 +53,27 @@ func FindIPByMac(device ios.DeviceEntry) (NetworkInfo, error) {
5553
}
5654
}
5755

58-
return NetworkInfo{}, nil
56+
captureErrorResult := <-captureError
57+
if captureErrorResult != nil {
58+
return NetworkInfo{}, fmt.Errorf("FindIPByMac: failed to startCapture got err: %w", captureErrorResult)
59+
}
60+
61+
return NetworkInfo{}, fmt.Errorf("failed to get any IP matching the MAC: %s", mac)
5962
}
6063

61-
// FindIp reads pcap packets until one is found that matches the given MAC
62-
// and contains an IP address. This won't work if the iOS device "automatic Wifi address" privacy
63-
// feature is enabled. The MAC needs to be static.
64+
// FindIPByLazy reads pcap packets for a specified duration, whereafter it will
65+
// try to find the IP address that occurs the most times, and assume that is the IP of the device.
66+
// This is of course based on, that the device contacts mulitple IPs, and that there is some traffic.
67+
// If the device only contains a single IP, then it would be 50/50 which IP will be returned.
68+
// This is best effort! It's important to generate some traffic, when this function runs to get better results.
6469
func FindIPByLazy(device ios.DeviceEntry, capture_duration time.Duration) (NetworkInfo, error) {
65-
mac, err := ios.GetWifiMac(device)
66-
_ = mac
67-
if err != nil {
68-
return NetworkInfo{}, err
69-
}
70-
7170
// channel to receive PacketInfo
7271
c := make(chan PacketInfo)
7372
done := make(chan bool)
73+
captureError := make(chan error)
7474

7575
go func() {
76-
if err := startCapture(device, c, done); err != nil {
77-
log.Error(err)
78-
}
76+
captureError <- startCapture(device, c, done)
7977
}()
8078

8179
// captureDuration := time.Duration(10 * time.Second)
@@ -96,6 +94,11 @@ func FindIPByLazy(device ios.DeviceEntry, capture_duration time.Duration) (Netwo
9694
}
9795
}
9896

97+
captureErrorResult := <-captureError
98+
if captureErrorResult != nil {
99+
return NetworkInfo{}, fmt.Errorf("FindIPByLazy: failed to startCapture got err: %w", captureErrorResult)
100+
}
101+
99102
highestIPv4Hits, highestIPv4Addr := 0, ""
100103
for ipv4, hits := range ipv4Hits {
101104
if hits > highestIPv4Hits {
@@ -118,8 +121,9 @@ func FindIPByLazy(device ios.DeviceEntry, capture_duration time.Duration) (Netwo
118121
func startCapture(device ios.DeviceEntry, c chan PacketInfo, done chan bool) error {
119122
intf, err := ios.ConnectToService(device, "com.apple.pcapd")
120123
if err != nil {
121-
return err
124+
return fmt.Errorf("startCapture: failed connecting to com.apple.pcapd with err: %w", err)
122125
}
126+
123127
plistCodec := ios.NewPlistCodec()
124128
for {
125129
// if the channel is signalling to stop
@@ -131,22 +135,18 @@ func startCapture(device ios.DeviceEntry, c chan PacketInfo, done chan bool) err
131135

132136
b, err := plistCodec.Decode(intf.Reader())
133137
if err != nil {
134-
return err
138+
return fmt.Errorf("startCapture: failed decoding plistCodec err: %w", err)
135139
}
136140
decodedBytes, err := fromBytes(b)
137141
if err != nil {
138-
return err
142+
return fmt.Errorf("startCapture: failed decoding fromBytes err: %w", err)
139143
}
140144
_, packet, err := getPacket(decodedBytes)
141145
if err != nil {
142-
return err
146+
return fmt.Errorf("startCapture: failed getPacket err: %w", err)
143147
}
144148
if len(packet) > 0 {
145-
pInfo, err := parsePacket(packet)
146-
if err != nil {
147-
return err
148-
}
149-
149+
pInfo := parsePacket(packet)
150150
if !pInfo.complete() {
151151
continue
152152
}
@@ -158,7 +158,7 @@ func startCapture(device ios.DeviceEntry, c chan PacketInfo, done chan bool) err
158158
return nil
159159
}
160160

161-
func parsePacket(p []byte) (PacketInfo, error) {
161+
func parsePacket(p []byte) PacketInfo {
162162
packet := gopacket.NewPacket(p, layers.LayerTypeEthernet, gopacket.Default)
163163
res := PacketInfo{}
164164

@@ -183,5 +183,5 @@ func parsePacket(p []byte) (PacketInfo, error) {
183183
}
184184
}
185185

186-
return res, nil
186+
return res
187187
}

0 commit comments

Comments
 (0)