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

Use fstat() to check if there is a socket or not #31

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion bin/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include "exe.h"
#include "non.h"

#include <sys/stat.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
Expand Down Expand Up @@ -183,12 +184,22 @@ on_conn(options_t *opts, int con, int in, int out, const struct addrinfo *ai)
while (poll(pfds, 2, -1) >= 0) {
char buffer[64 * 1024] = {};
ssize_t ret;
struct stat st;

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

ret = read(pfds[i].fd, buffer, sizeof(buffer));
if (fstat(pfds[i].fd, &st)) {
fprintf(stderr, "Error in fstat. %m\n");
return -1;
}

if (S_ISSOCK(st.st_mode))
ret = recv(pfds[i].fd, buffer, sizeof(buffer), 0);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indentation is wrong.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, don't use braces for a single-line if statement.

else
ret = read(pfds[i].fd, buffer, sizeof(buffer));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indentation is wrong.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, don't use braces for a single-line if statement.


if (ret <= 0) {
if (pfds[i].revents != POLLHUP &&
(errno == EAGAIN || errno == EWOULDBLOCK))
Expand Down
14 changes: 13 additions & 1 deletion bin/non.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@

#include "non.h"

#include <sys/stat.h>
#include <stdio.h>
#include <stdint.h>
#include <unistd.h>
#include <errno.h>
Expand Down Expand Up @@ -97,8 +99,18 @@ non_write(int fd, void *buf, size_t len)
struct pollfd pfd = { .fd = fd, .events = POLLOUT };
uint8_t *b = buf;
ssize_t ret;
struct stat st;

if (fstat(fd, &st)) {
fprintf(stderr, "Error in fstat. %m\n");
return -1;
}

if (S_ISSOCK(st.st_mode))
ret = send(fd, buf, len, 0);
else
ret = write(fd, buf, len);

ret = write(fd, buf, len);
if (ret < 0) {
if (errno != EAGAIN)
return ret;
Expand Down