Skip to content
This repository has been 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

Conversation

DK-DARKmatter
Copy link

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]

@codecov-io
Copy link

codecov-io commented Mar 26, 2019

Codecov Report

Merging #31 into master will increase coverage by 0.86%.
The diff coverage is 72.72%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master      #31      +/-   ##
==========================================
+ Coverage   64.87%   65.73%   +0.86%     
==========================================
  Files          13       13              
  Lines        1170     1179       +9     
==========================================
+ Hits          759      775      +16     
+ Misses        411      404       -7
Impacted Files Coverage Δ
bin/non.c 44.44% <66.66%> (+1.58%) ⬆️
bin/main.c 71.64% <80%> (+0.1%) ⬆️
lib/tlssock.c 50% <0%> (+6.17%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 72f2c71...6588580. Read the comment docs.

bin/main.c Outdated

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.\n");
Copy link
Contributor

Choose a reason for hiding this comment

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

Also log what the error is.

bin/non.c Outdated
struct stat st;

if (fstat(fd, &st)) {
fprintf(stderr, "Error in fstat.\n");
Copy link
Contributor

Choose a reason for hiding this comment

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

(Here too.)

bin/main.c Outdated
if (S_ISSOCK(st.st_mode)) {
ret = recv(pfds[i].fd, buffer, sizeof(buffer), 0);
} 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.

Wrong indentation.

}

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.

if (S_ISSOCK(st.st_mode)) {
ret = recv(pfds[i].fd, buffer, sizeof(buffer), 0);
} 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.

bin/non.c Outdated

if (fstat(fd, &st)) {
fprintf(stderr, "Error in fstat. Errno is %d\n", errno);
return -1;
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.

bin/non.c Outdated
}

if (S_ISSOCK(st.st_mode)) {
ret = send(fd, buf, len, 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.

bin/non.c Outdated
if (S_ISSOCK(st.st_mode)) {
ret = send(fd, buf, len, 0);
} else {
ret = write(fd, buf, len);
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.

bin/main.c Outdated

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. Errno is %d\n", errno);
Copy link
Contributor

Choose a reason for hiding this comment

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

%m or strerror, please.

bin/non.c Outdated
struct stat st;

if (fstat(fd, &st)) {
fprintf(stderr, "Error in fstat. Errno is %d\n", errno);
Copy link
Contributor

Choose a reason for hiding this comment

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

Here too.

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:enarx-archive#30
Signed-off-by: Ke Zhao <[email protected]>
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants