/[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 10 - (show annotations)
Tue Aug 15 10:23:24 2000 UTC (23 years, 8 months ago) by matty
File MIME type: text/plain
File size: 3393 byte(s)
Major commit of work from laptop - done in various free moments.
Implemented encryption layer and some basic licensing negotiation.
Reorganised code somewhat. While this is not quite as clean, it is
a lot faster - our parser speed was becoming a bottle-neck.

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

  ViewVC Help
Powered by ViewVC 1.1.26