/[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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 9 - (hide annotations)
Tue Jul 25 12:34:29 2000 UTC (23 years, 10 months ago) by matty
File MIME type: text/plain
File size: 3106 byte(s)
Committing some awesome progress I made while overseas - this commit
really embodies a huge number of changes. We are now able to talk quite
fluently to a French NT Terminal Server - in normal usage only minor
font issues remain (handling of TEXT2 order is not perfect).

The next major hurdle is encryption, and it will be quite a big hurdle
- there seems to be some quite nasty session key stuff.

1 matty 3 /*
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 matty 9 int ret, rcvd = 0;
104     struct timeval tv;
105     fd_set rfds;
106 matty 3
107     STREAM_SIZE(conn->in, length);
108     conn->in.end = conn->in.offset = 0;
109    
110     while (length > 0)
111     {
112 matty 9 ui_process_events(conn->wnd, conn);
113 matty 3
114 matty 9 FD_ZERO(&rfds);
115     FD_SET(conn->tcp_socket, &rfds);
116     tv.tv_sec = 0;
117     tv.tv_usec = 100;
118    
119     ret = select(conn->tcp_socket+1, &rfds, NULL, NULL, &tv);
120    
121     if (ret)
122 matty 3 {
123 matty 9 rcvd = read(conn->tcp_socket, conn->in.data
124     + conn->in.end, length);
125    
126     if (rcvd <= 0)
127     {
128     fprintf(stderr, "read: %s\n",
129     strerror(errno));
130     return False;
131     }
132    
133     conn->in.end += rcvd;
134     length -= rcvd;
135 matty 3 }
136     }
137    
138     return True;
139     }

  ViewVC Help
Powered by ViewVC 1.1.26