diff options
Diffstat (limited to 'doc/examples')
-rw-r--r-- | doc/examples/socket.h | 102 | ||||
-rw-r--r-- | doc/examples/tls_client.cpp | 8 | ||||
-rw-r--r-- | doc/examples/tls_server.cpp | 3 |
3 files changed, 82 insertions, 31 deletions
diff --git a/doc/examples/socket.h b/doc/examples/socket.h index c4fa46600..6d28afacf 100644 --- a/doc/examples/socket.h +++ b/doc/examples/socket.h @@ -10,13 +10,55 @@ #include <stdexcept> -#include <sys/types.h> -#include <sys/socket.h> -#include <sys/time.h> -#include <netinet/in.h> -#include <netdb.h> -#include <unistd.h> -#include <errno.h> +#if defined(_MSC_VER) + #define SOCKET_IS_WINSOCK 1 +#endif + +#if !defined(SOCKET_IS_WINSOCK) + #define SOCKET_IS_WINSOCK 0 +#endif + +#if SOCKET_IS_WINSOCK + #include <winsock.h> + + typedef SOCKET socket_t; + const socket_t invalid_socket = INVALID_SOCKET; + #define socket_error_code WSAGetLastError() + + class SocketInitializer + { + public: + SocketInitializer() + { + WSADATA wsadata; + WSAStartup(MAKEWORD(2, 2), &wsadata); + } + + ~SocketInitializer() + { + WSACleanup(); + } + }; +#else + #include <sys/types.h> + #include <sys/socket.h> + #include <sys/time.h> + #include <netinet/in.h> + #include <netdb.h> + #include <unistd.h> + #include <errno.h> + + typedef int socket_t; + const socket_t invalid_socket = -1; + #define socket_error_code errno + + class SocketInitializer {}; +#endif + +#if !defined(MSG_NOSIGNAL) + #define MSG_NOSIGNAL 0 +#endif + #include <string.h> class Socket @@ -29,15 +71,15 @@ class Socket void close() { - if(sockfd != -1) + if(sockfd != invalid_socket) { if(::close(sockfd) != 0) throw std::runtime_error("Socket::close failed"); - sockfd = -1; + sockfd = invalid_socket; } } - Socket(int fd, const std::string& peer_id = "") : + Socket(socket_t fd, const std::string& peer_id = "") : peer(peer_id), sockfd(fd) { } @@ -46,7 +88,7 @@ class Socket ~Socket() { close(); } private: std::string peer; - int sockfd; + socket_t sockfd; }; class Server_Socket @@ -57,26 +99,26 @@ class Server_Socket */ Socket* accept() { - int retval = ::accept(sockfd, 0, 0); - if(retval == -1) + socket_t retval = ::accept(sockfd, 0, 0); + if(retval == invalid_socket) throw std::runtime_error("Server_Socket: accept failed"); return new Socket(retval); } void close() { - if(sockfd != -1) + if(sockfd != invalid_socket) { if(::close(sockfd) != 0) throw std::runtime_error("Server_Socket::close failed"); - sockfd = -1; + sockfd = invalid_socket; } } Server_Socket(unsigned short); ~Server_Socket() { close(); } private: - int sockfd; + socket_t sockfd; }; /** @@ -84,7 +126,7 @@ class Server_Socket */ Socket::Socket(const std::string& host, unsigned short port) : peer(host) { - sockfd = -1; + sockfd = invalid_socket; hostent* host_addr = ::gethostbyname(host.c_str()); @@ -93,8 +135,8 @@ Socket::Socket(const std::string& host, unsigned short port) : peer(host) if(host_addr->h_addrtype != AF_INET) // FIXME throw std::runtime_error("Socket: " + host + " has IPv6 address"); - int fd = ::socket(PF_INET, SOCK_STREAM, 0); - if(fd == -1) + socket_t fd = ::socket(PF_INET, SOCK_STREAM, 0); + if(fd == invalid_socket) throw std::runtime_error("Socket: Unable to acquire socket"); sockaddr_in socket_info; @@ -122,21 +164,22 @@ Socket::Socket(const std::string& host, unsigned short port) : peer(host) */ size_t Socket::read(unsigned char buf[], size_t length) { - if(sockfd == -1) + if(sockfd == invalid_socket) throw std::runtime_error("Socket::read: Socket not connected"); size_t got = 0; while(length) { - ssize_t this_time = ::recv(sockfd, buf + got, length, MSG_NOSIGNAL); + ssize_t this_time = ::recv(sockfd, (char*)buf + got, + length, MSG_NOSIGNAL); if(this_time == 0) break; if(this_time == -1) { - if(errno == EINTR) + if(socket_error_code == EINTR) this_time = 0; else throw std::runtime_error("Socket::read: Socket read failed"); @@ -153,17 +196,18 @@ size_t Socket::read(unsigned char buf[], size_t length) */ void Socket::write(const unsigned char buf[], size_t length) { - if(sockfd == -1) + if(sockfd == invalid_socket) throw std::runtime_error("Socket::write: Socket not connected"); size_t offset = 0; while(length) { - ssize_t sent = ::send(sockfd, buf + offset, length, MSG_NOSIGNAL); + ssize_t sent = ::send(sockfd, (const char*)buf + offset, + length, MSG_NOSIGNAL); if(sent == -1) { - if(errno == EINTR) + if(socket_error_code == EINTR) sent = 0; else throw std::runtime_error("Socket::write: Socket write failed"); @@ -179,10 +223,10 @@ void Socket::write(const unsigned char buf[], size_t length) */ Server_Socket::Server_Socket(unsigned short port) { - sockfd = -1; + sockfd = invalid_socket; - int fd = ::socket(PF_INET, SOCK_STREAM, 0); - if(fd == -1) + socket_t fd = ::socket(PF_INET, SOCK_STREAM, 0); + if(fd == invalid_socket) throw std::runtime_error("Server_Socket: Unable to acquire socket"); sockaddr_in socket_info; @@ -199,7 +243,7 @@ Server_Socket::Server_Socket(unsigned short port) throw std::runtime_error("Server_Socket: bind failed"); } - if(listen(fd, 100) != 0) // FIXME: totally arbitrary + if(::listen(fd, 100) != 0) // FIXME: totally arbitrary { ::close(fd); throw std::runtime_error("Server_Socket: listen failed"); diff --git a/doc/examples/tls_client.cpp b/doc/examples/tls_client.cpp index eb4a98817..4896a28f8 100644 --- a/doc/examples/tls_client.cpp +++ b/doc/examples/tls_client.cpp @@ -35,13 +35,15 @@ int main(int argc, char* argv[]) try { - LibraryInitializer init; + LibraryInitializer botan_init; std::string host = argv[1]; u32bit port = argc == 3 ? Botan::to_u32bit(argv[2]) : 443; printf("Connecting to %s:%d...\n", host.c_str(), port); + SocketInitializer socket_init; + Socket sock(argv[1], port); AutoSeeded_RNG rng; @@ -54,8 +56,12 @@ int main(int argc, char* argv[]) printf("Handshake extablished...\n"); +#if 0 std::string http_command = "GET / HTTP/1.1\r\n" "Server: " + host + ':' + to_string(port) + "\r\n\r\n"; +#else + std::string http_command = "GET / HTTP/1.0\r\n\r\n"; +#endif tls.write((const byte*)http_command.c_str(), http_command.length()); diff --git a/doc/examples/tls_server.cpp b/doc/examples/tls_server.cpp index be5677c12..0c68ead3d 100644 --- a/doc/examples/tls_server.cpp +++ b/doc/examples/tls_server.cpp @@ -39,7 +39,8 @@ int main(int argc, char* argv[]) try { - LibraryInitializer init; + LibraryInitializer botan_init; + SocketInitializer socket_init; AutoSeeded_RNG rng; |