Sync usage with man page.
[netbsd-mini2440.git] / external / apache2 / mDNSResponder / dist / mDNSShared / dnssd_ipc.c
blob5c1ec4d025473c748e479d9cf58018e9de7464fb
1 /* -*- Mode: C; tab-width: 4 -*-
3 * Copyright (c) 2003-2004, Apple Computer, Inc. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of its
14 * contributors may be used to endorse or promote products derived from this
15 * software without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
18 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
21 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 Change History (most recent first):
30 Log: dnssd_ipc.c,v $
31 Revision 1.23 2009/04/01 21:10:34 herscher
32 <rdar://problem/5925472> Current Bonjour code does not compile on Windows
34 Revision 1.22 2009/02/12 20:28:31 cheshire
35 Added some missing "const" declarations
37 Revision 1.21 2008/10/23 23:21:31 cheshire
38 Moved definition of dnssd_strerror() to be with the definition of dnssd_errno, in dnssd_ipc.h
40 Revision 1.20 2007/07/23 22:12:53 cheshire
41 <rdar://problem/5352299> Make mDNSResponder more defensive against malicious local clients
43 Revision 1.19 2007/05/16 01:06:52 cheshire
44 <rdar://problem/4471320> Improve reliability of kDNSServiceFlagsMoreComing flag on multiprocessor machines
46 Revision 1.18 2007/03/21 19:01:57 cheshire
47 <rdar://problem/5078494> IPC code not 64-bit-savvy: assumes long=32bits, and short=16bits
49 Revision 1.17 2006/10/27 00:38:22 cheshire
50 Strip accidental trailing whitespace from lines
52 Revision 1.16 2006/08/14 23:05:53 cheshire
53 Added "tab-width" emacs header line
55 Revision 1.15 2005/01/27 22:57:56 cheshire
56 Fix compile errors on gcc4
58 Revision 1.14 2004/10/06 02:22:20 cheshire
59 Changed MacRoman copyright symbol (should have been UTF-8 in any case :-) to ASCII-compatible "(c)"
61 Revision 1.13 2004/10/01 22:15:55 rpantos
62 rdar://problem/3824265: Replace APSL in client lib with BSD license.
64 Revision 1.12 2004/09/16 23:14:24 cheshire
65 Changes for Windows compatibility
67 Revision 1.11 2004/06/18 04:56:09 rpantos
68 casting goodness
70 Revision 1.10 2004/06/12 01:08:14 cheshire
71 Changes for Windows compatibility
73 Revision 1.9 2004/05/18 23:51:27 cheshire
74 Tidy up all checkin comments to use consistent "<rdar://problem/xxxxxxx>" format for bug numbers
76 Revision 1.8 2003/11/05 22:44:57 ksekar
77 <rdar://problem/3335230>: No bounds checking when reading data from client
78 Reviewed by: Stuart Cheshire
80 Revision 1.7 2003/08/12 19:56:25 cheshire
81 Update to APSL 2.0
85 #include "dnssd_ipc.h"
87 #if defined(_WIN32)
89 char *win32_strerror(int inErrorCode)
91 static char buffer[1024];
92 DWORD n;
93 memset(buffer, 0, sizeof(buffer));
94 n = FormatMessageA(
95 FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
96 NULL,
97 (DWORD) inErrorCode,
98 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
99 buffer,
100 sizeof(buffer),
101 NULL);
102 if (n > 0)
104 // Remove any trailing CR's or LF's since some messages have them.
105 while ((n > 0) && isspace(((unsigned char *) buffer)[n - 1]))
106 buffer[--n] = '\0';
108 return buffer;
111 #endif
113 void put_uint32(const uint32_t l, char **ptr)
115 (*ptr)[0] = (char)((l >> 24) & 0xFF);
116 (*ptr)[1] = (char)((l >> 16) & 0xFF);
117 (*ptr)[2] = (char)((l >> 8) & 0xFF);
118 (*ptr)[3] = (char)((l ) & 0xFF);
119 *ptr += sizeof(uint32_t);
122 uint32_t get_uint32(const char **ptr, const char *end)
124 if (!*ptr || *ptr + sizeof(uint32_t) > end)
126 *ptr = NULL;
127 return(0);
129 else
131 uint8_t *p = (uint8_t*) *ptr;
132 *ptr += sizeof(uint32_t);
133 return((uint32_t) ((uint32_t)p[0] << 24 | (uint32_t)p[1] << 16 | (uint32_t)p[2] << 8 | p[3]));
137 void put_uint16(uint16_t s, char **ptr)
139 (*ptr)[0] = (char)((s >> 8) & 0xFF);
140 (*ptr)[1] = (char)((s ) & 0xFF);
141 *ptr += sizeof(uint16_t);
144 uint16_t get_uint16(const char **ptr, const char *end)
146 if (!*ptr || *ptr + sizeof(uint16_t) > end)
148 *ptr = NULL;
149 return(0);
151 else
153 uint8_t *p = (uint8_t*) *ptr;
154 *ptr += sizeof(uint16_t);
155 return((uint16_t) ((uint16_t)p[0] << 8 | p[1]));
159 int put_string(const char *str, char **ptr)
161 if (!str) str = "";
162 strcpy(*ptr, str);
163 *ptr += strlen(str) + 1;
164 return 0;
167 int get_string(const char **ptr, const char *const end, char *buffer, int buflen)
169 if (!*ptr)
171 *buffer = 0;
172 return(-1);
174 else
176 char *lim = buffer + buflen; // Calculate limit
177 while (*ptr < end && buffer < lim)
179 char c = *buffer++ = *(*ptr)++;
180 if (c == 0) return(0); // Success
182 if (buffer == lim) buffer--;
183 *buffer = 0; // Failed, so terminate string,
184 *ptr = NULL; // clear pointer,
185 return(-1); // and return failure indication
189 void put_rdata(const int rdlen, const unsigned char *rdata, char **ptr)
191 memcpy(*ptr, rdata, rdlen);
192 *ptr += rdlen;
195 const char *get_rdata(const char **ptr, const char *end, int rdlen)
197 if (!*ptr || *ptr + rdlen > end)
199 *ptr = NULL;
200 return(0);
202 else
204 const char *rd = *ptr;
205 *ptr += rdlen;
206 return rd;
210 void ConvertHeaderBytes(ipc_msg_hdr *hdr)
212 hdr->version = htonl(hdr->version);
213 hdr->datalen = htonl(hdr->datalen);
214 hdr->ipc_flags = htonl(hdr->ipc_flags);
215 hdr->op = htonl(hdr->op );
216 hdr->reg_index = htonl(hdr->reg_index);