summaryrefslogtreecommitdiff
path: root/pfinet/misc.c
blob: 45cf61ae4f7b0a0469c5ce3976f4417956b43cda (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
/* 
   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 "pfinet.h"
#include <string.h>

/* Create a sock_user structure, initialized from SOCK and ISROOT. */
struct sock_user *
make_sock_user (struct socket *sock, int isroot)
{
  struct sock_user *user;
  
  user = ports_allocate_port (pfinet_bucket,
			      sizeof (struct sock_user),
			      socketport_class);
  
  user->isroot = isroot;
  user->sock = sock;
  sock->refcnt++;
  return user;
}

/* Create a sockaddr port.  Fill in *ADDR and *ADDRTYPE accordingly.
   The address should come from SOCK; PEER is 0 if we want this socket's 
   name and 1 if we want the peer's name. */
error_t
make_sockaddr_port (struct socket *sock, 
		    int peer,
		    mach_port_t *addr,
		    mach_msg_type_name_t *addrtype)
{
  char *buf[128];
  int buflen = 128;
  error_t err;
  struct sock_addr *addrstruct;

  err = (*sock->ops->getname) (sock, (struct sockaddr *)buf, &buflen, peer);
  if (err)
    return err;
  
  addrstruct = ports_allocate_port (pfinet_bucket,
				    sizeof (struct sock_addr) + buflen,
				    addrport_class);
  addrstruct->len = buflen;
  bcopy (buf, addrstruct->address, buflen);
  *addr = ports_get_right (addr);
  *addrtype = MACH_MSG_TYPE_MAKE_SEND;
  ports_port_deref (addrstruct);
  return 0;
}

struct sock_user *
begin_using_socket_port (mach_port_t port)
{
  return ports_lookup_port (pfinet_bucket, port, socketport_class);
}

void
end_using_socket_port (struct sock_user *user)
{
  ports_port_deref (user);
}

struct sock_addr *
begin_using_sockaddr_port (mach_port_t port)
{
  return ports_lookup_port (pfinet_bucket, port, addrport_class);
}

void
end_using_sockaddr_port (struct sock_addr *addr)
{
  ports_port_deref (addr);
}

/* Nothing need be done here. */
void
clean_addrport (void *arg)
{
}

/* Release the reference on the referenced socket. */
void
clean_socketport (void *arg)
{
  struct sock_user *user = arg;
  
  mutex_lock (&global_lock);
  
  user->sock->refcnt--;
  if (user->sock->refcnt == 0)
    sock_release (user->sock);
}

struct socket *
sock_alloc (void)
{
  struct socket *sock;
  struct wait_queue *wait, **waitp;

  sock = malloc (sizeof (struct wait_queue) 
		 + sizeof (struct wait_queue *)
		 + sizeof (struct socket));
  wait = (void *)sock + sizeof (struct socket);
  waitp = (void *)wait + sizeof (struct wait_queue);
  
  bzero (sock, sizeof (struct socket));
  sock->state = SS_UNCONNECTED;
  sock->wait = waitp;

  condition_init (&wait->c);

  *waitp = wait;

  return sock;
}

static inline void sock_release_peer(struct socket *peer)
{
	peer->state = SS_DISCONNECTING;
	wake_up_interruptible(peer->wait);
	sock_wake_async(peer, 1);
}

void
sock_release (struct socket *sock)
{
  	int oldstate;
	struct socket *peersock, *nextsock;

	if ((oldstate = sock->state) != SS_UNCONNECTED)
		sock->state = SS_DISCONNECTING;

	/*
	 *	Wake up anyone waiting for connections. 
	 */

	for (peersock = sock->iconn; peersock; peersock = nextsock) 
	{
		nextsock = peersock->next;
		sock_release_peer(peersock);
	}

	/*
	 * Wake up anyone we're connected to. First, we release the
	 * protocol, to give it a chance to flush data, etc.
	 */

	peersock = (oldstate == SS_CONNECTED) ? sock->conn : NULL;
	if (sock->ops) 
		(*sock->ops->release) (sock, peersock);
	if (peersock)
		sock_release_peer(peersock);
	free (sock);
}