Skip to content
This repository was archived by the owner on Jun 30, 2020. It is now read-only.

Commit bad47ff

Browse files
author
Ke Zhao
committed
Use fstat() to check if there is a socket or not
recv/send function can only be applied to a socket. We can use fstat() function to check if the fd is a socket or not. If so, we would like to use recv/send function to replace read/write. Related to:#30 Signed-off-by: Ke Zhao <[email protected]>
1 parent 72f2c71 commit bad47ff

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

bin/main.c

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include "exe.h"
2626
#include "non.h"
2727

28+
#include <sys/stat.h>
2829
#include <sys/types.h>
2930
#include <sys/socket.h>
3031
#include <netdb.h>
@@ -183,12 +184,23 @@ on_conn(options_t *opts, int con, int in, int out, const struct addrinfo *ai)
183184
while (poll(pfds, 2, -1) >= 0) {
184185
char buffer[64 * 1024] = {};
185186
ssize_t ret;
187+
struct stat st;
186188

187189
for (int i = 0; i < 2; i++) {
188190
if (!pfds[i].revents)
189191
continue;
190192

191-
ret = read(pfds[i].fd, buffer, sizeof(buffer));
193+
if (fstat(pfds[i].fd, &st)) {
194+
fprintf(stderr, "Error in fstat.\n");
195+
return -1;
196+
}
197+
198+
if (S_ISSOCK(st.st_mode)) {
199+
ret = recv(pfds[i].fd, buffer, sizeof(buffer), 0);
200+
} else {
201+
ret = read(pfds[i].fd, buffer, sizeof(buffer));
202+
}
203+
192204
if (ret <= 0) {
193205
if (pfds[i].revents != POLLHUP &&
194206
(errno == EAGAIN || errno == EWOULDBLOCK))

bin/non.c

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121

2222
#include "non.h"
2323

24+
#include <sys/stat.h>
25+
#include <stdio.h>
2426
#include <stdint.h>
2527
#include <unistd.h>
2628
#include <errno.h>
@@ -97,8 +99,19 @@ non_write(int fd, void *buf, size_t len)
9799
struct pollfd pfd = { .fd = fd, .events = POLLOUT };
98100
uint8_t *b = buf;
99101
ssize_t ret;
102+
struct stat st;
103+
104+
if (fstat(fd, &st)) {
105+
fprintf(stderr, "Error in fstat.\n");
106+
return -1;
107+
}
108+
109+
if (S_ISSOCK(st.st_mode)) {
110+
ret = send(fd, buf, len, 0);
111+
} else {
112+
ret = write(fd, buf, len);
113+
}
100114

101-
ret = write(fd, buf, len);
102115
if (ret < 0) {
103116
if (errno != EAGAIN)
104117
return ret;

0 commit comments

Comments
 (0)