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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
|
/* netio - creates socket ports via the filesystem
Copyright (C) 2001, 02 Moritz Schulte <moritz@duesseldorf.ccc.de>
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2 of the
License, 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 General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
USA */
#include <hurd/netfs.h>
#include <errno.h>
#include <stdlib.h>
#include <sys/socket.h>
#include "netio.h"
#include "lib.h"
/* The default stat information for netio nodes. */
struct stat stat_default;
/* Create a new node in the directory *DIR (if DIR is nonzero) and
store it in *NODE. If CONNECT is true (in which case *DIR has to
be a valid directory node), attach the new node to the list of
directory entries of *DIR. Return 0 on success or an error code. */
error_t
node_make_new (struct node *dir, int connect, struct node **node)
{
struct netnode *netnode;
error_t err;
err = my_malloc (sizeof (struct netnode), (void **) &netnode);
if (err)
return err;
*node = netfs_make_node (netnode);
if (! *node)
{
free (netnode);
return ENOMEM;
}
(*node)->nn->flags = 0;
(*node)->nn->host = 0;
(*node)->nn->port = 0;
(*node)->nn->protocol = 0;
(*node)->nn->sock = MACH_PORT_NULL;
(*node)->nn->addr = MACH_PORT_NULL;
(*node)->nn->connected = 0;
(*node)->nn->entries = 0;
(*node)->nn->dir = dir;
(*node)->nn_stat = stat_default;
if (dir)
netfs_nref (dir);
if (connect)
{
(*node)->next = dir->nn->entries;
(*node)->prevp = &dir->nn->entries;
if ((*node)->next)
(*node)->next->prevp = &(*node)->next;
dir->nn->entries = (*node);
}
else
{
(*node)->next = 0;
(*node)->prevp = 0;
}
return err;
}
/* Destroy the node and release a reference to the parent directory. */
error_t
node_destroy (struct node *np)
{
if (np->nn->flags & PORT_NODE)
{
if (np->nn->connected)
socket_shutdown (np->nn->sock, 2);
}
else if (np->nn->flags & HOST_NODE)
free (np->nn->host);
else if (np->nn->flags & PROTOCOL_NODE)
{
free (np->nn->protocol);
*np->prevp = np->next;
if (np->next)
np->next->prevp = np->prevp;
}
assert (np->nn->dir);
spin_unlock (&netfs_node_refcnt_lock); /* FIXME? Is this locking
okay? */
netfs_nput (np->nn->dir);
spin_lock (&netfs_node_refcnt_lock);
free (np->nn);
free (np);
return 0;
}
/* Create a new protocol node for *PROTOCOL in the directory *DIR.
Return 0 on success or an error code. */
error_t
node_make_protocol_node (struct node *dir, struct protocol *protocol)
{
struct node *node;
error_t err;
err = node_make_new (dir, 1, &node);
if (err)
return err;
node->nn->flags |= PROTOCOL_NODE;
node->nn->protocol = protocol;
node->nn_stat.st_mode = S_IFDIR
| S_IRUSR | S_IXUSR
| S_IRGRP | S_IXGRP
| S_IROTH | S_IXOTH;
node->nn_stat.st_ino = protocol->id;
node->nn_stat.st_nlink = 2;
fshelp_touch (&node->nn_stat, TOUCH_ATIME | TOUCH_CTIME | TOUCH_MTIME,
netio_maptime);
return err;
}
/* Create a new host node for HOST in the directory *DIR and store it
in *NODE. Return 0 on success or an error code. */
error_t
node_make_host_node (struct iouser *user, struct node *dir, char *host,
struct node **node)
{
struct node *np;
error_t err;
err = node_make_new (dir, 0, &np);
if (err)
return err;
np->nn->host = strdup (host);
if (! np->nn->host)
{
netfs_nrele (np);
err = ENOMEM;
goto out;
}
np->nn->flags |= HOST_NODE;
np->nn->protocol = dir->nn->protocol;
np->nn_stat.st_mode = S_IFDIR | S_IRUSR | S_IXUSR ;
fshelp_touch (&np->nn_stat, TOUCH_ATIME | TOUCH_CTIME | TOUCH_MTIME,
netio_maptime);
*node = np;
out:
return err;
}
/* Create a new port node for PORT in the directory *DIR and store it
in *NODE. Return 0 on success or an error code. */
error_t
node_make_port_node (struct iouser *user, struct node *dir,
char *port, struct node **node)
{
struct node *np;
error_t err;
err = node_make_new (dir, 0, &np);
if (err)
return err;
np->nn->flags |= PORT_NODE;
np->nn->host = dir->nn->host;
np->nn->protocol = dir->nn->protocol;
np->nn->port = (uint16_t) strtol (port, 0, 10);
np->nn->flags |= HOST_NODE;
np->nn->protocol = dir->nn->protocol;
np->nn_stat.st_mode = S_IFSOCK | S_IRUSR | S_IWUSR ;
fshelp_touch (&np->nn_stat, TOUCH_ATIME | TOUCH_CTIME | TOUCH_MTIME,
netio_maptime);
*node = np;
return err;
}
/* Create the root node. Return 0 on success or an error code. */
error_t
node_make_root_node (struct node **node)
{
struct node *np;
error_t err;
err = node_make_new (0, 0, &np);
if (err)
goto out;
np->nn->flags |= ROOT_NODE;
*node = np;
out:
return err;
}
/* Connect the socket of the node *NP. Return 0 on success or an
error code. */
error_t
node_connect_socket (struct node *np)
{
error_t err;
err = (*np->nn->protocol->socket_open) (np);
if (! err)
np->nn->connected = 1;
return err;
}
|