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

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

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 4 by matty, Wed May 10 07:36:34 2000 UTC revision 1328 by stargo, Fri Nov 3 23:51:35 2006 UTC
# Line 1  Line 1 
1  /*  /* -*- c-basic-offset: 8 -*-
2     rdesktop: A Remote Desktop Protocol client.     rdesktop: A Remote Desktop Protocol client.
3     Protocol services - TCP layer     Protocol services - TCP layer
4     Copyright (C) Matthew Chapman 1999-2000     Copyright (C) Matthew Chapman 1999-2005
5        
6     This program is free software; you can redistribute it and/or modify     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     it under the terms of the GNU General Public License as published by
# Line 12  Line 12 
12     but WITHOUT ANY WARRANTY; without even the implied warranty of     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.     GNU General Public License for more details.
15      
16     You should have received a copy of the GNU General Public License     You should have received a copy of the GNU General Public License
17     along with this program; if not, write to the Free Software     along with this program; if not, write to the Free Software
18     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19  */  */
20    
21  #include "includes.h"  #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    #include "rdesktop.h"
30    
31    #ifndef INADDR_NONE
32    #define INADDR_NONE ((unsigned long) -1)
33    #endif
34    
35    static int sock;
36    static struct stream in;
37    #ifndef WITH_SCARD
38    static struct stream out;
39    #endif
40    int g_tcp_port_rdp = TCP_PORT_RDP;
41    
42    /* Initialise TCP transport data packet */
43    STREAM
44    tcp_init(uint32 maxlen)
45    {
46            STREAM result = NULL;
47    
48    #ifdef WITH_SCARD
49            scard_lock(SCARD_LOCK_TCP);
50            result = scard_tcp_init();
51    #else
52            result = &out;
53    #endif
54    
55            if (maxlen > result->size)
56            {
57                    result->data = (uint8 *) xrealloc(result->data, maxlen);
58                    result->size = maxlen;
59            }
60    
61            result->p = result->data;
62            result->end = result->data + result->size;
63    #ifdef WITH_SCARD
64            scard_unlock(SCARD_LOCK_TCP);
65    #endif
66            return result;
67    }
68    
69    /* Send TCP transport data packet */
70    void
71    tcp_send(STREAM s)
72    {
73            int length = s->end - s->data;
74            int sent, total = 0;
75    
76    #ifdef WITH_SCARD
77            scard_lock(SCARD_LOCK_TCP);
78    #endif
79            while (total < length)
80            {
81                    sent = send(sock, s->data + total, length - total, 0);
82                    if (sent <= 0)
83                    {
84                            error("send: %s\n", strerror(errno));
85                            return;
86                    }
87    
88                    total += sent;
89            }
90    #ifdef WITH_SCARD
91            scard_unlock(SCARD_LOCK_TCP);
92    #endif
93    }
94    
95    /* Receive a message on the TCP layer */
96    STREAM
97    tcp_recv(STREAM s, uint32 length)
98    {
99            unsigned int new_length, end_offset, p_offset;
100            int rcvd = 0;
101    
102            if (s == NULL)
103            {
104                    /* read into "new" stream */
105                    if (length > in.size)
106                    {
107                            in.data = (uint8 *) xrealloc(in.data, length);
108                            in.size = length;
109                    }
110                    in.end = in.p = in.data;
111                    s = &in;
112            }
113            else
114            {
115                    /* append to existing stream */
116                    new_length = (s->end - s->data) + length;
117                    if (new_length > s->size)
118                    {
119                            p_offset = s->p - s->data;
120                            end_offset = s->end - s->data;
121                            s->data = (uint8 *) xrealloc(s->data, new_length);
122                            s->size = new_length;
123                            s->p = s->data + p_offset;
124                            s->end = s->data + end_offset;
125                    }
126            }
127    
128            while (length > 0)
129            {
130                    if (!ui_select(sock))
131                            /* User quit */
132                            return NULL;
133    
134                    rcvd = recv(sock, s->end, length, 0);
135                    if (rcvd < 0)
136                    {
137                            error("recv: %s\n", strerror(errno));
138                            return NULL;
139                    }
140                    else if (rcvd == 0)
141                    {
142                            error("Connection closed\n");
143                            return NULL;
144                    }
145    
146                    s->end += rcvd;
147                    length -= rcvd;
148            }
149    
150            return s;
151    }
152    
153  /* Establish a connection on the TCP layer */  /* Establish a connection on the TCP layer */
154  HCONN tcp_connect(char *server)  BOOL
155    tcp_connect(char *server)
156  {  {
157            int true_value = 1;
158    
159    #ifdef IPv6
160    
161            int n;
162            struct addrinfo hints, *res, *ressave;
163            char tcp_port_rdp_s[10];
164    
165            snprintf(tcp_port_rdp_s, 10, "%d", g_tcp_port_rdp);
166    
167            memset(&hints, 0, sizeof(struct addrinfo));
168            hints.ai_family = AF_UNSPEC;
169            hints.ai_socktype = SOCK_STREAM;
170    
171            if ((n = getaddrinfo(server, tcp_port_rdp_s, &hints, &res)))
172            {
173                    error("getaddrinfo: %s\n", gai_strerror(n));
174                    return False;
175            }
176    
177            ressave = res;
178            sock = -1;
179            while (res)
180            {
181                    sock = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
182                    if (!(sock < 0))
183                    {
184                            if (connect(sock, res->ai_addr, res->ai_addrlen) == 0)
185                                    break;
186                            close(sock);
187                            sock = -1;
188                    }
189                    res = res->ai_next;
190            }
191            freeaddrinfo(ressave);
192    
193            if (sock == -1)
194            {
195                    error("%s: unable to connect\n", server);
196                    return False;
197            }
198    
199    #else /* no IPv6 support */
200    
201          struct hostent *nslookup;          struct hostent *nslookup;
202          struct sockaddr_in servaddr;          struct sockaddr_in servaddr;
         struct connection *conn;  
         int sock;  
         int true = 1;  
203    
204          if ((nslookup = gethostbyname(server)) != NULL)          if ((nslookup = gethostbyname(server)) != NULL)
205          {          {
206                  memcpy(&servaddr.sin_addr, nslookup->h_addr, sizeof(servaddr.sin_addr));                  memcpy(&servaddr.sin_addr, nslookup->h_addr, sizeof(servaddr.sin_addr));
207          }          }
208          else if (!inet_aton(server, &servaddr.sin_addr))          else if ((servaddr.sin_addr.s_addr = inet_addr(server)) == INADDR_NONE)
209          {          {
210                  fprintf(stderr, "%s: unable to resolve host\n", server);                  error("%s: unable to resolve host\n", server);
211                  return NULL;                  return False;
212          }          }
213    
214          if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0)          if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0)
215          {          {
216                  fprintf(stderr, "socket: %s\n", strerror(errno));                  error("socket: %s\n", strerror(errno));
217                  return NULL;                  return False;
218          }          }
219    
220          servaddr.sin_family = AF_INET;          servaddr.sin_family = AF_INET;
221          servaddr.sin_port = htons(TCP_PORT_RDP);          servaddr.sin_port = htons(g_tcp_port_rdp);
222    
223          if (connect(sock, (struct sockaddr *)&servaddr, sizeof(struct sockaddr)) < 0)          if (connect(sock, (struct sockaddr *) &servaddr, sizeof(struct sockaddr)) < 0)
224          {          {
225                  fprintf(stderr, "connect: %s\n", strerror(errno));                  error("connect: %s\n", strerror(errno));
226                  close(sock);                  close(sock);
227                  return NULL;                  return False;
228          }          }
229    
230          setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, &true, sizeof(true));  #endif /* IPv6 */
231    
232            setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, (void *) &true_value, sizeof(true_value));
233    
234            in.size = 4096;
235            in.data = (uint8 *) xmalloc(in.size);
236    
237          conn = xmalloc(sizeof(struct connection));  #ifdef WITH_SCARD
238          STREAM_INIT(conn->in,  False);          scard_tcp_connect();
239          STREAM_INIT(conn->out, True);  #else
240            out.size = 4096;
241            out.data = (uint8 *) xmalloc(out.size);
242    #endif
243    
244          conn->tcp_socket = sock;          return True;
         return conn;  
245  }  }
246    
247  /* Disconnect on the TCP layer */  /* Disconnect on the TCP layer */
248  void tcp_disconnect(HCONN conn)  void
249    tcp_disconnect(void)
250  {  {
251          close(conn->tcp_socket);          close(sock);
         free(conn);  
252  }  }
253    
254  /* Send TCP transport data packet */  char *
255  BOOL tcp_send(HCONN conn)  tcp_get_address()
256  {  {
257          int length = conn->out.end;          static char ipaddr[32];
258          int sent, total = 0;          struct sockaddr_in sockaddr;
259            socklen_t len = sizeof(sockaddr);
260          while (total < length)          if (getsockname(sock, (struct sockaddr *) &sockaddr, &len) == 0)
261          {          {
262                  sent = write(conn->tcp_socket, conn->out.data + total,                  unsigned char *ip = (unsigned char *) &sockaddr.sin_addr;
263                               length - total);                  sprintf(ipaddr, "%d.%d.%d.%d", ip[0], ip[1], ip[2], ip[3]);
   
                 if (sent <= 0)  
                 {  
                         fprintf(stderr, "write: %s\n", strerror(errno));  
                         return False;  
                 }  
   
                 total += sent;  
264          }          }
265            else
266          conn->out.offset = 0;                  strcpy(ipaddr, "127.0.0.1");
267          conn->out.end = conn->out.size;          return ipaddr;
         return True;  
268  }  }
269    
270  /* Receive a message on the TCP layer */  /* reset the state of the tcp layer */
271  BOOL tcp_recv(HCONN conn, int length)  /* Support for Session Directory */
272    void
273    tcp_reset_state(void)
274  {  {
275          int rcvd;          sock = -1;              /* reset socket */
276    
277          STREAM_SIZE(conn->in, length);          /* Clear the incoming stream */
278          conn->in.end = conn->in.offset = 0;          if (in.data != NULL)
279                    xfree(in.data);
280          while (length > 0)          in.p = NULL;
281          {          in.end = NULL;
282                  rcvd = read(conn->tcp_socket, conn->in.data + conn->in.end,          in.data = NULL;
283                              length);          in.size = 0;
284            in.iso_hdr = NULL;
285                  if (rcvd <= 0)          in.mcs_hdr = NULL;
286                  {          in.sec_hdr = NULL;
287                          fprintf(stderr, "read: %s\n", strerror(errno));          in.rdp_hdr = NULL;
288                          return False;          in.channel_hdr = NULL;
289                  }  
290            /* Clear the outgoing stream(s) */
291                  conn->in.end += rcvd;  #ifdef WITH_SCARD
292                  length -= rcvd;          scard_tcp_reset_state();
293          }  #else
294            if (out.data != NULL)
295          return True;                  xfree(out.data);
296            out.p = NULL;
297            out.end = NULL;
298            out.data = NULL;
299            out.size = 0;
300            out.iso_hdr = NULL;
301            out.mcs_hdr = NULL;
302            out.sec_hdr = NULL;
303            out.rdp_hdr = NULL;
304            out.channel_hdr = NULL;
305    #endif
306  }  }

Legend:
Removed from v.4  
changed lines
  Added in v.1328

  ViewVC Help
Powered by ViewVC 1.1.26