/[rdesktop]/sourceforge.net/trunk/rdesktop/tcp.c
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Contents of /sourceforge.net/trunk/rdesktop/tcp.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 13 - (show annotations)
Sat Sep 16 12:07:55 2000 UTC (23 years, 8 months ago) by matty
File MIME type: text/plain
File size: 3452 byte(s)
FreeBSD compile fixes from Chris Knight <chris@aims.com.au>

1 /*
2 rdesktop: A Remote Desktop Protocol client.
3 Protocol services - TCP layer
4 Copyright (C) Matthew Chapman 1999-2000
5
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.
10
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.
15
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.
19 */
20
21 #include <unistd.h> /* select read write close */
22 #include <sys/socket.h> /* socket connect setsockopt */
23 #include <sys/time.h> /* timeval */
24 #include <netdb.h> /* gethostbyname */
25 #include <netinet/in.h> /* sockaddr_in */
26 #include <netinet/tcp.h> /* TCP_NODELAY */
27 #include <arpa/inet.h> /* inet_addr */
28 #include <errno.h> /* errno */
29 #include "rdesktop.h"
30
31 static int sock;
32 static struct stream in;
33 static struct stream out;
34
35 /* Initialise TCP transport data packet */
36 STREAM tcp_init(int maxlen)
37 {
38 if (maxlen > out.size)
39 {
40 out.data = xrealloc(out.data, maxlen);
41 out.size = maxlen;
42 }
43
44 out.p = out.data;
45 out.end = out.data + out.size;
46 return &out;
47 }
48
49 /* Send TCP transport data packet */
50 void tcp_send(STREAM s)
51 {
52 int length = s->end - s->data;
53 int sent, total = 0;
54
55 while (total < length)
56 {
57 sent = write(sock, s->data + total, length - total);
58
59 if (sent <= 0)
60 {
61 STATUS("write: %s\n", strerror(errno));
62 return;
63 }
64
65 total += sent;
66 }
67 }
68
69 /* Receive a message on the TCP layer */
70 STREAM tcp_recv(int length)
71 {
72 int ret, rcvd = 0;
73 struct timeval tv;
74 fd_set rfds;
75
76 if (length > in.size)
77 {
78 in.data = xrealloc(in.data, length);
79 in.size = length;
80 }
81
82 in.end = in.p = in.data;
83
84 while (length > 0)
85 {
86 ui_process_events();
87
88 FD_ZERO(&rfds);
89 FD_SET(sock, &rfds);
90 tv.tv_sec = 0;
91 tv.tv_usec = 100;
92
93 ret = select(sock+1, &rfds, NULL, NULL, &tv);
94
95 if (ret)
96 {
97 rcvd = read(sock, in.end, length);
98
99 if (rcvd <= 0)
100 {
101 STATUS("read: %s\n", strerror(errno));
102 return NULL;
103 }
104
105 in.end += rcvd;
106 length -= rcvd;
107 }
108 }
109
110 return &in;
111 }
112
113 /* Establish a connection on the TCP layer */
114 BOOL tcp_connect(char *server)
115 {
116 struct hostent *nslookup;
117 struct sockaddr_in servaddr;
118 int true = 1;
119
120 if ((nslookup = gethostbyname(server)) != NULL)
121 {
122 memcpy(&servaddr.sin_addr, nslookup->h_addr, sizeof(servaddr.sin_addr));
123 }
124 else if (!(servaddr.sin_addr.s_addr = inet_addr(server)))
125 {
126 STATUS("%s: unable to resolve host\n", server);
127 return False;
128 }
129
130 if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0)
131 {
132 STATUS("socket: %s\n", strerror(errno));
133 return False;
134 }
135
136 servaddr.sin_family = AF_INET;
137 servaddr.sin_port = htons(TCP_PORT_RDP);
138
139 if (connect(sock, (struct sockaddr *)&servaddr, sizeof(struct sockaddr)) < 0)
140 {
141 STATUS("connect: %s\n", strerror(errno));
142 close(sock);
143 return False;
144 }
145
146 setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, (void *)&true, sizeof(true));
147
148 in.size = 4096;
149 in.data = xmalloc(in.size);
150
151 out.size = 4096;
152 out.data = xmalloc(out.size);
153
154 return True;
155 }
156
157 /* Disconnect on the TCP layer */
158 void tcp_disconnect()
159 {
160 close(sock);
161 }

  ViewVC Help
Powered by ViewVC 1.1.26