[nio-cvs] r34 - branches/home/psmith/restructure/src/io

psmith at common-lisp.net psmith at common-lisp.net
Mon Jan 15 02:51:30 UTC 2007


Author: psmith
Date: Sun Jan 14 21:51:30 2007
New Revision: 34

Modified:
   branches/home/psmith/restructure/src/io/async-socket.lisp
Log:
Corrected socketaddr-in structure for linux
Added connect functionalilty



Modified: branches/home/psmith/restructure/src/io/async-socket.lisp
==============================================================================
--- branches/home/psmith/restructure/src/io/async-socket.lisp	(original)
+++ branches/home/psmith/restructure/src/io/async-socket.lisp	Sun Jan 14 21:51:30 2007
@@ -36,6 +36,7 @@
 (defconstant +sock-stream+ 1)
 (defconstant +sock-dgram+ 2)
 
+#+(or darwin macosx freebsd)
 (defcstruct sockaddr-in
   (len :uint8)
   (family :uint8)
@@ -43,6 +44,13 @@
   (addr :uint32)
   (zero :char :count 8))
 
+#+linux
+(defcstruct sockaddr-in
+  (family :uint16)
+  (port :uint16)
+  (addr :uint32)
+  (zero :char :count 8))
+
 (defconstant +sockaddr-in-len+ #.(+ 1 1 2 4 8))
 
 (defcstruct sockaddr-in6
@@ -82,6 +90,12 @@
   (sockaddr :pointer)
   (socklen :pointer))
 
+(defcfun ("connect" %connect) :int
+  (socket :int)
+  (sockaddr :pointer)
+  (socklent :int))
+
+
 ;;TODO put backlog on config
 (defun start-listen (socket-fd &optional (backlog 1000))
   (%listen socket-fd backlog))
@@ -92,25 +106,42 @@
 (defun make-inet-socket (&optional (type :tcp))
   (%socket +af-inet+ (ecase type (:tcp +sock-stream+) (:udp +sock-dgram+)) 0))
 
-(defun bind-inet-socket (socket-fd port &optional (addr "127.0.0.1"))
-  (with-foreign-object (sa 'sockaddr-in)
+
+(defun init-inet-socket(sa port addr)
 
     (memzero sa +sockaddr-in-len+)
 
     ;; init struct
-    (setf (foreign-slot-value sa 'sockaddr-in 'len) +sockaddr-in-len+
-	  (foreign-slot-value sa 'sockaddr-in 'port) (%htons port)
+    #+(or darwin macosx freebsd)
+    (setf (foreign-slot-value sa 'sockaddr-in 'len) +sockaddr-in-len+)
+
+    (setf (foreign-slot-value sa 'sockaddr-in 'port) (%htons port)
 	  (foreign-slot-value sa 'sockaddr-in 'family) +af-inet+)
 
     ;; set addr
     (if (/= (%inet-pton +af-inet+ addr (foreign-slot-pointer sa 'sockaddr-in 'addr)) 1)
-	(error "inet_pton: Bad address ~A!" addr))
+	(error "inet_pton: Bad address ~A!" addr)))
+
+
+(defun bind-inet-socket (socket-fd port &optional (addr "127.0.0.1"))
+  (with-foreign-object (sa 'sockaddr-in)
+    (init-inet-socket sa port addr)
 
     ;; bind
     (if (= (%bind socket-fd sa +sockaddr-in-len+) 0)
 	t
 	nil)))
 
+(defun connect-inet-socket (socket-fd addr port)
+  (with-foreign-object (sa 'sockaddr-in)
+    (init-inet-socket sa port addr)
+
+    (let ((res (%connect socket-fd sa +sockaddr-in-len+)))
+      (format t "connect ~A ~A~%" res (get-errno))
+      (if (= res -1)
+      	  nil
+      	  t))))
+
 
 ;;;; IPv6
 
@@ -146,6 +177,7 @@
    (remote-port :initform nil :initarg :remote-port)))
 
 
+
 (defun socket-accept (socket-fd connection-type)
   "Accept connection from SOCKET-FD. Allocates and returns socket structure denoting the connection."
 



More information about the Nio-cvs mailing list