Translating c records

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Translating c records

Rainer Stratmann
struct sockaddr_ll {
     unsigned short sll_family;  
     unsigned short sll_protocol;
     int            sll_ifindex;  
     unsigned short sll_hatype;  
     unsigned char  sll_pkttype;  
     unsigned char  sll_halen;    
     unsigned char  sll_addr[8];  
 };


type
 sockaddr_ll = record
  sll_family : word;
  sll_protocol : word;
  sll_ifindex : integer;
  sll_hatype : word;
  sll_pkttype : byte;
  sll_halen : byte;
  sll_addr: array[ 0 .. 7 ] of byte;
 end;

Is this correct?
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Translating c records

Michael Van Canneyt


On Wed, 24 Apr 2019, Rainer Stratmann wrote:

> struct sockaddr_ll {
>     unsigned short sll_family;
>     unsigned short sll_protocol;
>     int            sll_ifindex;
>     unsigned short sll_hatype;
>     unsigned char  sll_pkttype;
>     unsigned char  sll_halen;
>     unsigned char  sll_addr[8];
> };
>
>
> type
> sockaddr_ll = record
>  sll_family : word;
>  sll_protocol : word;
>  sll_ifindex : integer;
>  sll_hatype : word;
>  sll_pkttype : byte;
>  sll_halen : byte;
>  sll_addr: array[ 0 .. 7 ] of byte;
> end;
>
> Is this correct?

if you add
{$PPACKRECORDS C}

And make integer a longint (that is always 4 bytes, regardless of the mode
you're compiling in), then it should be OK.

You can always compare with

#include <stdio.h>
#include <yourcheader.h>

int main () {
   printf("%d\n",sizeof(struct sockaddr_l));
}

and in pascal
writeln(sizeof(sockaddr_ll));

Michael.
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Translating c records

silvioprog
In reply to this post by Rainer Stratmann
On Wed, Apr 24, 2019 at 5:41 PM Rainer Stratmann <[hidden email]> wrote:
struct sockaddr_ll {
     unsigned short sll_family;   
     unsigned short sll_protocol;
     int            sll_ifindex; 
     unsigned short sll_hatype;   
     unsigned char  sll_pkttype; 
     unsigned char  sll_halen;   
     unsigned char  sll_addr[8]; 
 };

type
 sockaddr_ll = record
  sll_family : word;
  sll_protocol : word;
  sll_ifindex : integer;
  sll_hatype : word;
  sll_pkttype : byte;
  sll_halen : byte;
  sll_addr: array[ 0 .. 7 ] of byte;
 end;

Is this correct?

It seems OK, however, if you want portability too, you should:

uses
  ctypes;

{$PACKRECORDS C} // already commented by Michael

type
  sockaddr_ll = record
    sll_family: cushort;
    sll_protocol: cushort;
    sll_ifindex: cint;
    sll_hatype: cushort;
    sll_pkttype: cuchar;
    sll_halen: cuchar;
    sll_addr: array[0..7] of cuchar;
 end;
 
cheers

--
Silvio Clécio

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal