/[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 12 by matty, Tue Aug 15 12:01:01 2000 UTC revision 30 by matty, Fri Sep 14 13:51:38 2001 UTC
# Line 1  Line 1 
1  /*  /*
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-2001
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 18  Line 18 
18     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19  */  */
20    
21  #include <unistd.h>     /* select read write close */  #include <unistd.h>             /* select read write close */
22  #include <sys/socket.h> /* socket connect setsockopt */  #include <sys/socket.h>         /* socket connect setsockopt */
23  #include <sys/time.h>   /* timeval */  #include <sys/time.h>           /* timeval */
24  #include <netdb.h>      /* gethostbyname */  #include <netdb.h>              /* gethostbyname */
25  #include <netinet/tcp.h> /* TCP_NODELAY */  #include <netinet/in.h>         /* sockaddr_in */
26  #include <arpa/inet.h>  /* sockaddr_in inet_addr */  #include <netinet/tcp.h>        /* TCP_NODELAY */
27  #include <errno.h>      /* errno */  #include <arpa/inet.h>          /* inet_addr */
28    #include <errno.h>              /* errno */
29  #include "rdesktop.h"  #include "rdesktop.h"
30    
31  static int sock;  static int sock;
# Line 32  static struct stream in; Line 33  static struct stream in;
33  static struct stream out;  static struct stream out;
34    
35  /* Initialise TCP transport data packet */  /* Initialise TCP transport data packet */
36  STREAM tcp_init(int maxlen)  STREAM
37    tcp_init(int maxlen)
38  {  {
39          if (maxlen > out.size)          if (maxlen > out.size)
40          {          {
# Line 46  STREAM tcp_init(int maxlen) Line 48  STREAM tcp_init(int maxlen)
48  }  }
49    
50  /* Send TCP transport data packet */  /* Send TCP transport data packet */
51  void tcp_send(STREAM s)  void
52    tcp_send(STREAM s)
53  {  {
54          int length = s->end - s->data;          int length = s->end - s->data;
55          int sent, total = 0;          int sent, total = 0;
56    
57          while (total < length)          while (total < length)
# Line 57  void tcp_send(STREAM s) Line 60  void tcp_send(STREAM s)
60    
61                  if (sent <= 0)                  if (sent <= 0)
62                  {                  {
63                          STATUS("write: %s\n", strerror(errno));                          error("write: %s\n", strerror(errno));
64                          return;                          return;
65                  }                  }
66    
# Line 66  void tcp_send(STREAM s) Line 69  void tcp_send(STREAM s)
69  }  }
70    
71  /* Receive a message on the TCP layer */  /* Receive a message on the TCP layer */
72  STREAM tcp_recv(int length)  STREAM
73    tcp_recv(int length)
74  {  {
75          int ret, rcvd = 0;          int ret, rcvd = 0;
76          struct timeval tv;          struct timeval tv;
# Line 89  STREAM tcp_recv(int length) Line 93  STREAM tcp_recv(int length)
93                  tv.tv_sec = 0;                  tv.tv_sec = 0;
94                  tv.tv_usec = 100;                  tv.tv_usec = 100;
95    
96                  ret = select(sock+1, &rfds, NULL, NULL, &tv);                  ret = select(sock + 1, &rfds, NULL, NULL, &tv);
97    
98                  if (ret)                  if (ret)
99                  {                  {
# Line 97  STREAM tcp_recv(int length) Line 101  STREAM tcp_recv(int length)
101    
102                          if (rcvd <= 0)                          if (rcvd <= 0)
103                          {                          {
104                                  STATUS("read: %s\n", strerror(errno));                                  error("read: %s\n", strerror(errno));
105                                  return NULL;                                  return NULL;
106                          }                          }
107    
# Line 110  STREAM tcp_recv(int length) Line 114  STREAM tcp_recv(int length)
114  }  }
115    
116  /* Establish a connection on the TCP layer */  /* Establish a connection on the TCP layer */
117  BOOL tcp_connect(char *server)  BOOL
118    tcp_connect(char *server)
119  {  {
120          struct hostent *nslookup;          struct hostent *nslookup;
121          struct sockaddr_in servaddr;          struct sockaddr_in servaddr;
# Line 118  BOOL tcp_connect(char *server) Line 123  BOOL tcp_connect(char *server)
123    
124          if ((nslookup = gethostbyname(server)) != NULL)          if ((nslookup = gethostbyname(server)) != NULL)
125          {          {
126                  memcpy(&servaddr.sin_addr, nslookup->h_addr, sizeof(servaddr.sin_addr));                  memcpy(&servaddr.sin_addr, nslookup->h_addr,
127                           sizeof(servaddr.sin_addr));
128          }          }
129          else if (!(servaddr.sin_addr.s_addr = inet_addr(server)))          else if (!(servaddr.sin_addr.s_addr = inet_addr(server)))
130          {          {
131                  STATUS("%s: unable to resolve host\n", server);                  error("%s: unable to resolve host\n", server);
132                  return False;                  return False;
133          }          }
134    
135          if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0)          if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0)
136          {          {
137                  STATUS("socket: %s\n", strerror(errno));                  error("socket: %s\n", strerror(errno));
138                  return False;                  return False;
139          }          }
140    
141          servaddr.sin_family = AF_INET;          servaddr.sin_family = AF_INET;
142          servaddr.sin_port = htons(TCP_PORT_RDP);          servaddr.sin_port = htons(TCP_PORT_RDP);
143    
144          if (connect(sock, (struct sockaddr *)&servaddr, sizeof(struct sockaddr)) < 0)          if (connect
145                (sock, (struct sockaddr *) &servaddr,
146                 sizeof(struct sockaddr)) < 0)
147          {          {
148                  STATUS("connect: %s\n", strerror(errno));                  error("connect: %s\n", strerror(errno));
149                  close(sock);                  close(sock);
150                  return False;                  return False;
151          }          }
152    
153          setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, (void *)&true, sizeof(true));          setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, (void *) &true,
154                       sizeof(true));
155    
156          in.size = 4096;          in.size = 4096;
157          in.data = xmalloc(in.size);          in.data = xmalloc(in.size);
# Line 154  BOOL tcp_connect(char *server) Line 163  BOOL tcp_connect(char *server)
163  }  }
164    
165  /* Disconnect on the TCP layer */  /* Disconnect on the TCP layer */
166  void tcp_disconnect()  void
167    tcp_disconnect()
168  {  {
169          close(sock);          close(sock);
170  }  }

Legend:
Removed from v.12  
changed lines
  Added in v.30

  ViewVC Help
Powered by ViewVC 1.1.26