net.c
2.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/* GNU mailutils - a suite of utilities for electronic mail
Copyright (C) 1999, 2000 Free Software Foundation, Inc.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU Library General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
#include <stdlib.h>
#include <errno.h>
#include <net0.h>
static struct _netregistrar _netreg[1] = { "tcp", _tcp_create, _tcp_set_option, _tcp_destroy };
int net_api_create(net_t *net, net_t parent, const char *type)
{
net_t n;
int i, napis, ret = 0;
if ( net == NULL || type == NULL )
return EINVAL;
*net = NULL;
if ( ( n = calloc(1, sizeof(*n)) ) == NULL )
return ENOMEM;
napis = sizeof(_netreg) / sizeof(_netreg[0]);
for( i = 0; i < napis; i++ ) {
if ( strcasecmp(_netreg[i].type, type) == 0 )
break;
}
if ( i == napis )
return ENOTSUP;
if ( ret = ( _netreg[i].create(&(n->data), &(n->api)) ) != 0 )
free(n);
n->parent = parent;
n->net_reg = &_netreg[i];
*net = n;
return 0;
}
int net_api_set_option(net_t net, const char *name, const char *value)
{
if ( net && name && value )
return net->net_reg->set_option(net->data, name, value);
return EINVAL;
}
int net_api_destroy(net_t *net)
{
net_t n;
if ( net == NULL || *net == NULL )
return EINVAL;
n = *net;
n->net_reg->destroy(&n->data);
free(n);
*net = NULL;
return 0;
}
int net_new(net_t net, netinstance_t *inst)
{
netinstance_t netinst;
int ret = 0;
if ( net == NULL || inst == NULL )
return EINVAL;
*inst = NULL;
if ( ( netinst = calloc(1, sizeof(*netinst)) ) == NULL )
return ENOMEM;
netinst->api = net->api;
if ( ( ret = net->api->new(net->data, net->parent, &(netinst->data)) ) != 0 ) {
free(netinst);
return ret;
}
*inst = netinst;
return 0;
}
int net_connect(netinstance_t inst, const char *host, int port)
{
if ( inst == NULL || host == NULL )
return EINVAL;
return inst->api->connect(inst->data, host, port);
}
int net_get_stream(netinstance_t inst, stream_t *iostr)
{
if ( inst == NULL || iostr == NULL )
return EINVAL;
return inst->api->get_stream(inst->data, iostr);
}
int net_close(netinstance_t inst)
{
if ( inst == NULL )
return EINVAL;
return inst->api->close(inst->data);
}
int net_free(netinstance_t *pinst)
{
int ret;
netinstance_t inst;
if ( pinst == NULL || *pinst == NULL )
return EINVAL;
inst = *pinst;
ret = inst->api->free(&(inst->data));
free(inst);
*pinst = NULL;
return ret;
}