local socket creation

local socket creation

Post by Sau » Thu, 21 Mar 2002 02:37:14



Hi

Is there a way to create a local socket (AF_UNIX) from the shell?
How can I set it's path?

Thanks,
Saul

 
 
 

local socket creation

Post by Barry Margoli » Thu, 21 Mar 2002 02:44:36




>Is there a way to create a local socket (AF_UNIX) from the shell?

I don't think so.  Since there aren't any shell commands to bind or connect
to a socket, what would you do with it if you could?

Quote:>How can I set it's path?

I don't know what this means (and I'm not talking about the use of "it's"
when you meant "its").

--

Genuity, Woburn, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.

 
 
 

local socket creation

Post by Brian Hile » Thu, 21 Mar 2002 08:07:35





>>Is there a way to create a local socket (AF_UNIX) from the shell?
> I don't think so.  Since there aren't any shell commands to bind or connect
> to a socket, what would you do with it if you could?

Very easily.

Example: get the current time and date from the "daytime" service:

read </dev/tcp/0.0.0.0/13    # daytime port number = 13
print $REPLY

"Udp" may be substituted for the "tcp" to use the UDP protocol instead
of TCP. 0.0.0.0 is the IP address understood to be the local machine;
127.0.0.1 is analogous.

This is one of the most handy -- and least known -- features of ksh88+.

Quote:>>How can I set it's path?
> I don't know what this means (and I'm not talking about the use of "it's"
> when you meant "its").

I presume you mean a socket file  (that is, with a ls -l listing
designated with an "s") Although this is a OS specific feature, one
may a filetree binding to a socket by redirecting the unit file number.

exec 3</dev/tcp/0.0.0.0/13
read -u 3
(etcetera)

It's even... fun... to play around with distributed programming with
the high-level sockets capability provided by ksh. Some version of ksh88
don't even document this, even though I have not seen any version past
88b _not_ have it.

=Brian

 
 
 

local socket creation

Post by Os M » Thu, 21 Mar 2002 12:56:27


I'm trying to basically do the same.. I'm able to stablish the
connection but I'm having issues with STDOUT/DTDIN/STDERR on the
script running as the daemon. here is my setup - hope it helps:

The following script gets called from the inetd daemon:
#!/usr/bin/ksh
# test.ksh

    test)   banner test >> /tmp/test.txt
            ;;
    *)      banner NOT >> /tmp/test.txt
            ;;
esac

The service was added to /etc/service:
kshtest    919191/tcp

Also added to inetd.conf (Script running as user kshuser - I'm sure if
the script needs its own name passed as an argument, i tried without
and same results):
kshtest    stream tcp nowait kshuser /usr/local/bin/test.ksh test.ksh
-x

Testing setup - The following should place the word text in the file
/tmp/text.txt but I get NOT instead which tells me that no arguments
are seen:
exec 3<>/dev/tcp/192.168.0.1/919191
print -u3 test


> Hi

> Is there a way to create a local socket (AF_UNIX) from the shell?
> How can I set it's path?

> Thanks,
> Saul

 
 
 

local socket creation

Post by Os M » Thu, 21 Mar 2002 13:25:26


Using a simple while loop to handle STDIN corrected the problem on my
previous post, here is the daemon corrected code:

#!/usr/bin/ksh
#test.ksh
while read line
do
        case "$line" in

    test)   banner test >> /tmp/test.txt
            ;;
    *)      banner NOT >> /tmp/test.txt
            ;;
esac
done


> Hi

> Is there a way to create a local socket (AF_UNIX) from the shell?
> How can I set it's path?

> Thanks,
> Saul

 
 
 

local socket creation

Post by Sau » Thu, 21 Mar 2002 15:26:58




> >Is there a way to create a local socket (AF_UNIX) from the shell?

> I don't think so.  Since there aren't any shell commands to bind or connect
> to a socket, what would you do with it if you could?

I'm writing a script that checks the correct creation of such files
over a mounted file system. I prefer to use a script that doesn't need
compiling any C code.

Quote:> >How can I set it's path?

> I don't know what this means

A local socket (AF_LOCAL or AF_UNIX) is always associated with a file
name on the local file system.
In the address structure for a local socket there's a field that can
be filled
with a file path.

struct sockaddr_un {
    sa_family_t  sun_family;              /* AF_UNIX */
    char         sun_path[UNIX_PATH_MAX]; /* pathname */

Quote:};

That's a quoate from man 7 unix:
When a socket is connected and it doesn't already have a local
address,
a unique address in the abstract namespace will be generated
automatically.

Quote:> (and I'm not talking about the use of "it's" when you meant "its").

oops. I'm not a native english speaker.

Here's a C program that demonstrates this:

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

int main(int argc, char *argv[])
{
        int     fd;
        struct sockaddr_un addr;

        if (argc != 2) {
                fprintf(stderr, "usage: %s  socket_path\n", argv[0]);
                exit(-1);
        }

        if ((fd = socket(AF_LOCAL, SOCK_STREAM, 0)) < 0) {
                fprintf(stderr, "Can't open socket   %s\n", strerror(errno));
                exit(-1);
        }

        memset(&addr, 0, sizeof(addr));
        addr.sun_family = AF_LOCAL;
        strncpy(addr.sun_path, argv[1], sizeof(addr.sun_path) - 1);
        if (bind(fd, (void*)&addr, sizeof(addr)) < 0) {
                fprintf(stderr, "Can't bind socket  %s\n", strerror(errno));
                exit(-1);
        }

        return 0;

Quote:}

After calling bind() a file of type socket is created at the passed
path.

Thanks,
Saul

 
 
 

local socket creation

Post by Marvin Massi » Thu, 21 Mar 2002 23:10:00


Try 'netcat -l -p <port>'.
 
 
 

local socket creation

Post by Barry Margoli » Fri, 22 Mar 2002 01:37:31






>> >Is there a way to create a local socket (AF_UNIX) from the shell?
...
>> >How can I set it's path?

>> I don't know what this means

>A local socket (AF_LOCAL or AF_UNIX) is always associated with a file
>name on the local file system.

I know that.  The path is associated with it when you create it (unless you
use socketpair(), which creates anonymous sockets, analogous to unnamed
pipes created by pipe()).  The phrase "set its path" implies that "it"
already exists, which means that its path is already set.  That's why your
question confused me.

--

Genuity, Woburn, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.

 
 
 

local socket creation

Post by Barry Margoli » Fri, 22 Mar 2002 01:39:59




>I'm trying to basically do the same..

Your question doesn't seem to be at all related to the OP's, since you're
doing Internet sockets and he specifically asked about Unix domain
sockets.

>The following script gets called from the inetd daemon:
>#!/usr/bin/ksh
># test.ksh

>    test)   banner test >> /tmp/test.txt
>            ;;
>    *)      banner NOT >> /tmp/test.txt
>            ;;
>esac

>The service was added to /etc/service:
>kshtest    919191/tcp

The highest possible TCP port number is 65535.

--

Genuity, Woburn, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.

 
 
 

local socket creation

Post by Barry Margoli » Fri, 22 Mar 2002 01:34:05







>>>Is there a way to create a local socket (AF_UNIX) from the shell?
>> I don't think so.  Since there aren't any shell commands to bind or connect
>> to a socket, what would you do with it if you could?

>Very easily.

>Example: get the current time and date from the "daytime" service:

>read </dev/tcp/0.0.0.0/13        # daytime port number = 13
>print $REPLY

>"Udp" may be substituted for the "tcp" to use the UDP protocol instead
>of TCP. 0.0.0.0 is the IP address understood to be the local machine;
>127.0.0.1 is analogous.

That's not a Unix domain socket, it's an Internet socket.

--

Genuity, Woburn, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.

 
 
 

1. creation of local Usenet newsgroups

We have been asked to create some local Usenet newsgroups.
Our System Administrator is in the hospital and we do not know how
he did it previously. Where should we look to determine the proper
procedure? We run NNTP with B news and the NN newsreader on a HP-UX
Unix machine.


Thank you,

Lee

The University of Medicine and Dentistry of New Jersey (USA)

2. Broadband Routing Advocacy Rrquired

3. raw socket creation prob ..

4. Red Hat on a Logical Drive

5. Socket creation problem

6. Fonts not found using ghostscript

7. adding kernel credential checks for socket creation

8. ? Do cron jobs even if machine is down when due

9. ping, route - socket creation - file permissions

10. Permission denied on socket creation

11. Socket Creation

12. High Volume TCP Socket Creation

13. Finding a socket's local port number