[NTLUG:Discuss] Socket error?

Steve Baker sjbaker1 at airmail.net
Mon Dec 20 22:18:20 CST 1999


Mohammad Saman wrote:

>   I am new to Linux Socket programming. I am trying to open a Raw socket to
> read ip packets. with:
> 
>    sockfd= socket(AF_INET, SOCK_RAW,0)
> 
> What happens is that sockfd returns a  "-1". I am logged in as root.
> The SUID  bit is set on the executable,and the excutable is owned by
> root, but I still cannot create a socket. Does anybody have any ideas
> why ? Thanks in advance.

Well, I've never played with SOCK_RAW - so I don't know what's wrong - but
here is how you proceed:

-1 is an error return - so the next step (as with any UNIX system call that
returns an error) is to check the value of 'errno' - the global that contains
the last error number.  Be careful not to make another system call before
you preserve the value of errno or it could get changed.

You *can* print a human readable error message with 'perror' - but since the
UNIX error numbers can mean different things in different contexts, that's
not as useful as you might hope.

The best bet is to search for that error number in '/usr/include/errno.h'
(or /usr/include/asm/errno.h or some such) - and find it's symbolic name.

Hence, if after calling socket, 'errno' is (say) 13, then a search in errno.h
will reveal that this is 'EACCES' - checking the socket() man page reveals that
EACCES (in this context) means:

       EACCES  Permission  to  create  a  socket of the specified
               type and/or protocol is denied.

In this case, the 'perror(str)' function would only have printed "Permission denied",
which is somewhat less informative.

It's a messy process.

--
Steve Baker                  http://web2.airmail.net/sjbaker1
sjbaker1 at airmail.net (home)  http://www.woodsoup.org/~sbaker
sjbaker at hti.com      (work)






More information about the Discuss mailing list