chicken-users
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Chicken-users] udp.scm: new udp-bound-port


From: Daishi Kato
Subject: [Chicken-users] udp.scm: new udp-bound-port
Date: Fri, 05 Aug 2005 14:15:03 +0900
User-agent: Wanderlust/2.15.1 (Almost Unreal) Emacs/21.4 Mule/5.0 (SAKAKI)

Hi,

Here is a small patch for udp.scm, adding udp-bound-port,
which is especially needed when you pass port=0 in udp-open-socket.

8<------8<------8<------8<------8<------8<------8<------8<------
--- udp.scm.orig        2005-04-12 06:42:39.000000000 +0900
+++ udp.scm     2005-08-05 14:05:02.000000000 +0900
@@ -62,7 +62,7 @@
          io:set-exception-handler!
          udp-socket? udp-bound? udp-connected? udp-open-socket
          udp-open-socket* udp-bind! udp-connect! udp-send udp-sendto
-         udp-recv udp-recvfrom udp-close-socket)
+         udp-recv udp-recvfrom udp-close-socket udp-bound-port)
  (bound-to-procedure
   ##net#socket ##net#bind ##net#connect ##net#close ##net#recv ##net#recvfrom
   ##net#send ##net#sendto ##net#select ##net#gethostaddr ##sys#update-errno
@@ -132,6 +132,13 @@
     "if(val == -1) return(0);"
     "return(fcntl(fd, F_SETFL, val | O_NONBLOCK) != -1);"))
 
+(define ##net#getsockport
+  (foreign-lambda* int ([int s])
+    "struct sockaddr_in sa;"
+    "int len = sizeof(struct sockaddr_in);"
+    "if(getsockname(s, (struct sockaddr *)&sa, (socklen_t *)&len) != 0) 
return(-1);"
+    "else return(ntohs(sa.sin_port));") )
+
 (define ##net#gethostaddr
   (foreign-lambda* bool ((pointer saddr) (c-string host) (unsigned-short port))
     "struct hostent *he = gethostbyname(host);"
@@ -377,6 +384,14 @@
           (##net#error "bind" host port)
           (##sys#setslot sock 2 #t)))))
 
+(define udp-bound-port
+  (lambda (sock)
+    (let* ([fd (io:descriptor sock)]
+           [port (##net#getsockport fd)])
+      (if (eq? -1 port)
+        (##net#error "getsockport"))
+      port)))
+
 ;;; udp-connect! : udp-socket host-string port -> unspecified
 ;;; "connect" a socket.  In the case of UDP this does nothing more than
 ;;; store a peer address in the kernel socket structure for use with
8<------8<------8<------8<------8<------8<------8<------8<------

I also noticed, an extra line in ##net#getsockport in tcp.scm;
  "unsigned char *prt;"

And, to make my compiler happy, I added a cast;
  &len  ->  (socklen_t *)&len

Both are applied in this patch, wouldn't it be better
to do the same in tcp.scm?

Thanks,
Daishi




reply via email to

[Prev in Thread] Current Thread [Next in Thread]