FreeRDP/libfreerdp/utils/tcp.c

263 lines
4.5 KiB
C
Raw Normal View History

2012-03-06 23:41:07 +04:00
/**
2012-10-09 07:02:04 +04:00
* FreeRDP: A Remote Desktop Protocol Implementation
2012-03-06 23:41:07 +04:00
* TCP Utils
*
* Copyright 2012 Marc-Andre Moreau <marcandre.moreau@gmail.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
2012-12-14 22:15:27 +04:00
#include <winpr/windows.h>
#include <winpr/crt.h>
2012-03-06 23:41:07 +04:00
#include <freerdp/utils/tcp.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
2012-03-06 23:41:07 +04:00
#include <time.h>
#include <errno.h>
#include <fcntl.h>
#ifndef _WIN32
2012-03-06 23:41:07 +04:00
#include <netdb.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <sys/select.h>
2012-03-06 23:41:07 +04:00
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <net/if.h>
#ifdef __APPLE__
#ifndef TCP_KEEPIDLE
#define TCP_KEEPIDLE TCP_KEEPALIVE
#endif
#endif
#else /* ifdef _WIN32 */
2012-05-05 03:36:35 +04:00
#include <winpr/windows.h>
2012-12-14 22:11:07 +04:00
#include <winpr/crt.h>
2012-03-06 23:41:07 +04:00
#define SHUT_RDWR SD_BOTH
#define close(_fd) closesocket(_fd)
#endif
2012-03-07 00:30:24 +04:00
#ifndef MSG_NOSIGNAL
#define MSG_NOSIGNAL 0
2012-03-07 00:30:24 +04:00
#endif
2012-03-06 23:41:07 +04:00
int freerdp_tcp_connect(const char* hostname, int port)
{
int status;
int sockfd;
char servname[32];
struct addrinfo hints;
struct addrinfo* ai = NULL;
struct addrinfo* res = NULL;
2012-03-06 23:41:07 +04:00
ZeroMemory(&hints, sizeof(struct addrinfo));
2012-03-06 23:41:07 +04:00
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = AI_PASSIVE;
hints.ai_protocol = 0;
hints.ai_canonname = NULL;
hints.ai_addr = NULL;
hints.ai_next = NULL;
2012-03-06 23:41:07 +04:00
sprintf_s(servname, 32, "%d", port);
2012-03-06 23:41:07 +04:00
status = getaddrinfo(hostname, servname, &hints, &res);
if (status != 0)
{
//fprintf(stderr, "tcp_connect: getaddrinfo (%s)\n", gai_strerror(status));
2012-03-06 23:41:07 +04:00
return -1;
}
sockfd = -1;
2012-05-22 00:01:24 +04:00
2012-03-06 23:41:07 +04:00
for (ai = res; ai; ai = ai->ai_next)
{
sockfd = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
if (sockfd < 0)
continue;
if (connect(sockfd, ai->ai_addr, ai->ai_addrlen) == 0)
{
fprintf(stderr, "connected to %s:%s\n", hostname, servname);
2012-03-06 23:41:07 +04:00
break;
}
2012-05-22 00:01:24 +04:00
2012-03-06 23:41:07 +04:00
close(sockfd);
sockfd = -1;
}
2012-05-22 00:01:24 +04:00
2012-03-06 23:41:07 +04:00
freeaddrinfo(res);
if (sockfd == -1)
{
fprintf(stderr, "unable to connect to %s:%s\n", hostname, servname);
2012-03-06 23:41:07 +04:00
return -1;
}
return sockfd;
}
int freerdp_tcp_read(int sockfd, BYTE* data, int length)
2012-03-06 23:41:07 +04:00
{
int status;
status = recv(sockfd, data, length, 0);
if (status == 0)
{
return -1; /* peer disconnected */
}
else if (status < 0)
{
#ifdef _WIN32
int wsa_error = WSAGetLastError();
/* No data available */
if (wsa_error == WSAEWOULDBLOCK)
return 0;
fprintf(stderr, "recv() error: %d\n", wsa_error);
2012-03-06 23:41:07 +04:00
#else
/* No data available */
if (errno == EAGAIN || errno == EWOULDBLOCK)
return 0;
perror("recv");
#endif
return -1;
}
return status;
}
int freerdp_tcp_write(int sockfd, BYTE* data, int length)
2012-03-06 23:41:07 +04:00
{
int status;
status = send(sockfd, data, length, MSG_NOSIGNAL);
if (status < 0)
{
#ifdef _WIN32
int wsa_error = WSAGetLastError();
/* No data available */
if (wsa_error == WSAEWOULDBLOCK)
status = 0;
else
perror("send");
2012-03-06 23:41:07 +04:00
#else
if (errno == EAGAIN || errno == EWOULDBLOCK)
status = 0;
else
perror("send");
#endif
}
return status;
}
int freerdp_tcp_wait_read(int sockfd)
{
fd_set fds;
if (sockfd < 1)
2013-01-14 14:12:30 +04:00
{
fprintf(stderr, "Invalid socket to watch: %d\n", sockfd);
return 0 ;
2013-01-14 14:12:30 +04:00
}
FD_ZERO(&fds);
FD_SET(sockfd, &fds);
2013-01-14 14:12:30 +04:00
select(sockfd+1, &fds, NULL, NULL, NULL);
return 0;
}
int freerdp_tcp_wait_write(int sockfd)
{
fd_set fds;
if (sockfd < 1)
2013-01-14 14:12:30 +04:00
{
fprintf(stderr, "Invalid socket to watch: %d\n", sockfd);
return 0;
2013-01-14 14:12:30 +04:00
}
FD_ZERO(&fds);
FD_SET(sockfd, &fds);
2013-01-14 14:12:30 +04:00
select(sockfd+1, NULL, &fds, NULL, NULL);
return 0;
}
2012-03-06 23:41:07 +04:00
int freerdp_tcp_disconnect(int sockfd)
{
if (sockfd != -1)
{
shutdown(sockfd, SHUT_RDWR);
close(sockfd);
}
return 0;
}
2012-03-18 21:13:34 +04:00
int freerdp_tcp_set_no_delay(int sockfd, BOOL no_delay)
2012-03-18 21:13:34 +04:00
{
2012-10-09 11:26:39 +04:00
UINT32 option_value;
2012-03-18 21:13:34 +04:00
socklen_t option_len;
option_value = no_delay;
option_len = sizeof(option_value);
setsockopt(sockfd, IPPROTO_TCP, TCP_NODELAY, (void*) &option_value, option_len);
return 0;
}
int freerdp_wsa_startup()
{
#ifdef _WIN32
WSADATA wsaData;
return WSAStartup(0x101, &wsaData);
#else
return 0;
#endif
}
int freerdp_wsa_cleanup()
{
#ifdef _WIN32
return WSACleanup();
#else
return 0;
#endif
}