forked from xythian/wp-lambdamoo
-
Notifications
You must be signed in to change notification settings - Fork 17
/
net_bsd_lcl.c
217 lines (193 loc) · 5.52 KB
/
net_bsd_lcl.c
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
/******************************************************************************
Copyright (c) 1992, 1995, 1996 Xerox Corporation. All rights reserved.
Portions of this code were written by Stephen White, aka ghond.
Use and copying of this software and preparation of derivative works based
upon this software are permitted. Any distribution of this software or
derivative works must comply with all applicable United States export
control laws. This software is made available AS IS, and Xerox Corporation
makes no warranty about the software, its performance or its conformity to
any specification. Any person obtaining a copy of this software is requested
to send their name and post office or electronic mail address to:
Pavel Curtis
Xerox PARC
3333 Coyote Hill Rd.
Palo Alto, CA 94304
Pavel@Xerox.Com
*****************************************************************************/
/* Multi-user networking protocol implementation for local clients on BSD UNIX
*/
#include <errno.h> /* EMFILE */
#include "my-socket.h" /* socket(), AF_UNIX, SOCK_STREAM,
* bind(), struct sockaddr, accept(),
* shutdown(), connect() */
#include "my-stdio.h" /* remove() */
#include "my-string.h" /* strcpy() */
#include <sys/un.h> /* struct sockaddr_un */
#include "my-unistd.h" /* close() */
#include "config.h"
#include "log.h"
#include "net_proto.h"
#include "storage.h"
#include "structures.h"
#include "utils.h"
typedef struct listener {
struct listener *next;
int fd;
const char *filename;
} listener;
static listener *all_listeners = 0;
const char *
proto_name(void)
{
return "BSD single-host";
}
const char *
proto_usage_string(void)
{
return "[server-connect-file]";
}
int
proto_initialize(struct proto *proto, Var * desc, int argc, char **argv)
{
const char *connect_file = DEFAULT_CONNECT_FILE;
proto->pocket_size = 1;
proto->believe_eof = 1;
proto->eol_out_string = "\n";
if (argc > 1)
return 0;
else if (argc == 1) {
connect_file = argv[0];
}
desc->type = TYPE_STR;
desc->v.str = str_dup(connect_file);
return 1;
}
enum error
proto_make_listener(Var desc, int *fd, Var * canon, const char **name)
{
struct sockaddr_un address;
int s;
const char *connect_file;
listener *l;
if (desc.type != TYPE_STR)
return E_TYPE;
connect_file = desc.v.str;
s = socket(AF_UNIX, SOCK_STREAM, 0);
if (s < 0) {
log_perror("Creating listening socket");
return E_QUOTA;
}
address.sun_family = AF_UNIX;
strcpy(address.sun_path, connect_file);
if (bind(s, (struct sockaddr *) &address,
sizeof(address.sun_family) + strlen(connect_file))) {
enum error e = E_QUOTA;
log_perror("Binding listening socket");
if (errno == EACCES)
e = E_PERM;
close(s);
return e;
}
l = mymalloc(sizeof(listener), M_NETWORK);
l->next = all_listeners;
all_listeners = l;
l->filename = str_dup(connect_file);
l->fd = s;
*fd = s;
*canon = var_ref(desc);
*name = l->filename;
return E_NONE;
}
int
proto_listen(int fd)
{
listen(fd, 5);
return 1;
}
enum proto_accept_error
proto_accept_connection(int listener_fd, int *read_fd, int *write_fd,
const char **name)
{
int fd;
static struct sockaddr_un address;
int addr_length = sizeof(address);
fd = accept(listener_fd, (struct sockaddr *) &address, &addr_length);
if (fd < 0) {
if (errno == EMFILE)
return PA_FULL;
else {
log_perror("Accepting new network connection");
return PA_OTHER;
}
}
*read_fd = *write_fd = fd;
*name = "??";
return PA_OKAY;
}
void
proto_close_connection(int read_fd, int write_fd)
{
/* read_fd and write_fd are the same, so we only need to deal with one. */
close(read_fd);
}
void
proto_close_listener(int fd)
{
listener *l, **ll;
for (l = all_listeners, ll = &all_listeners; l; ll = &(l->next),
l = l->next)
if (l->fd == fd) {
close(l->fd);
remove(l->filename);
*ll = l->next;
free_str(l->filename);
myfree(l, M_NETWORK);
return;
}
errlog("Can't find fd in PROTO_CLOSE_LISTENER!\n");
}
char rcsid_net_bsd_lcl[] = "$Id$";
/* $Log$
/* Revision 1.2 1997/03/03 04:19:02 nop
/* GNU Indent normalization
/*
* Revision 1.1.1.1 1997/03/03 03:45:02 nop
* LambdaMOO 1.8.0p5
*
* Revision 2.4 1996/03/10 01:13:22 pavel
* Moved definition of DEFAULT_CONNECT_FILE to options.h. Release 1.8.0.
*
* Revision 2.3 1996/02/08 06:35:21 pavel
* Renamed err/logf() to errlog/oklog(). Updated copyright notice for 1996.
* Release 1.8.0beta1.
*
* Revision 2.2 1995/12/31 00:00:45 pavel
* Added support for the new multiple-listening-points interface.
* Release 1.8.0alpha4.
*
* Revision 2.1 1995/12/28 00:34:57 pavel
* Removed old support for protocol-specific input EOL conventions.
* Release 1.8.0alpha3.
*
* Revision 2.0 1995/11/30 04:46:53 pavel
* New baseline version, corresponding to release 1.8.0alpha1.
*
* Revision 1.6 1992/10/23 23:03:47 pavel
* Added copyright notice.
*
* Revision 1.5 1992/10/21 03:02:35 pavel
* Converted to use new automatic configuration system.
*
* Revision 1.4 1992/10/06 01:36:23 pavel
* Moved non-blocking code to net_multi.c, replacing it with code to set
* proto->believe_eof appropriately.
*
* Revision 1.3 1992/09/26 02:25:02 pavel
* Added support for printing the network protocol name on server start-up.
*
* Revision 1.2 1992/09/24 00:39:47 pavel
* Removed useless #include's.
*
* Revision 1.1 1992/09/23 22:33:38 pavel
* Initial RCS-controlled version.
*/