2020-07-09 08:03:55 +00:00
|
|
|
(load "../bindings/libc.scm")
|
2020-07-09 14:20:59 +00:00
|
|
|
(load "../bindings/utils.scm")
|
2020-07-09 10:51:53 +00:00
|
|
|
|
2020-07-09 10:15:00 +00:00
|
|
|
(define (udpsock-create fx)
|
2020-07-09 08:03:55 +00:00
|
|
|
(fx
|
|
|
|
(check-err
|
2020-07-09 10:12:38 +00:00
|
|
|
(socket 'AF_INET 'SOCK_DGRAM 'IPPROTO_IP)
|
2020-07-09 08:03:55 +00:00
|
|
|
"Unable to init UDP socket")))
|
|
|
|
|
2020-07-09 10:15:00 +00:00
|
|
|
(define (udpsock-reuseaddr sock)
|
|
|
|
(alloc
|
|
|
|
(ftype-sizeof int)
|
|
|
|
(lambda (activation)
|
|
|
|
(foreign-set! 'int activation 0 1)
|
|
|
|
(check-err
|
|
|
|
(setsockopt
|
|
|
|
sock
|
|
|
|
'SOL_SOCKET
|
|
|
|
'SO_REUSEADDR
|
|
|
|
activation
|
|
|
|
(ftype-sizeof int))
|
|
|
|
"Unable to set REUSE ADDRESS"))))
|
|
|
|
|
2020-07-09 12:16:50 +00:00
|
|
|
(define (udpsock-bind sock host port)
|
2020-07-09 10:51:53 +00:00
|
|
|
(alloc
|
|
|
|
(ftype-sizeof sockaddr_in)
|
2020-07-09 12:16:50 +00:00
|
|
|
(lambda (raw-addr)
|
|
|
|
(let ([addr (make-ftype-pointer sockaddr_in raw-addr)])
|
|
|
|
(ftype-set! sockaddr_in (sin_family) addr (domain->int 'AF_INET))
|
|
|
|
(ftype-set! sockaddr_in (sin_port) addr (htons port))
|
|
|
|
(check-err
|
|
|
|
(inet_pton
|
|
|
|
'AF_INET
|
|
|
|
host
|
|
|
|
(ftype-pointer-address
|
|
|
|
(ftype-&ref sockaddr_in (sin_addr) addr)))
|
|
|
|
"Unable to convert your IP address to binary")
|
|
|
|
(bind
|
|
|
|
sock
|
|
|
|
(ftype-pointer-address addr)
|
|
|
|
(ftype-sizeof sockaddr_in))))))
|
2020-07-09 10:51:53 +00:00
|
|
|
|
2020-07-09 13:59:25 +00:00
|
|
|
(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)))
|
|
|
|
|
2020-07-09 10:15:00 +00:00
|
|
|
(udpsock-create
|
2020-07-09 10:51:53 +00:00
|
|
|
(lambda (sock)
|
|
|
|
(udpsock-reuseaddr sock)
|
|
|
|
(udpsock-bind sock "0.0.0.0" 1337)
|
2020-07-09 13:59:25 +00:00
|
|
|
(udpsock-readblock
|
|
|
|
sock
|
|
|
|
(lambda (host port buf size)
|
2020-07-09 14:20:59 +00:00
|
|
|
(printf "~a~%" size)
|
|
|
|
(printf "~a~%" port)
|
|
|
|
(printf "~a~%" host)
|
2020-07-09 14:22:41 +00:00
|
|
|
(printf
|
|
|
|
"~a~%"
|
|
|
|
(char*->string
|
|
|
|
(make-ftype-pointer char buf)))
|
2020-07-09 13:59:25 +00:00
|
|
|
))))
|