summaryrefslogtreecommitdiff
path: root/pflocal/socket.c
blob: 06a766c18be7178c3631abe76d148bdd1e92eab5 (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
/* Socket-specific operations

   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 "sock.h"
#include "connq.h"

#include "socket_S.h"

/* Connect two sockets */
error_t
S_socket_connect2 (struct sock_user *user1, struct sock_user *user2)
{
  if (!user1 || !user2)
    return EOPNOTSUPP;
  return sock_connect (user1->sock, user2->sock);
}

/* Make sure we have a queue to listen on.  */
static error_t
ensure_connq (struct sock *sock)
{
  error_t err = 0;
  mutex_lock (&sock->lock);
  if (!sock->connq)
    err = connq_create (0, &sock->connq);
  mutex_unlock (&sock->lock);
  return err;
}

/* Return a new connection from a socket previously listened.  */
error_t
S_socket_accept (struct sock_user *user,
		 mach_port_t *port, mach_msg_type_name_t *port_type,
		 mach_port_t *peer_addr, mach_msg_type_name_t *peer_addr_type)
{
  error_t err;

  if (!user)
    return EOPNOTSUPP;

  err = ensure_connq (sock);
  if (!err)
    {
      struct connq_request *req;
      struct sock *peer_sock;

      err =
	connq_listen (sock->connq, sock->flags & SOCK_NONBLOCK,
		      &req, &peer_sock);
      if (!err)
	{
	  struct sock *conn_sock;

	  err = sock_clone (sock, &conn_sock);
	  if (!err)
	    {
	      err = sock_connect (conn_sock, peer_sock);
	      if (!err)
		{
		  err = sock_create_port (conn_sock, port, port_type);
		  if (!err)
		    {
		      *addr = ports_get_right (peer_sock->addr);
		      *addr_type = MACH_MSG_MAKE_SEND;
		    }
		  else
		    /* TEAR DOWN THE CONNECTION XXX */;
		}
	      if (err)
		sock_free (conn_sock);
	    }

	  /* Communicate any error (or success) to the connecting thread.  */
	  connq_request_complete (req, err);
	}
    }

  return err;
}

/* Prepare a socket of appropriate type for future accept operations.  */
error_t
S_socket_listen (struct sock_user *user, int queue_limit)
{
  error_t err;
  if (!user)
    return EOPNOTSUPP;
  err = ensure_connq (sock);
  if (!err)
    err = connq_set_length (sock->connq, queue_limit);
  return err;
}

error_t
S_socket_connect (struct sock_user *user, struct addr *addr)
{
  struct sock *peer;

  if (! user)
    return EOPNOTSUPP;
  if (! addr)
    return EADDRNOTAVAIL;

  err = addr_get_sock (addr, &peer);
  if (err)
    return err;

  return sock_connect (user->sock, peer);
}

error_t
S_socket_bind (struct sock_user *user, struct addr *addr)
{
  if (! user)
    return EOPNOTSUPP;
  if (! addr)
    return EADDRNOTAVAIL;

  return sock_bind (user->sock, addr);
}

error_t
S_socket_shutdown (struct sock_user *user, int what)
{
  if (! user)
    return EOPNOTSUPP;
  sock_shutdown (user->sock,
		   (what != 1 ? SOCK_SHUTDOWN_READ : 0)
		 | (what != 0 ? SOCK_SHUTDOWN_WRITE : 0));
  return 0;
}

error_t
S_socket_name (struct sock_user *user,
	       mach_port_t *addr_port, mach_msg_type_name_t *addr_port_type)
{
  if (!user)
    return EOPNOTSUPP;
  *addr_port_type = MACH_MSG_MAKE_SEND;
  return sock_get_addr_port (user->sock, &addr_port);
}

error_t
S_socket_peername (struct sock_user *user,
		   mach_port_t *addr_port,
		   mach_msg_type_name_t *addr_port_type)
{
  if (!user)
    return EOPNOTSUPP;
  *addr_port_type = MACH_MSG_MAKE_SEND;
  return sock_get_peer_addr_port (user->sock, &addr_port);
}

/* Stubs for currently unsupported rpcs.  */

error_t
S_socket_getopt (struct sock_user *user,
		 int level, int opt,
		 char **value, unsigned *value_len)
{
  return EOPNOTSUPP;
}

error_t
S_socket_setopt (struct sock_user *user,
		 int level, int opt,
		 char *value, unsigned value_len)
{
  return EOPNOTSUPP;
}

error_t
S_socket_send (struct sock_user *user,
	       mach_port_t addr, int flags,
	       char *data, unsigned data_len,
	       mach_port_t *ports, unsigned num_ports,
	       char *control, unsigned control_len,
	       int *amount)
{
  if (flags & MSG_OOB)
    /* BSD local sockets don't support OOB data.  */
    return EOPNOTSUPP;
  return EOPNOTSUPP;
}

error_t
S_socket_recv (struct sock_user *user,
	       mach_port_t *addr, int flags,
	       char **data, unsigned *data_len,
	       mach_port_t **ports, mach_msg_type_name_t *ports_type,
	       unsigned *num_ports,
	       char **control, unsigned *control_len,
	       int *out_flags, int amount)
{
  if (flags & MSG_OOB)
    /* BSD local sockets don't support OOB data.  */
    return EOPNOTSUPP;
  return EOPNOTSUPP;
}