2 * Copyright (C) ARM Limited 2010-2013. All rights reserved.
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
16 #include <netinet/in.h>
17 #include <sys/socket.h>
25 #define CLOSE_SOCKET(x) closesocket(x)
26 #define SHUTDOWN_RX_TX SD_BOTH
27 #define snprintf _snprintf
29 #define CLOSE_SOCKET(x) close(x)
30 #define SHUTDOWN_RX_TX SHUT_RDWR
33 OlySocket::OlySocket(int port, bool multiple) {
36 if (WSAStartup(0x0202, &wsaData) != 0) {
37 logg->logError(__FILE__, __LINE__, "Windows socket initialization failed");
43 createServerSocket(port);
45 createSingleServerConnection(port);
49 OlySocket::OlySocket(int port, char* host) {
51 createClientSocket(host, port);
54 OlySocket::~OlySocket() {
56 CLOSE_SOCKET(mSocketID);
60 void OlySocket::shutdownConnection() {
61 // Shutdown is primarily used to unblock other threads that are blocking on send/receive functions
62 shutdown(mSocketID, SHUTDOWN_RX_TX);
65 void OlySocket::closeSocket() {
66 // Used for closing an accepted socket but keeping the server socket active
68 CLOSE_SOCKET(mSocketID);
73 void OlySocket::closeServerSocket() {
74 if (CLOSE_SOCKET(mFDServer) != 0) {
75 logg->logError(__FILE__, __LINE__, "Failed to close server socket.");
81 void OlySocket::createClientSocket(char* hostname, int portno) {
83 // TODO: Implement for Windows
86 struct addrinfo hints, *res, *res0;
88 snprintf(buf, sizeof(buf), "%d", portno);
90 memset((void*)&hints, 0, sizeof(hints));
91 hints.ai_family = PF_UNSPEC;
92 hints.ai_socktype = SOCK_STREAM;
94 if (getaddrinfo(hostname, buf, &hints, &res0)) {
95 logg->logError(__FILE__, __LINE__, "Client socket failed to get address info for %s", hostname);
98 for (res=res0; res!=NULL; res = res->ai_next) {
99 if ( res->ai_family != PF_INET || res->ai_socktype != SOCK_STREAM ) {
102 mSocketID = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
106 if (connect(mSocketID, res->ai_addr, res->ai_addrlen) < 0) {
115 if (mSocketID <= 0) {
116 logg->logError(__FILE__, __LINE__, "Could not connect to client socket. Ensure ARM Streamline is running.");
122 void OlySocket::createSingleServerConnection(int port) {
123 createServerSocket(port);
125 mSocketID = acceptConnection();
129 void OlySocket::createServerSocket(int port) {
130 int family = AF_INET6;
133 mFDServer = socket(PF_INET6, SOCK_STREAM, IPPROTO_TCP);
136 mFDServer = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
138 logg->logError(__FILE__, __LINE__, "Error creating server socket");
143 // Enable address reuse, another solution would be to create the server socket once and only close it when the object exits
145 if (setsockopt(mFDServer, SOL_SOCKET, SO_REUSEADDR, (const char*)&on, sizeof(on)) != 0) {
146 logg->logError(__FILE__, __LINE__, "Setting server socket options failed");
150 // Create sockaddr_in structure, ensuring non-populated fields are zero
151 struct sockaddr_in6 sockaddr;
152 memset((void*)&sockaddr, 0, sizeof(sockaddr));
153 sockaddr.sin6_family = family;
154 sockaddr.sin6_port = htons(port);
155 sockaddr.sin6_addr = in6addr_any;
157 // Bind the socket to an address
158 if (bind(mFDServer, (const struct sockaddr*)&sockaddr, sizeof(sockaddr)) < 0) {
159 logg->logError(__FILE__, __LINE__, "Binding of server socket failed.\nIs an instance already running?");
163 // Listen for connections on this socket
164 if (listen(mFDServer, 1) < 0) {
165 logg->logError(__FILE__, __LINE__, "Listening of server socket failed");
170 // mSocketID is always set to the most recently accepted connection
171 // The user of this class should maintain the different socket connections, e.g. by forking the process
172 int OlySocket::acceptConnection() {
173 if (mFDServer <= 0) {
174 logg->logError(__FILE__, __LINE__, "Attempting multiple connections on a single connection server socket or attempting to accept on a client socket");
178 // Accept a connection, note that this call blocks until a client connects
179 mSocketID = accept(mFDServer, NULL, NULL);
181 logg->logError(__FILE__, __LINE__, "Socket acceptance failed");
187 void OlySocket::send(char* buffer, int size) {
188 if (size <= 0 || buffer == NULL) {
193 int n = ::send(mSocketID, buffer, size, 0);
195 logg->logError(__FILE__, __LINE__, "Socket send error");
203 // Returns the number of bytes received
204 int OlySocket::receive(char* buffer, int size) {
205 if (size <= 0 || buffer == NULL) {
209 int bytes = recv(mSocketID, buffer, size, 0);
211 logg->logError(__FILE__, __LINE__, "Socket receive error");
213 } else if (bytes == 0) {
214 logg->logMessage("Socket disconnected");
220 // Receive exactly size bytes of data. Note, this function will block until all bytes are received
221 int OlySocket::receiveNBytes(char* buffer, int size) {
223 while (size > 0 && buffer != NULL) {
224 bytes = recv(mSocketID, buffer, size, 0);
226 logg->logError(__FILE__, __LINE__, "Socket receive error");
228 } else if (bytes == 0) {
229 logg->logMessage("Socket disconnected");
238 // Receive data until a carriage return, line feed, or null is encountered, or the buffer fills
239 int OlySocket::receiveString(char* buffer, int size) {
240 int bytes_received = 0;
247 while (!found && bytes_received < size) {
248 // Receive a single character
249 int bytes = recv(mSocketID, &buffer[bytes_received], 1, 0);
251 logg->logError(__FILE__, __LINE__, "Socket receive error");
253 } else if (bytes == 0) {
254 logg->logMessage("Socket disconnected");
258 // Replace carriage returns and line feeds with zero
259 if (buffer[bytes_received] == '\n' || buffer[bytes_received] == '\r' || buffer[bytes_received] == '\0') {
260 buffer[bytes_received] = '\0';
267 return bytes_received;