LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-11-2005, 03:12 AM   #1
Thinking
Member
 
Registered: Oct 2003
Posts: 249

Rep: Reputation: 30
is it possible to identify the kind of file descriptor?


hiho@ll

i thought about an abstraction class for file descriptors (sockets (udp/tcp), pipes, ...)

i want to make a multiplexed server which listens with select() for a change of such a descriptor

the problem is:
at a specific point i have to handle the descriptor with the right methods (for example tcp socket with accept and udp with recvfrom())

to do this i need to know the kind of the file descriptor which changed it's state

is there such a method in linux?
input a file descriptor
output the kind of descriptor

thx@ll
 
Old 08-11-2005, 04:10 AM   #2
primo
Member
 
Registered: Jun 2005
Posts: 542

Rep: Reputation: 34
fstat(2) is ok for filesystem based file-descriptors: files, directories, named pipes, unix domain sockets, etc

isatty(3) "returns 1 if desc is an open descriptor connected to a terminal and 0 else."
This library function is usually implemented with fstat(2).

For sockets, you may use a combination of getsockname(2) and getsockopt(2)
With getsockname(2) you get the address type (AF_INET) in the sockaddr buffer,
and with getsockopt(..., SOL_SOCKET, SO_TYPE, ...) you get the socket type...
According to socket(7):
Quote:
SO_TYPE - Gets the socket type as an integer (like SOCK_STREAM). Can only be read with getsockopt.
So, with AF_INET / SOCK_STREAM you can be 99% sure it's a TCP/IP socket
The same accounts for SOCK_DGRAM (UDP/IP)

Note that functions that expect sockets and are passed a valid (opened) non-socket file descriptor will return errno == ENOTSOCK (Socket operation on non-socket),

Also, attempting to lseek(2) a pipe, socket or fifo will return ESPIPE. Check the manpage...
I would try a harmless lseek(fd, 0, SEEK_CUR) and check errno

If the context or meanings are sometimes not clear, then check other Unix'es manpages at http://www.freebsd.org/cgi/man.cgi
 
Old 08-11-2005, 05:43 AM   #3
Thinking
Member
 
Registered: Oct 2003
Posts: 249

Original Poster
Rep: Reputation: 30
cool
sounds good
i'll give it a try
thx
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Get the absolute path of file using file descriptor. appas Programming 7 01-19-2012 11:47 AM
new to pcmcia, how to identify what kind? glorsplitz Linux - Wireless Networking 1 09-12-2005 06:06 AM
Change File Descriptor ilnli Linux - General 1 05-15-2005 05:43 PM
apt-file returns nothing; 'bad file descriptor' overbored Debian 3 10-03-2004 09:13 PM
File descriptor lido Linux - Newbie 5 07-17-2003 11:58 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:28 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration