From bdd81e2b0585e22d616907b0add216646a5e525c Mon Sep 17 00:00:00 2001 From: Abdulaziz Ghuloum Date: Fri, 11 Apr 2008 07:01:27 -0400 Subject: [PATCH] accept and accept-nonblocking now set the port-id to a string representing the incoming address like "nnn.nnn.nnn.nnn:pppp" --- lab/greeting-server.ss | 2 ++ scheme/ikarus.io.ss | 24 +++++++++++++++++++----- scheme/last-revision | 2 +- src/ikarus-io.c | 34 ++++++++++++++++++++++++++++++++-- 4 files changed, 54 insertions(+), 8 deletions(-) diff --git a/lab/greeting-server.ss b/lab/greeting-server.ss index 06876b6..6e3f9c1 100755 --- a/lab/greeting-server.ss +++ b/lab/greeting-server.ss @@ -23,6 +23,8 @@ (lambda () (let f () (let-values ([(op ip) (accept-connection s)]) + (printf "Connection from address ~a\n" + op) (let ([op (transcoded-port op (native-transcoder))] [ip (transcoded-port ip (native-transcoder))]) (display "What's your name? " op) diff --git a/scheme/ikarus.io.ss b/scheme/ikarus.io.ss index b77c577..9203db2 100644 --- a/scheme/ikarus.io.ss +++ b/scheme/ikarus.io.ss @@ -1206,7 +1206,10 @@ #| 20 |# "invalid file name" #| 21 |# "non-blocking operation would block" #| 22 |# "broken pipe (e.g., writing to a closed process or socket)" - #| 23 |# "connection refused")) + #| 23 |# "connection refused" + #| 24 |# "not a socket" + #| 25 |# "not enough memory to perform operation" + )) (define (io-error who id err . other-conditions) (let ([err (fxnot err)]) @@ -2136,7 +2139,6 @@ (define out-queue '()) (define in-queue '()) - (define (process-events) (if (null? out-queue) (if (null? in-queue) @@ -2241,12 +2243,22 @@ (define (do-accept-connection s who blocking?) + (define (make-socket-info x) + (unless (= (bytevector-length x) 16) + (error who "BUG: unexpected return value" x)) + (format "~s.~s.~s.~s:~s" + (bytevector-u8-ref x 4) + (bytevector-u8-ref x 5) + (bytevector-u8-ref x 6) + (bytevector-u8-ref x 7) + (+ (* 256 (bytevector-u8-ref x 2)) + (bytevector-u8-ref x 3)))) (unless (tcp-server? s) (die who "not a tcp server" s)) - (let ([fd (tcp-server-fd s)]) + (let ([fd (tcp-server-fd s)] [bv (make-bytevector 16)]) (unless fd (die who "server is closed" s)) - (let ([sock (foreign-call "ikrt_accept" fd)]) + (let ([sock (foreign-call "ikrt_accept" fd bv)]) (cond [(eq? sock EAGAIN-error-code) (call/cc @@ -2254,8 +2266,10 @@ (add-io-event fd k 'r) (process-events))) (do-accept-connection s who blocking?)] + [(< sock 0) + (io-error who s sock)] [else - (socket->ports sock who #f blocking?)])))) + (socket->ports sock who (make-socket-info bv) blocking?)])))) (define (accept-connection s) (do-accept-connection s 'accept-connection #t)) diff --git a/scheme/last-revision b/scheme/last-revision index 5bf2252..cd3f37d 100644 --- a/scheme/last-revision +++ b/scheme/last-revision @@ -1 +1 @@ -1444 +1445 diff --git a/src/ikarus-io.c b/src/ikarus-io.c index fee8138..e8385e9 100644 --- a/src/ikarus-io.c +++ b/src/ikarus-io.c @@ -59,6 +59,8 @@ ikrt_io_error(){ case EAGAIN : return fix(-22); /* hardcoded in ikarus.io.ss */ case EPIPE : return fix(-23); case ECONNREFUSED : return fix(-24); + case ENOTSOCK : return fix(-25); + case ENOBUFS : return fix(-26); } return fix(-1); } @@ -251,12 +253,37 @@ ikrt_listen(ikptr port, ikpcb* pcb){ return fix(sock); } +#if 0 +not used +ikptr +ikrt_getsockname(ikptr s, ikpcb* pcb){ + socklen_t size = sizeof(struct sockaddr); + ikptr bv = ik_safe_alloc(pcb, align(disp_bytevector_data+size)) + + bytevector_tag; + int r = getsockname(unfix(s), + (struct sockaddr*)(bv+off_bytevector_data), + &size); + if(r == 0){ + ref(bv, off_bytevector_length) = fix(size); + return bv; + } else { + return ikrt_io_error(); + } +} +#endif + + + ikptr -ikrt_accept(ikptr s, ikpcb* pcb){ - int sock = accept(unfix(s), NULL, NULL); +ikrt_accept(ikptr s, ikptr bv, ikpcb* pcb){ + socklen_t addrlen = unfix(ref(bv, off_bytevector_length)); + int sock = accept(unfix(s), + (struct sockaddr*) (bv+off_bytevector_data), + &addrlen); if(sock < 0){ return ikrt_io_error(); } + ref(bv, off_bytevector_length) = fix(addrlen); return fix(sock); } @@ -287,3 +314,6 @@ ikrt_file_ctime(ikptr filename, ikptr res){ return fix(0); } + + +