ReactOS 0.4.15-dev-7958-gcd0bb1a
tcp.c File Reference
#include "precomp.h"
Include dependency graph for tcp.c:

Go to the source code of this file.

Macros

#define TCP_CLOSE(_sck)   close(_sck)
 
#define TCP_STRERROR   strerror(errno)
 
#define TCP_SLEEP(_n)   sleep(_n)
 
#define TCP_BLOCKS   (errno == EWOULDBLOCK)
 
#define INADDR_NONE   ((unsigned long) -1)
 
#define STREAM_COUNT   1
 

Functions

STREAM tcp_init (uint32 maxlen)
 
void tcp_send (STREAM s)
 
STREAM tcp_recv (STREAM s, uint32 length)
 
RD_BOOL tcp_connect (char *server)
 
void tcp_disconnect (void)
 
chartcp_get_address ()
 
RD_BOOL tcp_is_connected ()
 
void tcp_reset_state (void)
 

Variables

static int g_sock
 
static struct stream g_in
 
static struct stream g_out [STREAM_COUNT]
 
int g_tcp_port_rdp = TCP_PORT_RDP
 
RD_BOOL g_user_quit
 
RD_BOOL g_network_error
 
RD_BOOL g_reconnect_loop
 

Macro Definition Documentation

◆ INADDR_NONE

#define INADDR_NONE   ((unsigned long) -1)

Definition at line 42 of file tcp.c.

◆ STREAM_COUNT

#define STREAM_COUNT   1

Definition at line 48 of file tcp.c.

◆ TCP_BLOCKS

#define TCP_BLOCKS   (errno == EWOULDBLOCK)

Definition at line 38 of file tcp.c.

◆ TCP_CLOSE

#define TCP_CLOSE (   _sck)    close(_sck)

Definition at line 35 of file tcp.c.

◆ TCP_SLEEP

#define TCP_SLEEP (   _n)    sleep(_n)

Definition at line 37 of file tcp.c.

◆ TCP_STRERROR

#define TCP_STRERROR   strerror(errno)

Definition at line 36 of file tcp.c.

Function Documentation

◆ tcp_connect()

RD_BOOL tcp_connect ( char server)

Definition at line 717 of file tcp.c.

718{
719 socklen_t option_len;
720 uint32 option_value;
721 int i;
722
723#ifdef IPv6
724
725 int n;
726 struct addrinfo hints, *res, *ressave;
727 char tcp_port_rdp_s[10];
728
729 snprintf(tcp_port_rdp_s, 10, "%d", g_tcp_port_rdp);
730
731 memset(&hints, 0, sizeof(struct addrinfo));
732 hints.ai_family = AF_UNSPEC;
733 hints.ai_socktype = SOCK_STREAM;
734
735 if ((n = getaddrinfo(server, tcp_port_rdp_s, &hints, &res)))
736 {
737 error("getaddrinfo: %s\n", gai_strerror(n));
738 return False;
739 }
740
741 ressave = res;
742 g_sock = -1;
743 while (res)
744 {
745 g_sock = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
746 if (!(g_sock < 0))
747 {
748 if (connect(g_sock, res->ai_addr, res->ai_addrlen) == 0)
749 break;
751 g_sock = -1;
752 }
753 res = res->ai_next;
754 }
755 freeaddrinfo(ressave);
756
757 if (g_sock == -1)
758 {
759 error("%s: unable to connect\n", server);
760 return False;
761 }
762
763#else /* no IPv6 support */
764
765 struct hostent *nslookup;
766 struct sockaddr_in servaddr;
767
768 if ((nslookup = gethostbyname(server)) != NULL)
769 {
770 memcpy(&servaddr.sin_addr, nslookup->h_addr, sizeof(servaddr.sin_addr));
771 }
772 else if ((servaddr.sin_addr.s_addr = inet_addr(server)) == INADDR_NONE)
773 {
774 error("%s: unable to resolve host\n", server);
775 return False;
776 }
777
778 if ((g_sock = socket(AF_INET, SOCK_STREAM, 0)) < 0)
779 {
780 error("socket: %s\n", TCP_STRERROR);
781 return False;
782 }
783
784 servaddr.sin_family = AF_INET;
785 servaddr.sin_port = htons((uint16) g_tcp_port_rdp);
786
787 if (connect(g_sock, (struct sockaddr *) &servaddr, sizeof(struct sockaddr)) < 0)
788 {
789 if (!g_reconnect_loop)
790 error("connect: %s\n", TCP_STRERROR);
791
793 g_sock = -1;
794 return False;
795 }
796
797#endif /* IPv6 */
798
799 option_value = 1;
800 option_len = sizeof(option_value);
801 setsockopt(g_sock, IPPROTO_TCP, TCP_NODELAY, (void *) &option_value, option_len);
802 /* receive buffer must be a least 16 K */
803 if (getsockopt(g_sock, SOL_SOCKET, SO_RCVBUF, (void *) &option_value, &option_len) == 0)
804 {
805 if (option_value < (1024 * 16))
806 {
807 option_value = 1024 * 16;
808 option_len = sizeof(option_value);
809 setsockopt(g_sock, SOL_SOCKET, SO_RCVBUF, (void *) &option_value,
810 option_len);
811 }
812 }
813
814 g_in.size = 4096;
815 g_in.data = (uint8 *) xmalloc(g_in.size);
816
817 for (i = 0; i < STREAM_COUNT; i++)
818 {
819 g_out[i].size = 4096;
820 g_out[i].data = (uint8 *) xmalloc(g_out[i].size);
821 }
822
823#ifdef WITH_SSL
824 g_ssl_server = xmalloc(strlen(server)+1);
825#endif /* WITH_SSL */
826
827 return True;
828}
ACPI_SIZE strlen(const char *String)
Definition: utclib.c:269
void * xmalloc(int size)
Definition: uimain.c:747
#define TCP_CLOSE(_sck)
Definition: tcp.c:35
static struct stream g_out[STREAM_COUNT]
Definition: tcp.c:74
int g_tcp_port_rdp
Definition: tcp.c:75
static struct stream g_in
Definition: tcp.c:73
#define STREAM_COUNT
Definition: tcp.c:48
#define INADDR_NONE
Definition: tcp.c:42
#define TCP_STRERROR
Definition: tcp.c:36
RD_BOOL g_reconnect_loop
Definition: uimain.c:86
static int g_sock
Definition: tcp.c:72
unsigned short uint16
Definition: types.h:30
unsigned int uint32
Definition: types.h:32
#define False
Definition: types.h:25
#define True
Definition: types.h:24
unsigned char uint8
Definition: types.h:28
#define NULL
Definition: types.h:112
#define IPPROTO_TCP
Definition: ip.h:196
#define SOCK_STREAM
Definition: tcpip.h:118
#define AF_INET
Definition: tcpip.h:117
PHOSTENT WSAAPI gethostbyname(IN const char FAR *name)
Definition: getxbyxx.c:221
GLsizeiptr size
Definition: glext.h:5919
GLdouble n
Definition: glext.h:7729
GLuint res
Definition: glext.h:9613
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint i
Definition: glfuncs.h:248
#define inet_addr(cp)
Definition: inet.h:98
#define error(str)
Definition: mkdosfs.c:1605
#define memcpy(s1, s2, n)
Definition: mkisofs.h:878
#define htons(x)
Definition: module.h:215
int socklen_t
Definition: tcp.c:35
#define memset(x, y, z)
Definition: compat.h:39
namespace GUID const ADDRINFOEXW * hints
Definition: sock.c:80
INT WSAAPI setsockopt(IN SOCKET s, IN INT level, IN INT optname, IN CONST CHAR FAR *optval, IN INT optlen)
Definition: sockctrl.c:421
INT WSAAPI getsockopt(IN SOCKET s, IN INT level, IN INT optname, OUT CHAR FAR *optval, IN OUT INT FAR *optlen)
Definition: sockctrl.c:271
SOCKET WSAAPI socket(IN INT af, IN INT type, IN INT protocol)
Definition: socklife.c:143
#define TCP_NODELAY
Definition: tcpdef.h:117
static rfbScreenInfoPtr server
Definition: vnc.c:74
#define SO_RCVBUF
Definition: winsock.h:189
#define SOL_SOCKET
Definition: winsock.h:398
#define AF_UNSPEC
Definition: winsock.h:344
#define snprintf
Definition: wintirpc.h:48
#define getaddrinfo
Definition: wspiapi.h:44
#define freeaddrinfo
Definition: wspiapi.h:46

Referenced by iso_connect(), iso_reconnect(), and LibTCPConnectCallback().

◆ tcp_disconnect()

void tcp_disconnect ( void  )

Definition at line 832 of file tcp.c.

833{
834#ifdef WITH_SSL
835 if (g_ssl)
836 {
837 xfree(g_ssl->peek_msg_mem);
838 g_ssl->peek_msg_mem = NULL;
839 g_ssl->peek_msg = NULL;
840 g_ssl->peek_len = 0;
841 xfree(g_ssl->ssl_buf);
842 g_ssl->ssl_buf = NULL;
843 xfree(g_ssl->extra_buf);
844 g_ssl->extra_buf = NULL;
845 g_ssl->extra_len = 0;
846 if (SecIsValidHandle(&g_ssl->ssl_ctx))
847 DeleteSecurityContext(&g_ssl->ssl_ctx);
852 if (g_ssl_server)
853 {
854 xfree(g_ssl_server);
855 g_ssl_server = NULL;
856 }
857 g_ssl = NULL;
858 g_ssl_initialized = False;
859 }
860#endif /* WITH_SSL */
862 g_sock = -1;
863}
void xfree(void *mem)
Definition: uimain.c:758
static SecHandle compat_cred_handle
static BOOL cred_handle_initialized
static BOOL have_compat_cred_handle
static SecHandle cred_handle
#define SecIsValidHandle(x)
Definition: sspi.h:63
SECURITY_STATUS WINAPI DeleteSecurityContext(PCtxtHandle phContext)
Definition: wrapper.c:450
SECURITY_STATUS WINAPI FreeCredentialsHandle(PCredHandle phCredential)
Definition: wrapper.c:151

Referenced by iso_connect(), iso_disconnect(), and iso_reconnect().

◆ tcp_get_address()

char * tcp_get_address ( void  )

Definition at line 866 of file tcp.c.

867{
868 static char ipaddr[32];
869 struct sockaddr_in sockaddr;
870 socklen_t len = sizeof(sockaddr);
871 if (getsockname(g_sock, (struct sockaddr *) &sockaddr, &len) == 0)
872 {
873 uint8 *ip = (uint8 *) & sockaddr.sin_addr;
874 sprintf(ipaddr, "%d.%d.%d.%d", ip[0], ip[1], ip[2], ip[3]);
875 }
876 else
877 strcpy(ipaddr, "127.0.0.1");
878 return ipaddr;
879}
char * strcpy(char *DstString, const char *SrcString)
Definition: utclib.c:388
GLenum GLsizei len
Definition: glext.h:6722
#define sprintf(buf, format,...)
Definition: sprintf.c:55
INT WSAAPI getsockname(IN SOCKET s, OUT LPSOCKADDR name, IN OUT INT FAR *namelen)
Definition: sockctrl.c:213
Definition: dhcpd.h:62

Referenced by rdp_send_logon_info(), and wWinMain().

◆ tcp_init()

STREAM tcp_init ( uint32  maxlen)

Definition at line 82 of file tcp.c.

83{
84 static int cur_stream_id = 0;
86
87#ifdef WITH_SCARD
89#endif
90 result = &g_out[cur_stream_id];
91 cur_stream_id = (cur_stream_id + 1) % STREAM_COUNT;
92
93 if (maxlen > result->size)
94 {
95 result->data = (uint8 *) xrealloc(result->data, maxlen);
96 result->size = maxlen;
97 }
98
99 result->p = result->data;
100 result->end = result->data + result->size;
101#ifdef WITH_SCARD
103#endif
104 return result;
105}
#define SCARD_LOCK_TCP
Definition: constants.h:579
void scard_lock(int lock)
void scard_unlock(int lock)
void * xrealloc(void *oldmem, size_t size)
Definition: uimain.c:736
GLuint64EXT * result
Definition: glext.h:11304
Definition: parse.h:23

Referenced by iso_init(), iso_send_connection_request(), iso_send_msg(), and lwip_init().

◆ tcp_is_connected()

RD_BOOL tcp_is_connected ( void  )

Definition at line 882 of file tcp.c.

883{
884 struct sockaddr_in sockaddr;
885 socklen_t len = sizeof(sockaddr);
886 if (getpeername(g_sock, (struct sockaddr *) &sockaddr, &len))
887 return True;
888 return False;
889}
INT WSAAPI getpeername(IN SOCKET s, OUT LPSOCKADDR name, IN OUT INT FAR *namelen)
Definition: sockctrl.c:167

◆ tcp_recv()

STREAM tcp_recv ( STREAM  s,
uint32  length 
)

Definition at line 344 of file tcp.c.

345{
346 uint32 new_length, end_offset, p_offset;
347 int rcvd = 0;
348
349 if (g_network_error == True)
350 return NULL;
351
352 if (s == NULL)
353 {
354 /* read into "new" stream */
355 if (length > g_in.size)
356 {
357 g_in.data = (uint8 *) xrealloc(g_in.data, length);
358 g_in.size = length;
359 }
360 g_in.end = g_in.p = g_in.data;
361 s = &g_in;
362 }
363 else
364 {
365 /* append to existing stream */
366 new_length = (s->end - s->data) + length;
367 if (new_length > s->size)
368 {
369 p_offset = s->p - s->data;
370 end_offset = s->end - s->data;
371 s->data = (uint8 *) xrealloc(s->data, new_length);
372 s->size = new_length;
373 s->p = s->data + p_offset;
374 s->end = s->data + end_offset;
375 }
376 }
377
378 while (length > 0)
379 {
380#ifdef WITH_SSL
381 if (!g_ssl)
382#endif /* WITH_SSL */
383 {
384 if (!ui_select(g_sock))
385 {
386 /* User quit */
388 return NULL;
389 }
390 }
391
392#ifdef WITH_SSL
393 if (g_ssl)
394 {
395 SIZE_T size = 0;
396 BOOL eof;
397 DWORD res;
398
399 if (g_ssl->peek_msg)
400 {
401 size = min(length, g_ssl->peek_len);
402 memcpy(s->end, g_ssl->peek_msg, size);
403 g_ssl->peek_len -= size;
404 g_ssl->peek_msg += size;
405 s->end += size;
406
407 if (!g_ssl->peek_len)
408 {
409 xfree(g_ssl->peek_msg_mem);
410 g_ssl->peek_msg_mem = g_ssl->peek_msg = NULL;
411 }
412
413 return s;
414 }
415
416 do
417 {
418 res = read_ssl_chunk((BYTE*)s->end, length, TRUE, &size, &eof);
419 if (res != ERROR_SUCCESS)
420 {
421 if (res == WSAEWOULDBLOCK)
422 {
423 if (size)
424 {
426 }
427 }
428 else
429 {
430 error("read_ssl_chunk: %d (%s)\n", res, TCP_STRERROR);
432 return NULL;
433 }
434 break;
435 }
436 }
437 while (!size && !eof);
438 rcvd = size;
439 }
440 else
441 {
442#endif /* WITH_SSL */
443 rcvd = recv(g_sock, (char *)s->end, length, 0);
444 if (rcvd < 0)
445 {
446 if (rcvd == -1 && TCP_BLOCKS)
447 {
448 rcvd = 0;
449 }
450 else
451 {
452 error("recv: %d (%s)\n", rcvd, TCP_STRERROR);
454 return NULL;
455 }
456 }
457 else if (rcvd == 0)
458 {
459 error("Connection closed\n");
460 return NULL;
461 }
462#ifdef WITH_SSL
463 }
464#endif /* WITH_SSL */
465
466 s->end += rcvd;
467 length -= rcvd;
468 }
469
470 return s;
471}
int ui_select(int rdp_socket)
Definition: uimain.c:164
RD_BOOL g_network_error
Definition: uimain.c:77
#define TCP_BLOCKS
Definition: tcp.c:38
RD_BOOL g_user_quit
Definition: uimain.c:76
#define ERROR_SUCCESS
Definition: deptool.c:10
#define TRUE
Definition: types.h:120
static BOOL read_ssl_chunk(struct netconn *conn, void *buf, SIZE_T buf_size, SIZE_T *ret_size, BOOL *eof)
Definition: net.c:448
INT WSAAPI recv(IN SOCKET s, OUT CHAR FAR *buf, IN INT len, IN INT flags)
Definition: recv.c:23
unsigned int BOOL
Definition: ntddk_ex.h:94
unsigned long DWORD
Definition: ntddk_ex.h:95
GLdouble s
Definition: gl.h:2039
GLuint GLsizei GLsizei * length
Definition: glext.h:6040
#define min(a, b)
Definition: monoChain.cc:55
_Check_return_ _CRTIMP int __cdecl __cdecl eof(_In_ int _FileHandle)
ULONG_PTR SIZE_T
Definition: typedefs.h:80
#define WSAEWOULDBLOCK
Definition: winerror.h:1948
unsigned char BYTE
Definition: xxhash.c:193

Referenced by iso_recv_msg(), LibTCPAccept(), LibTCPConnectCallback(), and test_tcp_new_counters_pcb().

◆ tcp_reset_state()

void tcp_reset_state ( void  )

Definition at line 894 of file tcp.c.

895{
896 int i;
897
898 /* Clear the incoming stream */
899 if (g_in.data != NULL)
900 xfree(g_in.data);
901 g_in.p = NULL;
902 g_in.end = NULL;
903 g_in.data = NULL;
904 g_in.size = 0;
905 g_in.iso_hdr = NULL;
906 g_in.mcs_hdr = NULL;
907 g_in.sec_hdr = NULL;
908 g_in.rdp_hdr = NULL;
909 g_in.channel_hdr = NULL;
910
911 /* Clear the outgoing stream(s) */
912 for (i = 0; i < STREAM_COUNT; i++)
913 {
914 if (g_out[i].data != NULL)
915 xfree(g_out[i].data);
916 g_out[i].p = NULL;
917 g_out[i].end = NULL;
918 g_out[i].data = NULL;
919 g_out[i].size = 0;
920 g_out[i].iso_hdr = NULL;
921 g_out[i].mcs_hdr = NULL;
922 g_out[i].sec_hdr = NULL;
923 g_out[i].rdp_hdr = NULL;
924 g_out[i].channel_hdr = NULL;
925 }
926}
GLint GLenum GLsizei GLsizei GLsizei GLint GLsizei const GLvoid * data
Definition: gl.h:1950

Referenced by iso_reset_state().

◆ tcp_send()

void tcp_send ( STREAM  s)

Definition at line 270 of file tcp.c.

271{
272 int length = s->end - s->data;
273 int sent, total = 0;
274
275 if (g_network_error == True)
276 return;
277
278#ifdef WITH_SCARD
280#endif
281 while (total < length)
282 {
283#ifdef WITH_SSL
284 if (g_ssl)
285 {
286 const BYTE *ptr = s->data + total;
287 size_t chunk_size;
288
289 sent = 0;
290
291 while (length - total)
292 {
293 chunk_size = min(length - total, g_ssl->ssl_sizes.cbMaximumMessage);
294 if (!send_ssl_chunk(ptr, chunk_size))
295 {
296#ifdef WITH_SCARD
298#endif
299
300 //error("send_ssl_chunk: %d (%s)\n", sent, TCP_STRERROR);
302 return;
303 }
304
305 sent += chunk_size;
306 ptr += chunk_size;
307 length -= chunk_size;
308 }
309 }
310 else
311 {
312#endif /* WITH_SSL */
313 sent = send(g_sock, (const char *)s->data + total, length - total, 0);
314 if (sent <= 0)
315 {
316 if (sent == -1 && TCP_BLOCKS)
317 {
318 TCP_SLEEP(0);
319 sent = 0;
320 }
321 else
322 {
323#ifdef WITH_SCARD
325#endif
326
327 error("send: %d (%s)\n", sent, TCP_STRERROR);
329 return;
330 }
331 }
332#ifdef WITH_SSL
333 }
334#endif /* WITH_SSL */
335 total += sent;
336 }
337#ifdef WITH_SCARD
339#endif
340}
@ sent
Definition: SystemMenu.c:27
#define TCP_SLEEP(_n)
Definition: tcp.c:37
static BOOL send_ssl_chunk(struct netconn *conn, const void *msg, size_t size)
Definition: net.c:398
INT WSAAPI send(IN SOCKET s, IN CONST CHAR FAR *buf, IN INT len, IN INT flags)
Definition: send.c:23
size_t total
static PVOID ptr
Definition: dispmode.c:27

Referenced by iso_send(), iso_send_connection_request(), and iso_send_msg().

Variable Documentation

◆ g_in

◆ g_network_error

RD_BOOL g_network_error
extern

Definition at line 77 of file uimain.c.

Referenced by tcp_recv(), and tcp_send().

◆ g_out

struct stream g_out[STREAM_COUNT]
static

Definition at line 74 of file tcp.c.

Referenced by convert_r5g5_snorm_l6_unorm_ext(), tcp_connect(), tcp_init(), and tcp_reset_state().

◆ g_reconnect_loop

RD_BOOL g_reconnect_loop
extern

Definition at line 86 of file uimain.c.

Referenced by tcp_connect().

◆ g_sock

int g_sock
static

Definition at line 72 of file tcp.c.

Referenced by tcp_connect(), tcp_disconnect(), tcp_get_address(), tcp_is_connected(), tcp_recv(), and tcp_send().

◆ g_tcp_port_rdp

int g_tcp_port_rdp = TCP_PORT_RDP

Definition at line 75 of file tcp.c.

Referenced by parse_parameters(), tcp_connect(), and wWinMain().

◆ g_user_quit

RD_BOOL g_user_quit
extern

Definition at line 76 of file uimain.c.

Referenced by tcp_recv().