blob: 032e04f7d6b0b940d01fce7672670c666fc6783d [file] [log] [blame]
[email protected]774bdcc2012-01-19 03:44:381// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]0840cc72009-11-24 16:14:532// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "base/sync_socket.h"
6#include <limits.h>
7#include <stdio.h>
8#include <windows.h>
9#include <sys/types.h>
[email protected]0840cc72009-11-24 16:14:5310#include "base/logging.h"
11
[email protected]0840cc72009-11-24 16:14:5312namespace base {
13
14namespace {
[email protected]774bdcc2012-01-19 03:44:3815// IMPORTANT: do not change how this name is generated because it will break
16// in sandboxed scenarios as we might have by-name policies that allow pipe
17// creation. Also keep the secure random number generation.
18const wchar_t kPipeNameFormat[] = L"\\\\.\\pipe\\chrome.sync.%u.%u.%lu";
19const size_t kPipePathMax = arraysize(kPipeNameFormat) + (3 * 10) + 1;
[email protected]0840cc72009-11-24 16:14:5320
21// To avoid users sending negative message lengths to Send/Receive
22// we clamp message lengths, which are size_t, to no more than INT_MAX.
23const size_t kMaxMessageLength = static_cast<size_t>(INT_MAX);
24
25const int kOutBufferSize = 4096;
26const int kInBufferSize = 4096;
27const int kDefaultTimeoutMilliSeconds = 1000;
28
[email protected]0840cc72009-11-24 16:14:5329} // namespace
30
[email protected]5895ee12011-12-22 19:33:2731const SyncSocket::Handle SyncSocket::kInvalidHandle = INVALID_HANDLE_VALUE;
32
[email protected]0840cc72009-11-24 16:14:5333bool SyncSocket::CreatePair(SyncSocket* pair[2]) {
34 Handle handles[2];
35 SyncSocket* tmp_sockets[2];
36
37 // Create the two SyncSocket objects first to avoid ugly cleanup issues.
38 tmp_sockets[0] = new SyncSocket(kInvalidHandle);
39 if (tmp_sockets[0] == NULL) {
40 return false;
41 }
42 tmp_sockets[1] = new SyncSocket(kInvalidHandle);
43 if (tmp_sockets[1] == NULL) {
44 delete tmp_sockets[0];
45 return false;
46 }
47
48 wchar_t name[kPipePathMax];
49 do {
50 unsigned int rnd_name;
51 if (rand_s(&rnd_name) != 0)
52 return false;
[email protected]774bdcc2012-01-19 03:44:3853 swprintf(name, kPipePathMax,
54 kPipeNameFormat,
55 GetCurrentProcessId(),
56 GetCurrentThreadId(),
[email protected]0840cc72009-11-24 16:14:5357 rnd_name);
58 handles[0] = CreateNamedPipeW(
59 name,
60 PIPE_ACCESS_DUPLEX | FILE_FLAG_FIRST_PIPE_INSTANCE,
61 PIPE_TYPE_BYTE | PIPE_READMODE_BYTE,
62 1,
63 kOutBufferSize,
64 kInBufferSize,
65 kDefaultTimeoutMilliSeconds,
66 NULL);
[email protected]774bdcc2012-01-19 03:44:3867 } while ((handles[0] == INVALID_HANDLE_VALUE) &&
68 (GetLastError() == ERROR_PIPE_BUSY));
69
70 if (handles[0] == INVALID_HANDLE_VALUE) {
71 NOTREACHED();
72 return false;
73 }
74 // The SECURITY_ANONYMOUS flag means that the server side (pair[0]) cannot
75 // impersonate the client (pair[1]). This allows us not to care which side
76 // ends up in which side of a privilege boundary.
[email protected]0840cc72009-11-24 16:14:5377 handles[1] = CreateFileW(name,
78 GENERIC_READ | GENERIC_WRITE,
79 0, // no sharing.
80 NULL, // default security attributes.
81 OPEN_EXISTING, // opens existing pipe.
82 SECURITY_SQOS_PRESENT | SECURITY_ANONYMOUS,
[email protected]0840cc72009-11-24 16:14:5383 NULL); // no template file.
84 if (handles[1] == INVALID_HANDLE_VALUE) {
85 CloseHandle(handles[0]);
86 return false;
87 }
88 if (ConnectNamedPipe(handles[0], NULL) == FALSE) {
89 DWORD error = GetLastError();
90 if (error != ERROR_PIPE_CONNECTED) {
91 CloseHandle(handles[0]);
92 CloseHandle(handles[1]);
93 return false;
94 }
95 }
96 // Copy the handles out for successful return.
97 tmp_sockets[0]->handle_ = handles[0];
98 pair[0] = tmp_sockets[0];
99 tmp_sockets[1]->handle_ = handles[1];
100 pair[1] = tmp_sockets[1];
101 return true;
102}
103
104bool SyncSocket::Close() {
105 if (handle_ == kInvalidHandle) {
106 return false;
107 }
108 BOOL retval = CloseHandle(handle_);
109 handle_ = kInvalidHandle;
110 return retval ? true : false;
111}
112
113size_t SyncSocket::Send(const void* buffer, size_t length) {
[email protected]d7a93ad2011-04-22 13:13:07114 DCHECK_LE(length, kMaxMessageLength);
[email protected]0840cc72009-11-24 16:14:53115 size_t count = 0;
116 while (count < length) {
117 DWORD len;
118 // The following statement is for 64 bit portability.
119 DWORD chunk = static_cast<DWORD>(
120 ((length - count) <= UINT_MAX) ? (length - count) : UINT_MAX);
121 if (WriteFile(handle_, static_cast<const char*>(buffer) + count,
122 chunk, &len, NULL) == FALSE) {
123 return (0 < count) ? count : 0;
124 }
125 count += len;
126 }
127 return count;
128}
129
130size_t SyncSocket::Receive(void* buffer, size_t length) {
[email protected]d7a93ad2011-04-22 13:13:07131 DCHECK_LE(length, kMaxMessageLength);
[email protected]0840cc72009-11-24 16:14:53132 size_t count = 0;
133 while (count < length) {
134 DWORD len;
135 DWORD chunk = static_cast<DWORD>(
136 ((length - count) <= UINT_MAX) ? (length - count) : UINT_MAX);
137 if (ReadFile(handle_, static_cast<char*>(buffer) + count,
138 chunk, &len, NULL) == FALSE) {
139 return (0 < count) ? count : 0;
140 }
141 count += len;
142 }
143 return count;
144}
145
[email protected]d8b65912009-12-04 22:53:22146size_t SyncSocket::Peek() {
147 DWORD available = 0;
148 PeekNamedPipe(handle_, NULL, 0, NULL, &available, NULL);
149 return available;
150}
151
[email protected]0840cc72009-11-24 16:14:53152} // namespace base