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 "includes.h" |
22 |
|
23 |
/* Establish a connection on the TCP layer */ |
24 |
HCONN tcp_connect(char *server) |
25 |
{ |
26 |
struct hostent *nslookup; |
27 |
struct sockaddr_in servaddr; |
28 |
struct connection *conn; |
29 |
int sock; |
30 |
int true = 1; |
31 |
|
32 |
if ((nslookup = gethostbyname(server)) != NULL) |
33 |
{ |
34 |
memcpy(&servaddr.sin_addr, nslookup->h_addr, sizeof(servaddr.sin_addr)); |
35 |
} |
36 |
else if (!inet_aton(server, &servaddr.sin_addr)) |
37 |
{ |
38 |
fprintf(stderr, "%s: unable to resolve host\n", server); |
39 |
return NULL; |
40 |
} |
41 |
|
42 |
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) |
43 |
{ |
44 |
fprintf(stderr, "socket: %s\n", strerror(errno)); |
45 |
return NULL; |
46 |
} |
47 |
|
48 |
servaddr.sin_family = AF_INET; |
49 |
servaddr.sin_port = htons(TCP_PORT_RDP); |
50 |
|
51 |
if (connect(sock, (struct sockaddr *)&servaddr, sizeof(struct sockaddr)) < 0) |
52 |
{ |
53 |
fprintf(stderr, "connect: %s\n", strerror(errno)); |
54 |
close(sock); |
55 |
return NULL; |
56 |
} |
57 |
|
58 |
setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, &true, sizeof(true)); |
59 |
|
60 |
conn = xmalloc(sizeof(struct connection)); |
61 |
STREAM_INIT(conn->in, False); |
62 |
STREAM_INIT(conn->out, True); |
63 |
|
64 |
conn->tcp_socket = sock; |
65 |
return conn; |
66 |
} |
67 |
|
68 |
/* Disconnect on the TCP layer */ |
69 |
void tcp_disconnect(HCONN conn) |
70 |
{ |
71 |
close(conn->tcp_socket); |
72 |
free(conn); |
73 |
} |
74 |
|
75 |
/* Send TCP transport data packet */ |
76 |
BOOL tcp_send(HCONN conn) |
77 |
{ |
78 |
int length = conn->out.end; |
79 |
int sent, total = 0; |
80 |
|
81 |
while (total < length) |
82 |
{ |
83 |
sent = write(conn->tcp_socket, conn->out.data + total, |
84 |
length - total); |
85 |
|
86 |
if (sent <= 0) |
87 |
{ |
88 |
fprintf(stderr, "write: %s\n", strerror(errno)); |
89 |
return False; |
90 |
} |
91 |
|
92 |
total += sent; |
93 |
} |
94 |
|
95 |
conn->out.offset = 0; |
96 |
conn->out.end = conn->out.size; |
97 |
return True; |
98 |
} |
99 |
|
100 |
/* Receive a message on the TCP layer */ |
101 |
BOOL tcp_recv(HCONN conn, int length) |
102 |
{ |
103 |
int rcvd; |
104 |
|
105 |
STREAM_SIZE(conn->in, length); |
106 |
conn->in.end = conn->in.offset = 0; |
107 |
|
108 |
while (length > 0) |
109 |
{ |
110 |
rcvd = read(conn->tcp_socket, conn->in.data + conn->in.end, |
111 |
length); |
112 |
|
113 |
if (rcvd <= 0) |
114 |
{ |
115 |
fprintf(stderr, "read: %s\n", strerror(errno)); |
116 |
return False; |
117 |
} |
118 |
|
119 |
conn->in.end += rcvd; |
120 |
length -= rcvd; |
121 |
} |
122 |
|
123 |
return True; |
124 |
} |