Diffstat (limited to 'noncore/net/opierdesktop/tcp.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r-- | noncore/net/opierdesktop/tcp.cpp | 181 |
1 files changed, 181 insertions, 0 deletions
diff --git a/noncore/net/opierdesktop/tcp.cpp b/noncore/net/opierdesktop/tcp.cpp new file mode 100644 index 0000000..0a2ca3e --- a/dev/null +++ b/noncore/net/opierdesktop/tcp.cpp | |||
@@ -0,0 +1,181 @@ | |||
1 | /* | ||
2 | rdesktop: A Remote Desktop Protocol client. | ||
3 | Protocol services - TCP layer | ||
4 | Copyright (C) Matthew Chapman 1999-2002 | ||
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 <fcntl.h> | ||
29 | #include <errno.h> /* errno */ | ||
30 | #include "rdesktop.h" | ||
31 | |||
32 | #ifndef INADDR_NONE | ||
33 | #define INADDR_NONE ((unsigned long) -1) | ||
34 | #endif | ||
35 | |||
36 | static int sock; | ||
37 | static struct stream in; | ||
38 | static struct stream out; | ||
39 | extern int tcp_port_rdp; | ||
40 | |||
41 | /* Initialise TCP transport data packet */ | ||
42 | STREAM | ||
43 | tcp_init(unsigned int maxlen) | ||
44 | { | ||
45 | if (maxlen > out.size) | ||
46 | { | ||
47 | out.data = (unsigned char*)xrealloc(out.data, maxlen); | ||
48 | out.size = maxlen; | ||
49 | } | ||
50 | |||
51 | out.p = out.data; | ||
52 | out.end = out.data + out.size; | ||
53 | return &out; | ||
54 | } | ||
55 | |||
56 | /* Send TCP transport data packet */ | ||
57 | void | ||
58 | tcp_send(STREAM s) | ||
59 | { | ||
60 | int length = s->end - s->data; | ||
61 | int sent, total = 0; | ||
62 | |||
63 | while (total < length) | ||
64 | { | ||
65 | sent = send(sock, s->data + total, length - total, 0); | ||
66 | if (sent == -1 && errno == EWOULDBLOCK) | ||
67 | { | ||
68 | usleep(1000); | ||
69 | } | ||
70 | else if (sent <= 0) | ||
71 | { | ||
72 | error("send: %s\n", strerror(errno)); | ||
73 | return; | ||
74 | } | ||
75 | else | ||
76 | total += sent; | ||
77 | } | ||
78 | } | ||
79 | |||
80 | /* Receive a message on the TCP layer */ | ||
81 | STREAM | ||
82 | tcp_recv(unsigned int length) | ||
83 | { | ||
84 | int rcvd = 0; | ||
85 | |||
86 | if (length > in.size) | ||
87 | { | ||
88 | in.data = (unsigned char*)xrealloc(in.data, length); | ||
89 | in.size = length; | ||
90 | } | ||
91 | |||
92 | in.end = in.p = in.data; | ||
93 | |||
94 | while (length > 0) | ||
95 | { | ||
96 | if (!ui_select(sock)) | ||
97 | /* User quit */ | ||
98 | return NULL; | ||
99 | |||
100 | rcvd = recv(sock, in.end, length, 0); | ||
101 | if (rcvd == -1 && errno == EWOULDBLOCK) | ||
102 | { | ||
103 | usleep(1000); | ||
104 | } | ||
105 | else if (rcvd == -1) | ||
106 | { | ||
107 | error("recv: %s\n", strerror(errno)); | ||
108 | return NULL; | ||
109 | } | ||
110 | else if (rcvd == 0) | ||
111 | return NULL; | ||
112 | else | ||
113 | { | ||
114 | in.end += rcvd; | ||
115 | length -= rcvd; | ||
116 | } | ||
117 | } | ||
118 | |||
119 | return ∈ | ||
120 | } | ||
121 | |||
122 | /* Establish a connection on the TCP layer */ | ||
123 | BOOL | ||
124 | tcp_connect(char *server) | ||
125 | { | ||
126 | struct hostent *nslookup; | ||
127 | struct sockaddr_in servaddr; | ||
128 | int l_true = 1; | ||
129 | |||
130 | if ((nslookup = gethostbyname(server)) != NULL) | ||
131 | { | ||
132 | memcpy(&servaddr.sin_addr, nslookup->h_addr, sizeof(servaddr.sin_addr)); | ||
133 | } | ||
134 | else if ((servaddr.sin_addr.s_addr = inet_addr(server)) == INADDR_NONE) | ||
135 | { | ||
136 | error("%s: unable to resolve host\n", server); | ||
137 | return False; | ||
138 | } | ||
139 | |||
140 | if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) | ||
141 | { | ||
142 | error("socket: %s\n", strerror(errno)); | ||
143 | return False; | ||
144 | } | ||
145 | |||
146 | servaddr.sin_family = AF_INET; | ||
147 | servaddr.sin_port = htons(tcp_port_rdp); | ||
148 | |||
149 | if (connect(sock, (struct sockaddr *) &servaddr, sizeof(struct sockaddr)) < 0) | ||
150 | { | ||
151 | error("connect: %s\n", strerror(errno)); | ||
152 | close(sock); | ||
153 | return False; | ||
154 | } | ||
155 | |||
156 | /* set non blocking */ | ||
157 | { | ||
158 | int op; | ||
159 | op = fcntl(sock, F_GETFL); | ||
160 | op |= O_NONBLOCK; | ||
161 | fcntl(sock, F_SETFL, op); | ||
162 | } | ||
163 | |||
164 | //fcntl(sock, O_NONBLOCK); | ||
165 | setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, (void *) &l_true, sizeof(l_true)); | ||
166 | |||
167 | in.size = 4096; | ||
168 | in.data = (unsigned char*)xmalloc(in.size); | ||
169 | |||
170 | out.size = 4096; | ||
171 | out.data = (unsigned char*)xmalloc(out.size); | ||
172 | |||
173 | return True; | ||
174 | } | ||
175 | |||
176 | /* Disconnect on the TCP layer */ | ||
177 | void | ||
178 | tcp_disconnect(void) | ||
179 | { | ||
180 | close(sock); | ||
181 | } | ||