Page 1 of 1

help with struct

Posted: Wed Apr 08, 2020 11:51 am
by AndyMK
Hi, can someone help with creating a structure from

Code: Select all

struct net;

struct sockaddr_nl
{
        sa_family_t        nl_family;        /* AF_NETLINK        */
        unsigned short        nl_pad;                /* zero                */
        __u32                nl_pid;                /* port ID        */
               __u32                nl_groups;        /* multicast groups mask */
};

struct nlmsghdr
{
        __u32                nlmsg_len;        /* Length of message including header */
        __u16                nlmsg_type;        /* Message content */
        __u16                nlmsg_flags;        /* Additional flags */
        __u32                nlmsg_seq;        /* Sequence number */
        __u32                nlmsg_pid;        /* Sending process port ID */
};
taken from https://sites.uclouvain.be/SystInfo/usr ... ink.h.html

Thanks

Re: help with struct

Posted: Wed Apr 08, 2020 12:58 pm
by infratec
Maybe this works:

Code: Select all

Structure sockaddr_nl Align #PB_Structure_AlignC
  nl_family.u   ; AF_NETLINK
  nl_pad.u      ; zero
  nl_pid.l      ; port ID
  nl_groups.l   ; multicast groups mask
EndStructure

Structure nlmsghdr Align #PB_Structure_AlignC
  nlmsg_len.l   ; Length of message including header
  nlmsg_type.u  ; Message content
  nlmsg_flags.u ; Additional flags
  nlmsg_seq.l   ; Sequence number
  nlmsg_pid.l   ; Sending process port ID
EndStructure

Re: help with struct

Posted: Wed Apr 08, 2020 1:03 pm
by AndyMK
Thanks, ill try it out

Re: help with struct

Posted: Sat Apr 11, 2020 2:58 am
by mchael
why a struct? Why not a multi col array? Be faster and easier if working with a database or file structure.

Re: help with struct

Posted: Sat Apr 11, 2020 4:35 am
by Josh
mchael wrote:why a struct? Why not a multi col array? Be faster and easier if working with a database or file structure.
What does an array have to do with a structure? Do you know why Andy needs the requested structure? I'm sorry mchael but you have no idea what you're talking about.

Re: help with struct

Posted: Sat Apr 11, 2020 10:14 pm
by AndyMK
Josh wrote:
mchael wrote:why a struct? Why not a multi col array? Be faster and easier if working with a database or file structure.
What does an array have to do with a structure? Do you know why Andy needs the requested structure? I'm sorry mchael but you have no idea what you're talking about.
Thanks for your support josh. I was trying to get netfilter queue working in linux to create a userland bridge where i could inspect packets passing between two ethernet interfaces. In the end i went with AF_PACKET raw sockets. It currently passes 1Gbit/sec at around 10% cpu usage on a quad core i5 machine. I think most of the usage is in the kernal. Code below if anyone wants to experiment. You need two spare ethernet adapters and you will have to enter the index number for each one. You must use ethtool to turn off offloading stuff on each ethernet adapter. You must also put each adapter in promiscuous mode.

Code: Select all

sudo ethtool --offload  enp1s0f0 gro off     <-- do for both interfaces
sudo ip link set enp1s0f0 promisc on         <-- do for both interfaces

Code: Select all

;run as root or start PB with sudo if you are using the debugger 

#AF_PACKET = 17
#SOCK_RAW = 3
#ETH_P_ALL = $0003

Structure sockaddr_ll
  sll_family.u    ;/* Always AF_PACKET */
  sll_protocol.u  ;/* Physical layer protocol */
  sll_ifindex.i   ;/* Interface number */
  sll_hatype.u    ;/* ARP hardware type */
  sll_pkttype.b   ;/* Packet type */
  sll_halen.b     ;/* Length of address */
  sll_addr.b[8]   ;/* Physical layer address */
EndStructure

Global socket1, socket2

;it's ugly but I used these 2 lines to get the index values for the two interfaces. Quick and dirty :)
;if_indextoname_(3, @iface)
;iface1.s = PeekS(@iface,-1,#PB_Ascii)

Procedure in(*val)
  Debug "Thread 1 Started"
  
  *sptr = socket_config.sockaddr_ll
    
  socket1 = socket_(#AF_PACKET, #SOCK_RAW, htons_(#ETH_P_ALL))
  If socket1 <> -1
            
    socket_config\sll_family = #AF_PACKET
    socket_config\sll_ifindex = 3
    socket_config\sll_protocol = htons_(#ETH_P_ALL)
        
    If bind_(socket1, *sptr, SizeOf(socket_config)) = 0
      *buffer = AllocateMemory(65536)
      
      Repeat
        recv = recv_(socket1, *buffer, 65536, 0);, info, SizeOf(info))
        If recv > 0
          send_(socket2, *buffer, recv, 0)
        Else
          Debug "in error"
        EndIf
      ForEver
    EndIf
    
  EndIf
    
EndProcedure

Procedure out(*val)
  Debug "Thread 2 Started"
  
  *sptr = socket_config.sockaddr_ll
    
  socket2 = socket_(#AF_PACKET, #SOCK_RAW, htons_(#ETH_P_ALL))
  If socket2 <> -1
    
    socket_config\sll_family = #AF_PACKET
    socket_config\sll_ifindex = 4
    socket_config\sll_protocol = htons_(#ETH_P_ALL)
    
    If bind_(socket2, *sptr, SizeOf(socket_config)) = 0
      *buffer = AllocateMemory(65536)
      
      Repeat
        recv = recv_(socket2, *buffer, 65536, 0);, info, SizeOf(info))
        If recv > 0
          send_(socket1, *buffer, recv, 0)
        Else
          Debug "in error"
        EndIf
      ForEver
    EndIf
    
  EndIf
  
EndProcedure

in = CreateThread(@in(), 0)
out = CreateThread(@out(), 0)
WaitThread(in)