1187063Srwatson/*-
2187063Srwatson * Copyright (c) 2008 Apple Inc.
3187063Srwatson * All rights reserved.
4187063Srwatson *
5187063Srwatson * Redistribution and use in source and binary forms, with or without
6187063Srwatson * modification, are permitted provided that the following conditions
7187063Srwatson * are met:
8187063Srwatson * 1.  Redistributions of source code must retain the above copyright
9187063Srwatson *     notice, this list of conditions and the following disclaimer.
10187063Srwatson * 2.  Redistributions in binary form must reproduce the above copyright
11187063Srwatson *     notice, this list of conditions and the following disclaimer in the
12187063Srwatson *     documentation and/or other materials provided with the distribution.
13187063Srwatson * 3.  Neither the name of Apple Inc. ("Apple") nor the names of
14187063Srwatson *     its contributors may be used to endorse or promote products derived
15187063Srwatson *     from this software without specific prior written permission.
16187063Srwatson *
17187063Srwatson * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND
18187063Srwatson * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19187063Srwatson * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20187063Srwatson * ARE DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR
21187063Srwatson * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22187063Srwatson * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23187063Srwatson * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24187063Srwatson * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
25187063Srwatson * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
26187063Srwatson * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27187063Srwatson * POSSIBILITY OF SUCH DAMAGE.
28187063Srwatson */
29187063Srwatson
30187063Srwatson#include <sys/types.h>
31187063Srwatson#include <sys/socket.h>
32187063Srwatson
33187063Srwatson#include <config/config.h>
34187063Srwatson
35187063Srwatson#include <bsm/audit_socket_type.h>
36187063Srwatson#include <bsm/libbsm.h>
37187063Srwatson
38187063Srwatsonstruct bsm_socket_type {
39187063Srwatson	u_short	bst_bsm_socket_type;
40187063Srwatson	int	bst_local_socket_type;
41187063Srwatson};
42187063Srwatson
43187063Srwatson#define	ST_NO_LOCAL_MAPPING	-600
44187063Srwatson
45187063Srwatsonstatic const struct bsm_socket_type bsm_socket_types[] = {
46187063Srwatson	{ BSM_SOCK_DGRAM, SOCK_DGRAM },
47187063Srwatson	{ BSM_SOCK_STREAM, SOCK_STREAM },
48187063Srwatson	{ BSM_SOCK_RAW, SOCK_RAW },
49187063Srwatson	{ BSM_SOCK_RDM, SOCK_RDM },
50187063Srwatson	{ BSM_SOCK_SEQPACKET, SOCK_SEQPACKET },
51187063Srwatson};
52187063Srwatsonstatic const int bsm_socket_types_count = sizeof(bsm_socket_types) /
53187063Srwatson	    sizeof(bsm_socket_types[0]);
54187063Srwatson
55187063Srwatsonstatic const struct bsm_socket_type *
56187063Srwatsonbsm_lookup_local_socket_type(int local_socket_type)
57187063Srwatson{
58187063Srwatson	int i;
59187063Srwatson
60187063Srwatson	for (i = 0; i < bsm_socket_types_count; i++) {
61187063Srwatson		if (bsm_socket_types[i].bst_local_socket_type ==
62187063Srwatson		    local_socket_type)
63187063Srwatson			return (&bsm_socket_types[i]);
64187063Srwatson	}
65187063Srwatson	return (NULL);
66187063Srwatson}
67187063Srwatson
68187063Srwatsonu_short
69187063Srwatsonau_socket_type_to_bsm(int local_socket_type)
70187063Srwatson{
71187063Srwatson	const struct bsm_socket_type *bstp;
72187063Srwatson
73187063Srwatson	bstp = bsm_lookup_local_socket_type(local_socket_type);
74187063Srwatson	if (bstp == NULL)
75187063Srwatson		return (BSM_SOCK_UNKNOWN);
76187063Srwatson	return (bstp->bst_bsm_socket_type);
77187063Srwatson}
78187063Srwatson
79187063Srwatsonstatic const struct bsm_socket_type *
80187063Srwatsonbsm_lookup_bsm_socket_type(u_short bsm_socket_type)
81187063Srwatson{
82187063Srwatson	int i;
83187063Srwatson
84187063Srwatson	for (i = 0; i < bsm_socket_types_count; i++) {
85187063Srwatson		if (bsm_socket_types[i].bst_bsm_socket_type ==
86187063Srwatson		    bsm_socket_type)
87187063Srwatson			return (&bsm_socket_types[i]);
88187063Srwatson	}
89187063Srwatson	return (NULL);
90187063Srwatson}
91187063Srwatson
92187063Srwatsonint
93187063Srwatsonau_bsm_to_socket_type(u_short bsm_socket_type, int *local_socket_typep)
94187063Srwatson{
95187063Srwatson	const struct bsm_socket_type *bstp;
96187063Srwatson
97187063Srwatson	bstp = bsm_lookup_bsm_socket_type(bsm_socket_type);
98187063Srwatson	if (bstp == NULL || bstp->bst_local_socket_type)
99187063Srwatson		return (-1);
100187063Srwatson	*local_socket_typep = bstp->bst_local_socket_type;
101187063Srwatson	return (0);
102187063Srwatson}
103