Skip to content

Commit

Permalink
Haiku: Detect the DLT instead of hard-coding it. [skip ci]
Browse files Browse the repository at this point in the history
In pcap_activate_haiku() retrieve Haiku interface type for the device
and either use a correct DLT value for a supported type or reject the
interface.  Comment on what currently works and what does not.  This
will likely require follow-up changes after more testing, but on a WIP
development snapshot of Haiku this works correctly, including packet
capture and decoding.

> ./tcpdump -D
1.tap/0 [Up, Connection status unknown]
2.tun/0 [Up, Connection status unknown]
3./dev/net/ipro1000/0 [Up, Connection status unknown]
4.loop [Up, Loopback]

> ./tcpdump -i 1 -L
Data link types for tap/0 (use option -y to set):
  EN10MB (Ethernet)

> ./tcpdump -i 2 -L
Data link types for tun/0 (use option -y to set):
  RAW (Raw IP)

> ./tcpdump -i 3 -L
Data link types for /dev/net/ipro1000/0 (use option -y to set):
  EN10MB (Ethernet)

> ./tcpdump -i 4 -L
Data link types for loop (use option -y to set):
  RAW (Raw IP)
  • Loading branch information
infrastation committed Feb 17, 2024
1 parent 5927086 commit 3a8efb6
Showing 1 changed file with 32 additions and 2 deletions.
34 changes: 32 additions & 2 deletions pcap-haiku.c
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,8 @@ pcap_activate_haiku(pcap_t *handle)
goto error;

// pcap_stats_haiku() will need a baseline for ps_ifdrop.
// At the time of this writing SIOCGIFSTATS returns EINVAL for AF_LINK
// sockets.
if (ioctl_ifreq(handle, handlep->aux_socket, SIOCGIFSTATS, "SIOCGIFSTATS") < 0) {
// Detect a non-existent network interface at least at the
// first ioctl() use.
Expand All @@ -202,6 +204,36 @@ pcap_activate_haiku(pcap_t *handle)
if ((handle->fd = dgram_socket(handle, AF_LINK)) < 0)
goto error;

// Derive a DLT from the interface type.
// At the time of this writing SIOCGIFTYPE cannot be used for this
// purpose: it returns EINVAL for AF_LINK sockets and sets ifr_type to
// 0 for AF_INET sockets. Use the same method as Haiku ifconfig does
// (SIOCGIFADDR and AF_LINK).
if (ioctl_ifreq(handle, handle->fd, SIOCGIFADDR, "SIOCGIFADDR") < 0)
goto error;
struct sockaddr_dl *sdl = (struct sockaddr_dl *)&handlep->ifreq.ifr_addr;
if (sdl->sdl_family != AF_LINK) {
snprintf(handle->errbuf, PCAP_ERRBUF_SIZE,
"Got AF %d instead of AF_LINK for interface \"%s\".",
sdl->sdl_family, handle->opt.device);
goto error;
}
switch (sdl->sdl_type) {
case IFT_ETHER:
// This includes tap (L2) mode tunnels too.
handle->linktype = DLT_EN10MB;
break;
case IFT_LOOP:
case IFT_TUNNEL: // This means tun (L3) mode tunnels only.

This comment has been minimized.

Copy link
@korli

korli Feb 17, 2024

Contributor

@infrastation IFT_TUNNEL isn't defined on r1beta4. For compatibility:

#ifndef IFT_TUNNEL
#define IFT_TUNNEL IFT_TUN
#endif

This comment has been minimized.

Copy link
@infrastation

infrastation Feb 17, 2024

Author Member

The next commit addresses this problem slightly differently.

This comment has been minimized.

Copy link
@korli

korli Feb 17, 2024

Contributor

Package builders run on r1beta4, the same packages are used on nightlies, until builders are upgraded to the next release.

This comment has been minimized.

Copy link
@infrastation

infrastation Feb 17, 2024

Author Member

Thank you for explaining, let me see how to accommodate this.

This comment has been minimized.

Copy link
@infrastation

infrastation Feb 18, 2024

Author Member

My working copy source compiles on both R1/Beta4 and a development snapshot, both binaries work according to the runtime OS now matter where compiled. This required some additional changes, I am going to clean it up and commit tomorrow.

This comment has been minimized.

Copy link
@infrastation

infrastation Feb 18, 2024

Author Member

This is pull request #1278 now.

This comment has been minimized.

Copy link
@korli

korli Feb 18, 2024

Contributor

Is it expected to compile on x86_gcc2? there are a few errors, see https://github.com/korli/libpcap/actions/runs/7949333668/job/21700354445

gcc -D_BSD_SOURCE  -fpic -I.  -DBUILDING_PCAP -Dpcap_EXPORTS -DHAVE_CONFIG_H  -D_BSD_SOURCE -D_BSD_SOURCE    -c ./pcap-haiku.c
/home/runner/work/libpcap/libpcap/pcap-haiku.c: In function `pcap_read_haiku':
/home/runner/work/libpcap/libpcap/pcap-haiku.c:55: parse error before `fromLength'
/home/runner/work/libpcap/libpcap/pcap-haiku.c:57: `fromLength' undeclared (first use in this function)
/home/runner/work/libpcap/libpcap/pcap-haiku.c:57: (Each undeclared identifier is reported only once
/home/runner/work/libpcap/libpcap/pcap-haiku.c:57: for each function it appears in.)
/home/runner/work/libpcap/libpcap/pcap-haiku.c:60: parse error before `ts'
/home/runner/work/libpcap/libpcap/pcap-haiku.c:73: parse error before `struct'
/home/runner/work/libpcap/libpcap/pcap-haiku.c:74: `handlep' undeclared (first use in this function)
/home/runner/work/libpcap/libpcap/pcap-haiku.c:75: parse error before `captureLength'
/home/runner/work/libpcap/libpcap/pcap-haiku.c:76: `captureLength' undeclared (first use in this function)
/home/runner/work/libpcap/libpcap/pcap-haiku.c:90: parse error before `struct'
/home/runner/work/libpcap/libpcap/pcap-haiku.c:91: `header' undeclared (first use in this function)
/home/runner/work/libpcap/libpcap/pcap-haiku.c:93: `ts' undeclared (first use in this function)
/home/runner/work/libpcap/libpcap/pcap-haiku.c: In function `pcap_cleanup_haiku':
/home/runner/work/libpcap/libpcap/pcap-haiku.c:142: parse error before `struct'
/home/runner/work/libpcap/libpcap/pcap-haiku.c:143: `handlep' undeclared (first use in this function)
/home/runner/work/libpcap/libpcap/pcap-haiku.c: In function `pcap_activate_haiku':
/home/runner/work/libpcap/libpcap/pcap-haiku.c:214: parse error before `struct'
/home/runner/work/libpcap/libpcap/pcap-haiku.c:215: `sdl' undeclared (first use in this function)
/home/runner/work/libpcap/libpcap/pcap-haiku.c:224: warning: unreachable code at beginning of switch statement
/home/runner/work/libpcap/libpcap/pcap-haiku.c: In function `pcapint_create_interface':
/home/runner/work/libpcap/libpcap/pcap-haiku.c:300: parse error before `*'
/home/runner/work/libpcap/libpcap/pcap-haiku.c: At top level:
/home/runner/work/libpcap/libpcap/pcap-haiku.c:300: parse error before `}'
/home/runner/work/libpcap/libpcap/pcap-haiku.c:308: `handlep' used prior to declaration
/home/runner/work/libpcap/libpcap/pcap-haiku.c:308: `handle' undeclared here (not in a function)
/home/runner/work/libpcap/libpcap/pcap-haiku.c:309: parse error before `->'
/home/runner/work/libpcap/libpcap/pcap-haiku.c:310: parse error before `->'
/home/runner/work/libpcap/libpcap/pcap-haiku.c:310: warning: data definition has no type or storage class
Makefile:86: recipe for target 'pcap-haiku.o' failed`

This comment has been minimized.

Copy link
@guyharris

guyharris Feb 18, 2024

Member

Neither libpcap 1.10 nor tcpdump 4.99 are expected to compile on any compiler that doesn't support C99 or later - and the configure script (and probably CMake file) should fail when checking for a compiler that supports that.

Is that the old compiler that's kept around on 32-bit machines for binary compatibility?

This comment has been minimized.

Copy link
@infrastation

infrastation Feb 18, 2024

Author Member

GCC 2 has been considered earlier, please see #1153 (comment)

If indeed it is possible to switch libpcap and tcpdump Haiku packages to a newer GCC, it would be a good occasion to do this and to reduce the problem space. What do you think?

This comment has been minimized.

Copy link
@korli

korli Feb 18, 2024

Contributor

OK. the libpcap gcc2 build will be disabled then, nothing seems to use it.

handle->linktype = DLT_RAW;
break;
default:
snprintf(handle->errbuf, PCAP_ERRBUF_SIZE,
"Unknown interface type 0x%0x for interface \"%s\".",
sdl->sdl_type, handle->opt.device);
goto error;
}

// start monitoring
if (ioctl_ifreq(handle, handle->fd, SIOCSPACKETCAP, "SIOCSPACKETCAP") < 0)
goto error;
Expand Down Expand Up @@ -240,8 +272,6 @@ pcap_activate_haiku(pcap_t *handle)
}

handle->offset = 0;
handle->linktype = DLT_EN10MB;
// TODO: check interface type!

return 0;
error:
Expand Down

0 comments on commit 3a8efb6

Please sign in to comment.