2 * Copyright (C) 1999 AT&T Laboratories Cambridge. All Rights Reserved.
4 * This is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This software is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this software; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
19 * 03-05-2002 tim@tjansen.de: removed Xt event processing for krdc
23 * sockets.c - functions to deal with sockets.
27 #include <sys/socket.h>
29 #include <netinet/in.h>
30 #include <netinet/tcp.h>
31 #include <arpa/inet.h>
35 #include "vncviewer.h"
37 void PrintInHex(char *buf
, int len
);
39 Bool errorMessageOnReadFailure
= True
;
42 static char buf
[BUF_SIZE
];
43 static char *bufoutptr
= buf
;
44 static unsigned int buffered
= 0;
46 /* Wait duration of select in seconds */
47 #define SELECT_PERIOD 3
51 * ReadFromRFBServer is called whenever we want to read some data from the RFB
55 ReadFromRFBServer(char *out
, unsigned int n
)
65 memcpy(out
, bufoutptr
, n
);
71 memcpy(out
, bufoutptr
, buffered
);
81 while (buffered
< n
) {
85 i
= read(rfbsock
, buf
+ buffered
, BUF_SIZE
- buffered
);
89 if (errno
== EWOULDBLOCK
|| errno
== EAGAIN
) {
93 tx
.tv_sec
= SELECT_PERIOD
;
95 if ((e
=select(rfbsock
+1, &fds
, NULL
, &fds
, &tx
)) < 0) {
96 perror("krdc: select read");
101 perror("krdc: read");
105 fprintf(stderr
,"VNC server closed connection\n");
112 memcpy(out
, bufoutptr
, n
);
115 return isQuitFlagSet() ? False
: True
;
123 i
= read(rfbsock
, out
, n
);
126 if (errno
== EWOULDBLOCK
|| errno
== EAGAIN
) {
128 FD_SET(rfbsock
,&fds
);
130 tx
.tv_sec
= SELECT_PERIOD
;
132 if ((e
=select(rfbsock
+1, &fds
, NULL
, &fds
, &tx
)) < 0) {
133 perror("krdc: select");
138 perror("krdc: read");
142 fprintf(stderr
,"VNC server closed connection\n");
150 return isQuitFlagSet() ? False
: True
;
156 * Write an exact number of bytes, and don't return until you've sent them.
157 * Note: this should only be called by the WriterThread
161 WriteExact(int sock
, const char *_buf
, int n
)
172 j
= write(sock
, _buf
+ i
, (n
- i
));
175 if (errno
== EWOULDBLOCK
|| errno
== EAGAIN
) {
177 FD_SET(rfbsock
,&fds
);
179 tx
.tv_sec
= SELECT_PERIOD
;
181 if ((e
=select(rfbsock
+1, NULL
, &fds
, NULL
, &tx
)) < 0) {
182 perror("krdc: select write");
187 perror("krdc: write");
191 fprintf(stderr
,"write failed\n");
202 * ConnectToTcpAddr connects to the given TCP port.
206 ConnectToTcpAddr(unsigned int host
, int port
)
209 struct sockaddr_in addr
;
212 addr
.sin_family
= AF_INET
;
213 addr
.sin_port
= htons(port
);
214 addr
.sin_addr
.s_addr
= host
;
216 sock
= socket(AF_INET
, SOCK_STREAM
, 0);
218 perror("krdc: ConnectToTcpAddr: socket");
219 return -(int)INIT_CONNECTION_FAILED
;
222 if (connect(sock
, (struct sockaddr
*)&addr
, sizeof(addr
)) < 0) {
223 perror("krdc: ConnectToTcpAddr: connect");
225 return -(int)INIT_NO_SERVER
;
228 if (setsockopt(sock
, IPPROTO_TCP
, TCP_NODELAY
,
229 (char *)&one
, sizeof(one
)) < 0) {
230 perror("krdc: ConnectToTcpAddr: setsockopt");
232 return -(int)INIT_CONNECTION_FAILED
;
235 if (fcntl(sock
, F_SETFL
, O_NONBLOCK
) < 0) {
236 perror(": AcceptTcpConnection: fcntl");
238 return -(int)INIT_CONNECTION_FAILED
;
246 * StringToIPAddr - convert a host string to an IP address.
250 StringToIPAddr(const char *str
, unsigned int *addr
)
254 if (strcmp(str
,"") == 0) {
255 *addr
= 0; /* local */
259 *addr
= inet_addr(str
);
264 hp
= gethostbyname(str
);
267 *addr
= *(unsigned int *)hp
->h_addr
;
276 * Print out the contents of a packet for debugging.
280 PrintInHex(char *_buf
, int len
)
287 fprintf(stderr
,"ReadExact: ");
289 for (i
= 0; i
< len
; i
++)
291 if ((i
% 16 == 0) && (i
!= 0)) {
295 str
[i
% 16] = (((c
> 31) && (c
< 127)) ? c
: '.');
296 fprintf(stderr
,"%02x ",(unsigned char)c
);
301 fprintf(stderr
,"%s\n",str
);
306 for (j
= i
% 16; j
< 16; j
++)
309 if ((j
% 4) == 3) fprintf(stderr
," ");
312 fprintf(stderr
,"%s\n",str
);
318 void freeSocketsResources() {
321 errorMessageOnReadFailure
= True
;