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 * $P4: //depot/projects/trustedbsd/openbsm/libbsm/bsm_socket_type.c#1 $
30187063Srwatson */
31187063Srwatson
32187063Srwatson#include <sys/types.h>
33187063Srwatson#include <sys/socket.h>
34187063Srwatson
35187063Srwatson#include <config/config.h>
36187063Srwatson
37187063Srwatson#include <bsm/audit_socket_type.h>
38187063Srwatson#include <bsm/libbsm.h>
39187063Srwatson
40187063Srwatsonstruct bsm_socket_type {
41187063Srwatson	u_short	bst_bsm_socket_type;
42187063Srwatson	int	bst_local_socket_type;
43187063Srwatson};
44187063Srwatson
45187063Srwatson#define	ST_NO_LOCAL_MAPPING	-600
46187063Srwatson
47187063Srwatsonstatic const struct bsm_socket_type bsm_socket_types[] = {
48187063Srwatson	{ BSM_SOCK_DGRAM, SOCK_DGRAM },
49187063Srwatson	{ BSM_SOCK_STREAM, SOCK_STREAM },
50187063Srwatson	{ BSM_SOCK_RAW, SOCK_RAW },
51187063Srwatson	{ BSM_SOCK_RDM, SOCK_RDM },
52187063Srwatson	{ BSM_SOCK_SEQPACKET, SOCK_SEQPACKET },
53187063Srwatson};
54187063Srwatsonstatic const int bsm_socket_types_count = sizeof(bsm_socket_types) /
55187063Srwatson	    sizeof(bsm_socket_types[0]);
56187063Srwatson
57187063Srwatsonstatic const struct bsm_socket_type *
58187063Srwatsonbsm_lookup_local_socket_type(int local_socket_type)
59187063Srwatson{
60187063Srwatson	int i;
61187063Srwatson
62187063Srwatson	for (i = 0; i < bsm_socket_types_count; i++) {
63187063Srwatson		if (bsm_socket_types[i].bst_local_socket_type ==
64187063Srwatson		    local_socket_type)
65187063Srwatson			return (&bsm_socket_types[i]);
66187063Srwatson	}
67187063Srwatson	return (NULL);
68187063Srwatson}
69187063Srwatson
70187063Srwatsonu_short
71187063Srwatsonau_socket_type_to_bsm(int local_socket_type)
72187063Srwatson{
73187063Srwatson	const struct bsm_socket_type *bstp;
74187063Srwatson
75187063Srwatson	bstp = bsm_lookup_local_socket_type(local_socket_type);
76187063Srwatson	if (bstp == NULL)
77187063Srwatson		return (BSM_SOCK_UNKNOWN);
78187063Srwatson	return (bstp->bst_bsm_socket_type);
79187063Srwatson}
80187063Srwatson
81187063Srwatsonstatic const struct bsm_socket_type *
82187063Srwatsonbsm_lookup_bsm_socket_type(u_short bsm_socket_type)
83187063Srwatson{
84187063Srwatson	int i;
85187063Srwatson
86187063Srwatson	for (i = 0; i < bsm_socket_types_count; i++) {
87187063Srwatson		if (bsm_socket_types[i].bst_bsm_socket_type ==
88187063Srwatson		    bsm_socket_type)
89187063Srwatson			return (&bsm_socket_types[i]);
90187063Srwatson	}
91187063Srwatson	return (NULL);
92187063Srwatson}
93187063Srwatson
94187063Srwatsonint
95187063Srwatsonau_bsm_to_socket_type(u_short bsm_socket_type, int *local_socket_typep)
96187063Srwatson{
97187063Srwatson	const struct bsm_socket_type *bstp;
98187063Srwatson
99187063Srwatson	bstp = bsm_lookup_bsm_socket_type(bsm_socket_type);
100187063Srwatson	if (bstp == NULL || bstp->bst_local_socket_type)
101187063Srwatson		return (-1);
102187063Srwatson	*local_socket_typep = bstp->bst_local_socket_type;
103187063Srwatson	return (0);
104187063Srwatson}
105