/[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 13 - (hide annotations)
Sat Sep 16 12:07:55 2000 UTC (23 years, 8 months ago) by matty
File MIME type: text/plain
File size: 3452 byte(s)
FreeBSD compile fixes from Chris Knight <chris@aims.com.au>

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 10 #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 matty 13 #include <netinet/in.h> /* sockaddr_in */
26 matty 10 #include <netinet/tcp.h> /* TCP_NODELAY */
27 matty 13 #include <arpa/inet.h> /* inet_addr */
28 matty 10 #include <errno.h> /* errno */
29     #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 10 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 10 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     memcpy(&servaddr.sin_addr, nslookup->h_addr, sizeof(servaddr.sin_addr));
123     }
124 matty 12 else if (!(servaddr.sin_addr.s_addr = inet_addr(server)))
125 matty 10 {
126     STATUS("%s: unable to resolve host\n", server);
127     return False;
128     }
129    
130     if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0)
131     {
132     STATUS("socket: %s\n", strerror(errno));
133     return False;
134     }
135    
136     servaddr.sin_family = AF_INET;
137     servaddr.sin_port = htons(TCP_PORT_RDP);
138    
139     if (connect(sock, (struct sockaddr *)&servaddr, sizeof(struct sockaddr)) < 0)
140     {
141     STATUS("connect: %s\n", strerror(errno));
142     close(sock);
143     return False;
144     }
145    
146 matty 12 setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, (void *)&true, sizeof(true));
147 matty 10
148     in.size = 4096;
149     in.data = xmalloc(in.size);
150    
151     out.size = 4096;
152     out.data = xmalloc(out.size);
153    
154 matty 3 return True;
155     }
156 matty 10
157     /* Disconnect on the TCP layer */
158     void tcp_disconnect()
159     {
160     close(sock);
161     }

  ViewVC Help
Powered by ViewVC 1.1.26