From daf57c0410b3a67a3045d407d0d65e30841d4d88 Mon Sep 17 00:00:00 2001 From: Jesse Engle Date: Mon, 4 Sep 2023 09:16:47 -0400 Subject: [PATCH] Adds socket family and datagram functions for communicating via netlink protocol. --- cffi-socket.lisp | 40 ++++++++++++++++++++++++++++++++++++++++ grovel-socket.lisp | 13 +++++++------ package.lisp | 5 +++++ 3 files changed, 52 insertions(+), 6 deletions(-) diff --git a/cffi-socket.lisp b/cffi-socket.lisp index e7edf10..4aa4a77 100644 --- a/cffi-socket.lisp +++ b/cffi-socket.lisp @@ -206,6 +206,26 @@ (error-errno "recv")) r)))) +(defcfun ("recvfrom" c-recvfrom) ssize-t + (sockfd :int) + (buf :pointer) + (len size-t) + (flags :int) + (src-addr (:pointer (:struct sockaddr))) + (addrlen (:pointer socklen-t))) + +(defun recv-from (sockfd buffer flags &key (srcaddr (null-pointer)) (addrlen (null-pointer)) (start 0) (end (length buffer))) + (declare (type (array (unsigned-byte 8)) buffer)) + (let ((len (- end start))) + (with-foreign-pointer (buf len) + (let ((r (c-recvfrom sockfd buf len flags srcaddr addrlen))) + (dotimes (i len) + (setf (aref buffer start) (mem-aref buf :unsigned-char i)) + (incf start)) + (when (< r 0) + (error-errno "recv-from")) + r)))) + (defun recv-sequence (sockfd buffer flags &key (start 0) (end (length buffer))) (loop (unless (< start end) @@ -242,6 +262,26 @@ (error "end of file")) (incf start r)))) +(defcfun ("sendto" c-sendto) ssize-t + (sockfd :int) + (buf :pointer) + (len size-t) + (flags :int) + (dest-addr (:pointer (:struct sockaddr))) + (addrlen socklen-t)) + +(defun send-to (sockfd buffer flags dstaddr addrlen &key (start 0) (end (length buffer))) + (declare (type (array (unsigned-byte 8)) buffer)) + (let ((len (- end start))) + (with-foreign-pointer (buf len) + (let ((r (c-sendto sockfd buf len flags dstaddr addrlen))) + (dotimes (i len) + (setf (aref buffer start) (mem-aref buf :unsigned-char i)) + (incf start)) + (when (< r 0) + (error-errno "send-to")) + r)))) + (defcfun ("shutdown" c-shutdown) :int (sockfd :int) (how :int)) diff --git a/grovel-socket.lisp b/grovel-socket.lisp index 24a7d6c..481d200 100644 --- a/grovel-socket.lisp +++ b/grovel-socket.lisp @@ -23,12 +23,13 @@ (include "sys/types.h") (include "sys/socket.h") -(constant (+af-unix+ "AF_UNIX")) -(constant (+af-local+ "AF_LOCAL")) -(constant (+af-inet+ "AF_INET")) -(constant (+af-inet6+ "AF_INET6")) -(constant (+af-ipx+ "AF_IPX")) -(constant (+af-packet+ "AF_PACKET")) +(constant (+af-unix+ "AF_UNIX")) +(constant (+af-local+ "AF_LOCAL")) +(constant (+af-inet+ "AF_INET")) +(constant (+af-inet6+ "AF_INET6")) +(constant (+af-ipx+ "AF_IPX")) +(constant (+af-packet+ "AF_PACKET")) +(constant (+af-netlink+ "AF_NETLINK")) (constant (+sock-stream+ "SOCK_STREAM")) (constant (+sock-dgram+ "SOCK_DGRAM")) diff --git a/package.lisp b/package.lisp index 83a74b5..c6bad92 100644 --- a/package.lisp +++ b/package.lisp @@ -33,6 +33,7 @@ #:+af-local+ #:+af-packet+ #:+af-unix+ + #:+af-netlink+ #:+shut-rd+ #:+shut-rdwr+ #:+shut-wr+ @@ -51,7 +52,9 @@ #:c-listen #:c-ntohs #:c-recv + #:c-recvfrom #:c-send + #:c-sendto #:c-shutdown #:c-socket #:connect @@ -62,8 +65,10 @@ #:listen #:ntohs #:recv + #:recv-from #:recv-sequence #:send + #:send-to #:send-sequence #:shutdown #:sockaddr-to-string