summaryrefslogtreecommitdiff
path: root/pfinet/socket-ops.c
blob: 9998ca8c15507e9c22a2d9567281fb0eaadfd4d8 (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
/* Interface functions for the socket.defs interface.
   Copyright (C) 1995 Free Software Foundation, Inc.
   Written by Michael I. Bushnell, p/BSG.

   This file is part of the GNU Hurd.

   The GNU Hurd 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.

   The GNU Hurd 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, USA. */


#include "socket_S.h"



/* Listen on a socket. */
error_t
S_socket_listen (struct user_sock *user, int queue_limit)
{
  if (!user)
    return EOPNOTSUPP;
  
  mutex_lock (&global_lock);
  
  if (user->sock->state == SS_UNCONNECTED)
    {
      if (user->sock->ops && user->sock->ops->listen)
	(*user->sock->ops->listen) (user->sock, queue_limit);
      user->sock->flags |= SO_ACCEPTCON;
      mutex_unlock (&global_lock);
      return 0;
    }
  else
    {
      mutex_unlock (&global_lock);
      return EINVAL;
    }
}

error_t
S_socket_accept (struct sock_user *user,
		 mach_port_t *new_port,
		 mach_msg_type_name_t *new_port_type,
		 mach_port_t *addr_port,
		 mach_msg_type_name_t *addr_port_type)
{
  struct socket *sock, *newsock;
  error_t err;
  int i;

  if (!user)
    return EOPNOTSUPP;
  
  mutex_lock (&global_lock);
  
  sock = user->sock;
  newsock = 0;
  err = 0;

  if ((sock->state != SS_UNCONNECTED)
      || (!(sock->flags & SO_ACCEPTCON)))
    err = EINVAL;
  else if (!(newsock = sock_alloc ()))
    err = ENOSR;
  
  if (err)
    goto out;

  newsock->type = sock->type;
  newsock->ops = sock->ops;
  
  err = - (*sock->ops->dup) (newsock, sock);
  if (err)
    goto out;
  
  /* O_NONBLOCK flag in Linux comes from fd table for third
     arg here...  what to do?  XXX */
  err = - (*sock->ops->accept) (sock, newsock, 0);
  if (err)
    goto out;
  
  make_sockaddr_port (newsock, 1, addr_port, addr_port_type);

  *new_port = ports_get_right (make_sock_user (newsock, sock->isroot));
  *new_port_type = MACH_MSG_TYPE_MAKE_SEND;

 out:
  if (err && newsock)
    sock_release (newsock);
  mutex_unlock (&global_lock);
  return err;
}

error_t
S_socket_connect (struct sock_user *user,
		  struct sock_addr *addr)
{
  struct socket *sock;
  error_t err;

  if (!user || !addr)
    return EOPNOTSUPP;
  
  sock = user->sock;
  
  mutex_lock (&global_lock);

  err = 0;
  
  if (sock->state == SS_CONNECTED
      && sock->type != SOCK_DGRAM)
    err = EISCONN;
  else if (sock->state != SS_UNCONNECTED
	   && sock->state != SS_CONNECTING)
    err = EINVAL;
  
  
  if (!err)
    /* Flags here come from fd table in Linux for O_NONBLOCK... XXX */
    err = - (*sock->ops->connect) (sock, addr->address, addr->len, 0);
  
  mutex_unlock (&global_lock);
  
  return err;
}

error_t
S_socket_bind (struct user_sock *user,
	       struct sock_addr *addr)
{
  error_t err;
  
  if (!sock)
    return EOPNOTSUPP;
  
  mutex_lock (&global_lock);
  err = (*user->sock->ops->bind) (user->sock, addr->address, addr->len);
  mutex_unlock (&global_lock);
  
  return err;
}

error_t
S_socket_name (struct sock_user *user,
	       mach_port_t *addr_port,
	       mach_port_name_t *addr_port_name)
{
  if (!user)
    return EOPNOTSUPP;
  
  mutex_lock (&global_lock);
  make_sockaddr_port (user->sock, 0, addr_port, addr_port_name);
  mutex_unlock (&global_lock);
  return 0;
}

error_t
S_socket_peername (struct sock_user *user,
		   mach_port_t *addr_port,
		   mach_port_name_t *addr_port_name)
{
  error_t err;

  if (!user)
    return EOPNOTSUPP;
  
  mutex_lock (&global_lock);
  err = make_sockaddr_port (user->sock, 1, addr_port, addr_port_name);
  mutex_unlock (&global_lock);
  
  return err;
}

error_t
S_socket_connect2 (struct sock_user *user1,
		   struct sock_user *user2)
{
  error_t err;
  
  if (!user1 || !user2)
    return EOPNOTSUPP;
  
  mutex_lock (&global_lock);

  if (user1->sock->type != user2->sock->type)
    err = EINVAL;
  else if (user1->sock->state != SS_UNCONNECTED
	   && user2->sock->state != SS_UNCONNECTED)
    err = EISCONN;
  else
    err = - (*user1->sock->ops->socketpair) (user1->sock, user2->sock);
  
  if (!err)
    {
      user1->sock->conn = user2->sock;
      user2->sock->conn = user1->sock;
      user1->sock->state = SS_CONNECTED;
      user2->sock->state = SS_CONNECTED;
    }
  
  mutex_unlock (&global_lock);
  return err;
}

error_t
S_socket_create_address (mach_port_t server,
			 int sockaddr_type,
			 char *data,
			 mach_msg_number_t *data_len,
			 mach_port_t *addr_port,
			 mach_msg_type_name_t *addr_port_type,
			 int binding)
{
  struct sock_addr *addr;
  
  if (sockaddr_type != AF_INET)
    return EAFNOTSUPP;
  
  addr = ports_allocate_port (pfinet_bucket, 
			      sizeof (struct sock_addr) + data_len,
			      addrport_class);
  addr->len = data_len;
  bcopy (data, addr->address, data_len);
  
  *addr_port = ports_get_right (addr);
  *addr_port_type = MACH_MSG_TYPE_MAKE_SEND;
  return 0;
}