2 * WINGs WMConnection function library
4 * Copyright (c) 1999-2002 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 if (cPtr
->delegate
&& cPtr
->delegate
->didTimeout
) {
178 (*cPtr
->delegate
->didTimeout
)(cPtr
->delegate
, cPtr
);
180 WMCloseConnection(cPtr
);
181 cPtr
->state
= WCTimedOut
; /* the above set state to WCClosed */
188 inputHandler(int fd
, int mask
, void *clientData
)
190 WMConnection
*cPtr
= (WMConnection
*)clientData
;
192 if (cPtr
->state
==WCClosed
|| cPtr
->state
==WCDied
)
195 if ((mask
& WIWriteMask
)) {
198 if (cPtr
->state
== WCInProgress
) {
200 int len
= sizeof(result
);
203 if (getsockopt(cPtr
->sock
, SOL_SOCKET
, SO_ERROR
,
204 (void*)&result
, &len
) == 0 && result
!= 0) {
205 cPtr
->state
= WCFailed
;
206 WCErrorCode
= result
;
208 /* should call wsyserrorwithcode(result, ...) here? */
210 cPtr
->state
= WCConnected
;
214 if (cPtr
->handler
.write
) {
215 WMDeleteInputHandler(cPtr
->handler
.write
);
216 cPtr
->handler
.write
= NULL
;
219 if (cPtr
->openTimeout
.handler
) {
220 WMDeleteTimerHandler(cPtr
->openTimeout
.handler
);
221 cPtr
->openTimeout
.handler
= NULL
;
224 if (cPtr
->delegate
&& cPtr
->delegate
->didInitialize
)
225 (*cPtr
->delegate
->didInitialize
)(cPtr
->delegate
, cPtr
);
227 /* we use failed and not cPtr->state here, because cPtr may be
228 * destroyed by the delegate called above if the connection failed
232 } else if (cPtr
->state
== WCConnected
) {
233 result
= WMFlushConnection(cPtr
);
234 if (result
>0 && cPtr
->delegate
&& cPtr
->delegate
->canResumeSending
) {
235 (*cPtr
->delegate
->canResumeSending
)(cPtr
->delegate
, cPtr
);
243 /* if the connection died, may get destroyed in the delegate, so retain */
246 if ((mask
& WIReadMask
) && cPtr
->delegate
->didReceiveInput
)
247 (*cPtr
->delegate
->didReceiveInput
)(cPtr
->delegate
, cPtr
);
249 if ((mask
& WIExceptMask
) && cPtr
->delegate
->didCatchException
)
250 (*cPtr
->delegate
->didCatchException
)(cPtr
->delegate
, cPtr
);
257 setSocketNonBlocking(int sock
, Bool flag
)
262 state
= fcntl(sock
, F_GETFL
, 0);
265 /* set WCErrorCode here? -Dan*/
269 isNonBlock
= (state
& NONBLOCK_OPT
) != 0;
274 state
|= NONBLOCK_OPT
;
278 state
&= ~NONBLOCK_OPT
;
281 if (fcntl(sock
, F_SETFL
, state
) < 0) {
282 /* set WCErrorCode here? -Dan*/
291 setConnectionAddress(WMConnection
*cPtr
, struct sockaddr_in
*socketaddr
)
293 wassertr(cPtr
->address
==NULL
);
295 cPtr
->address
= wstrdup(inet_ntoa(socketaddr
->sin_addr
));
296 cPtr
->service
= wmalloc(16);
297 sprintf(cPtr
->service
, "%hu", ntohs(socketaddr
->sin_port
));
298 cPtr
->protocol
= wstrdup("tcp");
302 static struct sockaddr_in
*
303 getSocketAddress(char* name
, char* service
, char* protocol
)
305 static struct sockaddr_in socketaddr
;
308 if (!protocol
|| protocol
[0]==0)
311 memset(&socketaddr
, 0, sizeof(struct sockaddr_in
));
312 socketaddr
.sin_family
= AF_INET
;
315 * If we were given a hostname, we use any address for that host.
316 * Otherwise we expect the given name to be an address unless it is
317 * NULL (any address).
319 if (name
&& name
[0]!=0) {
320 WMHost
*host
= WMGetHostWithName(name
);
323 return NULL
; /* name is not a hostname nor a number and dot adr */
325 name
= WMGetHostAddress(host
);
326 #ifndef HAVE_INET_ATON
327 if ((socketaddr
.sin_addr
.s_addr
= inet_addr(name
)) == INADDR_NONE
) {
329 if (inet_aton(name
, &socketaddr
.sin_addr
) == 0) {
336 socketaddr
.sin_addr
.s_addr
= htonl(INADDR_ANY
);
339 if (!service
|| service
[0]==0) {
340 socketaddr
.sin_port
= 0;
341 } else if ((sp
= getservbyname(service
, protocol
))==0) {
345 portNumber
= strtoul(service
, &endptr
, 10);
347 if (service
[0]!=0 && *endptr
==0 && portNumber
<65536) {
348 socketaddr
.sin_port
= htons(portNumber
);
353 socketaddr
.sin_port
= sp
->s_port
;
361 dummyHandler(int signum
)
367 createConnectionWithSocket(int sock
, Bool closeOnRelease
)
370 struct sigaction sig_action
;
372 cPtr
= wmalloc(sizeof(WMConnection
));
374 memset(cPtr
, 0, sizeof(WMConnection
));
376 fcntl(sock
, F_SETFD
, FD_CLOEXEC
); /* by default close on exec */
379 cPtr
->openTimeout
.timeout
= OpenTimeout
;
380 cPtr
->openTimeout
.handler
= NULL
;
381 cPtr
->sendTimeout
.timeout
= DefaultTimeout
;
382 cPtr
->sendTimeout
.handler
= NULL
;
383 cPtr
->closeOnRelease
= closeOnRelease
;
384 cPtr
->shutdownOnClose
= 1;
386 WMCreateArrayWithDestructor(16, (WMFreeDataProc
*)WMReleaseData
);
387 cPtr
->state
= WCNotConnected
;
388 cPtr
->timeoutState
= WCTNone
;
390 /* ignore dead pipe */
391 if (!SigInitialized
) {
392 /* Because POSIX mandates that only signal with handlers are reset
393 * accross an exec*(), we do not want to propagate ignoring SIGPIPEs
394 * to children. Hence the dummy handler. Philippe Troin <phil@fifi.org>
396 sig_action
.sa_handler
= &dummyHandler
;
397 sig_action
.sa_flags
= SA_RESTART
;
398 sigaction(SIGPIPE
, &sig_action
, NULL
);
399 SigInitialized
= True
;
408 WMCreateConnectionWithSocket(int sock
, Bool closeOnRelease
)
411 struct sockaddr_in clientname
;
415 cPtr
= createConnectionWithSocket(sock
, closeOnRelease
);
416 cPtr
->wasNonBlocking
= WMIsConnectionNonBlocking(cPtr
);
417 cPtr
->isNonBlocking
= cPtr
->wasNonBlocking
;
419 /* some way to find out if it is connected, and binded. can't find
420 if it listens though!!!
423 size
= sizeof(clientname
);
424 n
= getpeername(sock
, (struct sockaddr
*) &clientname
, &size
);
426 /* Since we have a peer, it means we are connected */
427 cPtr
->state
= WCConnected
;
429 size
= sizeof(clientname
);
430 n
= getsockname(sock
, (struct sockaddr
*) &clientname
, &size
);
432 /* We don't have a peer, but we are binded to an address.
433 * Assume we are listening on it (we don't know that for sure!)
435 cPtr
->state
= WCListening
;
437 cPtr
->state
= WCNotConnected
;
447 * host is the name on which we want to listen for incoming connections,
448 * and it must be a name of this host, or NULL if we want to listen
449 * on any incoming address.
450 * service is either a service name as present in /etc/services, or the port
451 * number we want to listen on. If NULL, a random port between
452 * 1024 and 65535 will be assigned to us.
453 * protocol is one of "tcp" or "udp". If NULL, "tcp" will be used by default.
454 * currently only "tcp" is supported.
457 WMCreateConnectionAsServerAtAddress(char *host
, char *service
, char *protocol
)
460 struct sockaddr_in
*socketaddr
;
466 if ((socketaddr
= getSocketAddress(host
, service
, protocol
)) == NULL
) {
467 wwarning(_("Bad address-service-protocol combination"));
471 /* Create the actual socket */
472 sock
= socket(PF_INET
, SOCK_STREAM
, 0);
479 * Set socket options. We try to make the port reusable and have it
480 * close as fast as possible without waiting in unnecessary wait states
484 setsockopt(sock
, SOL_SOCKET
, SO_REUSEADDR
, (void *)&on
, sizeof(on
));
486 if (bind(sock
, (struct sockaddr
*)socketaddr
, sizeof(*socketaddr
)) < 0) {
492 if (listen(sock
, 10) < 0) {
498 /* Find out what is the address/service/protocol we get */
499 /* In case some of address/service/protocol were NULL */
500 size
= sizeof(*socketaddr
);
501 if (getsockname(sock
, (struct sockaddr
*)socketaddr
, &size
) < 0) {
507 cPtr
= createConnectionWithSocket(sock
, True
);
508 cPtr
->state
= WCListening
;
509 WMSetConnectionNonBlocking(cPtr
, True
);
511 setConnectionAddress(cPtr
, socketaddr
);
518 WMCreateConnectionToAddress(char *host
, char *service
, char *protocol
)
521 struct sockaddr_in
*socketaddr
;
526 wassertrv(service
!=NULL
&& service
[0]!=0, NULL
);
528 if (host
==NULL
|| host
[0]==0)
531 if ((socketaddr
= getSocketAddress(host
, service
, protocol
)) == NULL
) {
532 wwarning(_("Bad address-service-protocol combination"));
536 /* Create the actual socket */
537 sock
= socket(PF_INET
, SOCK_STREAM
, 0);
542 /* make socket blocking while we connect. */
543 setSocketNonBlocking(sock
, False
);
544 if (connect(sock
, (struct sockaddr
*)socketaddr
, sizeof(*socketaddr
)) < 0) {
550 cPtr
= createConnectionWithSocket(sock
, True
);
551 cPtr
->state
= WCConnected
;
552 WMSetConnectionNonBlocking(cPtr
, True
);
553 setConnectionAddress(cPtr
, socketaddr
);
560 WMCreateConnectionToAddressAndNotify(char *host
, char *service
, char *protocol
)
563 struct sockaddr_in
*socketaddr
;
569 wassertrv(service
!=NULL
&& service
[0]!=0, NULL
);
571 if (host
==NULL
|| host
[0]==0)
574 if ((socketaddr
= getSocketAddress(host
, service
, protocol
)) == NULL
) {
575 wwarning(_("Bad address-service-protocol combination"));
579 /* Create the actual socket */
580 sock
= socket(PF_INET
, SOCK_STREAM
, 0);
585 isNonBlocking
= setSocketNonBlocking(sock
, True
);
586 if (connect(sock
, (struct sockaddr
*)socketaddr
, sizeof(*socketaddr
)) < 0) {
587 if (errno
!=EINPROGRESS
) {
594 cPtr
= createConnectionWithSocket(sock
, True
);
595 cPtr
->state
= WCInProgress
;
596 cPtr
->isNonBlocking
= isNonBlocking
;
598 cPtr
->handler
.write
= WMAddInputHandler(cPtr
->sock
, WIWriteMask
,
601 cPtr
->openTimeout
.handler
=
602 WMAddTimerHandler(cPtr
->openTimeout
.timeout
*1000, openTimeout
, cPtr
);
604 setConnectionAddress(cPtr
, socketaddr
);
611 removeAllHandlers(WMConnection
*cPtr
)
613 if (cPtr
->handler
.read
)
614 WMDeleteInputHandler(cPtr
->handler
.read
);
615 if (cPtr
->handler
.write
)
616 WMDeleteInputHandler(cPtr
->handler
.write
);
617 if (cPtr
->handler
.exception
)
618 WMDeleteInputHandler(cPtr
->handler
.exception
);
619 if (cPtr
->openTimeout
.handler
)
620 WMDeleteTimerHandler(cPtr
->openTimeout
.handler
);
621 if (cPtr
->sendTimeout
.handler
)
622 WMDeleteTimerHandler(cPtr
->sendTimeout
.handler
);
624 cPtr
->handler
.read
= NULL
;
625 cPtr
->handler
.write
= NULL
;
626 cPtr
->handler
.exception
= NULL
;
627 cPtr
->openTimeout
.handler
= NULL
;
628 cPtr
->sendTimeout
.handler
= NULL
;
633 WMDestroyConnection(WMConnection
*cPtr
)
635 if (cPtr
->closeOnRelease
&& cPtr
->sock
>=0) {
636 if (cPtr
->shutdownOnClose
) {
637 shutdown(cPtr
->sock
, SHUT_RDWR
);
642 removeAllHandlers(cPtr
);
643 WMFreeArray(cPtr
->outputQueue
); /* will also free the items with the destructor */
646 wfree(cPtr
->address
);
647 wfree(cPtr
->service
);
648 wfree(cPtr
->protocol
);
656 WMCloseConnection(WMConnection
*cPtr
)
659 if (cPtr
->shutdownOnClose
) {
660 shutdown(cPtr
->sock
, SHUT_RDWR
);
666 removeAllHandlers(cPtr
);
667 clearOutputQueue(cPtr
);
669 cPtr
->state
= WCClosed
;
674 WMAcceptConnection(WMConnection
*listener
)
676 struct sockaddr_in clientname
;
679 WMConnection
*newConnection
;
682 wassertrv(listener
&& listener
->state
==WCListening
, NULL
);
684 size
= sizeof(clientname
);
685 newSock
= accept(listener
->sock
, (struct sockaddr
*) &clientname
, &size
);
687 WCErrorCode
= ((errno
!=EAGAIN
&& errno
!=EWOULDBLOCK
) ? errno
: 0);
691 newConnection
= createConnectionWithSocket(newSock
, True
);
692 WMSetConnectionNonBlocking(newConnection
, True
);
693 newConnection
->state
= WCConnected
;
694 setConnectionAddress(newConnection
, &clientname
);
696 return newConnection
;
701 WMGetConnectionAddress(WMConnection
*cPtr
)
703 return cPtr
->address
;
708 WMGetConnectionService(WMConnection
*cPtr
)
710 return cPtr
->service
;
715 WMGetConnectionProtocol(WMConnection
*cPtr
)
717 return cPtr
->protocol
;
722 WMGetConnectionSocket(WMConnection
*cPtr
)
729 WMGetConnectionState(WMConnection
*cPtr
)
735 WMConnectionTimeoutState
736 WMGetConnectionTimeoutState(WMConnection
*cPtr
)
738 return cPtr
->timeoutState
;
743 WMEnqueueConnectionData(WMConnection
*cPtr
, WMData
*data
)
745 wassertrv(cPtr
->state
!=WCNotConnected
&& cPtr
->state
!=WCListening
, False
);
746 wassertrv(cPtr
->state
!=WCInProgress
&& cPtr
->state
!=WCFailed
, False
);
748 if (cPtr
->state
!=WCConnected
)
751 WMAddToArray(cPtr
->outputQueue
, WMRetainData(data
));
758 * -1 - not connected or connection died while sending
759 * 0 - couldn't send the data (or part of it). data is saved in a queue
760 * and will be sent when possible. after it is sent the canResumeSending
761 * callback will be called.
762 * 1 - data was succesfully sent
765 WMSendConnectionData(WMConnection
*cPtr
, WMData
*data
)
768 TimeoutData
*tPtr
= &cPtr
->sendTimeout
;
769 const unsigned char *dataBytes
;
771 wassertrv(cPtr
->state
!=WCNotConnected
&& cPtr
->state
!=WCListening
, -1);
772 wassertrv(cPtr
->state
!=WCInProgress
&& cPtr
->state
!=WCFailed
, -1);
774 if (cPtr
->state
!=WCConnected
)
777 /* If we have no data just flush the queue, else try to send data */
778 if (data
&& WMGetDataLength(data
)>0) {
779 WMAddToArray(cPtr
->outputQueue
, WMRetainData(data
));
780 /* If there already was something in queue, and also a write input
781 * handler is established, it means we were unable to send, so
782 * return and let the write handler notify us when we can send.
784 if (WMGetArrayItemCount(cPtr
->outputQueue
)>1 && cPtr
->handler
.write
)
788 while (WMGetArrayItemCount(cPtr
->outputQueue
) > 0) {
789 data
= WMGetFromArray(cPtr
->outputQueue
, 0);
790 dataBytes
= (const unsigned char *)WMDataBytes(data
);
791 len
= WMGetDataLength(data
);
792 pos
= cPtr
->bufPos
; /* where we're left last time */
795 bytes
= write(cPtr
->sock
, dataBytes
+pos
, len
- pos
);
801 /* save the position where we're left and add a timeout */
803 if (!tPtr
->handler
) {
804 tPtr
->handler
= WMAddTimerHandler(tPtr
->timeout
*1000,
807 if (!cPtr
->handler
.write
) {
808 cPtr
->handler
.write
=
809 WMAddInputHandler(cPtr
->sock
, WIWriteMask
,
815 cPtr
->state
= WCDied
;
816 removeAllHandlers(cPtr
);
817 if (cPtr
->delegate
&& cPtr
->delegate
->didDie
)
818 (*cPtr
->delegate
->didDie
)(cPtr
->delegate
, cPtr
);
824 WMDeleteFromArray(cPtr
->outputQueue
, 0);
827 WMDeleteTimerHandler(tPtr
->handler
);
828 tPtr
->handler
= NULL
;
830 /*if (cPtr->handler.write) {
831 WMDeleteInputHandler(cPtr->handler.write);
832 cPtr->handler.write = NULL;
836 if (cPtr
->handler
.write
) {
837 WMDeleteInputHandler(cPtr
->handler
.write
);
838 cPtr
->handler
.write
= NULL
;
846 * WMGetConnectionAvailableData(connection):
848 * will return a WMData structure containing the available data on the
849 * specified connection. If connection is non-blocking (default) and no data
850 * is available when this function is called, an empty WMData is returned.
852 * If an error occurs while reading or the other side closed connection,
853 * it will return NULL.
854 * Also trying to read from an already died or closed connection is
855 * considered to be an error condition, and will return NULL.
858 WMGetConnectionAvailableData(WMConnection
*cPtr
)
860 char buffer
[NETBUF_SIZE
];
864 wassertrv(cPtr
->state
!=WCNotConnected
&& cPtr
->state
!=WCListening
, NULL
);
865 wassertrv(cPtr
->state
!=WCInProgress
&& cPtr
->state
!=WCFailed
, NULL
);
867 if (cPtr
->state
!=WCConnected
)
873 nbytes
= read(cPtr
->sock
, buffer
, NETBUF_SIZE
);
879 aData
= WMCreateDataWithCapacity(0);
883 cPtr
->state
= WCDied
;
884 removeAllHandlers(cPtr
);
885 if (cPtr
->delegate
&& cPtr
->delegate
->didDie
)
886 (*cPtr
->delegate
->didDie
)(cPtr
->delegate
, cPtr
);
889 } else if (nbytes
==0) { /* the other side has closed connection */
890 cPtr
->state
= WCClosed
;
891 removeAllHandlers(cPtr
);
892 if (cPtr
->delegate
&& cPtr
->delegate
->didDie
)
893 (*cPtr
->delegate
->didDie
)(cPtr
->delegate
, cPtr
);
895 aData
= WMCreateDataWithBytes(buffer
, nbytes
);
903 WMSetConnectionDelegate(WMConnection
*cPtr
, ConnectionDelegate
*delegate
)
905 wassertr(cPtr
->sock
>= 0);
906 /* Don't try to set the delegate multiple times */
907 wassertr(cPtr
->delegate
== NULL
);
909 cPtr
->delegate
= delegate
;
910 if (delegate
&& delegate
->didReceiveInput
&& !cPtr
->handler
.read
)
911 cPtr
->handler
.read
= WMAddInputHandler(cPtr
->sock
, WIReadMask
,
913 if (delegate
&& delegate
->didCatchException
&& !cPtr
->handler
.exception
)
914 cPtr
->handler
.exception
= WMAddInputHandler(cPtr
->sock
, WIExceptMask
,
921 WMIsConnectionNonBlocking(WMConnection
*cPtr
)
926 state
= fcntl(cPtr
->sock
, F_GETFL
, 0);
929 /* If we can't use fcntl on socket, this probably also means we could
930 * not use fcntl to set non-blocking mode, and since a socket defaults
931 * to blocking when created, return False as the best assumption */
935 return ((state
& NONBLOCK_OPT
)!=0);
937 return cPtr
->isNonBlocking
;
944 WMSetConnectionNonBlocking(WMConnection
*cPtr
, Bool flag
)
946 wassertrv(cPtr
!=NULL
&& cPtr
->sock
>=0, False
);
948 flag
= ((flag
==0) ? 0 : 1);
950 if (cPtr
->isNonBlocking
== flag
)
953 if (setSocketNonBlocking(cPtr
->sock
, flag
)==True
) {
954 cPtr
->isNonBlocking
= flag
;
963 WMSetConnectionCloseOnExec(WMConnection
*cPtr
, Bool flag
)
965 wassertrv(cPtr
!=NULL
&& cPtr
->sock
>=0, False
);
967 if (fcntl(cPtr
->sock
, F_SETFD
, ((flag
==0) ? 0 : FD_CLOEXEC
)) < 0) {
976 WMSetConnectionShutdownOnClose(WMConnection
*cPtr
, Bool flag
)
978 cPtr
->shutdownOnClose
= ((flag
==0) ? 0 : 1);
983 WMGetConnectionClientData(WMConnection
*cPtr
)
985 return cPtr
->clientData
;
990 WMSetConnectionClientData(WMConnection
*cPtr
, void *data
)
992 cPtr
->clientData
= data
;
997 WMGetConnectionFlags(WMConnection
*cPtr
)
1004 WMSetConnectionFlags(WMConnection
*cPtr
, unsigned int flags
)
1006 cPtr
->uflags
= flags
;
1011 WMGetConnectionUnsentData(WMConnection
*cPtr
)
1013 return cPtr
->outputQueue
;
1018 WMSetConnectionDefaultTimeout(unsigned int timeout
)
1021 DefaultTimeout
= DEF_TIMEOUT
;
1023 DefaultTimeout
= timeout
;
1029 WMSetConnectionOpenTimeout(unsigned int timeout
)
1032 OpenTimeout
= DefaultTimeout
;
1034 OpenTimeout
= timeout
;
1040 WMSetConnectionSendTimeout(WMConnection
*cPtr
, unsigned int timeout
)
1043 cPtr
->sendTimeout
.timeout
= DefaultTimeout
;
1045 cPtr
->sendTimeout
.timeout
= timeout
;