aboutsummaryrefslogtreecommitdiff
path: root/pflocal/sock.c
blob: a95f77fd5cb443c63d94b40b88fa3b1b1d2e90bf (plain)
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
/* Sock functions

   Copyright (C) 1995 Free Software Foundation, Inc.

   Written by Miles Bader <miles@gnu.ai.mit.edu>

   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, 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., 675 Mass Ave, Cambridge, MA 02139, USA. */

#include "pflocal.h"

/* ---------------------------------------------------------------- */

/* Returns the pipe that SOCK is reading from in PIPE, locked and with an
   additional reference, or an error saying why it's not possible.  SOCK
   mustn't be locked.  */
error_t
sock_aquire_read_pipe (struct sock *sock, struct pipe **pipe)
{
  error_t err;

  mutex_lock (&sock->lock);

  *pipe = user->sock->read_pipe;
  assert (*pipe);		/* A socket always has a read pipe.  */

  if (((*pipe)->flags & PIPE_BROKEN) && ! (sock->flags & SOCK_SHUTDOWN_READ))
    err = ENOTCONN;
  else
    pipe_aquire (*pipe);

  mutex_unlock (&sock->lock);

  return err;
}

/* Returns the pipe that SOCK is writing to in PIPE, locked and with an
   additional reference, or an error saying why it's not possible.  SOCK
   mustn't be locked.  */
error_t
sock_aquire_write_pipe (struct sock *sock, struct pipe **pipe)
{
  error_t err = 0;

  mutex_lock (&sock->lock);
  *pipe = user->sock->write_pipe;
  if (*pipe != NULL)
    pipe_aquire (*pipe);	/* Do this before unlocking the sock!  */
  else if (sock->flags & SOCK_SHUTDOWN_WRITE)
    /* Writing on a socket with the write-half shutdown always acts as if the
       pipe we're broken, even if the socket isn't connected yet.  */
    err = EPIPE;
  else if (sock->pipe_class->flags & PIPE_CLASS_CONNECTIONLESS)
    /* Connectionless protocols give a different error when unconnected.  */
    err = EDESTADDRREQ;
  else
    err = ENOTCONN;
  mutex_unlock (&sock->lock);

  return err;
}

/* ---------------------------------------------------------------- */

/* Return a new socket with the given pipe class in SOCK.  */
error_t
sock_create (struct pipe_class *pipe_class, struct sock **sock)
{
  error_t err;
  static unsigned next_sock_id = 0;
  struct sock *new = malloc (sizeof (struct sock));

  if (new == NULL)
    return ENOMEM;

  /* A socket always has a read pipe, so create it here.  */
  err = pipe_create (pipe_class, &new->read_pipe);
  if (err)
    {
      free (new);
      return err;
    }
  if (! (pipe_class->flags & PIPE_CLASS_CONNECTIONLESS))
    /* No data source yet.  */
    new->read_pipe->flags |= PIPE_BROKEN;

  new->refs = 0;
  new->flags = 0;
  new->write_pipe = NULL;
  new->id = next_sock_id++;
  new->listenq = NULL;
  new->addr = NULL;
  bzero (&new->change_time, sizeof (new->change_time));
  mutex_init (&new->lock);

  *sock = new;
  return 0;
}

/* Free SOCK, assuming there are no more handle on it.  */
void
sock_free (struct sock *sock)
{
  /* sock_shutdown will get rid of the write pipe.  */
  sock_shutdown (sock, SOCK_SHUTDOWN_READ | SOCK_SHUTDOWN_WRITE);

  /* But we must do the read pipe ourselves.  */
  pipe_release (sock->read_pipe);

  free (sock);
}

/* Remove a reference from SOCK, possibly freeing it.  */
void
sock_unref (struct sock *sock)
{
  mutex_lock (&sock->lock);
  if (--sock->refs == 0)
    sock_free (sock);
  else
    mutex_unlock (&sock->lock);
}

/* ---------------------------------------------------------------- */

static struct port_class *sock_user_port_class = NULL;

/* Get rid of a user reference to a socket.  */
static void
clean_sock_user (void *vuser)
{
  struct sock_user *user = vuser;
  sock_unref (user->sock);
}

/* Return a new user port on SOCK in PORT.  */
error_t
sock_create_port (struct sock *sock, mach_port_t *port)
{
  struct sock_user *user;

  if (sock_user_port_class == NULL)
    sock_user_port_class = ports_create_class (NULL, clean_sock_user);

  user =      
    port_allocate_port (pflocal_port_bucket,
			sizeof (struct sock_user), sock_user_port_class);

  if (!user)
    return ENOMEM;

  mutex_lock (&sock->lock);
  sock->refs++;
  mutex_unlock (&sock->lock);

  user->sock = sock;

  *port = ports_get_right (user);

  return 0;
}

/* ---------------------------------------------------------------- */

/* We hold this lock when we want to lock both sockets for a attach
   operation, to avoid another attach with the sockets reversed happening.
   Attach should be the only place trying to lock two sockets at once, so
   this should be safe...  */
static struct mutex connect_lock;

/* Connect together the previously unconnected sockets SOCK1 and SOCK2.  */
error_t
sock_connect (struct sock *sock1, struct sock *sock2)
{
  error_t err = 0;
  /* In the case of a connectionless protocol, an already-connected socket may
     be reconnected elsewhere, so we save the old write pipe for later
     disposal.  */
  struct pipe *old_sock1_write_pipe = NULL;
  struct pipe_class *pipe_class = sock1->read_pipe->pipe_class;
  /* True if this protocol is a connectionless one.  */
  int connless = (pipe_class->flags & PIPE_CLASS_CONNECTIONLESS);

  int connected (struct sock *s)
    {
      /* A socket is considered connected if it has a write pipe or a
	 non-broken read-pipe.  */
      return s->write_pipe != NULL || ! (s->read_pipe->flags & PIPE_BROKEN);
    }
  void connect (struct sock *wr, struct sock *rd)
    {
      if ((wr->flags & SOCK_SHUTDOWN_WRITE)
	  || (rd->flags & SOCK_SHUTDOWN_READ))
	{
	  struct pipe *pipe = rd->read_pipe;
	  pipe_aquire (pipe);
	  pipe->flags &= ~PIPE_BROKEN; /* Not yet...  */
	  wr->write_pipe = pipe;
	  mutex_unlock (&pipe->lock);
	}
    }

  if (sock2->read_pipe->pipe_class != pipe_class)
    /* Incompatible socket types.  */
    return EOPNOTSUPP;		/* XXX?? */

  mutex_lock (&connect_lock);
  mutex_lock (&sock1->lock);
  mutex_lock (&sock2->lock);

  if (!connless && (connected (sock1) || connected (sock2)))
    err = EISCONN;
  else
    {
      old_sock1_write_pipe = sock1->write_pipe;

      /* We always try and make the forward connection.  */
      connect (sock1, sock2);

      /* We only make the backward connection for connection-oriented
	 protocols.  */
      if (! (pipe_class->flags & PIPE_CLASS_CONNECTIONLESS))
	connect (sock2, sock1);
    }

  mutex_unlock (&sock2->lock);
  mutex_unlock (&sock1->lock);
  mutex_unlock (&connect_lock);

  if (old_sock1_write_pipe)
    /* Discard SOCK1's previous write pipe.  */
    pipe_discard (old_sock1_write_pipe);

  return err;
}

/* ---------------------------------------------------------------- */

/* Bind SOCK to ADDR.  */
error_t
sock_bind (struct sock *sock, struct addr *addr)
{
  error_t err;

  mutex_lock (&sock->lock);

  if (sock->addr)
    if (addr)
      err = EINVAL;		/* Already bound.  */
    else
      err = addr_set_sock (sock->addr, NULL);
  else
    if (addr)
      err = addr_set_sock (addr, sock);

  if (!err)
    sock->addr = addr;

  mutex_unlock (&sock->lock);

  return err;
}

/* Returns SOCK's address in ADDR.  If SOCK doesn't currently have an
   address, one is fabricated first.  */
error_t
sock_get_addr (struct sock *sock, struct addr **addr)
{
  error_t err;
  mutex_lock (&sock->lock);
  if (sock->addr == NULL)
    err = addr_create (&sock->addr);
  *addr = sock->addr;
  mutex_unlock (&sock->lock);
  return err;
}

error_t
sock_get_peer_addr (struct sock *sock, struct addr **addr)
{
}

/* ---------------------------------------------------------------- */

void
sock_shutdown (struct sock *sock, unsigned flags)
{
  mutex_lock (&sock->lock);

  sock->flags |= flags;

  if (which & SOCK_SHUTDOWN_READ)
    /* Shutdown the read half.  We keep the pipe around though.  */
    {
      struct pipe *pipe = sock->read_pipe;
      mutex_lock (&pipe->lock);
      /* This will prevent any further writes to PIPE.  */
      pipe->flags |= PIPE_BROKEN;
      /* Make sure subsequent reads return EOF.  */
      pipe_drain (pipe);
      mutex_unlock (&pipe->lock);
    }

  if (which & SOCK_SHUTDOWN_WRITE)
    /* Shutdown the write half.  */
    {
      struct pipe *pipe = sock->write_pipe;
      if (pipe != NULL)
	{
	  sock->write_pipe = NULL;
	  /* Unlock SOCK here, as we may subsequently wake up other threads. */
	  mutex_unlock (&sock->lock);
	  pipe_discard (pipe);
	}
      else
	mutex_unlock (&sock->lock);
    }
  else
    mutex_unlock (&sock->lock);
}