Skip to content

Commit 31e19b6

Browse files
committed
in progress
1 parent 845fbd3 commit 31e19b6

20 files changed

+350
-82
lines changed

Makefile

+4-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ clone:
1717
lsquic:
1818
cd lsquic && cmake -DAS_SHARED_LIB=1 -DBORINGSSL_DIR=../boringssl . && make -j $(shell nproc)
1919

20-
ffi: src/ffi.lisp src/ffi-dns.lisp src/ffi-udp.lisp
20+
ffi: src/ffi.lisp src/ffi-dns.lisp src/ffi-udp.lisp src/ffi-lsquic-helpers.lisp
2121

2222
src/ffi.lisp: lsquic.i Makefile
2323
swig -cffi -module ffi -outdir src/lsquic/ -I./lsquic/include/ lsquic.i
@@ -28,5 +28,8 @@ src/ffi-dns.lisp: dns.i Makefile c-src/libdns.so
2828
src/ffi-udp.lisp: udp.i Makefile c-src/libudp.so
2929
swig -cffi -module ffi -outdir src/udp -I./c-src udp.i
3030

31+
src/ffi-lsquic-helpers.lisp: lsquic-helpers.i Makefile c-src/liblsquic-helpers.so
32+
swig -cffi -module ffi-helpers -outdir src/lsquic -I./c-src lsquic-helpers.i
33+
3134
test:
3235
@$(LISP) $($(LISP)_TEST_OPTS)

c-src/#lsquic-helpers.c#

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#include <stdlib.h>
2+
#include <stdio.h>
3+
#include "../lsquic/include/lsxpack_header.h"
4+
#include "../lsquic/include/lsquic_types.h"
5+
#include "../lsquic/include/lsquic.h"
6+
#include <stdlib.h>
7+
8+
void view_header(struct lsquic_http_headers *hdr) {
9+
unsigned char *p = (unsigned char *)hdr;
10+
printf("Header count: %d\n", hdr->count);
11+
for (int x = 0; x < hdr->count; x++) {
12+
printf("BufP: %p\n", hdr->headers[x].buf);
13+
printf("BufS: %s\n", hdr->headers[x].buf);
14+
printf("NameLen: %d\n", hdr->headers[x].name_len);
15+
printf("NameOff: %d\n", hdr->headers[x].name_offset);
16+
printf("ValLen: %d\n", hdr->headers[x].val_len);
17+
printf("ValOff: %d\n", hdr->headers[x].val_offset);
18+
for (int i = 0; i < sizeof(struct lsxpack_header); i++) {
19+
printf("0x%02x ", p[i]);
20+
}
21+
printf("\n");
22+
}
23+
}
24+
25+
void view_lsxpack_header(struct lsxpack_header *hdr) {
26+
unsigned char *p = (unsigned char*) hdr;
27+
printf("New lsxpack header:\n");
28+
printf("Buf: %p\n", hdr->buf);
29+
printf("Buf: %s\n", hdr->buf);
30+
printf("Buf: %d\n", hdr->name_len);
31+
printf("Buf: %d\n", hdr->name_offset);
32+
printf("Buf: %d\n", hdr->val_len);
33+
printf("Buf: %d\n", hdr->val_offset);
34+
printf("Buf: %d\n", hdr->buf == hdr);
35+
for (int i = 0; i < sizeof(struct lsxpack_header); i++)
36+
printf("0x%02x ", p[i]);
37+
printf("\n");
38+
39+
}

c-src/Makefile

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
all: dns.so udp.so
1+
all: dns.so udp.so lsquic-helpers.so
22

33
dns.so:
44
gcc -c -Wall -fpic dns.c
@@ -7,3 +7,7 @@ dns.so:
77
udp.so:
88
gcc -I../lsquic/include -c -Wall -fpic udp.c
99
gcc -shared -o libudp.so udp.o
10+
11+
lsquic-helpers.so:
12+
gcc -I../lsquic/include -c -Wall -fpic lsquic-helpers.c
13+
gcc -shared -o liblsquic-helpers.so lsquic-helpers.o

c-src/lsquic-helpers.c

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#include <stdlib.h>
2+
#include <stdio.h>
3+
#include "../lsquic/include/lsxpack_header.h"
4+
#include "../lsquic/include/lsquic_types.h"
5+
#include "../lsquic/include/lsquic.h"
6+
#include <stdlib.h>
7+
8+
void view_header(struct lsquic_http_headers *hdr) {
9+
unsigned char *p = (unsigned char *)hdr;
10+
printf("Header count: %d\n", hdr->count);
11+
for (int x = 0; x < hdr->count; x++) {
12+
printf("BufP: %p\n", hdr->headers[x].buf);
13+
printf("BufS: %s\n", hdr->headers[x].buf);
14+
printf("NameLen: %d\n", hdr->headers[x].name_len);
15+
printf("NameOff: %d\n", hdr->headers[x].name_offset);
16+
printf("ValLen: %d\n", hdr->headers[x].val_len);
17+
printf("ValOff: %d\n", hdr->headers[x].val_offset);
18+
for (int i = 0; i < sizeof(struct lsxpack_header); i++) {
19+
printf("0x%02x ", p[i]);
20+
}
21+
printf("\n");
22+
}
23+
}
24+
25+
void view_lsxpack_header(struct lsxpack_header *hdr) {
26+
unsigned char *p = (unsigned char*) hdr;
27+
printf("New lsxpack header:\n");
28+
printf("Buf: %p\n", hdr->buf);
29+
printf("Buf: %s\n", hdr->buf);
30+
printf("Buf: %d\n", hdr->name_len);
31+
printf("Buf: %d\n", hdr->name_offset);
32+
printf("Buf: %d\n", hdr->val_len);
33+
printf("Buf: %d\n", hdr->val_offset);
34+
printf("Buf: %d\n", hdr->buf == hdr);
35+
for (int i = 0; i < sizeof(struct lsxpack_header); i++)
36+
printf("0x%02x ", p[i]);
37+
printf("\n");
38+
39+
}

c-src/lsquic-helpers.h

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#include "../lsquic/include/lsxpack_header.h"
2+
#include "../lsquic/include/lsquic_types.h"
3+
#include "../lsquic/include/lsquic.h"
4+
5+
6+
struct lsquic_http_headers *new_headers(size_t header_count);
7+
void add_header(const char *name, const char *val);
8+
void view_header(struct lsquic_http_headers *hdr);
9+
void view_lsxpack_header(struct lsxpack_header *hdr);

lsquic

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
Subproject commit 48a85d6802b9ef00da84ba8c35149554dd1e8c03
1+
Subproject commit c163433496b0a8fd2f2df0cec84dd4445517f3d5

lsquic-helpers.i

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
%feature("intern_function", "chomp-lsquic");
2+
%feature("export");
3+
%typemap(cin) int * "(:pointer :int)";
4+
%typemap(cin) size_t ":unsigned-int";
5+
%typemap(cin) lsxpack_strlen_t ":uint16";
6+
%typemap(cin) uint32_t ":uint32";
7+
%typemap(cin) uint16_t ":uint16";
8+
%typemap(cin) uint8_t ":uint8";
9+
10+
%insert("lisphead") %{
11+
(in-package :lsquic)
12+
%}
13+
%include "lsquic-helpers.h"

lsquic.i

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
%feature("export");
33
%typemap(cin) int * "(:pointer :int)";
44
%typemap(cin) size_t ":unsigned-int";
5-
%typemap(cin) lsxpack_strlen_t ":unsigned-int";
5+
%typemap(cin) lsxpack_strlen_t ":uint16";
66
%typemap(cin) uint32_t ":uint32";
77
%typemap(cin) uint16_t ":uint16";
88
%typemap(cin) uint8_t ":uint8";

src/cffi-helpers/tyranny.lisp

+16-4
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#:with-pointer-to
66
#:with-pointer-to-int
77
#:safe-foreign-alloc
8+
#:with-initialize-foreign-struct-with-fill
89
#:with-initialize-foreign-struct))
910

1011
(in-package :cffi-helpers)
@@ -31,13 +32,24 @@
3132
`(with-pointer-to (,binding-form :int)
3233
,@body))
3334

34-
(defun safe-foreign-alloc (type &key (count 1))
35-
(static-vectors:fill-foreign-memory (cffi:foreign-alloc type :count count) (cffi:foreign-type-size type) 0))
35+
(defun safe-foreign-alloc (type &key (count 1) (fill-value 0))
36+
(static-vectors:fill-foreign-memory
37+
(cffi:foreign-alloc type :count count)
38+
(* count (cffi:foreign-type-size type))
39+
fill-value))
3640

3741
(defmacro with-initialize-foreign-struct (type &body body)
3842
(let* ((struct-field-names (cffi:foreign-slot-names (list :struct type)))
3943
(struct-sym (gensym)))
4044
`(let ((,struct-sym (safe-foreign-alloc (list :struct ',type))))
4145
(cffi:with-foreign-slots (,struct-field-names ,struct-sym (:struct ,type))
42-
(progn ,@body)
43-
,struct-sym))))
46+
(progn ,@body))
47+
,struct-sym)))
48+
49+
(defmacro with-initialize-foreign-struct-with-fill (type fill-value &body body)
50+
(let* ((struct-field-names (cffi:foreign-slot-names (list :struct type)))
51+
(struct-sym (gensym)))
52+
`(let ((,struct-sym (safe-foreign-alloc (list :struct ',type) :fill-value ,fill-value)))
53+
(cffi:with-foreign-slots (,struct-field-names ,struct-sym (:struct ,type))
54+
(progn ,@body))
55+
,struct-sym)))

src/http3/http3.lisp

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
:verb verb
3232
:body body))
3333
(pipe (lsquic:new-stream client request)))
34-
(wait-for-response pipe)))
34+
(lsquic:wait-for-response pipe)))
3535

3636
(defmacro request-no-body (verb)
3737
(let* ((sym-name (symbol-name verb))

src/http3/test/tests.lisp

+15-9
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,19 @@
55
(def-suite :http3)
66
(in-suite :http3)
77

8-
(test can-create-request
9-
(let ((request (make-instance 'lsquic:request :authority "google.com")))
10-
(is (lsquic:lsxpack-headers request))))
8+
;; (test can-create-request
9+
;; (let ((request (make-instance 'lsquic:request :authority "google.com")))
10+
;; (is (lsquic:lsxpack-headers request))))
11+
12+
;; (test can-make-network-request
13+
;; (let ((client (make-instance 'lsquic:client
14+
;; :quic-version "Q050"
15+
;; :host "google.com")))
16+
;; (http3:connect client)
17+
;; (http3:make-get-request client :path "/")))
1118

12-
(test can-make-network-request
13-
(let ((request (make-instance 'lsquic:request :authority "google.com"))
14-
(client (make-instance 'lsquic:client
15-
:quic-version "Q050"
16-
:host "google.com")))
17-
(http3:connect client)))
19+
20+
(test header-packer
21+
(let ((request (make-instance 'lsquic:request :authority "google.com")))
22+
(lsquic:lsxpack-headers request)
23+
(is (eq 1 0))))

src/lsquic/#ttt.lisp#

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
(in-package :lsquic)
2+
3+
4+
(cffi:defcstruct swb
5+
(buf :pointer))
6+
7+
(cffi:defcstruct sswb
8+
(st :pointer))
9+
10+
11+
(defun make-swb ()
12+
(let ((s (foreign-alloc '(:struct swb))))
13+
(with-foreign-slots ((buf) s (:struct swb))
14+
(setf buf (cffi:foreign-string-alloc "Hello there")))
15+
s))
16+
17+
(defun print-swb (s)
18+
(with-foreign-slots ((buf) s (:struct swb))
19+
(cffi:foreign-funcall "printf" :string "%s\n" :pointer buf)
20+
(cffi:foreign-funcall "fflush" :int 0)))
21+
22+
(defun make-sswb (n)
23+
(let* ((sw (foreign-alloc '(:struct sswb)))
24+
(swbs (loop for i upto n collect (make-swb)))
25+
(pswbs (cffi:foreign-alloc :pointer :initial-contents swbs)))
26+
(with-foreign-slots ((st) sw (:struct sswb))
27+
(setf st pswbs))
28+
(with-foreign-slots ((st) sw (:struct sswb))
29+
(dotimes (i n)
30+
(print-swb (mem-aref st :pointer n))))))
31+

src/lsquic/ffi-helpers.lisp

+80
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
;;; This file was automatically generated by SWIG (http://www.swig.org).
2+
;;; Version 3.0.12
3+
;;;
4+
;;; Do not make changes to this file unless you know what you are doing--modify
5+
;;; the SWIG interface file instead.
6+
7+
(in-package :lsquic)
8+
9+
10+
11+
;;;SWIG wrapper code starts here
12+
13+
(cl:defmacro defanonenum (cl:&body enums)
14+
"Converts anonymous enums to defconstants."
15+
`(cl:progn ,@(cl:loop for value in enums
16+
for index = 0 then (cl:1+ index)
17+
when (cl:listp value) do (cl:setf index (cl:second value)
18+
value (cl:first value))
19+
collect `(cl:defconstant ,value ,index))))
20+
21+
(cl:eval-when (:compile-toplevel :load-toplevel)
22+
(cl:unless (cl:fboundp 'swig-lispify)
23+
(cl:defun swig-lispify (name flag cl:&optional (package cl:*package*))
24+
(cl:labels ((helper (lst last rest cl:&aux (c (cl:car lst)))
25+
(cl:cond
26+
((cl:null lst)
27+
rest)
28+
((cl:upper-case-p c)
29+
(helper (cl:cdr lst) 'upper
30+
(cl:case last
31+
((lower digit) (cl:list* c #\- rest))
32+
(cl:t (cl:cons c rest)))))
33+
((cl:lower-case-p c)
34+
(helper (cl:cdr lst) 'lower (cl:cons (cl:char-upcase c) rest)))
35+
((cl:digit-char-p c)
36+
(helper (cl:cdr lst) 'digit
37+
(cl:case last
38+
((upper lower) (cl:list* c #\- rest))
39+
(cl:t (cl:cons c rest)))))
40+
((cl:char-equal c #\_)
41+
(helper (cl:cdr lst) '_ (cl:cons #\- rest)))
42+
(cl:t
43+
(cl:error "Invalid character: ~A" c)))))
44+
(cl:let ((fix (cl:case flag
45+
((constant enumvalue) "+")
46+
(variable "*")
47+
(cl:t ""))))
48+
(cl:intern
49+
(cl:concatenate
50+
'cl:string
51+
fix
52+
(cl:nreverse (helper (cl:concatenate 'cl:list name) cl:nil cl:nil))
53+
fix)
54+
package))))))
55+
56+
;;;SWIG wrapper code ends here
57+
58+
59+
(cffi:defcfun ("new_headers" #.(chomp-lsquic "new_headers" 'function)) :pointer
60+
(header_count :unsigned-int))
61+
62+
(cl:export '#.(chomp-lsquic "new_headers" 'function))
63+
64+
(cffi:defcfun ("add_header" #.(chomp-lsquic "add_header" 'function)) :void
65+
(name :string)
66+
(val :string))
67+
68+
(cl:export '#.(chomp-lsquic "add_header" 'function))
69+
70+
(cffi:defcfun ("view_header" #.(chomp-lsquic "view_header" 'function)) :void
71+
(hdr :pointer))
72+
73+
(cl:export '#.(chomp-lsquic "view_header" 'function))
74+
75+
(cffi:defcfun ("view_lsxpack_header" #.(chomp-lsquic "view_lsxpack_header" 'function)) :void
76+
(hdr :pointer))
77+
78+
(cl:export '#.(chomp-lsquic "view_lsxpack_header" 'function))
79+
80+

src/lsquic/ffi.lisp

+6-14
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
((cl:lower-case-p c)
3434
(helper (cl:cdr lst) 'lower (cl:cons (cl:char-upcase c) rest)))
3535
((cl:digit-char-p c)
36-
(helper (cl:cdr lst) 'digit
36+
(helper (cl:cdr lst) 'digit
3737
(cl:case last
3838
((upper lower) (cl:list* c #\- rest))
3939
(cl:t (cl:cons c rest)))))
@@ -92,7 +92,7 @@
9292

9393
(cl:export '#.(chomp-lsquic "LSQUIC_MINOR_VERSION" 'constant))
9494

95-
(cl:defconstant #.(chomp-lsquic "LSQUIC_PATCH_VERSION" 'constant) 10)
95+
(cl:defconstant #.(chomp-lsquic "LSQUIC_PATCH_VERSION" 'constant) 8)
9696

9797
(cl:export '#.(chomp-lsquic "LSQUIC_PATCH_VERSION" 'constant))
9898

@@ -861,14 +861,6 @@
861861

862862
(cl:export '#.(chomp-lsquic "lsquic_stream_writev" 'function))
863863

864-
(cffi:defcfun ("lsquic_stream_pwritev" #.(chomp-lsquic "lsquic_stream_pwritev" 'function)) :pointer
865-
(s :pointer)
866-
(preadv :pointer)
867-
(user_data :pointer)
868-
(n_to_write :pointer))
869-
870-
(cl:export '#.(chomp-lsquic "lsquic_stream_pwritev" 'function))
871-
872864
(cffi:defcstruct #.(chomp-lsquic "lsquic_reader" 'classname)
873865
(#.(chomp-lsquic "lsqr_read" 'slotname) :pointer)
874866
(#.(chomp-lsquic "lsqr_size" 'slotname) :pointer)
@@ -1213,10 +1205,10 @@
12131205
(#.(chomp-lsquic "buf" 'slotname) :string)
12141206
(#.(chomp-lsquic "name_hash" 'slotname) :uint32)
12151207
(#.(chomp-lsquic "nameval_hash" 'slotname) :uint32)
1216-
(#.(chomp-lsquic "name_offset" 'slotname) :unsigned-int)
1217-
(#.(chomp-lsquic "name_len" 'slotname) :unsigned-int)
1218-
(#.(chomp-lsquic "val_offset" 'slotname) :unsigned-int)
1219-
(#.(chomp-lsquic "val_len" 'slotname) :unsigned-int)
1208+
(#.(chomp-lsquic "name_offset" 'slotname) :uint16)
1209+
(#.(chomp-lsquic "name_len" 'slotname) :uint16)
1210+
(#.(chomp-lsquic "val_offset" 'slotname) :uint16)
1211+
(#.(chomp-lsquic "val_len" 'slotname) :uint16)
12201212
(#.(chomp-lsquic "chain_next_idx" 'slotname) :uint16)
12211213
(#.(chomp-lsquic "hpack_index" 'slotname) :uint8)
12221214
(#.(chomp-lsquic "qpack_index" 'slotname) :uint8)

src/lsquic/io.lisp

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,5 @@
1414
(sb-posix:close (input pipe))
1515
(sb-posix:close (output pipe)))
1616

17-
(defcallback write-pipe :void ((ctx :pointer) (buf :pointer) (len :int) (fin :int))
18-
(sb-posix:write (output (weird-pointers:restore ctx)) buf len))
17+
(defmethod wait-for-response ((pipe pipe))
18+
(sleep 60))

src/lsquic/lsquic.asd

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
:description "FFI Bindings to litespeed QUIC"
77
:components ((:file "package")
88
(:file "ffi")
9+
(:file "ffi-helpers")
910
(:file "logging")
1011
(:file "sockets")
1112
(:file "lsquic")

0 commit comments

Comments
 (0)