diff options
author | Alan Antonuk <alan.antonuk@gmail.com> | 2013-06-12 16:59:19 -0700 |
---|---|---|
committer | Alan Antonuk <alan.antonuk@gmail.com> | 2013-06-13 16:40:36 -0700 |
commit | 0218bb262b9323f431ed84f5c1d1eb1c5bdb1c8b (patch) | |
tree | 1fb756c31dda6bc7736fa1650b87d65ff839c382 | |
parent | 22e41b8b52bace283c424d9a125656fcb0a41120 (diff) | |
download | rabbitmq-c-0218bb262b9323f431ed84f5c1d1eb1c5bdb1c8b.tar.gz |
Move amqp_os_socket* funcs to amqp_socket.h/c
Doing this for code clarity, as its easier to see what the issues are
when you have all implementations in one file
-rw-r--r-- | Makefile.am | 4 | ||||
-rw-r--r-- | librabbitmq/CMakeLists.txt | 1 | ||||
-rw-r--r-- | librabbitmq/amqp_framing.c | 1 | ||||
-rw-r--r-- | librabbitmq/amqp_openssl.c | 3 | ||||
-rw-r--r-- | librabbitmq/amqp_private.h | 6 | ||||
-rw-r--r-- | librabbitmq/amqp_socket.c | 119 | ||||
-rw-r--r-- | librabbitmq/amqp_socket.h | 22 | ||||
-rw-r--r-- | librabbitmq/codegen.py | 1 | ||||
-rw-r--r-- | librabbitmq/unix/socket.c | 102 | ||||
-rw-r--r-- | librabbitmq/unix/socket.h | 73 | ||||
-rw-r--r-- | librabbitmq/win32/socket.c | 116 | ||||
-rw-r--r-- | librabbitmq/win32/socket.h | 76 |
12 files changed, 136 insertions, 388 deletions
diff --git a/Makefile.am b/Makefile.am index 17804b5..40d2821 100644 --- a/Makefile.am +++ b/Makefile.am @@ -55,15 +55,11 @@ librabbitmq_librabbitmq_la_LDFLAGS = \ $(SSL_LIBS) if OS_UNIX -librabbitmq_librabbitmq_la_SOURCES += librabbitmq/unix/socket.c -librabbitmq_librabbitmq_la_SOURCES += librabbitmq/unix/socket.h librabbitmq_librabbitmq_la_SOURCES += librabbitmq/unix/threads.h librabbitmq_librabbitmq_la_CFLAGS += -I$(top_srcdir)/librabbitmq/unix endif if OS_WIN32 -librabbitmq_librabbitmq_la_SOURCES += librabbitmq/win32/socket.c -librabbitmq_librabbitmq_la_SOURCES += librabbitmq/win32/socket.h librabbitmq_librabbitmq_la_SOURCES += librabbitmq/win32/threads.h librabbitmq_librabbitmq_la_CFLAGS += -I$(top_srcdir)/librabbitmq/win32 librabbitmq_librabbitmq_la_CFLAGS += -I$(top_srcdir)/librabbitmq/win32/msinttypes diff --git a/librabbitmq/CMakeLists.txt b/librabbitmq/CMakeLists.txt index 0d274c5..f310743 100644 --- a/librabbitmq/CMakeLists.txt +++ b/librabbitmq/CMakeLists.txt @@ -120,7 +120,6 @@ set(RABBITMQ_SOURCES ${AMQP_FRAMING_C_PATH} amqp_api.c amqp.h amqp_connection.c amqp_mem.c amqp_private.h amqp_socket.c amqp_table.c amqp_url.c amqp_socket.h amqp_tcp_socket.c amqp_tcp_socket.h - ${SOCKET_IMPL}/socket.h ${SOCKET_IMPL}/socket.c ${AMQP_SSL_SRCS} ) diff --git a/librabbitmq/amqp_framing.c b/librabbitmq/amqp_framing.c index 62b01f6..e065e48 100644 --- a/librabbitmq/amqp_framing.c +++ b/librabbitmq/amqp_framing.c @@ -39,7 +39,6 @@ #endif #include "amqp_private.h" -#include "socket.h" #include <stdint.h> #include <stdio.h> #include <stdlib.h> diff --git a/librabbitmq/amqp_openssl.c b/librabbitmq/amqp_openssl.c index 9b962b5..0f6c12c 100644 --- a/librabbitmq/amqp_openssl.c +++ b/librabbitmq/amqp_openssl.c @@ -26,8 +26,10 @@ #endif #include "amqp_ssl_socket.h" +#include "amqp_socket.h" #include "amqp_private.h" #include "threads.h" + #include <ctype.h> #include <openssl/conf.h> #include <openssl/err.h> @@ -35,7 +37,6 @@ #include <stdlib.h> #include <string.h> -#include "socket.h" static int initialize_openssl(void); static int destroy_openssl(void); diff --git a/librabbitmq/amqp_private.h b/librabbitmq/amqp_private.h index cc3c7f2..e152c8c 100644 --- a/librabbitmq/amqp_private.h +++ b/librabbitmq/amqp_private.h @@ -45,8 +45,10 @@ #include "amqp_framing.h" #include <string.h> -#ifdef HAVE_ARPA_INET_H -#include <arpa/inet.h> +#ifdef _WIN32 +# include <Winsock2.h> +#else +# include <arpa/inet.h> #endif /* GCC attributes */ diff --git a/librabbitmq/amqp_socket.c b/librabbitmq/amqp_socket.c index 7649c74..3c6a42e 100644 --- a/librabbitmq/amqp_socket.c +++ b/librabbitmq/amqp_socket.c @@ -40,8 +40,6 @@ #include "amqp_private.h" -#include "socket.h" - #include <assert.h> #include <stdarg.h> #include <stdint.h> @@ -49,6 +47,111 @@ #include <stdlib.h> #include <string.h> +#include <errno.h> + +#ifdef _WIN32 +# ifndef WIN32_LEAN_AND_MEAN +# define WIN32_LEAN_AND_MEAN +# endif +# include <Winsock2.h> +# include <ws2tcpip.h> +#else +# include <sys/types.h> /* On older BSD this must come before net includes */ +# include <netinet/in.h> +# include <netinet/tcp.h> +# include <sys/socket.h> +# include <netdb.h> +# include <sys/uio.h> +# include <fcntl.h> +# include <unistd.h> +#endif + +static int +amqp_os_socket_init(void) +{ +#ifdef _WIN32 + static called_wsastartup = 0; + if (!called_wsastartup) { + WSADATA data; + int res = WSAStartup(0x0202, &data); + if (res) { + return AMQP_STATUS_TCP_SOCKETLIB_INIT_ERROR; + } + + called_wsastartup = 1; + } + return AMQP_STATUS_OK; + +#else + return AMQP_STATUS_OK; +#endif +} + +static int +amqp_os_socket_socket(int domain, int type, int protocol) +{ +#ifdef _WIN32 + /* + This cast is to squash warnings on Win64, see: + http://stackoverflow.com/questions/1953639/is-it-safe-to-cast-socket-to-int-under-win64 + */ + return (int)socket(domain, type, protocol); +#else + int flags; + + int s = socket(domain, type, protocol); + if (s < 0) { + return s; + } + + /* Always enable CLOEXEC on the socket */ + flags = fcntl(s, F_GETFD); + if (flags == -1 + || fcntl(s, F_SETFD, (long)(flags | FD_CLOEXEC)) == -1) { + int e = errno; + close(s); + errno = e; + return -1; + } + + return s; + +#endif +} + +static int +amqp_os_socket_setsockopt(int sock, int level, int optname, + const void *optval, size_t optlen) +{ +#ifdef _WIN32 + /* the winsock setsockopt function has its 4th argument as a + const char * */ + return setsockopt(sock, level, optname, (const char *)optval, optlen); +#else + return setsockopt(sock, level, optname, optval, optlen); +#endif +} + +int +amqp_os_socket_error(void) +{ +#ifdef _WIN32 + return WSAGetLastError(); +#else + return errno; +#endif +} + +int +amqp_os_socket_close(int sockfd) +{ +#ifdef _WIN32 + return closesocket(sockfd); +#else + return close(sockfd); +#endif +} + ssize_t amqp_socket_writev(amqp_socket_t *self, struct iovec *iov, int iovcnt) { @@ -118,7 +221,7 @@ int amqp_open_socket(char const *hostname, int last_error = AMQP_STATUS_OK; int one = 1; /* for setsockopt */ - last_error = amqp_socket_init(); + last_error = amqp_os_socket_init(); if (AMQP_STATUS_OK != last_error) { return last_error; } @@ -137,23 +240,19 @@ int amqp_open_socket(char const *hostname, } for (addr = address_list; addr; addr = addr->ai_next) { - /* - This cast is to squash warnings on Win64, see: - http://stackoverflow.com/questions/1953639/is-it-safe-to-cast-socket-to-int-under-win64 - */ - sockfd = (int)socket(addr->ai_family, addr->ai_socktype, addr->ai_protocol); + sockfd = amqp_os_socket_socket(addr->ai_family, addr->ai_socktype, addr->ai_protocol); if (-1 == sockfd) { last_error = AMQP_STATUS_SOCKET_ERROR; continue; } #ifdef DISABLE_SIGPIPE_WITH_SETSOCKOPT - if (0 != amqp_socket_setsockopt(sockfd, SOL_SOCKET, SO_NOSIGPIPE, &one, sizeof(one))) { + if (0 != amqp_os_socket_setsockopt(sockfd, SOL_SOCKET, SO_NOSIGPIPE, &one, sizeof(one))) { last_error = AMQP_STATUS_SOCKET_ERROR; amqp_os_socket_close(sockfd); continue; } #endif /* DISABLE_SIGPIPE_WITH_SETSOCKOPT */ - if (0 != amqp_socket_setsockopt(sockfd, IPPROTO_TCP, TCP_NODELAY, &one, sizeof(one)) + if (0 != amqp_os_socket_setsockopt(sockfd, IPPROTO_TCP, TCP_NODELAY, &one, sizeof(one)) || 0 != connect(sockfd, addr->ai_addr, addr->ai_addrlen)) { last_error = AMQP_STATUS_SOCKET_ERROR; amqp_os_socket_close(sockfd); diff --git a/librabbitmq/amqp_socket.h b/librabbitmq/amqp_socket.h index 695befb..52815fe 100644 --- a/librabbitmq/amqp_socket.h +++ b/librabbitmq/amqp_socket.h @@ -29,10 +29,19 @@ #define AMQP_SOCKET_H #include "amqp.h" -#include "socket.h" + +#ifdef _WIN32 +# include <WinSock2.h> +#endif AMQP_BEGIN_DECLS +int +amqp_os_socket_error(void); + +int +amqp_os_socket_close(int sockfd); + /* Socket callbacks. */ typedef ssize_t (*amqp_socket_writev_fn)(void *, struct iovec *, int); typedef ssize_t (*amqp_socket_send_fn)(void *, const void *, size_t); @@ -58,6 +67,17 @@ struct amqp_socket_t_ { const struct amqp_socket_class_t *klass; }; + +#ifdef _WIN32 +/* WinSock2 calls iovec WSABUF with different parameter names. + * this is really a WSABUF with different names + */ +struct iovec { + u_long iov_len; + char FAR *iov_base; +}; +#endif + /** * Write to a socket. * diff --git a/librabbitmq/codegen.py b/librabbitmq/codegen.py index 7cdf5e2..c8e6d7a 100644 --- a/librabbitmq/codegen.py +++ b/librabbitmq/codegen.py @@ -379,7 +379,6 @@ def genErl(spec): #endif #include "amqp_private.h" -#include "socket.h" #include <stdint.h> #include <stdio.h> #include <stdlib.h> diff --git a/librabbitmq/unix/socket.c b/librabbitmq/unix/socket.c deleted file mode 100644 index 69ec6e5..0000000 --- a/librabbitmq/unix/socket.c +++ /dev/null @@ -1,102 +0,0 @@ -/* vim:set ft=c ts=2 sw=2 sts=2 et cindent: */ -/* - * ***** BEGIN LICENSE BLOCK ***** - * Version: MIT - * - * Portions created by Alan Antonuk are Copyright (c) 2012-2013 - * Alan Antonuk. All Rights Reserved. - * - * Portions created by VMware are Copyright (c) 2007-2012 VMware, Inc. - * All Rights Reserved. - * - * Portions created by Tony Garnock-Jones are Copyright (c) 2009-2010 - * VMware, Inc. and Tony Garnock-Jones. All Rights Reserved. - * - * Permission is hereby granted, free of charge, to any person - * obtaining a copy of this software and associated documentation - * files (the "Software"), to deal in the Software without - * restriction, including without limitation the rights to use, copy, - * modify, merge, publish, distribute, sublicense, and/or sell copies - * of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be - * included in all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, - * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF - * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND - * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS - * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN - * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN - * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - * ***** END LICENSE BLOCK ***** - */ - -#ifdef HAVE_CONFIG_H -#include "config.h" -#endif - -#include "amqp_private.h" -#include "socket.h" -#include <errno.h> -#include <fcntl.h> -#include <stdint.h> -#include <stdlib.h> -#include <string.h> -#include <unistd.h> - -int -amqp_socket_init(void) -{ - return AMQP_STATUS_OK; -} - -int -amqp_socket_socket(int domain, int type, int proto) -{ - int flags; - - int s = socket(domain, type, proto); - if (s < 0) { - return s; - } - - /* Always enable CLOEXEC on the socket */ - flags = fcntl(s, F_GETFD); - if (flags == -1 - || fcntl(s, F_SETFD, (long)(flags | FD_CLOEXEC)) == -1) { - int e = errno; - close(s); - errno = e; - return -1; - } - - return s; -} - -char * -amqp_os_error_string(int err) -{ - return strdup(strerror(err)); -} - -int -amqp_os_socket_close(int sockfd) -{ - return close(sockfd); -} - -ssize_t -amqp_os_socket_writev(int sockfd, const struct iovec *iov, - int iovcnt) -{ - return writev(sockfd, iov, iovcnt); -} - -int -amqp_os_socket_error(void) -{ - return errno; -} diff --git a/librabbitmq/unix/socket.h b/librabbitmq/unix/socket.h deleted file mode 100644 index 34de1a2..0000000 --- a/librabbitmq/unix/socket.h +++ /dev/null @@ -1,73 +0,0 @@ -/* vim:set ft=c ts=2 sw=2 sts=2 et cindent: */ -#ifndef librabbitmq_unix_socket_h -#define librabbitmq_unix_socket_h - -/* - * ***** BEGIN LICENSE BLOCK ***** - * Version: MIT - * - * Portions created by Alan Antonuk are Copyright (c) 2012-2013 - * Alan Antonuk. All Rights Reserved. - * - * Portions created by VMware are Copyright (c) 2007-2012 VMware, Inc. - * All Rights Reserved. - * - * Portions created by Tony Garnock-Jones are Copyright (c) 2009-2010 - * VMware, Inc. and Tony Garnock-Jones. All Rights Reserved. - * - * Permission is hereby granted, free of charge, to any person - * obtaining a copy of this software and associated documentation - * files (the "Software"), to deal in the Software without - * restriction, including without limitation the rights to use, copy, - * modify, merge, publish, distribute, sublicense, and/or sell copies - * of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be - * included in all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, - * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF - * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND - * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS - * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN - * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN - * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - * ***** END LICENSE BLOCK ***** - */ - -#include <errno.h> -#include <sys/types.h> /* On older BSD this must come before net includes */ -#include <netinet/in.h> -#include <netinet/tcp.h> -#include <sys/socket.h> -#include <netdb.h> -#include <sys/uio.h> - -int -amqp_socket_init(void); - -int -amqp_socket_socket(int domain, int type, int proto); - -int -amqp_os_socket_error(void); - -int -amqp_os_socket_close(int sockfd); - -ssize_t -amqp_os_socket_writev(int sockfd, const struct iovec *iov, int iovcnt); - -#define amqp_socket_setsockopt setsockopt - -#if defined(SO_NOSIGPIPE) && !defined(MSG_NOSIGNAL) -# define DISABLE_SIGPIPE_WITH_SETSOCKOPT -#endif - -#ifndef MSG_NOSIGNAL -# define MSG_NOSIGNAL 0x0 -#endif - -#endif diff --git a/librabbitmq/win32/socket.c b/librabbitmq/win32/socket.c deleted file mode 100644 index aaadfbb..0000000 --- a/librabbitmq/win32/socket.c +++ /dev/null @@ -1,116 +0,0 @@ -/* vim:set ft=c ts=2 sw=2 sts=2 et cindent: */ -/* - * ***** BEGIN LICENSE BLOCK ***** - * Version: MIT - * - * Portions created by Alan Antonuk are Copyright (c) 2012-2013 - * Alan Antonuk. All Rights Reserved. - * - * Portions created by VMware are Copyright (c) 2007-2012 VMware, Inc. - * All Rights Reserved. - * - * Portions created by Tony Garnock-Jones are Copyright (c) 2009-2010 - * VMware, Inc. and Tony Garnock-Jones. All Rights Reserved. - * - * Permission is hereby granted, free of charge, to any person - * obtaining a copy of this software and associated documentation - * files (the "Software"), to deal in the Software without - * restriction, including without limitation the rights to use, copy, - * modify, merge, publish, distribute, sublicense, and/or sell copies - * of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be - * included in all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, - * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF - * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND - * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS - * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN - * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN - * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - * ***** END LICENSE BLOCK ***** - */ - -/* See http://msdn.microsoft.com/en-us/library/ms737629%28VS.85%29.aspx */ -#define WIN32_LEAN_AND_MEAN - -#ifdef HAVE_CONFIG_H -#include "config.h" -#endif - -#include "amqp_private.h" -#include "socket.h" -#include <stdint.h> -#include <stdlib.h> -#include <windows.h> - -static int called_wsastartup; - -int -amqp_socket_init(void) -{ - if (!called_wsastartup) { - WSADATA data; - int res = WSAStartup(0x0202, &data); - if (res) { - return AMQP_STATUS_TCP_SOCKETLIB_INIT_ERROR; - } - - called_wsastartup = 1; - } - - return AMQP_STATUS_OK; -} - -char * -amqp_os_error_string(int err) -{ - char *msg, *copy; - - if (!FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM - | FORMAT_MESSAGE_ALLOCATE_BUFFER, - NULL, err, - MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), - (LPSTR)&msg, 0, NULL)) { - return strdup("(error retrieving Windows error message)"); - } - - copy = strdup(msg); - LocalFree(msg); - return copy; -} - -int -amqp_socket_setsockopt(int sock, int level, int optname, - const void *optval, size_t optlen) -{ - /* the winsock setsockopt function has its 4th argument as a - const char * */ - return setsockopt(sock, level, optname, (const char *)optval, optlen); -} - -int -amqp_os_socket_close(int sockfd) -{ - return closesocket(sockfd); -} - -ssize_t -amqp_os_socket_writev(int sock, struct iovec *iov, int nvecs) -{ - DWORD ret; - if (WSASend(sock, (LPWSABUF)iov, nvecs, &ret, 0, NULL, NULL) == 0) { - return ret; - } else { - return -1; - } -} - -int -amqp_os_socket_error(void) -{ - return AMQP_STATUS_SOCKET_ERROR; -} diff --git a/librabbitmq/win32/socket.h b/librabbitmq/win32/socket.h deleted file mode 100644 index cddbee5..0000000 --- a/librabbitmq/win32/socket.h +++ /dev/null @@ -1,76 +0,0 @@ -/* vim:set ft=c ts=2 sw=2 sts=2 et cindent: */ -#ifndef librabbitmq_windows_socket_h -#define librabbitmq_windows_socket_h - -/* - * ***** BEGIN LICENSE BLOCK ***** - * Version: MIT - * - * Portions created by Alan Antonuk are Copyright (c) 2012-2013 - * Alan Antonuk. All Rights Reserved. - * - * Portions created by VMware are Copyright (c) 2007-2012 VMware, Inc. - * All Rights Reserved. - * - * Portions created by Tony Garnock-Jones are Copyright (c) 2009-2010 - * VMware, Inc. and Tony Garnock-Jones. All Rights Reserved. - * - * Permission is hereby granted, free of charge, to any person - * obtaining a copy of this software and associated documentation - * files (the "Software"), to deal in the Software without - * restriction, including without limitation the rights to use, copy, - * modify, merge, publish, distribute, sublicense, and/or sell copies - * of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be - * included in all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, - * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF - * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND - * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS - * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN - * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN - * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - * ***** END LICENSE BLOCK ***** - */ - -#include "amqp_private.h" -#include <winsock2.h> -#include <WS2tcpip.h> - -/* same as WSABUF */ -struct iovec { - u_long iov_len; - void *iov_base; -}; - -int -amqp_socket_init(void); - -#define amqp_socket_socket socket - -char * -amqp_os_error_string(int err); - -int -amqp_socket_setsockopt(int sock, int level, int optname, - const void *optval, size_t optlen); - -int -amqp_os_socket_close(int sockfd); - - -ssize_t -amqp_os_socket_writev(int sock, struct iovec *iov, int nvecs); - -int -amqp_os_socket_error(void); - -#ifndef MSG_NOSIGNAL -# define MSG_NOSIGNAL 0x0 -#endif - -#endif |