-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapue.c
176 lines (149 loc) · 4.19 KB
/
apue.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
/* ======================================================================
* Copyright (c) 1998-1999 The Johns Hopkins University.
* All rights reserved.
* The following code was written by Theo Schlossnagle for use in the
* Backhand project at The Center for Networking and Distributed Systems
* at The Johns Hopkins University.
* Please refer to the LICENSE file before using this software.
* ======================================================================
*/
#include "config.h"
#include "alarm.h"
#define FIFO_MODE (S_IRUSR|S_IWUSR)
#ifdef USE_FATTACH
int serv_listen(const char *name) {
int tempfd, fd[2], len;
unlink(name);
if((tempfd = creat(name, FIFO_MODE)) < 0)
return(-1);
if (close(tempfd) < 0)
return(-2);
if(pipe(fd) < 0)
return(-3);
if(ioctl(fd[1], I_PUSH, "connld") < 0)
return(-4);
if(fattach(fd[1], name) < 0)
return(-5);
return(fd[0]);
}
int cli_conn(const char *name) {
int fd;
if((fd = open(name, O_RDWR)) < 0)
return(-1);
if(isastream(fd) == 0)
return(-2);
return(fd);
}
#elif defined(WIN32)
/* TCP sockets. Should use named pipes, but I doubt spread can schedule/select on those */
int serv_accept(int listenfd) {
int clifd;
unsigned int len;
struct sockaddr_in addr;
len = sizeof(addr);
if((clifd = accept(listenfd, (struct sockaddr *) &addr, &len)) < 0)
return -1;
return clifd;
}
int serv_listen(const char *name) {
int fd, len, oldmask;
struct sockaddr_in addr;
/* create a Unix domain stream socket */
if((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
wack_alarm(PRINT, "could not open socket");
return -1;
}
len = sizeof(addr);
if (WSAStringToAddress((char*)name, AF_INET, NULL, (struct sockaddr*)&addr,&len) != 0) {
wack_alarm(PRINT, "couldn't resolve address string %s\n", name);
return -1;
}
if(bind(fd, (struct sockaddr *) &addr, len) < 0) {
wack_alarm(PRINT, "bind %s failed.", name);
close(fd);
return -1;
}
if(listen(fd, 5) < 0) {
wack_alarm(PRINT, "listen on %s failed.", name);
close(fd);
return -1;
}
return(fd);
}
int cli_conn(const char *name) {
int fd, len;
struct sockaddr_in addr;
/* create a Unix domain stream socket */
if((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
return(-1);
len = sizeof(addr);
if (WSAStringToAddress((char*)name, AF_INET, NULL, (struct sockaddr*)&addr,&len) != 0) {
wack_alarm(PRINT, "couldn't resolve address string %s\n", name);
return -1;
}
if(connect(fd, (struct sockaddr *) &addr, len) < 0) {
close(fd);
fd=-1;
wack_alarm(PRINT, "connect to: %s failed.", name);
}
return(fd);
}
#else
int serv_accept(int listenfd) {
int clifd;
unsigned int len;
struct sockaddr_un unix_addr;
len = sizeof(unix_addr);
if((clifd = accept(listenfd, (struct sockaddr *) &unix_addr, &len)) < 0)
return -1;
return clifd;
}
int serv_listen(const char *name) {
int fd, len, oldmask;
struct sockaddr_un unix_addr;
/* create a Unix domain stream socket */
if((fd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
wack_alarm(PRINT, "could not open socket");
return -1;
}
unlink(name);
memset(&unix_addr, 0, sizeof(unix_addr));
unix_addr.sun_family = AF_UNIX;
strcpy(unix_addr.sun_path, name);
len = SUN_LEN(&unix_addr);
/* bind the name to the descriptor */
oldmask = umask((~FIFO_MODE)&0777);
if(bind(fd, (struct sockaddr *) &unix_addr, len) < 0) {
wack_alarm(PRINT, "bind to %s failed.", name);
umask(oldmask);
close(fd);
return -1;
}
umask(oldmask);
if(listen(fd, 5) < 0) {
wack_alarm(PRINT, "listen on %s failed.", name);
close(fd);
return -1;
}
return(fd);
}
int cli_conn(const char *name) {
int fd, len;
struct sockaddr_un unix_addr;
/* create a Unix domain stream socket */
if((fd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0)
return(-1);
memset(&unix_addr, 0, sizeof(unix_addr));
unix_addr.sun_family = AF_UNIX;
strcpy(unix_addr.sun_path, name);
unix_addr.sun_path[strlen(name)]='\0';
len = SUN_LEN(&unix_addr);
if(connect(fd, (struct sockaddr *) &unix_addr, len) < 0) {
close(fd);
fd=-1;
wack_alarm(PRINT, "connect to: %s failed.", name);
}
return(fd);
}
#endif
/* vim:se ts=2 sw=2 et: */