Skip to content

Adds Netlink protocol support. #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions cffi-socket.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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))
Expand Down
13 changes: 7 additions & 6 deletions grovel-socket.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
Expand Down
5 changes: 5 additions & 0 deletions package.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#:+af-local+
#:+af-packet+
#:+af-unix+
#:+af-netlink+
#:+shut-rd+
#:+shut-rdwr+
#:+shut-wr+
Expand All @@ -51,7 +52,9 @@
#:c-listen
#:c-ntohs
#:c-recv
#:c-recvfrom
#:c-send
#:c-sendto
#:c-shutdown
#:c-socket
#:connect
Expand All @@ -62,8 +65,10 @@
#:listen
#:ntohs
#:recv
#:recv-from
#:recv-sequence
#:send
#:send-to
#:send-sequence
#:shutdown
#:sockaddr-to-string
Expand Down