libchannel
an abstraction around communications channels
Main Page
Modules
Data Structures
Files
File List
Globals
All
Data Structures
Files
Functions
Groups
libchannel.c
Go to the documentation of this file.
1
2
/*-
3
* Copyright (c) 2012 Jonathan Anderson
4
* All rights reserved.
5
*
6
* This software was developed by SRI International and the University of
7
* Cambridge Computer Laboratory under DARPA/AFRL contract (FA8750-10-C-0237)
8
* ("CTSRD"), as part of the DARPA CRASH research programme.
9
*
10
* Redistribution and use in source and binary forms, with or without
11
* modification, are permitted provided that the following conditions
12
* are met:
13
* 1. Redistributions of source code must retain the above copyright
14
* notice, this list of conditions and the following disclaimer.
15
* 2. Redistributions in binary form must reproduce the above copyright
16
* notice, this list of conditions and the following disclaimer in the
17
* documentation and/or other materials provided with the distribution.
18
*
19
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29
* SUCH DAMAGE.
30
*/
31
32
#include "
libchannel.h
"
33
#include "
libchannel-internal.h
"
34
35
#include <assert.h>
36
#include <stdlib.h>
37
#include <string.h>
38
39
40
void
41
channel_init(
channel
*c,
int
magic,
42
int
(*size)(
channel
*),
43
int
(*copy)(
channel
*,
void
*dest),
44
int
(*send)(
channel
*,
struct
message
*)
45
)
46
{
47
assert(c != NULL);
48
49
c->magic = magic;
50
51
c->size = size;
52
c->copy = copy;
53
c->send = send;
54
}
55
56
int
57
channel_size
(
channel
*c)
58
{
59
assert(
channel_isvalid
(c));
60
61
int
size = c->size(c);
62
assert(size > 0);
63
64
return
size;
65
}
66
67
int
68
channel_copy(
void
*dest,
channel
*src)
69
{
70
assert(
channel_isvalid
(src));
71
72
int
size =
channel_size
(src);
73
memcpy(dest, src, size);
74
75
return
size;
76
}
77
78
channel
*
79
channel_wrap_socket
(
int
sock)
80
{
81
return
uds_wrap(uds_create(sock));
82
}
83
84
bool
85
channel_isvalid
(
channel
*c)
86
{
87
if
(c == NULL)
return
false
;
88
if
(c->size == NULL)
return
false
;
89
if
(c->copy == NULL)
return
false
;
90
if
(c->send == NULL)
return
false
;
91
92
// As we implement other kinds of channels, add more magic tests here.
93
if
(c->magic == UDS_MAGIC)
return
true
;
94
95
return
false
;
96
}
97
98
void
99
channel_destroy
(
channel
*c)
100
{
101
assert(
channel_isvalid
(c));
102
free(c);
103
}
104
105
int
106
channel_send
(
struct
channel
*c,
struct
message
*m)
107
{
108
if
(!
channel_isvalid
(c))
return
-1;
109
return
c->send(c, m);
110
}
111
Generated on Sat Sep 1 2012 01:19:09 for libchannel by
1.8.1.2