Skip site navigation (1)Skip section navigation (2)
Date:      Mon, 21 May 2007 00:11:48 +0200
From:      Martin Kulas <coolaz@web.de>
To:        freebsd-current@freebsd.org
Subject:   [SCTP] nonblocking write()
Message-ID:  <20070520221148.GA2169@thunderbird.local>

next in thread | raw e-mail | index | archive | help

--xHFwDpU9dbj6ez1V
Content-Type: text/plain; charset=iso-8859-1
Content-Disposition: inline
Content-Transfer-Encoding: quoted-printable

Hello!


I hope this is the right mailing list. I am running -CURRENT and=20
I am trying to get an application (thttpd) to use SCTP sockets =20
in one-to-one style.

But it does not work.  I tracked the problem down to this:
The SCTP socket is made non-blocking.  Then write() is called.
Write() succeeds if we do not write too many bytes;
net.inet.sctp.sendspace is the limit for an successfull write().

Is this behaviour intended?
Perhaps I am coding something wrong, so here is sample code:

$ nl nb.c
 1	#include <stdio.h>
 2	#include <arpa/inet.h>
 3	#include <netinet/in.h>
 4	#include <sys/types.h>
 5	#include <unistd.h>
 6	#include <sys/socket.h>
 7	#include <err.h>
 8	#include <strings.h>
 9	#include <errno.h>
10	#include <string.h>
11	#include <fcntl.h>
  =09
12	#define DST_IP	 "127.0.0.1"
13	#define DST_PORT 1234
  =09
14	int
15	main()
16	{
17	    int s;
18	    struct sockaddr_in sa;
19	    int ret;
20	    int n;
21	    char buf[BUFSZ];
22	    int toSend;
  =09
23	    printf("BUFSZ: %u\n", BUFSZ);
24	#ifdef USE_SCTP
25	    printf("using sctp\n");
26	    s =3D socket(AF_INET, SOCK_STREAM, IPPROTO_SCTP);
27	#else
28	    printf("using tcp\n");
29	    s =3D socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
30	#endif
31	    if ( s =3D=3D -1 )
32		err(1, "socket() failed");
  =09
33	#ifdef USE_NONBLOCKING
34	    printf("using nonblocking i/o\n");
35	    ret =3D fcntl(s, F_GETFL);
36	    if ( ret =3D=3D -1 )
37		err(1, "fcntl() failed\n");
38	    ret |=3D O_NONBLOCK;
39	    ret =3D fcntl(s, F_SETFL, ret);
40	    if ( ret =3D=3D -1 )
41		err(1, "fcntl() failed\n");
42	#endif
  =09
43	    bzero(&sa, sizeof(sa));
44	    sa.sin_family =3D AF_INET;
45	    sa.sin_port =3D htons(DST_PORT);
46	    n =3D inet_pton(AF_INET, DST_IP, &sa.sin_addr);
47	    if ( n !=3D 1 )
48		err(1, "inet_pton() failed");
  =09
49	    ret =3D connect(s, (struct sockaddr *) &sa, sizeof(sa));
50	    if ( ret =3D=3D -1 )
51		err(1, "connect() failed");
52	    memset(buf, 'a', BUFSZ);
  =09
53	#ifdef USE_WRITE
54	    printf("using write() i/o\n");
55	    toSend =3D BUFSZ;
56	    while ( toSend > 0 ) {
57		n =3D write(s, buf, toSend);
58		if ( n > 0 ) {
59		    /* OK */
60		    printf("%d bytes send\n", n);
61		    toSend -=3D n;
62		} else if ( n =3D=3D 0 ) {
63		    err(1, "write() EOF (0 bytes written)\n");
64		} else  {
65		    printf("n: %d: %s\n", n, strerror(errno));
66		    sleep(1);
67		}
68	    }
69	#endif
  =09
70	    return 0;
71	}


$ cc -Wall -DUSE_SCTP -DUSE_WRITE -DUSE_NONBLOCKING -DBUFSZ=3D30000 -o nb n=
b.c=20
$ ./nb                                                                     =
=20
BUFSZ: 30000
using sctp
using nonblocking i/o
using write() i/o
30000 bytes send
$ cc -Wall -DUSE_SCTP -DUSE_WRITE -DUSE_NONBLOCKING -DBUFSZ=3D300000 -o nb =
nb.c
$ ./nb                                                                     =
 =20
BUFSZ: 300000
using sctp
using nonblocking i/o
using write() i/o
n: -1: Resource temporarily unavailable
n: -1: Resource temporarily unavailable
n: -1: Resource temporarily unavailable
n: -1: Resource temporarily unavailable
n: -1: Resource temporarily unavailable
n: -1: Resource temporarily unavailable
^C


I googled a bit but I do not get any answer if this behaviour is correct=20
for non-blocking SCTP socket in one-to-one style.

Thanks in advance,
Martin


--=20
PGP Key: http://www.stud.uni-hamburg.de/~kulas/mkulas_pubkey.asc

--xHFwDpU9dbj6ez1V
Content-Type: application/pgp-signature
Content-Disposition: inline

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.3 (FreeBSD)

iD8DBQFGUMeju1jKg1agQ1oRAqLsAKDcUizqObwfhip1yWOJMgAVZpt/LACfbuav
xo771wg2rDc29ByHb38dvro=
=w1aG
-----END PGP SIGNATURE-----

--xHFwDpU9dbj6ez1V--




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20070520221148.GA2169>