/[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 24 - (hide annotations)
Sat Jan 6 03:12:10 2001 UTC (23 years, 4 months ago) by matty
File MIME type: text/plain
File size: 3488 byte(s)
ran indent (-bli0 -i8 -cli8 -npcs -npsl)

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

  ViewVC Help
Powered by ViewVC 1.1.26