Recent

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

r.lukasiak

  • Full Member
  • ***
  • Posts: 167
Re: Quiche (and BoringSSL) bindings
« Reply #45 on: October 08, 2024, 07:57:32 pm »
@silvercoder70 and @tetrastes,thanks for joining!
I'm still processing all your messages, will answer ASAP.

@Fred vS
Quote
undefined reference to `SSL_CTX_set_min_proto_version'
undefined reference to `SSL_CTX_set_max_proto_version'
it's becasue in your PATH you have only the "classic" OpenSSL, not BoringSSL.

notice:
Code: Pascal  [Select][+][-]
  1. {$L ./libs/libssl.so}
  2. {$L ./libs/libcrypto.so}

here you can get the file with the libs https://www.dropbox.com/scl/fi/8owmnokwe2wqwxssiq09w/Quiche_BoringSSL_test.tar.gz?rlkey=ln5cybe6xwh4qf6a48fsgjzdg&st=byzouhgu&dl=0
just generate the keys and place them in ./keys



Fred vS

  • Hero Member
  • *****
  • Posts: 3394
    • StrumPract is the musicians best friend
Re: Quiche (and BoringSSL) bindings
« Reply #46 on: October 08, 2024, 08:02:52 pm »
@Fred vS
Quote
undefined reference to `SSL_CTX_set_min_proto_version'
undefined reference to `SSL_CTX_set_max_proto_version'
it's becasue in your PATH you have only the "classic" OpenSSL, not BoringSSL.

Ha, ok, (and indeed, BoringXXL  :-X).
I will try asap with the boring libs.
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 #47 on: October 08, 2024, 09:07:04 pm »
May be I don't understand something, but from https://github.com/cloudflare/quiche/tree/master/quiche#calling-quiche-from-cc:
Quote
Calling quiche from C/C++

quiche exposes a thin C API on top of the Rust API that can be used to more easily integrate quiche into C/C++ applications (as well as in other languages that allow calling C APIs via some form of FFI). The C API follows the same design of the Rust one, modulo the constraints imposed by the C language itself.

When running cargo build, a static library called libquiche.a will be built automatically alongside the Rust one. This is fully stand-alone and can be linked directly into C/C++ applications.

Note that in order to enable the FFI API, the ffi feature must be enabled (it is disabled by default), by passing --features ffi to cargo.

this would explain the error that's talking about FFI... The problem with .so s that I use Quiche fromPortage tree (Gentoo Linux), in order to make it.a I'd have to build it by myself which I alreay tried, with the debug flag but so far I wasn't able to have it work. I'll give it another try.



You have to read about variable parameters in Pascal (https://www.freepascal.org/docs-html/current/ref/refsu65.html#x181-20500014.4.2).
For example, this
Code: Pascal  [Select][+][-]
  1. function quiche_conn_recv(var conn: PQuiche_Conn; var buf: PByte; var buf_len: CSize_t;
  2.                   var info: PQuiche_Recv_Info): CSsize_t; cdecl; external QUICHE_LIB;
does not correspond to
Code: C  [Select][+][-]
  1. ssize_t quiche_conn_recv(quiche_conn *conn, uint8_t *buf, size_t buf_len,
  2.                          const quiche_recv_info *info);
  3.  

Yes, this code is not correct, we know it, it was for testing and of course did not help.
It is a variation of code give by h2pas (see first post of OP).

and in Makefile from examples I see that C examples are linked with this libquiche.a.
But you link with dynamic libquiche.so. I suppose that it is generated by rust. Why do you think that it's functions are C declared?
how about this:
Code: Pascal  [Select][+][-]
  1. function quiche_conn_recv(conn: PQuiche_Conn; buf: PByte; buf_len: CSize_t;
  2.                   const info: PQuiche_Recv_Info): CSsize_t; cdecl; external QUICHE_LIB;
now without vars, the const  like in the header, but his reminds me what Fred said about C's const being Pascal's var  :-\


whereas in the Pascal code, this parameter (Precv_info) is passed in is not allocated any memory OR assigned Nil?

Hope this helps?

Yes, I think. You are the most attentive!

Yep, well seen, but in a previous post, OP did it, maybe do it again with his last code?:

Code: Pascal  [Select][+][-]
  1. FillChar(recv_info, SizeOf(recv_info), 0);
indeed, I missed this line in my new version. Added but without any positive outcome :/ the same error persist

Fred vS

  • Hero Member
  • *****
  • Posts: 3394
    • StrumPract is the musicians best friend
Re: Quiche (and BoringSSL) bindings
« Reply #48 on: October 08, 2024, 09:25:31 pm »
now without vars, the const  like in the header, but his reminds me what Fred said about C's const being Pascal's var  :-\
Ooops, no, I said that strangely the h2pas program translate const into var.
But of course C const are not Pas var.

I still fight with your demo, the libs are loaded, the key recognized and... I get same crash as you.
But also a error of bad connection.
Anyway something to eat...
Code: Text  [Select][+][-]
  1. Quiche version: 0.22.0
  2. BoringSSL version: 1.1.1.7
  3. QUIC configuration CREATED
  4. Cert OK
  5. Priv_key OK
  6. SSL_CTX CREATED!
  7. SSL_CTX_use_certificate_file OK!
  8. SSL_CTX_use_PrivateKey_file OK!
  9. Private key MATCHES certificate
  10. SSL_new OK
  11. --------------------------------------------------
  12. QUIC connection created successfully!
  13. QUIC connection ISN'T established   // HERE no connect
  14. Conn NOT CLOSED
  15. Trace ID Length: 16
  16. Trace ID: 2 186 213 85 62 176 136 248 221 208 96 9 218 116 60 185
  17. SizeOf(recv_info)=32
  18. bytes_received...
  19. An unhandled exception occurred at $0000791CA064BDD4:
  20. EAccessViolation: Access violation
  21.   $0000791CA064BDD4
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 #49 on: October 08, 2024, 09:48:15 pm »
Quote
Ooops, no, I said that strangely the h2pas program translate const into var.
But of course C const are not Pas var.
ohh, now I get it. I'm sorry for twisting your words  :-[

Quote
QUIC connection created successfully!
QUIC connection ISN'T established   // HERE no connect
Conn NOT CLOSED
beside the final error, this is another mystery.  Interestingly in the original version it's the opposite; "QUIC connection IS established" and "Conn CLOSED". I've been literally spelling both  filles and I can't find the difference in their implementation that causes the different output. :/

Fred vS

  • Hero Member
  • *****
  • Posts: 3394
    • StrumPract is the musicians best friend
Re: Quiche (and BoringSSL) bindings
« Reply #50 on: October 08, 2024, 09:56:41 pm »
this is another mystery.  Interestingly in the original version it's the opposite; "QUIC connection IS established" and "Conn CLOSED". I've been literally spelling both  filles and I can't find the difference in their implementation that causes the different output. :/

Hum, what is "the original version" ?
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

tetrastes

  • Hero Member
  • *****
  • Posts: 567
Re: Quiche (and BoringSSL) bindings
« Reply #51 on: October 08, 2024, 10:06:03 pm »
whereas in the Pascal code, this parameter (Precv_info) is passed in is not allocated any memory OR assigned Nil?

Hope this helps?

Yes, I think. You are the most attentive!

Yep, well seen, but in a previous post, OP did it, maybe do it again with his last code?:

Code: Pascal  [Select][+][-]
  1. FillChar(recv_info, SizeOf(recv_info), 0);
indeed, I missed this line in my new version. Added but without any positive outcome :/ the same error persist

Note this:
Well, I think it must be initialized not with zeros, but with some actual values, like in C code.

I was looking at the code from r.lukasiak and ... compared with code here

https://android.googlesource.com/platform/external/rust/crates/quiche/+/HEAD/examples/server.c

and noticed the last parameter is filled out ...

Code: C  [Select][+][-]
  1.  quiche_recv_info recv_info = {
  2.             (struct sockaddr *)&peer_addr,
  3.             peer_addr_len,
  4.             conns->local_addr,
  5.             conns->local_addr_len,
  6.         };
  7.         ssize_t done = quiche_conn_recv(conn_io->conn, buf, read, &recv_info);

whereas in the Pascal code, this parameter (Precv_info) is passed in is not allocated any memory OR assigned Nil?

Hope this helps?

The fields of recv_info must be actual values of your connection.


r.lukasiak

  • Full Member
  • ***
  • Posts: 167
Re: Quiche (and BoringSSL) bindings
« Reply #52 on: October 08, 2024, 10:08:07 pm »
@Fred vS
Quote
Hum, what is "the original version" ?

the one I had when I started this post. BoringSSLand Quiche bindings in separate files + the test program apart, with plenty of comments and not tested functions/procedures. Last night I "refactored" it including only functions that I use in the test program. The old version was in 3 files and with few hundred lines of comments, mostly different versions of functions I was testing.

r.lukasiak

  • Full Member
  • ***
  • Posts: 167
Re: Quiche (and BoringSSL) bindings
« Reply #53 on: October 08, 2024, 10:45:47 pm »
ohh.... you mean alike I did with quiche_conn_new_with_tls:
Code: Pascal  [Select][+][-]
  1. FillChar(localAddr, SizeOf(localAddr), 0);
  2.   localAddr.sin_family := AF_INET;
  3.   localAddr.sin_port := htons(12345);
  4.   localAddr.sin_addr := StrToNetAddr('192.168.1.4');
  5.   FillChar(peerAddr, SizeOf(peerAddr), 0);
  6.   peerAddr.sin_family := AF_INET;
  7.   peerAddr.sin_port := htons(0);
  8.   peerAddr.sin_addr.s_addr := INADDR_ANY;
  9.  
  10.   recv_info.from := @localAddr;;
  11.   recv_info.from_len := SizeOf(localAddr);
  12.   recv_info.&to := @peerAddr;;
  13.   recv_info.to_len := SizeOf(peerAddr);
  14.  
  15.   Precv_info := @recv_info;  
  16.  
?

and.... it WORKED!! well, before I start bragging that it "works like a charm"... I'd better say "the error is gone"  :D
Quote
Quiche version: 0.22.0
BoringSSL version: 1.1.1.7
QUIC configuration CREATED
Cert ERROR
Priv_key OK
SSL_CTX CREATED!
SSL_CTX_use_certificate_file ERROR!
SSL_CTX_use_PrivateKey_file OK!
Private key DOES NOT match certificate
SSL_new OK
--------------------------------------------------
QUIC connection created successfully!
QUIC connection ISN'T established
Conn NOT CLOSED
Trace ID Length: 16
Trace ID: 2 56 65 240 209 12 232 155 16 48 10 66 217 99 228 195
SizeOf(recv_info)=32

bytes_received...
bytes_received...
bytes_received...
bytes_received...
...
...
...
and now, the thing that doesn't let me sleep since I started playing with this... how can I actually find out if it really works and not only not throwing any error? debugging is highly limited. Yesterday I thought that Quiche_stats was working "like a charm" but it turned out that even the definition was messed up but it wasn't giving any errors so I marked it as "works" in the very beginning of this journey.

Fred vS

  • Hero Member
  • *****
  • Posts: 3394
    • StrumPract is the musicians best friend
Re: Quiche (and BoringSSL) bindings
« Reply #54 on: October 08, 2024, 11:00:42 pm »
Wait, wait, so really the game is already over?

And the warrior who found the guilty is silvercoder70:
https://forum.lazarus.freepascal.org/index.php/topic,68793.msg532737.html#msg532737 

confirmed by tetrastes
https://forum.lazarus.freepascal.org/index.php/topic,68793.msg532765.html#msg532765

Nice battle, and I am very happy that we win today, before deep night.

Have fun.

Fre;D
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

TRon

  • Hero Member
  • *****
  • Posts: 3463
Re: Quiche (and BoringSSL) bindings
« Reply #55 on: October 08, 2024, 11:04:12 pm »
how can I actually find out if it really works and not only not throwing any error?
Checking if your header translations are correct is the most boring/tedious part.

The easiest is by looking at the examples and re-create those in pascal and test/check. There are both a client and a server example written in c so you could compile f.i the server example in c and rewrite the client in pascal then run the server and see if the client works. Same can be done for the server (and in case you know the pascal client is already working then you can use that instead of the c version)

Somewhere in the documentation is listed that they have a online test server you can use though I have not looked at it so don't know if if you are able to receive enough feedback from the server.
This tagline is powered by AI

Fred vS

  • Hero Member
  • *****
  • Posts: 3394
    • StrumPract is the musicians best friend
Re: Quiche (and BoringSSL) bindings
« Reply #56 on: October 08, 2024, 11:18:17 pm »
It seems to really work.

Adding this:
 
Code: Pascal  [Select][+][-]
  1. bytes_received := quiche_conn_recv(PConn, buf, BUF_SIZE, Precv_info);
  2.   Writeln('bytes_received = ' + inttostr(bytes_received)); // adding this

Shows beautiful:
Code: Pascal  [Select][+][-]
  1. bytes_received = -3 // the first
  2. bytes_received...
  3. bytes_received = 32768 // the others
  4. bytes_received...
  5. bytes_received = 32768
  6. bytes_received...
  7. bytes_received = 32768
  8. bytes_received...
  9. bytes_received = 32768
  10. ...

Anyway, the victory is not far.  ;)

To resume, here it works with:
Code: Pascal  [Select][+][-]
  1. function quiche_conn_recv(conn: PQuiche_Conn; const buf: PByte; buf_len: CSize_t;
  2.                   const info: PQuiche_Recv_Info): CSsize_t; cdecl; external QUICHE_LIB;

+ initialize localAddr, peerAddr and recv_info with valid datas.

and this:
Code: Pascal  [Select][+][-]
  1. bytes_received := quiche_conn_recv(PConn, buf, BUF_SIZE, Precv_info);
« Last Edit: October 08, 2024, 11:30:17 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

r.lukasiak

  • Full Member
  • ***
  • Posts: 167
Re: Quiche (and BoringSSL) bindings
« Reply #57 on: October 08, 2024, 11:27:55 pm »
@Fred vS
Quote
Wait, wait, so really the game is already over?
I'm not quite sure... the error is gone for now but now I'll review each single function having in mind all I learnt from this thread.

Quote
And the warrior who found the guilty is silvercoder70:
https://forum.lazarus.freepascal.org/index.php/topic,68793.msg532737.html#msg532737

confirmed by tetrastes
https://forum.lazarus.freepascal.org/index.php/topic,68793.msg532765.html#msg532765
basically these 2  suggestions solved the original issue but we've gone through plenty of options and I learnt something from each one which is as important as the solution itself.

@TRon
Quote
Checking if your header translations are correct is the most boring/tedious part.
I see... When I was searching for info on the topic, I came across a post on this forum where someone said that this requires A LOT of patience, even using H2Pas involves a lot of manual adjustment.

Quote
The easiest is by looking at the examples and re-create those in pascal and test/check. There are both a client and a server example written in c so you could compile f.i the server example in c and rewrite the client in pascal then run the server and see if the client works. Same can be done for the server (and in case you know the pascal client is already working then you can use that instead of the c version)
thanks! I'll take this approach.

So for now (as I guess I'll be back soon with more questions), everyone, thanks a lot! As always, you rule!

cheers!

//edit: typoes
« Last Edit: October 08, 2024, 11:37:44 pm by r.lukasiak »

r.lukasiak

  • Full Member
  • ***
  • Posts: 167
Re: Quiche (and BoringSSL) bindings
« Reply #58 on: October 08, 2024, 11:47:28 pm »
However...
Quote
Quiche version: 0.22.0
BoringSSL version: 1.1.1.7
QUIC configuration CREATED
Cert ERROR
Priv_key OK
SSL_CTX CREATED!
SSL_CTX_use_certificate_file ERROR!
SSL_CTX_use_PrivateKey_file OK!
Private key DOES NOT match certificate
SSL_new OK
--------------------------------------------------
QUIC connection created successfully!
QUIC connection ISN'T established
Conn NOT CLOSED

Trace ID Length: 16
Trace ID: 2 56 65 240 209 12 232 155 16 48 10 66 217 99 228 195
SizeOf(recv_info)=32

bytes_received...
bytes_received...
bytes_received...
bytes_received...

I moved my keys when I was gziping the file for you and I forgot to move them back before testing,
and it still says "QUIC connection ISN'T established" (@Fred vS, how is it on your side?) but it supposedly "works"... hmm, I'm still not quite sure :D
« Last Edit: October 08, 2024, 11:49:10 pm by r.lukasiak »

Fred vS

  • Hero Member
  • *****
  • Posts: 3394
    • StrumPract is the musicians best friend
Re: Quiche (and BoringSSL) bindings
« Reply #59 on: October 09, 2024, 12:37:06 am »
Hum, I already cleaned all...
Ok, all reinstalled.
 
Important: You need to be in the directory of the project to run it otherwise I get same error than you for the keys:
Code: Pascal  [Select][+][-]
  1. > cd /the_directory_of_your _project.
Then run the app from there.

Here the code I used:
Code: Pascal  [Select][+][-]
  1. program project1;
  2.  
  3. {$mode objfpc}{$H+}
  4. {$PACKRECORDS C}
  5.  
  6. {$L ./libs/libssl.so}
  7. {$L ./libs/libcrypto.so}
  8. //{$L ./libs/libquiche_dbg.so}
  9.  
  10. uses
  11.   {$IFDEF UNIX}
  12.   cthreads,
  13.   {$ENDIF}
  14.   Classes,
  15.   Crt, SysUtils, CTypes, BaseUnix, Sockets;
  16.  
  17. type
  18.   CSsize_t = NativeInt;
  19.  
  20.   PQuiche_config = ^TQuiche_config;
  21.   TQuiche_config = record
  22.   end;
  23.  
  24.   PQuiche_conn = ^TQuiche_conn;
  25.   TQuiche_conn = record
  26.   end;
  27.  
  28.   PSSL_METHOD = ^TSSL_METHOD;
  29.   TSSL_METHOD = record
  30.   end;
  31.  
  32.   PSSL_CTX = ^TSSL_CTX;
  33.   TSSL_CTX = record
  34.   end;
  35.  
  36.   PSSL = ^TSSL;
  37.   TSSL = record
  38.   end;
  39.  
  40.   PQuiche_Recv_Info = ^TQuiche_Recv_Info;
  41.   TQuiche_Recv_Info = record
  42.       from : Psockaddr;
  43.       from_len : socklen_t;
  44.       &to : Psockaddr;
  45.       to_len : socklen_t;
  46.   end;
  47.  
  48.   PQuiche_stats = ^TQuiche_stats;
  49.   Tquiche_stats = record
  50.     recv : size_t;
  51.     sent : size_t;
  52.     lost : size_t;
  53.     retrans : size_t;
  54.     sent_bytes : uint64;
  55.     recv_bytes : uint64;
  56.     acked_bytes : uint64;
  57.     lost_bytes : uint64;
  58.     stream_retrans_bytes : uint64;
  59.     paths_count : size_t;
  60.     reset_stream_count_local : uint64;
  61.     stopped_stream_count_local : uint64;
  62.     reset_stream_count_remote : uint64;
  63.     stopped_stream_count_remote : uint64;
  64.   end;
  65.  
  66. var
  67.   i: integer;
  68.   ch: Char;
  69.   PConfig: Pquiche_config;
  70.   Pctx: PSSL_CTX;
  71.   SSL: PSSL;
  72.   method: PSSL_METHOD;
  73.   isServer: boolean = true;
  74.   scid, odcid: array[0..15] of Byte;
  75.   Pconn: PQuiche_conn;
  76.   Stats: TQuiche_stats;
  77.   PStats: PQuiche_stats;
  78.   localAddr, peerAddr: TSockAddr;
  79.   trace_id: PByte;
  80.   trace_id_len: Csize_t;
  81.   buf: PByte;
  82.   BUF_SIZE: size_t = 8192 * 4;
  83.   recv_info: TQuiche_recv_info;
  84.   Precv_info: PQuiche_recv_info;
  85.   bytes_received: CSsize_t;
  86.  
  87. const
  88.   QUICHE_LIB = 'libquiche.so';
  89.   BORINGSSL_LIB = 'libssl.so';
  90.   CRYPTO_LIB = 'libcrypto.so';
  91.   QUICHE_PROTOCOL_VERSION = 1;
  92.   TLS1_3_VERSION = $0304; //0x0304
  93.   SSL_FILETYPE_PEM = 1;
  94.   SSL_OP_NO_RENEGOTIATION = 0;
  95.  
  96. {---------- BORINGSSL ----------}
  97. function OpenSSL_version_num: cint; cdecl; external CRYPTO_LIB;
  98. function TLS_method: PSSL_METHOD; cdecl; external BORINGSSL_LIB;
  99. function SSL_CTX_new(const method: PSSL_METHOD): PSSL_CTX; cdecl; external BORINGSSL_LIB;
  100. procedure SSL_CTX_set_min_proto_version(ctx: PSSL_CTX; version: cint); cdecl; external BORINGSSL_LIB;
  101. procedure SSL_CTX_set_max_proto_version(ctx: PSSL_CTX; version: cint); cdecl; external BORINGSSL_LIB;
  102. function SSL_CTX_use_certificate_file(ctx: PSSL_CTX; const filename: PChar; type_: cint): cint; cdecl; external BORINGSSL_LIB;
  103. function SSL_CTX_use_PrivateKey_file(ctx: PSSL_CTX; const filename: PChar; type_: cint): cint; cdecl; external BORINGSSL_LIB;
  104. function SSL_CTX_check_private_key(ctx: PSSL_CTX): cint; cdecl; external BORINGSSL_LIB;
  105. function SSL_CTX_set_options(ctx: PSSL_CTX; options: UInt32 ): UInt32; cdecl; external BORINGSSL_LIB;
  106. function SSL_new(ctx: PSSL_CTX): PSSL; cdecl; external BORINGSSL_LIB;
  107.  
  108. {---------- QUICHE ----------}
  109. function quiche_version: PChar; cdecl; external QUICHE_LIB;
  110. function quiche_config_new(version: UInt32): PQuiche_config; cdecl; external QUICHE_LIB;
  111. function quiche_config_load_cert_chain_from_pem_file(var config: TQuiche_config; path: Pansichar) : longint; cdecl; external QUICHE_LIB;
  112. function quiche_config_load_priv_key_from_pem_file(var config: TQuiche_config; path: Pansichar): longint;cdecl;external QUICHE_LIB;
  113. function quiche_conn_new_with_tls(const scid: PByte; scid_len: csize_t;
  114.                                   const odcid: PByte; odcid_len: csize_t;
  115.                                   const local: PSockAddr; local_len: socklen_t;
  116.                                   const peer: PSockAddr; peer_len: socklen_t;
  117.                                   const config: PQuiche_config; ssl: PSSL;
  118.                                   is_server: Boolean): PQuiche_conn;
  119.                                   cdecl; external QUICHE_LIB;
  120.                                  
  121. function quiche_conn_set_session(conn: PQuiche_Conn; buf:PByte; buf_len:csize_t) : cint32; cdecl; external QUICHE_LIB;
  122.                                  
  123.                                  
  124. function quiche_conn_is_established(var conn: PQuiche_conn): cBool; cdecl; external QUICHE_LIB;
  125. function quiche_conn_is_closed(var conn:PQuiche_conn): cbool; cdecl; external QUICHE_LIB;
  126. procedure quiche_conn_trace_id(const conn: PQuiche_Conn; var out: PByte; var out_len: Csize_t); cdecl; external QUICHE_LIB;
  127. procedure quiche_conn_source_id(const conn: PQuiche_Conn; var out: PByte; var out_len: Csize_t); cdecl; external QUICHE_LIB;
  128. procedure quiche_conn_stats(var conn:PQuiche_conn; var &out:PQuiche_stats); cdecl; external QUICHE_LIB;
  129.  
  130. //function quiche_conn_recv(var conn:PQuiche_conn; buf:PByte; buf_len:csize_t; info:PQuiche_recv_info):cssize_t; cdecl; external QUICHE_LIB;
  131. {H2Pas}//function quiche_conn_recv(var conn:quiche_conn; var buf:Cuint8; buf_len:Csize_t; var info:quiche_recv_info):Cssize_t;cdecl;external QUICHE_LIB name 'quiche_conn_recv';
  132. //function quiche_conn_recv(var conn: PQuiche_Conn; buf: PByte; buf_len: CSize_t;
  133.   //                        var info: PQuiche_Recv_Info): CSsize_t; cdecl; external QUICHE_LIB;
  134. {TRon}  //function quiche_conn_recv(conn: pquiche_conn; buf: pcuint8; buf_len: size_t; const info: pquiche_recv_info): ssize_t; cdecl; external QUICHE_LIB;
  135. {Fred}// function quiche_conn_recv(conn: PQuiche_Conn; buf: PByte; buf_len: CSize_t;
  136.         //           var info: TQuiche_Recv_Info): CSsize_t; cdecl; external QUICHE_LIB;
  137. {Fred2} //function quiche_conn_recv(var conn: PQuiche_Conn; var buf: PByte; var buf_len: CSize_t;
  138.           //        var info: PQuiche_Recv_Info): CSsize_t; cdecl; external QUICHE_LIB;
  139. function quiche_conn_recv(conn: PQuiche_Conn; const buf: PByte; buf_len: CSize_t;
  140.                   const info: PQuiche_Recv_Info): CSsize_t; cdecl; external QUICHE_LIB;
  141.  
  142. begin
  143.   writeln('Quiche version: ', quiche_version); //returns: 0.22.0
  144.   writeln('BoringSSL version: ', Format('%d.%d.%d.%d',
  145.                  [(OpenSSL_version_num shr 28) and $F,
  146.                   (OpenSSL_version_num shr 20) and $F,
  147.                   (OpenSSL_version_num shr 12) and $F,
  148.                   (OpenSSL_version_num shr 4) and $F]));  //
  149.  
  150.   Pconfig := quiche_config_new(QUICHE_PROTOCOL_VERSION);
  151.   if Pconfig = nil then writeln('Failed to create QUIC configuration')
  152.                    else writeln('QUIC configuration CREATED');
  153.  
  154.   if quiche_config_load_cert_chain_from_pem_file(PConfig^, './keys/server.cert') = 0
  155.      then writeln('Cert OK') else writeln('Cert ERROR');
  156.   if quiche_config_load_priv_key_from_pem_file(PConfig^, './keys/server.key') = 0
  157.      then writeln('Priv_key OK') else writeln('Priv_key ERROR');
  158.  
  159.   method := TLS_method;
  160.   Pctx := SSL_CTX_new(method);
  161.   if Pctx = nil then Writeln('Failed to create SSL_CTX') else Writeln('SSL_CTX CREATED!');
  162.   SSL_CTX_set_min_proto_version(Pctx, TLS1_3_VERSION);
  163.   SSL_CTX_set_max_proto_version(Pctx, TLS1_3_VERSION);
  164.   if SSL_CTX_use_certificate_file(Pctx, './keys/server.cert', SSL_FILETYPE_PEM) <= 0
  165.      then Writeln('SSL_CTX_use_certificate_file ERROR!')
  166.      else writeln('SSL_CTX_use_certificate_file OK!');
  167.   if SSL_CTX_use_PrivateKey_file(Pctx, './keys/server.key', SSL_FILETYPE_PEM) <= 0
  168.      then Writeln('SSL_CTX_use_PrivateKey_file ERROR!')
  169.      else Writeln('SSL_CTX_use_PrivateKey_file OK!');
  170.   if SSL_CTX_check_private_key(Pctx) <= 0 then Writeln('Private key DOES NOT match certificate')
  171.                                           else Writeln('Private key MATCHES certificate');
  172.   SSL_CTX_set_options(Pctx, SSL_OP_NO_RENEGOTIATION);
  173.   SSL := SSL_new(Pctx);
  174.   if SSL = nil then writeln('SSL_new ERROR')
  175.                  else writeln('SSL_new OK ');
  176.  
  177.  
  178.   Randomize;
  179.   for i := 0 to High(scid) do scid[i] := Random(256);
  180.   for i := 0 to High(odcid) do odcid[i] := Random(256);
  181.   FillChar(localAddr, SizeOf(localAddr), 0);
  182.   localAddr.sin_family := AF_INET;
  183.   localAddr.sin_port := htons(12345);
  184.   localAddr.sin_addr := StrToNetAddr('192.168.1.4');
  185.   FillChar(peerAddr, SizeOf(peerAddr), 0);
  186.   peerAddr.sin_family := AF_INET;
  187.   peerAddr.sin_port := htons(0);
  188.   peerAddr.sin_addr.s_addr := INADDR_ANY;
  189.  
  190.   Pconn := quiche_conn_new_with_tls(@scid[0], Length(scid),
  191.                                     @odcid[0], Length(odcid),
  192.                                     @localAddr, SizeOf(localAddr),
  193.                                     @peerAddr, SizeOf(peerAddr),
  194.                                     Pconfig, SSL, isServer);
  195.   Writeln('--------------------------------------------------');
  196.   if Pconn = nil then WriteLn('Failed to create QUIC connection')
  197.                  else WriteLn('QUIC connection created successfully!');
  198.   if quiche_conn_is_established(Pconn)
  199.      then WriteLn('QUIC connection IS established')
  200.      else WriteLn('QUIC connection ISN''T established');
  201.   if quiche_conn_is_closed(Pconn)
  202.        then Writeln('Conn CLOSED')
  203.        else Writeln('Conn NOT CLOSED');
  204.  
  205.   quiche_conn_source_id(PConn, trace_id, trace_id_len);
  206.   if (trace_id <> nil) and (trace_id_len > 0) then
  207.     begin
  208.       Writeln('Trace ID Length: ', trace_id_len);
  209.       Write('Trace ID: ');
  210.       for i := 0 to trace_id_len - 1 do Write(trace_id[i], ' ');
  211.       Writeln;
  212.     end else writeln('  trace_id = nil');
  213.  
  214.   writeln('SizeOf(recv_info)=',SizeOf(recv_info));
  215.   quiche_conn_stats(Pconn, PStats);
  216.  
  217.   GetMem(buf, BUF_SIZE);
  218. //  FillChar(recv_info, SizeOf(recv_info), 0);
  219.  
  220.    writeln(' avant quiche_conn_set_session');
  221.  
  222. //  Writeln(quiche_conn_set_session(PConn, @buf, BUF_SIZE));
  223.  
  224.   FillChar(localAddr, SizeOf(localAddr), 0);
  225.   localAddr.sin_family := AF_INET;
  226.   localAddr.sin_port := htons(12345);
  227.   localAddr.sin_addr := StrToNetAddr('192.168.1.4');
  228.   FillChar(peerAddr, SizeOf(peerAddr), 0);
  229.   peerAddr.sin_family := AF_INET;
  230.   peerAddr.sin_port := htons(0);
  231.   peerAddr.sin_addr.s_addr := INADDR_ANY;
  232.  
  233.   recv_info.from := @localAddr;;
  234.   recv_info.from_len := SizeOf(localAddr);
  235.   recv_info.&to := @peerAddr;;
  236.   recv_info.to_len := SizeOf(peerAddr);
  237.  
  238.   Precv_info := @recv_info;  
  239.  
  240.   Writeln;
  241.   repeat
  242.     Writeln('bytes_received...');
  243.     {TRon} //bytes_received := quiche_conn_recv(PConn, buf, BUF_SIZE, Precv_info);
  244.    // {Fred} bytes_received := quiche_conn_recv(PConn, buf, BUF_SIZE, recv_info);
  245.     {Fred2}bytes_received := quiche_conn_recv(PConn, buf, BUF_SIZE, Precv_info);
  246.     Writeln('bytes_received = ' + inttostr(bytes_received));
  247.     Sleep(10);
  248.     if KeyPressed then ch := ReadKey;
  249.   until Ch = #13;
  250.  
  251.   //TODO: cleaning
  252.  
  253. end.
  254.  
  255.  

and here the result:
Code: Text  [Select][+][-]
  1. Quiche version: 0.22.0
  2. BoringSSL version: 1.1.1.7
  3. QUIC configuration CREATED
  4. Cert OK
  5. Priv_key OK
  6. SSL_CTX CREATED!
  7. SSL_CTX_use_certificate_file OK!
  8. SSL_CTX_use_PrivateKey_file OK!
  9. Private key MATCHES certificate
  10. SSL_new OK
  11. --------------------------------------------------
  12. QUIC connection created successfully!
  13. QUIC connection ISN'T established
  14. Conn NOT CLOSED
  15. Trace ID Length: 16
  16. Trace ID: 205 247 189 17 202 103 191 159 132 79 54 168 8 133 19 96
  17. SizeOf(recv_info)=32
  18.  avant quiche_conn_set_session
  19.  
  20. bytes_received...
  21. bytes_received = -3
  22. bytes_received...
  23. bytes_received = 32768
  24. bytes_received...
  25. bytes_received = 32768
  26. bytes_received...
  27. ...

[EDIT] Ha, yes, I think I understood what you mean.
           Indeed strange that QUIC connection ISN'T established
           Also it seems to me that the buffer is filled very fast at each loop.

« Last Edit: October 09, 2024, 03:13:25 am 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

 

TinyPortal © 2005-2018