2 * WINGs WMConnection function library
4 * Copyright (c) 1999-2003 Dan Pascu
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 * - decide if we want to support connections with external sockets, else
25 * clean up the structure of the unneeded members.
26 * - decide what to do with all wwarning() calls that are still there.
40 #include <sys/socket.h>
41 #include <netinet/in.h>
42 #include <arpa/inet.h>
46 #include <sys/signal.h>
52 /* Some older systems does not define this (linux libc5, maybe others too) */
64 # define INADDR_NONE -1
67 /* Stuff for setting the sockets into non-blocking mode. */
68 /*#ifdef __POSIX_SOURCE
69 # define NONBLOCK_OPT O_NONBLOCK
71 # define NONBLOCK_OPT FNDELAY
74 #define NONBLOCK_OPT O_NONBLOCK
76 #define NETBUF_SIZE 4096
78 #define DEF_TIMEOUT 600 /* 600 seconds == 10 minutes */
84 static Bool SigInitialized
= False
;
86 static unsigned int DefaultTimeout
= DEF_TIMEOUT
;
87 static unsigned int OpenTimeout
= DEF_TIMEOUT
;
91 typedef struct TimeoutData
{
97 typedef struct W_Connection
{
98 int sock
; /* the socket we speak through */
101 WMHandlerID
*read
; /* the input read handler */
102 WMHandlerID
*write
; /* the input write handler */
103 WMHandlerID
*exception
; /* the input exception handler */
106 ConnectionDelegate
*delegate
; /* client delegates */
107 void *clientData
; /* client data */
108 unsigned int uflags
; /* flags for the client */
110 WMArray
*outputQueue
;
113 TimeoutData sendTimeout
;
114 TimeoutData openTimeout
;
116 WMConnectionState state
;
117 WMConnectionTimeoutState timeoutState
;
124 Bool shutdownOnClose
;
133 clearOutputQueue(WMConnection
*cPtr
)
136 WMEmptyArray(cPtr
->outputQueue
);
141 openTimeout(void *cdata
)
143 WMConnection
*cPtr
= (WMConnection
*) cdata
;
145 cPtr
->openTimeout
.handler
= NULL
;
146 if (cPtr
->handler
.write
) {
147 WMDeleteInputHandler(cPtr
->handler
.write
);
148 cPtr
->handler
.write
= NULL
;
150 if (cPtr
->state
!= WCConnected
) {
151 cPtr
->state
= WCTimedOut
;
152 cPtr
->timeoutState
= WCTWhileOpening
;
153 if (cPtr
->delegate
&& cPtr
->delegate
->didTimeout
) {
154 (*cPtr
->delegate
->didTimeout
)(cPtr
->delegate
, cPtr
);
156 WMCloseConnection(cPtr
);
157 cPtr
->state
= WCTimedOut
; /* the above set state to WCClosed */
164 sendTimeout(void *cdata
)
166 WMConnection
*cPtr
= (WMConnection
*) cdata
;
168 cPtr
->sendTimeout
.handler
= NULL
;
169 if (cPtr
->handler
.write
) {
170 WMDeleteInputHandler(cPtr
->handler
.write
);
171 cPtr
->handler
.write
= NULL
;
173 if (WMGetArrayItemCount(cPtr
->outputQueue
)>0) {
174 clearOutputQueue(cPtr
);
175 cPtr
->state
= WCTimedOut
;
176 cPtr
->timeoutState
= WCTWhileSending
;
177 // should we close it no matter what (after calling the didTimeout
179 if (cPtr
->delegate
&& cPtr
->delegate
->didTimeout
) {
180 (*cPtr
->delegate
->didTimeout
)(cPtr
->delegate
, cPtr
);
182 WMCloseConnection(cPtr
);
183 cPtr
->state
= WCTimedOut
; /* the above set state to WCClosed */
190 inputHandler(int fd
, int mask
, void *clientData
)
192 WMConnection
*cPtr
= (WMConnection
*)clientData
;
194 if (cPtr
->state
==WCClosed
|| cPtr
->state
==WCDied
)
197 if ((mask
& WIWriteMask
)) {
200 if (cPtr
->state
== WCInProgress
) {
202 int len
= sizeof(result
);
205 if (getsockopt(cPtr
->sock
, SOL_SOCKET
, SO_ERROR
,
206 (void*)&result
, &len
) == 0 && result
!= 0) {
207 cPtr
->state
= WCFailed
;
208 WCErrorCode
= result
;
210 /* should call wsyserrorwithcode(result, ...) here? */
212 cPtr
->state
= WCConnected
;
216 if (cPtr
->handler
.write
) {
217 WMDeleteInputHandler(cPtr
->handler
.write
);
218 cPtr
->handler
.write
= NULL
;
221 if (cPtr
->openTimeout
.handler
) {
222 WMDeleteTimerHandler(cPtr
->openTimeout
.handler
);
223 cPtr
->openTimeout
.handler
= NULL
;
226 if (cPtr
->delegate
&& cPtr
->delegate
->didInitialize
)
227 (*cPtr
->delegate
->didInitialize
)(cPtr
->delegate
, cPtr
);
229 /* we use failed and not cPtr->state here, because cPtr may be
230 * destroyed by the delegate called above if the connection failed
234 } else if (cPtr
->state
== WCConnected
) {
235 result
= WMFlushConnection(cPtr
);
236 if (result
>0 && cPtr
->delegate
&& cPtr
->delegate
->canResumeSending
) {
237 (*cPtr
->delegate
->canResumeSending
)(cPtr
->delegate
, cPtr
);
245 /* if the connection died, may get destroyed in the delegate, so retain */
248 if ((mask
& WIReadMask
) && cPtr
->delegate
->didReceiveInput
)
249 (*cPtr
->delegate
->didReceiveInput
)(cPtr
->delegate
, cPtr
);
251 if ((mask
& WIExceptMask
) && cPtr
->delegate
->didCatchException
)
252 (*cPtr
->delegate
->didCatchException
)(cPtr
->delegate
, cPtr
);
259 setSocketNonBlocking(int sock
, Bool flag
)
264 state
= fcntl(sock
, F_GETFL
, 0);
267 /* set WCErrorCode here? -Dan*/
271 isNonBlock
= (state
& NONBLOCK_OPT
) != 0;
276 state
|= NONBLOCK_OPT
;
280 state
&= ~NONBLOCK_OPT
;
283 if (fcntl(sock
, F_SETFL
, state
) < 0) {
284 /* set WCErrorCode here? -Dan*/
293 setConnectionAddress(WMConnection
*cPtr
, struct sockaddr_in
*socketaddr
)
295 wassertr(cPtr
->address
==NULL
);
297 cPtr
->address
= wstrdup(inet_ntoa(socketaddr
->sin_addr
));
298 cPtr
->service
= wmalloc(16);
299 sprintf(cPtr
->service
, "%hu", ntohs(socketaddr
->sin_port
));
300 cPtr
->protocol
= wstrdup("tcp");
304 static struct sockaddr_in
*
305 getSocketAddress(char* name
, char* service
, char* protocol
)
307 static struct sockaddr_in socketaddr
;
310 if (!protocol
|| protocol
[0]==0)
313 memset(&socketaddr
, 0, sizeof(struct sockaddr_in
));
314 socketaddr
.sin_family
= AF_INET
;
317 * If we were given a hostname, we use any address for that host.
318 * Otherwise we expect the given name to be an address unless it is
319 * NULL (any address).
321 if (name
&& name
[0]!=0) {
322 WMHost
*host
= WMGetHostWithName(name
);
325 return NULL
; /* name is not a hostname nor a number and dot adr */
327 name
= WMGetHostAddress(host
);
328 #ifndef HAVE_INET_ATON
329 if ((socketaddr
.sin_addr
.s_addr
= inet_addr(name
)) == INADDR_NONE
) {
331 if (inet_aton(name
, &socketaddr
.sin_addr
) == 0) {
338 socketaddr
.sin_addr
.s_addr
= htonl(INADDR_ANY
);
341 if (!service
|| service
[0]==0) {
342 socketaddr
.sin_port
= 0;
343 } else if ((sp
= getservbyname(service
, protocol
))==0) {
347 portNumber
= strtoul(service
, &endptr
, 10);
349 if (service
[0]!=0 && *endptr
==0 && portNumber
<65536) {
350 socketaddr
.sin_port
= htons(portNumber
);
355 socketaddr
.sin_port
= sp
->s_port
;
363 dummyHandler(int signum
)
369 createConnectionWithSocket(int sock
, Bool closeOnRelease
)
372 struct sigaction sig_action
;
374 cPtr
= wmalloc(sizeof(WMConnection
));
376 memset(cPtr
, 0, sizeof(WMConnection
));
378 fcntl(sock
, F_SETFD
, FD_CLOEXEC
); /* by default close on exec */
381 cPtr
->openTimeout
.timeout
= OpenTimeout
;
382 cPtr
->openTimeout
.handler
= NULL
;
383 cPtr
->sendTimeout
.timeout
= DefaultTimeout
;
384 cPtr
->sendTimeout
.handler
= NULL
;
385 cPtr
->closeOnRelease
= closeOnRelease
;
386 cPtr
->shutdownOnClose
= 1;
388 WMCreateArrayWithDestructor(16, (WMFreeDataProc
*)WMReleaseData
);
389 cPtr
->state
= WCNotConnected
;
390 cPtr
->timeoutState
= WCTNone
;
392 /* ignore dead pipe */
393 if (!SigInitialized
) {
394 /* Because POSIX mandates that only signal with handlers are reset
395 * accross an exec*(), we do not want to propagate ignoring SIGPIPEs
396 * to children. Hence the dummy handler. Philippe Troin <phil@fifi.org>
398 sig_action
.sa_handler
= &dummyHandler
;
399 sig_action
.sa_flags
= SA_RESTART
;
400 sigaction(SIGPIPE
, &sig_action
, NULL
);
401 SigInitialized
= True
;
410 WMCreateConnectionWithSocket(int sock
, Bool closeOnRelease
)
413 struct sockaddr_in clientname
;
417 cPtr
= createConnectionWithSocket(sock
, closeOnRelease
);
418 cPtr
->wasNonBlocking
= WMIsConnectionNonBlocking(cPtr
);
419 cPtr
->isNonBlocking
= cPtr
->wasNonBlocking
;
421 /* some way to find out if it is connected, and binded. can't find
422 if it listens though!!!
425 size
= sizeof(clientname
);
426 n
= getpeername(sock
, (struct sockaddr
*) &clientname
, &size
);
428 /* Since we have a peer, it means we are connected */
429 cPtr
->state
= WCConnected
;
431 size
= sizeof(clientname
);
432 n
= getsockname(sock
, (struct sockaddr
*) &clientname
, &size
);
434 /* We don't have a peer, but we are binded to an address.
435 * Assume we are listening on it (we don't know that for sure!)
437 cPtr
->state
= WCListening
;
439 cPtr
->state
= WCNotConnected
;
449 * host is the name on which we want to listen for incoming connections,
450 * and it must be a name of this host, or NULL if we want to listen
451 * on any incoming address.
452 * service is either a service name as present in /etc/services, or the port
453 * number we want to listen on. If NULL, a random port between
454 * 1024 and 65535 will be assigned to us.
455 * protocol is one of "tcp" or "udp". If NULL, "tcp" will be used by default.
456 * currently only "tcp" is supported.
459 WMCreateConnectionAsServerAtAddress(char *host
, char *service
, char *protocol
)
462 struct sockaddr_in
*socketaddr
;
468 if ((socketaddr
= getSocketAddress(host
, service
, protocol
)) == NULL
) {
469 wwarning(_("Bad address-service-protocol combination"));
473 /* Create the actual socket */
474 sock
= socket(PF_INET
, SOCK_STREAM
, 0);
481 * Set socket options. We try to make the port reusable and have it
482 * close as fast as possible without waiting in unnecessary wait states
486 setsockopt(sock
, SOL_SOCKET
, SO_REUSEADDR
, (void *)&on
, sizeof(on
));
488 if (bind(sock
, (struct sockaddr
*)socketaddr
, sizeof(*socketaddr
)) < 0) {
494 if (listen(sock
, 10) < 0) {
500 /* Find out what is the address/service/protocol we get */
501 /* In case some of address/service/protocol were NULL */
502 size
= sizeof(*socketaddr
);
503 if (getsockname(sock
, (struct sockaddr
*)socketaddr
, &size
) < 0) {
509 cPtr
= createConnectionWithSocket(sock
, True
);
510 cPtr
->state
= WCListening
;
511 WMSetConnectionNonBlocking(cPtr
, True
);
513 setConnectionAddress(cPtr
, socketaddr
);
520 WMCreateConnectionToAddress(char *host
, char *service
, char *protocol
)
523 struct sockaddr_in
*socketaddr
;
528 wassertrv(service
!=NULL
&& service
[0]!=0, NULL
);
530 if (host
==NULL
|| host
[0]==0)
533 if ((socketaddr
= getSocketAddress(host
, service
, protocol
)) == NULL
) {
534 wwarning(_("Bad address-service-protocol combination"));
538 /* Create the actual socket */
539 sock
= socket(PF_INET
, SOCK_STREAM
, 0);
544 /* make socket blocking while we connect. */
545 setSocketNonBlocking(sock
, False
);
546 if (connect(sock
, (struct sockaddr
*)socketaddr
, sizeof(*socketaddr
)) < 0) {
552 cPtr
= createConnectionWithSocket(sock
, True
);
553 cPtr
->state
= WCConnected
;
554 WMSetConnectionNonBlocking(cPtr
, True
);
555 setConnectionAddress(cPtr
, socketaddr
);
562 WMCreateConnectionToAddressAndNotify(char *host
, char *service
, char *protocol
)
565 struct sockaddr_in
*socketaddr
;
571 wassertrv(service
!=NULL
&& service
[0]!=0, NULL
);
573 if (host
==NULL
|| host
[0]==0)
576 if ((socketaddr
= getSocketAddress(host
, service
, protocol
)) == NULL
) {
577 wwarning(_("Bad address-service-protocol combination"));
581 /* Create the actual socket */
582 sock
= socket(PF_INET
, SOCK_STREAM
, 0);
587 isNonBlocking
= setSocketNonBlocking(sock
, True
);
588 if (connect(sock
, (struct sockaddr
*)socketaddr
, sizeof(*socketaddr
)) < 0) {
589 if (errno
!=EINPROGRESS
) {
596 cPtr
= createConnectionWithSocket(sock
, True
);
597 cPtr
->state
= WCInProgress
;
598 cPtr
->isNonBlocking
= isNonBlocking
;
600 cPtr
->handler
.write
= WMAddInputHandler(cPtr
->sock
, WIWriteMask
,
603 cPtr
->openTimeout
.handler
=
604 WMAddTimerHandler(cPtr
->openTimeout
.timeout
*1000, openTimeout
, cPtr
);
606 setConnectionAddress(cPtr
, socketaddr
);
613 removeAllHandlers(WMConnection
*cPtr
)
615 if (cPtr
->handler
.read
)
616 WMDeleteInputHandler(cPtr
->handler
.read
);
617 if (cPtr
->handler
.write
)
618 WMDeleteInputHandler(cPtr
->handler
.write
);
619 if (cPtr
->handler
.exception
)
620 WMDeleteInputHandler(cPtr
->handler
.exception
);
621 if (cPtr
->openTimeout
.handler
)
622 WMDeleteTimerHandler(cPtr
->openTimeout
.handler
);
623 if (cPtr
->sendTimeout
.handler
)
624 WMDeleteTimerHandler(cPtr
->sendTimeout
.handler
);
626 cPtr
->handler
.read
= NULL
;
627 cPtr
->handler
.write
= NULL
;
628 cPtr
->handler
.exception
= NULL
;
629 cPtr
->openTimeout
.handler
= NULL
;
630 cPtr
->sendTimeout
.handler
= NULL
;
635 WMDestroyConnection(WMConnection
*cPtr
)
637 if (cPtr
->closeOnRelease
&& cPtr
->sock
>=0) {
638 if (cPtr
->shutdownOnClose
) {
639 shutdown(cPtr
->sock
, SHUT_RDWR
);
644 removeAllHandlers(cPtr
);
645 WMFreeArray(cPtr
->outputQueue
); /* will also free the items with the destructor */
648 wfree(cPtr
->address
);
649 wfree(cPtr
->service
);
650 wfree(cPtr
->protocol
);
658 WMCloseConnection(WMConnection
*cPtr
)
661 if (cPtr
->shutdownOnClose
) {
662 shutdown(cPtr
->sock
, SHUT_RDWR
);
668 removeAllHandlers(cPtr
);
669 clearOutputQueue(cPtr
);
671 cPtr
->state
= WCClosed
;
676 WMAcceptConnection(WMConnection
*listener
)
678 struct sockaddr_in clientname
;
681 WMConnection
*newConnection
;
684 wassertrv(listener
&& listener
->state
==WCListening
, NULL
);
686 size
= sizeof(clientname
);
687 newSock
= accept(listener
->sock
, (struct sockaddr
*) &clientname
, &size
);
689 WCErrorCode
= ((errno
!=EAGAIN
&& errno
!=EWOULDBLOCK
) ? errno
: 0);
693 newConnection
= createConnectionWithSocket(newSock
, True
);
694 WMSetConnectionNonBlocking(newConnection
, True
);
695 newConnection
->state
= WCConnected
;
696 setConnectionAddress(newConnection
, &clientname
);
698 return newConnection
;
703 WMGetConnectionAddress(WMConnection
*cPtr
)
705 return cPtr
->address
;
710 WMGetConnectionService(WMConnection
*cPtr
)
712 return cPtr
->service
;
717 WMGetConnectionProtocol(WMConnection
*cPtr
)
719 return cPtr
->protocol
;
724 WMGetConnectionSocket(WMConnection
*cPtr
)
731 WMGetConnectionState(WMConnection
*cPtr
)
737 WMConnectionTimeoutState
738 WMGetConnectionTimeoutState(WMConnection
*cPtr
)
740 return cPtr
->timeoutState
;
745 WMEnqueueConnectionData(WMConnection
*cPtr
, WMData
*data
)
747 wassertrv(cPtr
->state
!=WCNotConnected
&& cPtr
->state
!=WCListening
, False
);
748 wassertrv(cPtr
->state
!=WCInProgress
&& cPtr
->state
!=WCFailed
, False
);
750 if (cPtr
->state
!=WCConnected
)
753 WMAddToArray(cPtr
->outputQueue
, WMRetainData(data
));
760 * -1 - not connected or connection died while sending
761 * 0 - couldn't send the data (or part of it). data is saved in a queue
762 * and will be sent when possible. after it is sent the canResumeSending
763 * callback will be called.
764 * 1 - data was succesfully sent
767 WMSendConnectionData(WMConnection
*cPtr
, WMData
*data
)
770 TimeoutData
*tPtr
= &cPtr
->sendTimeout
;
771 const unsigned char *dataBytes
;
773 wassertrv(cPtr
->state
!=WCNotConnected
&& cPtr
->state
!=WCListening
, -1);
774 wassertrv(cPtr
->state
!=WCInProgress
&& cPtr
->state
!=WCFailed
, -1);
776 if (cPtr
->state
!=WCConnected
)
779 /* If we have no data just flush the queue, else try to send data */
780 if (data
&& WMGetDataLength(data
)>0) {
781 WMAddToArray(cPtr
->outputQueue
, WMRetainData(data
));
782 /* If there already was something in queue, and also a write input
783 * handler is established, it means we were unable to send, so
784 * return and let the write handler notify us when we can send.
786 if (WMGetArrayItemCount(cPtr
->outputQueue
)>1 && cPtr
->handler
.write
)
790 while (WMGetArrayItemCount(cPtr
->outputQueue
) > 0) {
791 data
= WMGetFromArray(cPtr
->outputQueue
, 0);
792 dataBytes
= (const unsigned char *)WMDataBytes(data
);
793 len
= WMGetDataLength(data
);
794 pos
= cPtr
->bufPos
; /* where we're left last time */
797 bytes
= write(cPtr
->sock
, dataBytes
+pos
, len
- pos
);
803 /* save the position where we're left and add a timeout */
805 if (!tPtr
->handler
) {
806 tPtr
->handler
= WMAddTimerHandler(tPtr
->timeout
*1000,
809 if (!cPtr
->handler
.write
) {
810 cPtr
->handler
.write
=
811 WMAddInputHandler(cPtr
->sock
, WIWriteMask
,
817 cPtr
->state
= WCDied
;
818 removeAllHandlers(cPtr
);
819 if (cPtr
->delegate
&& cPtr
->delegate
->didDie
)
820 (*cPtr
->delegate
->didDie
)(cPtr
->delegate
, cPtr
);
826 WMDeleteFromArray(cPtr
->outputQueue
, 0);
829 WMDeleteTimerHandler(tPtr
->handler
);
830 tPtr
->handler
= NULL
;
832 /*if (cPtr->handler.write) {
833 WMDeleteInputHandler(cPtr->handler.write);
834 cPtr->handler.write = NULL;
838 if (cPtr
->handler
.write
) {
839 WMDeleteInputHandler(cPtr
->handler
.write
);
840 cPtr
->handler
.write
= NULL
;
848 * WMGetConnectionAvailableData(connection):
850 * will return a WMData structure containing the available data on the
851 * specified connection. If connection is non-blocking (default) and no data
852 * is available when this function is called, an empty WMData is returned.
854 * If an error occurs while reading or the other side closed connection,
855 * it will return NULL.
856 * Also trying to read from an already died or closed connection is
857 * considered to be an error condition, and will return NULL.
860 WMGetConnectionAvailableData(WMConnection
*cPtr
)
862 char buffer
[NETBUF_SIZE
];
866 wassertrv(cPtr
->state
!=WCNotConnected
&& cPtr
->state
!=WCListening
, NULL
);
867 wassertrv(cPtr
->state
!=WCInProgress
&& cPtr
->state
!=WCFailed
, NULL
);
869 if (cPtr
->state
!=WCConnected
)
875 nbytes
= read(cPtr
->sock
, buffer
, NETBUF_SIZE
);
881 aData
= WMCreateDataWithCapacity(0);
885 cPtr
->state
= WCDied
;
886 removeAllHandlers(cPtr
);
887 if (cPtr
->delegate
&& cPtr
->delegate
->didDie
)
888 (*cPtr
->delegate
->didDie
)(cPtr
->delegate
, cPtr
);
891 } else if (nbytes
==0) { /* the other side has closed connection */
892 cPtr
->state
= WCClosed
;
893 removeAllHandlers(cPtr
);
894 if (cPtr
->delegate
&& cPtr
->delegate
->didDie
)
895 (*cPtr
->delegate
->didDie
)(cPtr
->delegate
, cPtr
);
897 aData
= WMCreateDataWithBytes(buffer
, nbytes
);
905 WMSetConnectionDelegate(WMConnection
*cPtr
, ConnectionDelegate
*delegate
)
907 wassertr(cPtr
->sock
>= 0);
908 /* Don't try to set the delegate multiple times */
909 wassertr(cPtr
->delegate
== NULL
);
911 cPtr
->delegate
= delegate
;
912 if (delegate
&& delegate
->didReceiveInput
&& !cPtr
->handler
.read
)
913 cPtr
->handler
.read
= WMAddInputHandler(cPtr
->sock
, WIReadMask
,
915 if (delegate
&& delegate
->didCatchException
&& !cPtr
->handler
.exception
)
916 cPtr
->handler
.exception
= WMAddInputHandler(cPtr
->sock
, WIExceptMask
,
923 WMIsConnectionNonBlocking(WMConnection
*cPtr
)
928 state
= fcntl(cPtr
->sock
, F_GETFL
, 0);
931 /* If we can't use fcntl on socket, this probably also means we could
932 * not use fcntl to set non-blocking mode, and since a socket defaults
933 * to blocking when created, return False as the best assumption */
937 return ((state
& NONBLOCK_OPT
)!=0);
939 return cPtr
->isNonBlocking
;
946 WMSetConnectionNonBlocking(WMConnection
*cPtr
, Bool flag
)
948 wassertrv(cPtr
!=NULL
&& cPtr
->sock
>=0, False
);
950 flag
= ((flag
==0) ? 0 : 1);
952 if (cPtr
->isNonBlocking
== flag
)
955 if (setSocketNonBlocking(cPtr
->sock
, flag
)==True
) {
956 cPtr
->isNonBlocking
= flag
;
965 WMSetConnectionCloseOnExec(WMConnection
*cPtr
, Bool flag
)
967 wassertrv(cPtr
!=NULL
&& cPtr
->sock
>=0, False
);
969 if (fcntl(cPtr
->sock
, F_SETFD
, ((flag
==0) ? 0 : FD_CLOEXEC
)) < 0) {
978 WMSetConnectionShutdownOnClose(WMConnection
*cPtr
, Bool flag
)
980 cPtr
->shutdownOnClose
= ((flag
==0) ? 0 : 1);
985 WMGetConnectionClientData(WMConnection
*cPtr
)
987 return cPtr
->clientData
;
992 WMSetConnectionClientData(WMConnection
*cPtr
, void *data
)
994 cPtr
->clientData
= data
;
999 WMGetConnectionFlags(WMConnection
*cPtr
)
1001 return cPtr
->uflags
;
1006 WMSetConnectionFlags(WMConnection
*cPtr
, unsigned int flags
)
1008 cPtr
->uflags
= flags
;
1013 WMGetConnectionUnsentData(WMConnection
*cPtr
)
1015 return cPtr
->outputQueue
;
1020 WMSetConnectionDefaultTimeout(unsigned int timeout
)
1023 DefaultTimeout
= DEF_TIMEOUT
;
1025 DefaultTimeout
= timeout
;
1031 WMSetConnectionOpenTimeout(unsigned int timeout
)
1034 OpenTimeout
= DefaultTimeout
;
1036 OpenTimeout
= timeout
;
1042 WMSetConnectionSendTimeout(WMConnection
*cPtr
, unsigned int timeout
)
1045 cPtr
->sendTimeout
.timeout
= DefaultTimeout
;
1047 cPtr
->sendTimeout
.timeout
= timeout
;