From 20f038581b99d2f438ddf5a84a47581cf15f70e0 Mon Sep 17 00:00:00 2001 From: Vic Lee Date: Tue, 22 May 2012 19:57:19 +0800 Subject: [PATCH] libfreerdp-core: add client unix domain socket. --- include/freerdp/utils/uds.h | 28 +++++++++++++ libfreerdp-core/tcp.c | 55 ++++++++++++++---------- libfreerdp-utils/CMakeLists.txt | 1 + libfreerdp-utils/uds.c | 74 +++++++++++++++++++++++++++++++++ 4 files changed, 136 insertions(+), 22 deletions(-) create mode 100644 include/freerdp/utils/uds.h create mode 100644 libfreerdp-utils/uds.c diff --git a/include/freerdp/utils/uds.h b/include/freerdp/utils/uds.h new file mode 100644 index 000000000..ea0c781b1 --- /dev/null +++ b/include/freerdp/utils/uds.h @@ -0,0 +1,28 @@ +/** + * FreeRDP: A Remote Desktop Protocol Client + * Unix Domain Socket Utils + * + * Copyright 2012 Vic Lee + * + * 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. + */ + +#ifndef FREERDP_UDS_UTILS_H +#define FREERDP_UDS_UTILS_H + +#include +#include + +FREERDP_API int freerdp_uds_connect(const char* path); + +#endif /* FREERDP_UDS_UTILS_H */ diff --git a/libfreerdp-core/tcp.c b/libfreerdp-core/tcp.c index bfb642071..6c0390f31 100644 --- a/libfreerdp-core/tcp.c +++ b/libfreerdp-core/tcp.c @@ -46,6 +46,7 @@ #endif #include +#include #include #include #include @@ -115,30 +116,40 @@ boolean tcp_connect(rdpTcp* tcp, const char* hostname, uint16 port) uint32 option_value; socklen_t option_len; - tcp->sockfd = freerdp_tcp_connect(hostname, port); - - if (tcp->sockfd < 0) - return false; - - tcp_get_ip_address(tcp); - tcp_get_mac_address(tcp); - - option_value = 1; - option_len = sizeof(option_value); - setsockopt(tcp->sockfd, IPPROTO_TCP, TCP_NODELAY, (void*) &option_value, option_len); - - /* receive buffer must be a least 32 K */ - if (getsockopt(tcp->sockfd, SOL_SOCKET, SO_RCVBUF, (void*) &option_value, &option_len) == 0) + if (hostname[0] == '/') { - if (option_value < (1024 * 32)) - { - option_value = 1024 * 32; - option_len = sizeof(option_value); - setsockopt(tcp->sockfd, SOL_SOCKET, SO_RCVBUF, (void*) &option_value, option_len); - } - } + tcp->sockfd = freerdp_uds_connect(hostname); - tcp_set_keep_alive_mode(tcp); + if (tcp->sockfd < 0) + return false; + } + else + { + tcp->sockfd = freerdp_tcp_connect(hostname, port); + + if (tcp->sockfd < 0) + return false; + + tcp_get_ip_address(tcp); + tcp_get_mac_address(tcp); + + option_value = 1; + option_len = sizeof(option_value); + setsockopt(tcp->sockfd, IPPROTO_TCP, TCP_NODELAY, (void*) &option_value, option_len); + + /* receive buffer must be a least 32 K */ + if (getsockopt(tcp->sockfd, SOL_SOCKET, SO_RCVBUF, (void*) &option_value, &option_len) == 0) + { + if (option_value < (1024 * 32)) + { + option_value = 1024 * 32; + option_len = sizeof(option_value); + setsockopt(tcp->sockfd, SOL_SOCKET, SO_RCVBUF, (void*) &option_value, option_len); + } + } + + tcp_set_keep_alive_mode(tcp); + } return true; } diff --git a/libfreerdp-utils/CMakeLists.txt b/libfreerdp-utils/CMakeLists.txt index a12b5ce2b..6f62e7210 100644 --- a/libfreerdp-utils/CMakeLists.txt +++ b/libfreerdp-utils/CMakeLists.txt @@ -48,6 +48,7 @@ set(FREERDP_UTILS_SRCS tcp.c thread.c time.c + uds.c unicode.c wait_obj.c) diff --git a/libfreerdp-utils/uds.c b/libfreerdp-utils/uds.c new file mode 100644 index 000000000..46c527e67 --- /dev/null +++ b/libfreerdp-utils/uds.c @@ -0,0 +1,74 @@ +/** + * FreeRDP: A Remote Desktop Protocol Client + * Unix Domain Socket Utils + * + * Copyright 2012 Vic Lee + * + * 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. + */ + +#include + +#include +#include +#include +#include +#include +#include + +#ifndef _WIN32 + +#include +#include +#include +#include +#include +#include +#include +#include + +#endif + +int freerdp_uds_connect(const char* path) +{ +#ifndef _WIN32 + + int status; + int sockfd; + struct sockaddr_un addr; + + sockfd = socket(AF_UNIX, SOCK_STREAM, 0); + if (sockfd == -1) + { + perror("socket"); + return -1; + } + + addr.sun_family = AF_UNIX; + strncpy(addr.sun_path, path, sizeof(addr.sun_path)); + status = connect(sockfd, (struct sockaddr *) &addr, sizeof(addr)); + if (status < 0) + { + perror("connect"); + close(sockfd); + return -1; + } + + return sockfd; + +#else /* ifndef _WIN32 */ + + return -1; + +#endif +}