Server broadcasts presence to subnet (untested).
[dcpomatic.git] / src / lib / cross.cc
1 /*
2     Copyright (C) 2012 Carl Hetherington <cth@carlh.net>
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #include <fstream>
21 #include <boost/algorithm/string.hpp>
22 #include "cross.h"
23 #include "compose.hpp"
24 #include "log.h"
25 #ifdef DCPOMATIC_LINUX
26 #include <unistd.h>
27 #include <mntent.h>
28 #endif
29 #ifdef DCPOMATIC_WINDOWS
30 #include <windows.h>
31 #undef DATADIR
32 #include <shlwapi.h>
33 #endif
34 #ifdef DCPOMATIC_OSX
35 #include <sys/sysctl.h>
36 #include <mach-o/dyld.h>
37 #endif
38 #ifdef DCPOMATIC_POSIX
39 #include <sys/types.h>
40 #include <ifaddrs.h>
41 #include <netinet/in.h>
42 #include <arpa/inet.h>
43 #endif
44 #include "exceptions.h"
45
46 using std::pair;
47 using std::list;
48 using std::ifstream;
49 using std::string;
50 using std::wstring;
51 using std::make_pair;
52 using boost::shared_ptr;
53
54 void
55 dcpomatic_sleep (int s)
56 {
57 #ifdef DCPOMATIC_POSIX
58         sleep (s);
59 #endif
60 #ifdef DCPOMATIC_WINDOWS
61         Sleep (s * 1000);
62 #endif
63 }
64
65 /** @return A string of CPU information (model name etc.) */
66 string
67 cpu_info ()
68 {
69         string info;
70         
71 #ifdef DCPOMATIC_LINUX
72         ifstream f ("/proc/cpuinfo");
73         while (f.good ()) {
74                 string l;
75                 getline (f, l);
76                 if (boost::algorithm::starts_with (l, "model name")) {
77                         string::size_type const c = l.find (':');
78                         if (c != string::npos) {
79                                 info = l.substr (c + 2);
80                         }
81                 }
82         }
83 #endif
84
85 #ifdef DCPOMATIC_OSX
86         char buffer[64];
87         size_t N = sizeof (buffer);
88         if (sysctlbyname ("machdep.cpu.brand_string", buffer, &N, 0, 0) == 0) {
89                 info = buffer;
90         }
91 #endif          
92
93 #ifdef DCPOMATIC_WINDOWS
94         HKEY key;
95         if (RegOpenKeyEx (HKEY_LOCAL_MACHINE, L"HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0", 0, KEY_READ, &key) != ERROR_SUCCESS) {
96                 return info;
97         }
98
99         DWORD type;
100         DWORD data;
101         if (RegQueryValueEx (key, L"ProcessorNameString", 0, &type, 0, &data) != ERROR_SUCCESS) {
102                 return info;
103         }
104
105         if (type != REG_SZ) {
106                 return info;
107         }
108
109         wstring value (data / sizeof (wchar_t), L'\0');
110         if (RegQueryValueEx (key, L"ProcessorNameString", 0, 0, reinterpret_cast<LPBYTE> (&value[0]), &data) != ERROR_SUCCESS) {
111                 RegCloseKey (key);
112                 return info;
113         }
114
115         info = string (value.begin(), value.end());
116         
117         RegCloseKey (key);
118
119 #endif  
120         
121         return info;
122 }
123
124 #ifdef DCPOMATIC_OSX
125 /** @return Path of the Contents directory in the .app */
126 boost::filesystem::path
127 app_contents ()
128 {
129         uint32_t size = 1024;
130         char buffer[size];
131         if (_NSGetExecutablePath (buffer, &size)) {
132                 throw StringError ("_NSGetExecutablePath failed");
133         }
134         
135         boost::filesystem::path path (buffer);
136         path = boost::filesystem::canonical (path);
137         path = path.parent_path ();
138         path = path.parent_path ();
139         return path;
140 }
141 #endif
142
143 void
144 run_ffprobe (boost::filesystem::path content, boost::filesystem::path out, shared_ptr<Log> log)
145 {
146 #ifdef DCPOMATIC_WINDOWS
147         SECURITY_ATTRIBUTES security;
148         security.nLength = sizeof (security);
149         security.bInheritHandle = TRUE;
150         security.lpSecurityDescriptor = 0;
151
152         HANDLE child_stderr_read;
153         HANDLE child_stderr_write;
154         if (!CreatePipe (&child_stderr_read, &child_stderr_write, &security, 0)) {
155                 log->log ("ffprobe call failed (could not CreatePipe)");
156                 return;
157         }
158
159         wchar_t dir[512];
160         GetModuleFileName (GetModuleHandle (0), dir, sizeof (dir));
161         PathRemoveFileSpec (dir);
162         SetCurrentDirectory (dir);
163
164         STARTUPINFO startup_info;
165         ZeroMemory (&startup_info, sizeof (startup_info));
166         startup_info.cb = sizeof (startup_info);
167         startup_info.hStdError = child_stderr_write;
168         startup_info.dwFlags |= STARTF_USESTDHANDLES;
169
170         wchar_t command[512];
171         wcscpy (command, L"ffprobe.exe \"");
172
173         wchar_t file[512];
174         MultiByteToWideChar (CP_UTF8, 0, content.string().c_str(), -1, file, sizeof(file));
175         wcscat (command, file);
176
177         wcscat (command, L"\"");
178
179         PROCESS_INFORMATION process_info;
180         ZeroMemory (&process_info, sizeof (process_info));
181         if (!CreateProcess (0, command, 0, 0, TRUE, CREATE_NO_WINDOW, 0, 0, &startup_info, &process_info)) {
182                 log->log ("ffprobe call failed (could not CreateProcess)");
183                 return;
184         }
185
186         FILE* o = fopen (out.string().c_str(), "w");
187         if (!o) {
188                 log->log ("ffprobe call failed (could not create output file)");
189                 return;
190         }
191
192         CloseHandle (child_stderr_write);
193
194         while (1) {
195                 char buffer[512];
196                 DWORD read;
197                 if (!ReadFile(child_stderr_read, buffer, sizeof(buffer), &read, 0) || read == 0) {
198                         break;
199                 }
200                 fwrite (buffer, read, 1, o);
201         }
202
203         fclose (o);
204
205         WaitForSingleObject (process_info.hProcess, INFINITE);
206         CloseHandle (process_info.hProcess);
207         CloseHandle (process_info.hThread);
208         CloseHandle (child_stderr_read);
209 #endif
210
211 #ifdef DCPOMATIC_LINUX 
212         string ffprobe = "ffprobe \"" + content.string() + "\" 2> \"" + out.string() + "\"";
213         log->log (String::compose ("Probing with %1", ffprobe));
214         system (ffprobe.c_str ());
215 #endif
216
217 #ifdef DCPOMATIC_OSX
218         boost::filesystem::path path = app_contents();
219         path /= "MacOS";
220         path /= "ffprobe";
221         
222         string ffprobe = path.string() + " \"" + content.string() + "\" 2> \"" + out.string() + "\"";
223         log->log (String::compose ("Probing with %1", ffprobe));
224         system (ffprobe.c_str ());
225 #endif
226 }
227
228 list<pair<string, string> >
229 mount_info ()
230 {
231         list<pair<string, string> > m;
232         
233 #ifdef DCPOMATIC_LINUX
234         FILE* f = setmntent ("/etc/mtab", "r");
235         if (!f) {
236                 return m;
237         }
238         
239         while (1) {
240                 struct mntent* mnt = getmntent (f);
241                 if (!mnt) {
242                         break;
243                 }
244
245                 m.push_back (make_pair (mnt->mnt_dir, mnt->mnt_type));
246         }
247
248         endmntent (f);
249 #endif
250
251         return m;
252 }
253
254 boost::filesystem::path
255 openssl_path ()
256 {
257 #ifdef DCPOMATIC_WINDOWS
258         wchar_t dir[512];
259         GetModuleFileName (GetModuleHandle (0), dir, sizeof (dir));
260         PathRemoveFileSpec (dir);
261         
262         boost::filesystem::path path = dir;
263         path /= "openssl.exe";
264         return path;
265 #else   
266         /* We assume that it's on the path for Linux and OS X */
267         return "openssl";
268 #endif
269
270 }
271
272 list<string>
273 network_interfaces ()
274 {
275         list<string> interfaces;
276         
277 #ifdef DCPOMATIC_POSIX
278         struct ifaddrs* addresses = 0;
279
280         getifaddrs (&addresses);
281
282         for (struct ifaddrs* i = addresses; i; i = i->ifa_next) {
283                 if (i->ifa_addr->sa_family == AF_INET) {
284                         void* p = &((struct sockaddr_in *) i->ifa_addr)->sin_addr;
285                         char b[INET_ADDRSTRLEN];
286                         inet_ntop (AF_INET, p, b, INET_ADDRSTRLEN);
287                         interfaces.push_back (b);
288                 }
289         }
290
291         freeifaddrs (addresses);
292 #endif
293
294         return interfaces;
295 }