Recent

Author Topic: Quiche (and BoringSSL) bindings  (Read 3269 times)

r.lukasiak

  • Full Member
  • ***
  • Posts: 167
Quiche (and BoringSSL) bindings
« on: October 06, 2024, 04:47:20 pm »
Hi everyone!

I'm trying to create binding for Quiche, I managed to map several functions but as it's my very first time creating any bindings, I faced some issues.
First I used H2Pas but the result was overwhelming, it seemed to need plenty of manual adjustment so for educational purposes I decided to do it manually from the ground up. I started with seemingly most important functions, testing them on-the-go. I managed to map several BoringSSL functions needed to create SSL context and a couple of Quiche functions needed to create a QUIC connection  Till this point, everything seemed to be working, so I took up quiche_conn_recv and that's where I faced first issues. I mapped it the same way I learn so far but calling it gives me Access violation.

the header:
Code: C  [Select][+][-]
  1. typedef struct {
  2.     struct sockaddr *from;
  3.     socklen_t from_len;
  4.     struct sockaddr *to;
  5.     socklen_t to_len;
  6. } quiche_recv_info;
  7.  
  8. ssize_t quiche_conn_recv(quiche_conn *conn, uint8_t *buf, size_t buf_len,
  9.                          const quiche_recv_info *info);
  10.  
mapped as:
Code: Pascal  [Select][+][-]
  1. type
  2. PQuiche_conn = ^Quiche_conn;
  3.   Quiche_conn = record
  4.   end;
  5.  
  6. Pquiche_recv_info = ^quiche_recv_info;
  7.   quiche_recv_info = record
  8.       from : Psockaddr;
  9.       from_len : socklen_t;
  10.       to_ : Psockaddr;  //'to_' since 'to' is a reserved word
  11.       to_len : socklen_t;
  12.     end;
  13.  
  14. quiche_conn_recv(conn: PQuiche_Conn; buf: PByte; buf_len: CSize_t;
  15.                  info: PQuiche_Recv_Info): CSsize_t; cdecl; external QUICHE_LIB;
  16.  
and the way I call the function:
Code: Pascal  [Select][+][-]
  1. var
  2.   bytes_received: CSsize_t;
  3.   buf: array of Byte;
  4.   //buffer: array[0..8191] of Char;
  5.   recv_info: Quiche_recv_info;
  6.   Precv_info: PQuiche_recv_info;
  7.  
  8. SetLength(buf, 8192);
  9. FillChar(recv_info, SizeOf(recv_info), 0);
  10. Precv_info := @recv_info;
  11.  
  12. bytes_received := quiche_conn_recv(conn, @buf[0], length(buf), Precv_info);
  13.  
I was experimenting with the parameters but I wasn't able to make it work and I'm not sure if the problem is the mapping of the function or the parameters I pass to.
Calling quiche_conn_recv is giving:
Quote
An unhandled exception occurred at $00007F22FA45392A:
                                                                                           EAccessViolation: Access violation
                               $00007F22FA45392A

                                                --------------------------------------------------

After being stuck at this for a couple of days, I peeked on the file generated by H2Pas and it looks different to my solution:
Code: Pascal  [Select][+][-]
  1. type
  2.   Pquiche_conn = ^quiche_conn;
  3.   Pquiche_recv_info = ^quiche_recv_info;
  4.       quiche_recv_info = record
  5.           from : Psockaddr;
  6.           from_len : socklen_t;
  7.           to : Psockaddr;
  8.           to_len : socklen_t;
  9.         end;  
  10.  
  11. function quiche_conn_recv(var conn:quiche_conn; var buf:uint8_t; buf_len:size_t;
  12.                           var info:quiche_recv_info):ssize_t;cdecl;external External_library name 'quiche_conn_recv';
  13.  
here, var conn:quiche_conn and var info:quiche_recv_info are not pointers.

I'm puzzled. Any clue what I'm doing wrong?

thanks in advance!

Fred vS

  • Hero Member
  • *****
  • Posts: 3412
    • StrumPract is the musicians best friend
Re: Quiche (and BoringSSL) bindings
« Reply #1 on: October 06, 2024, 05:30:36 pm »
Hello.

Maybe try with this:
Code: Pascal  [Select][+][-]
  1. quiche_conn_recv(conn: PQuiche_Conn; buf: PByte; buf_len: CSize_t;
  2.                var info: PQuiche_Recv_Info): CSsize_t; cdecl; external QUICHE_LIB;

(I know, using var when C uses const is strange but in many translation of header it is what was done.)
I use Lazarus 2.2.0 32/64 and FPC 3.2.2 32/64 on Debian 11 64 bit, Windows 10, Windows 7 32/64, Windows XP 32,  FreeBSD 64.
Widgetset: fpGUI, MSEgui, Win32, GTK2, Qt.

https://github.com/fredvs
https://gitlab.com/fredvs
https://codeberg.org/fredvs

r.lukasiak

  • Full Member
  • ***
  • Posts: 167
Re: Quiche (and BoringSSL) bindings
« Reply #2 on: October 06, 2024, 05:49:37 pm »
thanks for such a quick response.

your solution has some effect, it's still an error but not Access violation:
Quote
thread '<unnamed>' panicked at src/ffi.rs:1891:14:
                                                                                        not implemented: unsupported address type
                                 stack backtrace:
                                                    0:     0x7f88be87f055 - <unknown>
                                                                                        1:     0x7f88be8a16bb - <unknown>
                            2:     0x7f88be87cf5f - <unknown>
                                                                3:     0x7f88be87ee2e - <unknown>
    4:     0x7f88be8800e9 - <unknown>
                                        5:     0x7f88be87fe8a - <unknown>
                                                                            6:     0x7f88be880583 - <unknown>
                7:     0x7f88be88042b - <unknown>
                                                    8:     0x7f88be87f519 - <unknown>
                                                                                        9:     0x7f88be880197 - <unknown>
                           10:     0x7f88be708343 - <unknown>
                                                               11:     0x7f88be721a33 - <unknown>
   12:     0x7f88be7643ce - quiche_conn_recv
                                              13:           0x402594 - $main
                                                                                                           at
...

Fred vS

  • Hero Member
  • *****
  • Posts: 3412
    • StrumPract is the musicians best friend
Re: Quiche (and BoringSSL) bindings
« Reply #3 on: October 06, 2024, 06:10:33 pm »
OK, so could you try with this:

Code: Pascal  [Select][+][-]
  1. var
  2.   ...
  3.   buf: PByte;
  4.   ...
  5.  
  6.   GetMem(buf, 8192);
  7.   ...
  8.   bytes_received := quiche_conn_recv(conn, @buf, 8192, Precv_info);
I use Lazarus 2.2.0 32/64 and FPC 3.2.2 32/64 on Debian 11 64 bit, Windows 10, Windows 7 32/64, Windows XP 32,  FreeBSD 64.
Widgetset: fpGUI, MSEgui, Win32, GTK2, Qt.

https://github.com/fredvs
https://gitlab.com/fredvs
https://codeberg.org/fredvs

r.lukasiak

  • Full Member
  • ***
  • Posts: 167
Re: Quiche (and BoringSSL) bindings
« Reply #4 on: October 06, 2024, 06:22:12 pm »
I tried your recommendation but I'm getting the same error :/

Fred vS

  • Hero Member
  • *****
  • Posts: 3412
    • StrumPract is the musicians best friend
Re: Quiche (and BoringSSL) bindings
« Reply #5 on: October 06, 2024, 06:40:03 pm »
I tried your recommendation but I'm getting the same error :/

 :'(

Did you try to add in your pascal header ?:
Code: Pascal  [Select][+][-]
  1. {$PACKRECORDS C}

If you get the same error when using array of Byte vs PByte, it could be that the error comes form Precv_info who is not filled correctly.
What is the declaration of Psockaddr and socklen_t?
Maybe use a declaration from ctype.pas instead.
I use Lazarus 2.2.0 32/64 and FPC 3.2.2 32/64 on Debian 11 64 bit, Windows 10, Windows 7 32/64, Windows XP 32,  FreeBSD 64.
Widgetset: fpGUI, MSEgui, Win32, GTK2, Qt.

https://github.com/fredvs
https://gitlab.com/fredvs
https://codeberg.org/fredvs

r.lukasiak

  • Full Member
  • ***
  • Posts: 167
Re: Quiche (and BoringSSL) bindings
« Reply #6 on: October 06, 2024, 07:01:24 pm »
Quote
{$PACKRECORDS C}
Thanks for reminding me this! I noticed this in some other bindings when I was gathering intel on the topic but I eventually forgot to try it. I did it now tho, but the error persist :/

Quote
What is the declaration of Psockaddr and socklen_t?
Code: Pascal  [Select][+][-]
  1. type
  2. socklen_t = cuint32; //from BaseUnix
Psockaddr from Sockets

Fred vS

  • Hero Member
  • *****
  • Posts: 3412
    • StrumPract is the musicians best friend
Re: Quiche (and BoringSSL) bindings
« Reply #7 on: October 06, 2024, 07:09:05 pm »
Hum, Quiche resists...
Dont worry we will win.

Of course, you did check if conn was assigned with something like this:
Code: Pascal  [Select][+][-]
  1. if assigned(conn) then
  2. bytes_received := quiche_conn_recv(conn, @buf, 8192, Precv_info)
  3. else writeln('Conn is not assigned ;-( );

I will do a walk with the dog, maybe a new idea will appear.
I use Lazarus 2.2.0 32/64 and FPC 3.2.2 32/64 on Debian 11 64 bit, Windows 10, Windows 7 32/64, Windows XP 32,  FreeBSD 64.
Widgetset: fpGUI, MSEgui, Win32, GTK2, Qt.

https://github.com/fredvs
https://gitlab.com/fredvs
https://codeberg.org/fredvs

r.lukasiak

  • Full Member
  • ***
  • Posts: 167
Re: Quiche (and BoringSSL) bindings
« Reply #8 on: October 06, 2024, 07:31:57 pm »
Quote
Hum, Quiche resists...
Dont worry we will win.
this sounds promising!

Quote
Of course, you did check if conn was assigned with something like this:
yes, I tested conn itself in several ways. It was my first suspect but it turned out to be innocent.
Code: Pascal  [Select][+][-]
  1.   if conn = nil then
  2.     begin
  3.       WriteLn('Failed to create QUIC connection.');
  4.       Exit;
  5.     end else
  6.     WriteLn('QUIC connection created successfully: ',PtrUInt(SSL) );
  7. .
  8. .
  9.   if Assigned(conn) then writeln('conn assigned') else writeln('conn NOT assigned');
  10. .
  11. .
  12.  
  13.   if quiche_conn_is_established(conn)
  14.        then WriteLn('QUIC connection IS established already.')
  15.        else WriteLn('QUIC connection ISN''T established yet.');
  16. .
  17. .
  18.   quiche_conn_stats(conn, Stats);  //it returns some values, I didn't dig into their making any sense but at first glance everything seems ok
  19.  
  20.  

and how about quiche_recv_info?
Code: Pascal  [Select][+][-]
  1. Pquiche_recv_info = ^quiche_recv_info;
  2.   quiche_recv_info = record
  3.       from : Psockaddr;
  4.       from_len : socklen_t;
  5.       to_ : Psockaddr;  //'to_' since 'to' is a reserved word
  6.       to_len : socklen_t;
  7.     end;  
  8.  
is it ok if the name of the 'to' field is changed? For obvious reasons it can't be 'to', but I'm not sure if it matters.
I also tried
Code: Pascal  [Select][+][-]
  1. PQuiche_Recv_Info = ^Quiche_Recv_Info;
  2. Quiche_recv_info = record
  3. end;
  4.  
as far as I understand, this way I leave the record as it originally is or am I wrong?

Fred vS

  • Hero Member
  • *****
  • Posts: 3412
    • StrumPract is the musicians best friend
Re: Quiche (and BoringSSL) bindings
« Reply #9 on: October 06, 2024, 07:45:11 pm »
and how about quiche_recv_info?
Code: Pascal  [Select][+][-]
  1. Pquiche_recv_info = ^quiche_recv_info;
  2.   quiche_recv_info = record
  3.       from : Psockaddr;
  4.       from_len : socklen_t;
  5.       to_ : Psockaddr;  //'to_' since 'to' is a reserved word
  6.       to_len : socklen_t;
  7.     end;  
  8.  
is it ok if the name of the 'to' field is changed? For obvious reasons it can't be 'to', but I'm not sure if it matters.
Hum, I dont know, I always use the same name as the one from the C headers but maybe it has no importance, only the index is used and the name does not matter.
Sure somebody here can confirm/infirm this.

Quote
Code: [Select]
PQuiche_Recv_Info = ^Quiche_Recv_Info;
Quiche_recv_info = record
end;
 
as far as I understand, this way I leave the record as it originally is or am I wrong?
Sorry again, I dont know ;-(
« Last Edit: October 06, 2024, 07:47:00 pm by Fred vS »
I use Lazarus 2.2.0 32/64 and FPC 3.2.2 32/64 on Debian 11 64 bit, Windows 10, Windows 7 32/64, Windows XP 32,  FreeBSD 64.
Widgetset: fpGUI, MSEgui, Win32, GTK2, Qt.

https://github.com/fredvs
https://gitlab.com/fredvs
https://codeberg.org/fredvs

Fred vS

  • Hero Member
  • *****
  • Posts: 3412
    • StrumPract is the musicians best friend
Re: Quiche (and BoringSSL) bindings
« Reply #10 on: October 06, 2024, 08:07:45 pm »
Quote
is it ok if the name of the 'to' field is changed? For obvious reasons it can't be 'to', but I'm not sure if it matters.

A fast check gives this:
https://stackoverflow.com/questions/17920716/is-it-possible-to-use-reserved-words-for-field-names

And the great news is that fpc also allows the "&" operator.

So you may try with this:
Code: Pascal  [Select][+][-]
  1. quiche_recv_info = record
  2.       from : Psockaddr;
  3.       from_len : socklen_t;
  4.       &to : Psockaddr;  //'to_' since 'to' is a reserved word
  5.       to_len : socklen_t;
  6.     end;

And by the way, also try with quiche_recv_info = packed record
I use Lazarus 2.2.0 32/64 and FPC 3.2.2 32/64 on Debian 11 64 bit, Windows 10, Windows 7 32/64, Windows XP 32,  FreeBSD 64.
Widgetset: fpGUI, MSEgui, Win32, GTK2, Qt.

https://github.com/fredvs
https://gitlab.com/fredvs
https://codeberg.org/fredvs

r.lukasiak

  • Full Member
  • ***
  • Posts: 167
Re: Quiche (and BoringSSL) bindings
« Reply #11 on: October 06, 2024, 08:28:54 pm »
Quote
&to
true...  :-[

Quote
And by the way, also try with quiche_recv_info = packed record
ahh yes, I tried this when you reminded me about {$PACKRECORDS C}. I'm sorry I forgot to mention.

The error persists :|

Fred vS

  • Hero Member
  • *****
  • Posts: 3412
    • StrumPract is the musicians best friend
Re: Quiche (and BoringSSL) bindings
« Reply #12 on: October 06, 2024, 08:48:20 pm »
The error persists :|

Maybe try with a bigger buffer: GetMem(buf, 8192 * 4);

It would be great to know from where the error comes, from the filing of the buffer or the filling of quiche_recv_info.
And the debugger seems not very cooperative.
Maybe try with a buffer of char to see if the error is the same as with a buffer of byte. If the error is the same, there is a problem with the buffer.
And try using a quiche_recv_info with record defined surely wrong to see if the error is the same.

Once we know who is the guilty, he will pay.
I use Lazarus 2.2.0 32/64 and FPC 3.2.2 32/64 on Debian 11 64 bit, Windows 10, Windows 7 32/64, Windows XP 32,  FreeBSD 64.
Widgetset: fpGUI, MSEgui, Win32, GTK2, Qt.

https://github.com/fredvs
https://gitlab.com/fredvs
https://codeberg.org/fredvs

r.lukasiak

  • Full Member
  • ***
  • Posts: 167
Re: Quiche (and BoringSSL) bindings
« Reply #13 on: October 06, 2024, 09:02:26 pm »
Bigger buffer,not PChar did the trick.The error is still the same.

Quote
And the debugger seems not very cooperative.
I'm on Gentoo Linux, I tried to rebuild Quiche with the debug flag but it fails and it seems to be a known issue. I also dowloaded the source and compiled it manualjy but it's giving me some linking error when I try to build my program.It looks like Quiche objects to debugging :D

r.lukasiak

  • Full Member
  • ***
  • Posts: 167
Re: Quiche (and BoringSSL) bindings
« Reply #14 on: October 06, 2024, 09:10:50 pm »
...and messing with Quiche_Recv_Info doesn't change anything when it comes to the error but...

I do
Code: Pascal  [Select][+][-]
  1. writeln('SizeOf(recv_info)=',SizeOf(recv_info));
right before calling quiche_conn_recv and the result reflects changes in the definition of Quiche_Recv_Info;
when it's (supposedly) correct, it returns 21, after I modified the record, it returned 24 but when I leave it "as it is",without deifning fields, it returns 0.I'm not sure if this information may help in anything.

 

TinyPortal © 2005-2018