Skip to contentSkip to conversation

Socket hack question.

DISCUSSION Joel Eriksson 1 participant 2 messages Apr 18, 2001

lore
Share & tools
lore
#0 lore raw
Quoted 11 lines · Andi Kleen
Quoted 8 lines · Joel Eriksson

Hello,

I am a kernel hacking newbie and am struggling to understand the networking subsystem. I would like to be able to add a systemcall, preferably asynchronous, that connects a socket with a filedescriptor (proxy(srcsd, dstfd)) so that everything received on srcsd is directly written to dstfd. The proxy should close when srcsd is closed or when a zero-size packet is sent (or something like that..).

That syscall already exists -- it's called sendfile.

Actually, I realised the similarities with sendfile() after I made the post. :-) But I thought sendfile() could only be used for sending data from a "regular" file descriptor to another file- or socket descriptor..?

The syscall I would like to implement is kind of the reverse to sendfile() since it should be used to copy data _from_ a socket descriptor to a file descriptor.

Hmm, I made a small test program and from what I can understand it seems to verify what I thought:

---
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#include <errno.h>
#include <stdio.h>

int main(void)

{
        pid_t pid;
        int fdarr[2];

        if (socketpair(AF_UNIX, SOCK_STREAM, 0, fdarr) ÿ-1) {
                perror("socketpair");
                return 1;
        }

        if ((pid ÿork()) ÿ-1) {
                perror("fork");
                return 1;
        } else if (pid) {
                close(fdarr[0]);
                if (sendfile(1, fdarr[1], NULL, 5) ÿ-1) {
                        perror("sendfile");
                        return 1;
                }
        } else {
                close(fdarr[1]);
                write(fdarr[0], "Test\n", 5);
        }

        return 0;
}

---

The output from the program is "sendfile: Invalid argument"..

Quoted 1 line

-Andi

Signature · 1 line
Joel Eriksson
#1 lore raw
Quoted 5 lines · Mark Hahn
Quoted 2 lines

post. :-) But I thought sendfile() could only be used for sending data from a "regular" file descriptor to another file- or socket descriptor..?

he said the syscall (ie, interface) already existed, not that it was implemented how you want it.

Well, that's right, I could still use the sendfile() interface. If I would like to implement sendfile() for socket -> [file|socket], would it be possible to do something like in the following pseudocode:

if srcfd is socket then

	s = sock struct for srcfd
	s->foo_member = dstfd
	s->data_ready = my_data_ready
	block until srcfd is closed

Where foo_member was added by me to the sock struct and my_data_ready writes to dstfd before calling the default data_ready function. To follow the sendfile() semantics I should add a max_forward or something too. Btw, how would I accomplish the block until srcfd is closed, is it possible (ok, anything is _possible_ but..)?

Signature · 1 line
Joel Eriksson