147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt/* -*- Mode: C; tab-width: 4 -*-
247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt *
347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt * Copyright (c) 2003-2004, Apple Computer, Inc. All rights reserved.
447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt *
547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt * Redistribution and use in source and binary forms, with or without
647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt * modification, are permitted provided that the following conditions are met:
747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt *
847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt * 1.  Redistributions of source code must retain the above copyright notice,
947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt *     this list of conditions and the following disclaimer.
1047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt * 2.  Redistributions in binary form must reproduce the above copyright notice,
1147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt *     this list of conditions and the following disclaimer in the documentation
1247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt *     and/or other materials provided with the distribution.
1347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt * 3.  Neither the name of Apple Computer, Inc. ("Apple") nor the names of its
1447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt *     contributors may be used to endorse or promote products derived from this
1547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt *     software without specific prior written permission.
1647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt *
1747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
1847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
1947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
2047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
2147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
2247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
2347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
2447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
2647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt */
2847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
2947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#include "dnssd_ipc.h"
3047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
3147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#if defined(_WIN32)
3247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
3347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltchar *win32_strerror(int inErrorCode)
3447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	{
3547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	static char buffer[1024];
3647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	DWORD       n;
3747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	memset(buffer, 0, sizeof(buffer));
3847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	n = FormatMessageA(
3947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt			FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
4047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt			NULL,
4147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt			(DWORD) inErrorCode,
4247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt			MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
4347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt			buffer,
4447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt			sizeof(buffer),
4547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt			NULL);
4647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	if (n > 0)
4747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt		{
4847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt		// Remove any trailing CR's or LF's since some messages have them.
4947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt		while ((n > 0) && isspace(((unsigned char *) buffer)[n - 1]))
5047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt			buffer[--n] = '\0';
5147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt		}
5247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	return buffer;
5347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	}
5447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
5547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#endif
5647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
5747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltvoid put_uint32(const uint32_t l, char **ptr)
5847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	{
5947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	(*ptr)[0] = (char)((l >> 24) &  0xFF);
6047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	(*ptr)[1] = (char)((l >> 16) &  0xFF);
6147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	(*ptr)[2] = (char)((l >>  8) &  0xFF);
6247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	(*ptr)[3] = (char)((l      ) &  0xFF);
6347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	*ptr += sizeof(uint32_t);
6447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	}
6547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
6647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltuint32_t get_uint32(const char **ptr, const char *end)
6747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	{
6847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	if (!*ptr || *ptr + sizeof(uint32_t) > end)
6947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt		{
7047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt		*ptr = NULL;
7147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt		return(0);
7247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt		}
7347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	else
7447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt		{
7547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt		uint8_t *p = (uint8_t*) *ptr;
7647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt		*ptr += sizeof(uint32_t);
7747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt		return((uint32_t) ((uint32_t)p[0] << 24 | (uint32_t)p[1] << 16 | (uint32_t)p[2] << 8 | p[3]));
7847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt		}
7947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	}
8047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
8147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltvoid put_uint16(uint16_t s, char **ptr)
8247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	{
8347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	(*ptr)[0] = (char)((s >>  8) &  0xFF);
8447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	(*ptr)[1] = (char)((s      ) &  0xFF);
8547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	*ptr += sizeof(uint16_t);
8647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	}
8747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
8847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltuint16_t get_uint16(const char **ptr, const char *end)
8947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	{
9047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	if (!*ptr || *ptr + sizeof(uint16_t) > end)
9147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt		{
9247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt		*ptr = NULL;
9347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt		return(0);
9447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt		}
9547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	else
9647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt		{
9747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt		uint8_t *p = (uint8_t*) *ptr;
9847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt		*ptr += sizeof(uint16_t);
9947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt		return((uint16_t) ((uint16_t)p[0] << 8 | p[1]));
10047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt		}
10147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	}
10247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
10347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltint put_string(const char *str, char **ptr)
10447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	{
10547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	if (!str) str = "";
10647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	strcpy(*ptr, str);
10747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	*ptr += strlen(str) + 1;
10847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	return 0;
10947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	}
11047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
11147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltint get_string(const char **ptr, const char *const end, char *buffer, int buflen)
11247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	{
11347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	if (!*ptr)
11447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt		{
11547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt		*buffer = 0;
11647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt		return(-1);
11747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt		}
11847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	else
11947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt		{
12047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt		char *lim = buffer + buflen;	// Calculate limit
12147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt		while (*ptr < end && buffer < lim)
12247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt			{
12347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt			char c = *buffer++ = *(*ptr)++;
12447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt			if (c == 0) return(0);		// Success
12547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt			}
12647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt		if (buffer == lim) buffer--;
12747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt		*buffer = 0;					// Failed, so terminate string,
12847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt		*ptr = NULL;					// clear pointer,
12947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt		return(-1);						// and return failure indication
13047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt		}
13147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	}
13247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
13347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltvoid put_rdata(const int rdlen, const unsigned char *rdata, char **ptr)
13447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	{
13547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	memcpy(*ptr, rdata, rdlen);
13647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	*ptr += rdlen;
13747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	}
13847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
13947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltconst char *get_rdata(const char **ptr, const char *end, int rdlen)
14047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	{
14147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	if (!*ptr || *ptr + rdlen > end)
14247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt		{
14347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt		*ptr = NULL;
14447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt		return(0);
14547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt		}
14647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	else
14747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt		{
14847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt		const char *rd = *ptr;
14947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt		*ptr += rdlen;
15047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt		return rd;
15147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt		}
15247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	}
15347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
15447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltvoid ConvertHeaderBytes(ipc_msg_hdr *hdr)
15547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	{
15647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	hdr->version   = htonl(hdr->version);
15747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	hdr->datalen   = htonl(hdr->datalen);
15847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	hdr->ipc_flags = htonl(hdr->ipc_flags);
15947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	hdr->op        = htonl(hdr->op );
16047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	hdr->reg_index = htonl(hdr->reg_index);
16147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	}
162