1 | /* ***** BEGIN LICENSE BLOCK *****
|
---|
2 | * Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
---|
3 | *
|
---|
4 | * The contents of this file are subject to the Mozilla Public License Version
|
---|
5 | * 1.1 (the "License"); you may not use this file except in compliance with
|
---|
6 | * the License. You may obtain a copy of the License at
|
---|
7 | * http://www.mozilla.org/MPL/
|
---|
8 | *
|
---|
9 | * Software distributed under the License is distributed on an "AS IS" basis,
|
---|
10 | * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
---|
11 | * for the specific language governing rights and limitations under the
|
---|
12 | * License.
|
---|
13 | *
|
---|
14 | * The Original Code is Mozilla IPC.
|
---|
15 | *
|
---|
16 | * The Initial Developer of the Original Code is
|
---|
17 | * Netscape Communications Corporation.
|
---|
18 | * Portions created by the Initial Developer are Copyright (C) 2002
|
---|
19 | * the Initial Developer. All Rights Reserved.
|
---|
20 | *
|
---|
21 | * Contributor(s):
|
---|
22 | * Darin Fisher <darin@netscape.com>
|
---|
23 | *
|
---|
24 | * Alternatively, the contents of this file may be used under the terms of
|
---|
25 | * either the GNU General Public License Version 2 or later (the "GPL"), or
|
---|
26 | * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
---|
27 | * in which case the provisions of the GPL or the LGPL are applicable instead
|
---|
28 | * of those above. If you wish to allow use of your version of this file only
|
---|
29 | * under the terms of either the GPL or the LGPL, and not to allow others to
|
---|
30 | * use your version of this file under the terms of the MPL, indicate your
|
---|
31 | * decision by deleting the provisions above and replace them with the notice
|
---|
32 | * and other provisions required by the GPL or the LGPL. If you do not delete
|
---|
33 | * the provisions above, a recipient may use your version of this file under
|
---|
34 | * the terms of any one of the MPL, the GPL or the LGPL.
|
---|
35 | *
|
---|
36 | * ***** END LICENSE BLOCK ***** */
|
---|
37 |
|
---|
38 | #include <string.h>
|
---|
39 | #include "ipcm.h"
|
---|
40 |
|
---|
41 | #include <iprt/asm.h>
|
---|
42 |
|
---|
43 | const nsID IPCM_TARGET =
|
---|
44 | { /* 753ca8ff-c8c2-4601-b115-8c2944da1150 */
|
---|
45 | 0x753ca8ff,
|
---|
46 | 0xc8c2,
|
---|
47 | 0x4601,
|
---|
48 | {0xb1, 0x15, 0x8c, 0x29, 0x44, 0xda, 0x11, 0x50}
|
---|
49 | };
|
---|
50 |
|
---|
51 | PRUint32
|
---|
52 | IPCM_NewRequestIndex()
|
---|
53 | {
|
---|
54 | static volatile uint32_t sRequestIndex = 0;
|
---|
55 | return ASMAtomicIncU32(&sRequestIndex);
|
---|
56 | }
|
---|
57 |
|
---|
58 | #if 0
|
---|
59 |
|
---|
60 | //
|
---|
61 | // MSG_TYPE values
|
---|
62 | //
|
---|
63 | const PRUint32 ipcmMessagePing::MSG_TYPE = IPCM_MSG_TYPE_PING;
|
---|
64 | const PRUint32 ipcmMessageError::MSG_TYPE = IPCM_MSG_TYPE_ERROR;
|
---|
65 | const PRUint32 ipcmMessageClientHello::MSG_TYPE = IPCM_MSG_TYPE_CLIENT_HELLO;
|
---|
66 | const PRUint32 ipcmMessageClientID::MSG_TYPE = IPCM_MSG_TYPE_CLIENT_ID;
|
---|
67 | const PRUint32 ipcmMessageClientInfo::MSG_TYPE = IPCM_MSG_TYPE_CLIENT_INFO;
|
---|
68 | const PRUint32 ipcmMessageClientAddName::MSG_TYPE = IPCM_MSG_TYPE_CLIENT_ADD_NAME;
|
---|
69 | const PRUint32 ipcmMessageClientDelName::MSG_TYPE = IPCM_MSG_TYPE_CLIENT_DEL_NAME;
|
---|
70 | const PRUint32 ipcmMessageClientAddTarget::MSG_TYPE = IPCM_MSG_TYPE_CLIENT_ADD_TARGET;
|
---|
71 | const PRUint32 ipcmMessageClientDelTarget::MSG_TYPE = IPCM_MSG_TYPE_CLIENT_DEL_TARGET;
|
---|
72 | const PRUint32 ipcmMessageQueryClientByName::MSG_TYPE = IPCM_MSG_TYPE_QUERY_CLIENT_BY_NAME;
|
---|
73 | const PRUint32 ipcmMessageQueryClientInfo::MSG_TYPE = IPCM_MSG_TYPE_QUERY_CLIENT_INFO;
|
---|
74 | const PRUint32 ipcmMessageForward::MSG_TYPE = IPCM_MSG_TYPE_FORWARD;
|
---|
75 | const PRUint32 ipcmMessageClientStatus::MSG_TYPE = IPCM_MSG_TYPE_CLIENT_STATUS;
|
---|
76 |
|
---|
77 | //
|
---|
78 | // CLIENT_INFO message
|
---|
79 | //
|
---|
80 | // +-----------------------------------------+
|
---|
81 | // | DWORD : MSG_TYPE |
|
---|
82 | // +--------------------+--------------------+
|
---|
83 | // | DWORD : clientID |
|
---|
84 | // +--------------------+--------------------+
|
---|
85 | // | DWORD : requestIndex |
|
---|
86 | // +--------------------+--------------------+
|
---|
87 | // | WORD : nameStart | WORD : nameCount |
|
---|
88 | // +--------------------+--------------------+
|
---|
89 | // | WORD : targetStart | WORD : targetCount |
|
---|
90 | // +--------------------+--------------------+
|
---|
91 | // | name[0] | (null byte) |
|
---|
92 | // +--------------------+--------------------+
|
---|
93 | // . . .
|
---|
94 | // . . .
|
---|
95 | // +--------------------+--------------------+
|
---|
96 | // | name[count - 1] | (null byte) |
|
---|
97 | // +--------------------+--------------------+
|
---|
98 | // | target[0] |
|
---|
99 | // +-----------------------------------------+
|
---|
100 | // . . .
|
---|
101 | // . . .
|
---|
102 | // +-----------------------------------------+
|
---|
103 | // | target[count - 1] |
|
---|
104 | // +-----------------------------------------+
|
---|
105 | //
|
---|
106 |
|
---|
107 | struct ipcmClientInfoHeader
|
---|
108 | {
|
---|
109 | PRUint32 mType;
|
---|
110 | PRUint32 mID;
|
---|
111 | PRUint32 mRequestIndex;
|
---|
112 | PRUint16 mNameStart;
|
---|
113 | PRUint16 mNameCount;
|
---|
114 | PRUint16 mTargetStart;
|
---|
115 | PRUint16 mTargetCount;
|
---|
116 | };
|
---|
117 |
|
---|
118 | ipcmMessageClientInfo::ipcmMessageClientInfo(PRUint32 cID, PRUint32 rIdx, const char *names[], const nsID *targets[])
|
---|
119 | {
|
---|
120 | ipcmClientInfoHeader hdr = {0};
|
---|
121 |
|
---|
122 | hdr.mType = MSG_TYPE;
|
---|
123 | hdr.mID = cID;
|
---|
124 | hdr.mRequestIndex = rIdx;
|
---|
125 | hdr.mNameStart = sizeof(hdr);
|
---|
126 |
|
---|
127 | PRUint32 i, namesLen = 0;
|
---|
128 |
|
---|
129 | i = 0;
|
---|
130 | while (names[i]) {
|
---|
131 | namesLen += (strlen(names[i]) + 1);
|
---|
132 | ++hdr.mNameCount;
|
---|
133 | ++i;
|
---|
134 | }
|
---|
135 |
|
---|
136 | i = 0;
|
---|
137 | while (targets[i]) {
|
---|
138 | ++hdr.mTargetCount;
|
---|
139 | ++i;
|
---|
140 | }
|
---|
141 |
|
---|
142 | //
|
---|
143 | // compute target array starting offset
|
---|
144 | //
|
---|
145 | hdr.mTargetStart = hdr.mNameStart + namesLen;
|
---|
146 |
|
---|
147 | //
|
---|
148 | // compute message length
|
---|
149 | //
|
---|
150 | PRUint32 dataLen = sizeof(hdr) + namesLen + hdr.mTargetCount * sizeof(nsID);
|
---|
151 |
|
---|
152 | Init(IPCM_TARGET, NULL, dataLen);
|
---|
153 |
|
---|
154 | //
|
---|
155 | // write message data
|
---|
156 | //
|
---|
157 | SetData(0, (const char *) &hdr, sizeof(hdr));
|
---|
158 |
|
---|
159 | PRUint32 offset = sizeof(hdr);
|
---|
160 |
|
---|
161 | for (i = 0; names[i]; ++i) {
|
---|
162 | PRUint32 len = strlen(names[i]) + 1;
|
---|
163 | SetData(offset, names[i], len);
|
---|
164 | offset += len;
|
---|
165 | }
|
---|
166 |
|
---|
167 | for (i = 0; targets[i]; ++i) {
|
---|
168 | PRUint32 len = sizeof(nsID);
|
---|
169 | SetData(offset, (const char *) targets[i], len);
|
---|
170 | offset += len;
|
---|
171 | }
|
---|
172 | }
|
---|
173 |
|
---|
174 | PRUint32
|
---|
175 | ipcmMessageClientInfo::ClientID() const
|
---|
176 | {
|
---|
177 | ipcmClientInfoHeader *hdr = (ipcmClientInfoHeader *) Data();
|
---|
178 | return hdr->mID;
|
---|
179 | }
|
---|
180 |
|
---|
181 | PRUint32
|
---|
182 | ipcmMessageClientInfo::RequestIndex() const
|
---|
183 | {
|
---|
184 | ipcmClientInfoHeader *hdr = (ipcmClientInfoHeader *) Data();
|
---|
185 | return hdr->mRequestIndex;
|
---|
186 | }
|
---|
187 |
|
---|
188 | PRUint32
|
---|
189 | ipcmMessageClientInfo::NameCount() const
|
---|
190 | {
|
---|
191 | ipcmClientInfoHeader *hdr = (ipcmClientInfoHeader *) Data();
|
---|
192 | return hdr->mNameCount;
|
---|
193 | }
|
---|
194 |
|
---|
195 | PRUint32
|
---|
196 | ipcmMessageClientInfo::TargetCount() const
|
---|
197 | {
|
---|
198 | ipcmClientInfoHeader *hdr = (ipcmClientInfoHeader *) Data();
|
---|
199 | return hdr->mTargetCount;
|
---|
200 | }
|
---|
201 |
|
---|
202 | const char *
|
---|
203 | ipcmMessageClientInfo::NextName(const char *name) const
|
---|
204 | {
|
---|
205 | ipcmClientInfoHeader *hdr = (ipcmClientInfoHeader *) Data();
|
---|
206 |
|
---|
207 | if (!name)
|
---|
208 | return (const char *) hdr + hdr->mNameStart;
|
---|
209 |
|
---|
210 | name += strlen(name) + 1;
|
---|
211 | if (name == (const char *) hdr + hdr->mTargetStart)
|
---|
212 | name = NULL;
|
---|
213 | return name;
|
---|
214 | }
|
---|
215 |
|
---|
216 | const nsID *
|
---|
217 | ipcmMessageClientInfo::NextTarget(const nsID *target) const
|
---|
218 | {
|
---|
219 | ipcmClientInfoHeader *hdr = (ipcmClientInfoHeader *) Data();
|
---|
220 |
|
---|
221 | if (!target)
|
---|
222 | return (const nsID *) (Data() + hdr->mTargetStart);
|
---|
223 |
|
---|
224 | if (++target == (const nsID *) (MsgBuf() + MsgLen()))
|
---|
225 | target = NULL;
|
---|
226 | return target;
|
---|
227 | }
|
---|
228 | #endif
|
---|
229 |
|
---|
230 | //
|
---|
231 | // FORWARD message
|
---|
232 | //
|
---|
233 | // +-------------------------+
|
---|
234 | // | DWORD : MSG_TYPE |
|
---|
235 | // +-------------------------+
|
---|
236 | // | clientID |
|
---|
237 | // +-------------------------+
|
---|
238 | // | innerMsgHeader |
|
---|
239 | // +-------------------------+
|
---|
240 | // | innerMsgData |
|
---|
241 | // +-------------------------+
|
---|
242 | //
|
---|
243 |
|
---|
244 | ipcmMessageForward::ipcmMessageForward(PRUint32 type,
|
---|
245 | PRUint32 cID,
|
---|
246 | const nsID &target,
|
---|
247 | const char *data,
|
---|
248 | PRUint32 dataLen)
|
---|
249 | {
|
---|
250 | int len = sizeof(ipcmMessageHeader) + // IPCM header
|
---|
251 | sizeof(cID) + // cID
|
---|
252 | IPC_MSG_HEADER_SIZE + // innerMsgHeader
|
---|
253 | dataLen; // innerMsgData
|
---|
254 |
|
---|
255 | Init(IPCM_TARGET, NULL, len);
|
---|
256 |
|
---|
257 | ipcmMessageHeader ipcmHdr =
|
---|
258 | { type, IPCM_NewRequestIndex() };
|
---|
259 |
|
---|
260 | SetData(0, (char *) &ipcmHdr, sizeof(ipcmHdr));
|
---|
261 | SetData(sizeof(ipcmHdr), (char *) &cID, sizeof(cID));
|
---|
262 |
|
---|
263 | ipcMessageHeader hdr;
|
---|
264 | hdr.mLen = IPC_MSG_HEADER_SIZE + dataLen;
|
---|
265 | hdr.mVersion = IPC_MSG_VERSION;
|
---|
266 | hdr.mFlags = 0;
|
---|
267 | hdr.mTarget = target;
|
---|
268 |
|
---|
269 | SetData(sizeof(ipcmHdr) + sizeof(cID), (char *) &hdr, IPC_MSG_HEADER_SIZE);
|
---|
270 | if (data)
|
---|
271 | SetInnerData(0, data, dataLen);
|
---|
272 | }
|
---|
273 |
|
---|
274 | void
|
---|
275 | ipcmMessageForward::SetInnerData(PRUint32 offset, const char *data, PRUint32 dataLen)
|
---|
276 | {
|
---|
277 | SetData(sizeof(ipcmMessageHeader) + 4 + IPC_MSG_HEADER_SIZE + offset, data, dataLen);
|
---|
278 | }
|
---|
279 |
|
---|
280 | PRUint32
|
---|
281 | ipcmMessageForward::ClientID() const
|
---|
282 | {
|
---|
283 | return ((PRUint32 *) Data())[2];
|
---|
284 | }
|
---|
285 |
|
---|
286 | const nsID &
|
---|
287 | ipcmMessageForward::InnerTarget() const
|
---|
288 | {
|
---|
289 | ipcMessageHeader *hdr = (ipcMessageHeader *) (Data() + 12);
|
---|
290 | return hdr->mTarget;
|
---|
291 | }
|
---|
292 |
|
---|
293 | const char *
|
---|
294 | ipcmMessageForward::InnerData() const
|
---|
295 | {
|
---|
296 | return Data() + 12 + IPC_MSG_HEADER_SIZE;
|
---|
297 | }
|
---|
298 |
|
---|
299 | PRUint32
|
---|
300 | ipcmMessageForward::InnerDataLen() const
|
---|
301 | {
|
---|
302 | ipcMessageHeader *hdr = (ipcMessageHeader *) (Data() + 12);
|
---|
303 | return hdr->mLen - IPC_MSG_HEADER_SIZE;
|
---|
304 | }
|
---|