diff options
Diffstat (limited to 'daemon/OlySocket.cpp')
-rw-r--r-- | daemon/OlySocket.cpp | 77 |
1 files changed, 19 insertions, 58 deletions
diff --git a/daemon/OlySocket.cpp b/daemon/OlySocket.cpp index 26e4768..28774e3 100644 --- a/daemon/OlySocket.cpp +++ b/daemon/OlySocket.cpp | |||
@@ -9,6 +9,7 @@ | |||
9 | #include "OlySocket.h" | 9 | #include "OlySocket.h" |
10 | 10 | ||
11 | #include <stdio.h> | 11 | #include <stdio.h> |
12 | #include <string.h> | ||
12 | #ifdef WIN32 | 13 | #ifdef WIN32 |
13 | #include <Winsock2.h> | 14 | #include <Winsock2.h> |
14 | #include <ws2tcpip.h> | 15 | #include <ws2tcpip.h> |
@@ -43,16 +44,18 @@ OlyServerSocket::OlyServerSocket(int port) { | |||
43 | createServerSocket(port); | 44 | createServerSocket(port); |
44 | } | 45 | } |
45 | 46 | ||
46 | OlySocket::OlySocket(int port, const char* host) { | ||
47 | createClientSocket(host, port); | ||
48 | } | ||
49 | |||
50 | OlySocket::OlySocket(int socketID) : mSocketID(socketID) { | 47 | OlySocket::OlySocket(int socketID) : mSocketID(socketID) { |
51 | } | 48 | } |
52 | 49 | ||
53 | #ifndef WIN32 | 50 | #ifndef WIN32 |
54 | 51 | ||
55 | OlyServerSocket::OlyServerSocket(const char* path) { | 52 | #define MIN(A, B) ({ \ |
53 | const __typeof__(A) __a = A; \ | ||
54 | const __typeof__(B) __b = B; \ | ||
55 | __a > __b ? __b : __a; \ | ||
56 | }) | ||
57 | |||
58 | OlyServerSocket::OlyServerSocket(const char* path, const size_t pathSize) { | ||
56 | // Create socket | 59 | // Create socket |
57 | mFDServer = socket(PF_UNIX, SOCK_STREAM, 0); | 60 | mFDServer = socket(PF_UNIX, SOCK_STREAM, 0); |
58 | if (mFDServer < 0) { | 61 | if (mFDServer < 0) { |
@@ -60,13 +63,11 @@ OlyServerSocket::OlyServerSocket(const char* path) { | |||
60 | handleException(); | 63 | handleException(); |
61 | } | 64 | } |
62 | 65 | ||
63 | unlink(path); | ||
64 | |||
65 | // Create sockaddr_in structure, ensuring non-populated fields are zero | 66 | // Create sockaddr_in structure, ensuring non-populated fields are zero |
66 | struct sockaddr_un sockaddr; | 67 | struct sockaddr_un sockaddr; |
67 | memset((void*)&sockaddr, 0, sizeof(sockaddr)); | 68 | memset((void*)&sockaddr, 0, sizeof(sockaddr)); |
68 | sockaddr.sun_family = AF_UNIX; | 69 | sockaddr.sun_family = AF_UNIX; |
69 | strncpy(sockaddr.sun_path, path, sizeof(sockaddr.sun_path) - 1); | 70 | memcpy(sockaddr.sun_path, path, MIN(pathSize, sizeof(sockaddr.sun_path))); |
70 | sockaddr.sun_path[sizeof(sockaddr.sun_path) - 1] = '\0'; | 71 | sockaddr.sun_path[sizeof(sockaddr.sun_path) - 1] = '\0'; |
71 | 72 | ||
72 | // Bind the socket to an address | 73 | // Bind the socket to an address |
@@ -82,24 +83,25 @@ OlyServerSocket::OlyServerSocket(const char* path) { | |||
82 | } | 83 | } |
83 | } | 84 | } |
84 | 85 | ||
85 | OlySocket::OlySocket(const char* path) { | 86 | int OlySocket::connect(const char* path, const size_t pathSize) { |
86 | mSocketID = socket(PF_UNIX, SOCK_STREAM, 0); | 87 | int fd = socket(PF_UNIX, SOCK_STREAM, 0); |
87 | if (mSocketID < 0) { | 88 | if (fd < 0) { |
88 | return; | 89 | return -1; |
89 | } | 90 | } |
90 | 91 | ||
91 | // Create sockaddr_in structure, ensuring non-populated fields are zero | 92 | // Create sockaddr_in structure, ensuring non-populated fields are zero |
92 | struct sockaddr_un sockaddr; | 93 | struct sockaddr_un sockaddr; |
93 | memset((void*)&sockaddr, 0, sizeof(sockaddr)); | 94 | memset((void*)&sockaddr, 0, sizeof(sockaddr)); |
94 | sockaddr.sun_family = AF_UNIX; | 95 | sockaddr.sun_family = AF_UNIX; |
95 | strncpy(sockaddr.sun_path, path, sizeof(sockaddr.sun_path) - 1); | 96 | memcpy(sockaddr.sun_path, path, MIN(pathSize, sizeof(sockaddr.sun_path))); |
96 | sockaddr.sun_path[sizeof(sockaddr.sun_path) - 1] = '\0'; | 97 | sockaddr.sun_path[sizeof(sockaddr.sun_path) - 1] = '\0'; |
97 | 98 | ||
98 | if (connect(mSocketID, (const struct sockaddr*)&sockaddr, sizeof(sockaddr)) < 0) { | 99 | if (::connect(fd, (const struct sockaddr*)&sockaddr, sizeof(sockaddr)) < 0) { |
99 | close(mSocketID); | 100 | close(fd); |
100 | mSocketID = -1; | 101 | return -1; |
101 | return; | ||
102 | } | 102 | } |
103 | |||
104 | return fd; | ||
103 | } | 105 | } |
104 | 106 | ||
105 | #endif | 107 | #endif |
@@ -137,47 +139,6 @@ void OlyServerSocket::closeServerSocket() { | |||
137 | mFDServer = 0; | 139 | mFDServer = 0; |
138 | } | 140 | } |
139 | 141 | ||
140 | void OlySocket::createClientSocket(const char* hostname, int portno) { | ||
141 | #ifdef WIN32 | ||
142 | // TODO: Implement for Windows | ||
143 | #else | ||
144 | char buf[32]; | ||
145 | struct addrinfo hints, *res, *res0; | ||
146 | |||
147 | snprintf(buf, sizeof(buf), "%d", portno); | ||
148 | mSocketID = -1; | ||
149 | memset((void*)&hints, 0, sizeof(hints)); | ||
150 | hints.ai_family = PF_UNSPEC; | ||
151 | hints.ai_socktype = SOCK_STREAM; | ||
152 | |||
153 | if (getaddrinfo(hostname, buf, &hints, &res0)) { | ||
154 | logg->logError(__FILE__, __LINE__, "Client socket failed to get address info for %s", hostname); | ||
155 | handleException(); | ||
156 | } | ||
157 | for (res=res0; res!=NULL; res = res->ai_next) { | ||
158 | if ( res->ai_family != PF_INET || res->ai_socktype != SOCK_STREAM ) { | ||
159 | continue; | ||
160 | } | ||
161 | mSocketID = socket(res->ai_family, res->ai_socktype, res->ai_protocol); | ||
162 | if (mSocketID < 0) { | ||
163 | continue; | ||
164 | } | ||
165 | if (connect(mSocketID, res->ai_addr, res->ai_addrlen) < 0) { | ||
166 | close(mSocketID); | ||
167 | mSocketID = -1; | ||
168 | } | ||
169 | if (mSocketID > 0) { | ||
170 | break; | ||
171 | } | ||
172 | } | ||
173 | freeaddrinfo(res0); | ||
174 | if (mSocketID <= 0) { | ||
175 | logg->logError(__FILE__, __LINE__, "Could not connect to client socket. Ensure ARM Streamline is running."); | ||
176 | handleException(); | ||
177 | } | ||
178 | #endif | ||
179 | } | ||
180 | |||
181 | void OlyServerSocket::createServerSocket(int port) { | 142 | void OlyServerSocket::createServerSocket(int port) { |
182 | int family = AF_INET6; | 143 | int family = AF_INET6; |
183 | 144 | ||