A first step to query UDP

This commit is contained in:
Quentin 2020-07-09 15:59:25 +02:00
parent 6f49424fb0
commit 757656573a
2 changed files with 68 additions and 5 deletions

View file

@ -63,11 +63,19 @@
(domain->int af)
src dst))
(define (htons host)
(define (inet_ntop af src dst size)
((foreign-procedure
"htons"
(short)
short) host))
"inet_ntop"
(int void* (* char) int)
void*)
(domain->int af)
src dst size))
(define (htons host)
((foreign-procedure "htons" (short) short) host))
(define (ntohs net)
((foreign-procedure "ntohs" (short) short) net))
(define (setsockopt sockfd level optname optval optlen)
((foreign-procedure
@ -85,4 +93,25 @@
(int void* int)
int) sockfd address address_len))
(define (listen sockfd backlog)
((foreign-procedure
"listen"
(int int)
int) sockfd backlog))
(define (msgflag->int flag)
(case flag
((MSG_DEFAULT) 0)
))
(define (recvfrom sockfd buf len msgflag src-addr addrlen)
((foreign-procedure
"recvfrom"
(int void* int int (* sockaddr_in) (* int))
int)
sockfd
buf
len
(msgflag->int msgflag)
src-addr
addrlen))

View file

@ -51,8 +51,42 @@
(ftype-pointer-address addr)
(ftype-sizeof sockaddr_in))))))
(define (udpsock-readblock sock fx)
(let*
([bufsize 1500] [straddrsize 255]
[buf (foreign-alloc bufsize)]
[straddr (make-ftype-pointer
char
(foreign-alloc straddrsize))]
[addrlen (make-ftype-pointer
int
(foreign-alloc (ftype-sizeof int)))]
[addr (make-ftype-pointer
sockaddr_in
(foreign-alloc (ftype-sizeof sockaddr_in)))]
[nread (recvfrom sock buf bufsize 'MSG_DEFAULT addr addrlen)])
; FIXME add a loop and check fx output to know if we continue
(fx
(inet_ntop
'AF_INET
(ftype-pointer-address
(ftype-&ref sockaddr_in (sin_addr) addr))
straddr
straddrsize)
(ntohs (ftype-ref sockaddr_in (sin_port) addr))
buf nread)
(foreign-free (ftype-pointer-address straddr))
(foreign-free (ftype-pointer-address addrlen))
(foreign-free (ftype-pointer-address addr))
(foreign-free buf)))
(udpsock-create
(lambda (sock)
(udpsock-reuseaddr sock)
(udpsock-bind sock "0.0.0.0" 1337)
(printf "~a~%" sock)))
(udpsock-readblock
sock
(lambda (host port buf size)
))))