libfreerdp-utils: added TCP utils
authorMarc-André Moreau <marcandre.moreau@gmail.com>
Tue, 6 Mar 2012 19:41:07 +0000 (14:41 -0500)
committerMarc-André Moreau <marcandre.moreau@gmail.com>
Tue, 6 Mar 2012 19:41:07 +0000 (14:41 -0500)
include/freerdp/utils/tcp.h
libfreerdp-core/tcp.c
libfreerdp-sspi/Kerberos/kerberos.c
libfreerdp-utils/CMakeLists.txt
libfreerdp-utils/tcp.c [new file with mode: 0644]

index 143a692..75dee45 100644 (file)
 #define FREERDP_TCP_UTILS_H
 
 #include <freerdp/api.h>
+#include <freerdp/types.h>
 #include <freerdp/utils/windows.h>
 
+FREERDP_API int freerdp_tcp_connect(const char* hostname, int port);
+FREERDP_API int freerdp_tcp_read(int sockfd, uint8* data, int length);
+FREERDP_API int freerdp_tcp_write(int sockfd, uint8* data, int length);
+FREERDP_API int freerdp_tcp_disconnect(int sockfd);
+
 #endif /* FREERDP_TCP_UTILS_H */
index 74e3abe..bfb6420 100644 (file)
@@ -45,6 +45,7 @@
 #define close(_fd) closesocket(_fd)
 #endif
 
+#include <freerdp/utils/tcp.h>
 #include <freerdp/utils/print.h>
 #include <freerdp/utils/stream.h>
 #include <freerdp/utils/memory.h>
@@ -111,50 +112,13 @@ void tcp_get_mac_address(rdpTcp * tcp)
 
 boolean tcp_connect(rdpTcp* tcp, const char* hostname, uint16 port)
 {
-       int status;
-       char servname[10];
        uint32 option_value;
        socklen_t option_len;
-       struct addrinfo hints = { 0 };
-       struct addrinfo * res, * ai;
 
-       memset(&hints, 0, sizeof(struct addrinfo));
-       hints.ai_family = AF_UNSPEC;
-       hints.ai_socktype = SOCK_STREAM;
+       tcp->sockfd = freerdp_tcp_connect(hostname, port);
 
-       snprintf(servname, sizeof(servname), "%d", port);
-       status = getaddrinfo(hostname, servname, &hints, &res);
-
-       if (status != 0)
-       {
-               printf("tcp_connect: getaddrinfo (%s)\n", gai_strerror(status));
+       if (tcp->sockfd < 0)
                return false;
-       }
-
-       tcp->sockfd = -1;
-       for (ai = res; ai; ai = ai->ai_next)
-       {
-               tcp->sockfd = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
-
-               if (tcp->sockfd < 0)
-                       continue;
-
-               if (connect(tcp->sockfd, ai->ai_addr, ai->ai_addrlen) == 0)
-               {
-                       printf("connected to %s:%s\n", hostname, servname);
-                       break;
-               }
-
-               close(tcp->sockfd);
-               tcp->sockfd = -1;
-       }
-       freeaddrinfo(res);
-
-       if (tcp->sockfd == -1)
-       {
-               printf("unable to connect to %s:%s\n", hostname, servname);
-               return false;
-       }
 
        tcp_get_ip_address(tcp);
        tcp_get_mac_address(tcp);
@@ -181,73 +145,18 @@ boolean tcp_connect(rdpTcp* tcp, const char* hostname, uint16 port)
 
 int tcp_read(rdpTcp* tcp, uint8* data, int length)
 {
-       int status;
-
-       status = recv(tcp->sockfd, data, length, 0);
-
-       if (status == 0)
-       {
-               /* Peer disconnected. */
-               return -1;
-       }
-       else if (status < 0)
-       {
-#ifdef _WIN32
-               int wsa_error = WSAGetLastError();
-
-               /* No data available */
-               if (wsa_error == WSAEWOULDBLOCK)
-                       return 0;
-
-               printf("recv() error: %d\n", wsa_error);
-#else
-               /* No data available */
-               if (errno == EAGAIN || errno == EWOULDBLOCK)
-                       return 0;
-
-               perror("recv");
-#endif
-               return -1;
-       }
-
-       return status;
+       return freerdp_tcp_read(tcp->sockfd, data, length);
 }
 
 int tcp_write(rdpTcp* tcp, uint8* data, int length)
 {
-       int status;
-
-       status = send(tcp->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");
-#else
-               if (errno == EAGAIN || errno == EWOULDBLOCK)
-                       status = 0;
-               else
-                       perror("send");
-#endif
-       }
-
-       return status;
+       return freerdp_tcp_write(tcp->sockfd, data, length);
 }
 
-boolean tcp_disconnect(rdpTcp * tcp)
+boolean tcp_disconnect(rdpTcp* tcp)
 {
-       if (tcp->sockfd != -1)
-       {
-               shutdown(tcp->sockfd, SHUT_RDWR);
-               close(tcp->sockfd);
-               tcp->sockfd = -1;
-       }
+       freerdp_tcp_disconnect(tcp->sockfd);
+       tcp->sockfd = -1;
 
        return true;
 }
index 8f16a83..ebe4859 100644 (file)
@@ -30,9 +30,6 @@
 #include <unistd.h>
 #include <sys/ioctl.h>
 #include <sys/socket.h>
-#else
-#define SHUT_RDWR SD_BOTH
-#define close(_fd) closesocket(_fd)
 #endif
 
 #define __USE_XOPEN
@@ -44,6 +41,8 @@
 #include "kerberos_decode.h"
 
 #include <freerdp/sspi/sspi.h>
+
+#include <freerdp/utils/tcp.h>
 #include <freerdp/utils/blob.h>
 #include <freerdp/utils/print.h>
 #include <freerdp/utils/memory.h>
@@ -240,31 +239,9 @@ KDCENTRY* krb_locate_kdc(rdpSettings* settings)
 
 int krb_tcp_connect(KRB_CONTEXT* krb_ctx, KDCENTRY* entry)
 {
-       int sockfd, status;
-       char service[10];
-       struct addrinfo hints = { 0 };
-       struct addrinfo * res, * ai;
-       sockfd = -1;
-       memset(&hints, 0, sizeof(struct addrinfo));
-       hints.ai_family = AF_UNSPEC;
-       hints.ai_socktype = SOCK_STREAM;
-
-       snprintf(service, sizeof(service), "%d", entry->port);
-
-       if ((status = getaddrinfo(entry->kdchost, service, &hints, &res)) != 0)
-               return -1;
+       int sockfd;
 
-       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)
-                       break;
-               close(sockfd);
-               sockfd = -1;
-       }
-       freeaddrinfo(res);
+       sockfd = freerdp_tcp_connect(entry->kdchost, entry->port);
 
        if (sockfd == -1)
                return -1;
@@ -272,57 +249,18 @@ int krb_tcp_connect(KRB_CONTEXT* krb_ctx, KDCENTRY* entry)
        krb_ctx->krbhost = xstrdup(entry->kdchost);
        krb_ctx->krbport = entry->port;
        krb_ctx->ksockfd = sockfd;
+
        return 0;
 }
 
 int krb_tcp_recv(KRB_CONTEXT* krb_ctx, uint8* data, uint32 length)
 {
-       int status;
-
-       status = recv(krb_ctx->ksockfd, data, length, 0);
-
-       if (status <= 0)
-       {
-#ifdef _WIN32
-               int wsa_error = WSAGetLastError();
-
-               /* No data available */
-               if (wsa_error == WSAEWOULDBLOCK)
-                       return 0;
-
-               /* When peer disconnects we get status 0 with no error. */
-               if (status < 0)
-                       printf("recv() error: %d\n", wsa_error);
-#else
-               /* No data available */
-               if (errno == EAGAIN || errno == EWOULDBLOCK)
-                       return 0;
-
-               /* When peer disconnects we get status 0 with no error. */
-               if (status < 0)
-                       perror("recv");
-#endif
-               return -1;
-       }
-
-       return status;
+       return freerdp_tcp_read(krb_ctx->ksockfd, data, length);
 }
 
 int krb_tcp_send(KRB_CONTEXT* krb_ctx, uint8* data, uint32 length)
 {
-       int status;
-
-       status = send(krb_ctx->ksockfd, data, length, MSG_NOSIGNAL);
-
-       if (status < 0)
-       {
-               if (errno == EAGAIN || errno == EWOULDBLOCK)
-                       status = 0;
-               else
-                       perror("send");
-       }
-
-       return status;
+       return freerdp_tcp_write(krb_ctx->ksockfd, data, length);
 }
 
 KRB_CONTEXT* krb_ContextNew()
index b92e570..a12b5ce 100644 (file)
@@ -45,6 +45,7 @@ set(FREERDP_UTILS_SRCS
        stream.c
        string.c
        svc_plugin.c
+       tcp.c
        thread.c
        time.c
        unicode.c
diff --git a/libfreerdp-utils/tcp.c b/libfreerdp-utils/tcp.c
new file mode 100644 (file)
index 0000000..b9b4778
--- /dev/null
@@ -0,0 +1,168 @@
+/**
+ * FreeRDP: A Remote Desktop Protocol Client
+ * 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.
+ */
+
+#include <freerdp/utils/tcp.h>
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <time.h>
+#include <errno.h>
+#include <fcntl.h>
+
+#ifndef _WIN32
+#include <netdb.h>
+#include <unistd.h>
+#include <sys/ioctl.h>
+#include <sys/socket.h>
+#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
+#define SHUT_RDWR SD_BOTH
+#define close(_fd) closesocket(_fd)
+#endif
+
+int freerdp_tcp_connect(const char* hostname, int port)
+{
+       int status;
+       int sockfd;
+       char servname[10];
+       struct addrinfo* ai;
+       struct addrinfo* res;
+       struct addrinfo hints = { 0 };
+
+       memset(&hints, 0, sizeof(struct addrinfo));
+       hints.ai_family = AF_UNSPEC;
+       hints.ai_socktype = SOCK_STREAM;
+
+       snprintf(servname, sizeof(servname), "%d", port);
+       status = getaddrinfo(hostname, servname, &hints, &res);
+
+       if (status != 0)
+       {
+               printf("tcp_connect: getaddrinfo (%s)\n", gai_strerror(status));
+               return -1;
+       }
+
+       sockfd = -1;
+       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)
+               {
+                       printf("connected to %s:%s\n", hostname, servname);
+                       break;
+               }
+
+               close(sockfd);
+               sockfd = -1;
+       }
+       freeaddrinfo(res);
+
+       if (sockfd == -1)
+       {
+               printf("unable to connect to %s:%s\n", hostname, servname);
+               return -1;
+       }
+
+       return sockfd;
+}
+
+int freerdp_tcp_read(int sockfd, uint8* data, int length)
+{
+       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;
+
+               printf("recv() error: %d\n", wsa_error);
+#else
+               /* No data available */
+               if (errno == EAGAIN || errno == EWOULDBLOCK)
+                       return 0;
+
+               perror("recv");
+#endif
+               return -1;
+       }
+
+       return status;
+}
+
+int freerdp_tcp_write(int sockfd, uint8* data, int length)
+{
+       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");
+#else
+               if (errno == EAGAIN || errno == EWOULDBLOCK)
+                       status = 0;
+               else
+                       perror("send");
+#endif
+       }
+
+       return status;
+}
+
+int freerdp_tcp_disconnect(int sockfd)
+{
+       if (sockfd != -1)
+       {
+               shutdown(sockfd, SHUT_RDWR);
+               close(sockfd);
+       }
+
+       return 0;
+}