using unix domain socket and sharing fd's - c

Not been able to figure out why recvmsg() blocks when i try this test app on ubuntu.
http://web.mit.edu/kolya/misc/break-chroot.c
thanks

I strongly remember file descriptor passing only working over Unix Datagram sockets, not Unix stream sockets. This may also necessitate resends. Your example is too large (and I'm too lazy) to do a proper analysis, but look here for alternative example code. I've used that example myself on both FreeBSD and Linux, it works.

Related

Partially Porting PJLIB - Without IOQUEUE, select abstraction, and socket abstraction API

I would like to use the PJSIP library to implement a small SIP softphone on an embedded system. Since this embedded system does not offer Linux or support POSIX, I would like to port the PJLIB library only partially, as described here (https://www.pjsip.org/porting.htm#mozTocId30930). The threading function can be deactivated via a macro, but I'm not quite sure yet how I have to set up this new transport function or where exactly it has to be included so that I can also bypass the IOQUEUE implementation and the PJLIB socket abstraction.
On my embedded system (Keil RTX) I can allocate a UDP socket and register a callback which is called on a network event. I also have a send function which I can use to send data packets. Although I have already looked into the stack, I can't find a way to get started.
Has anyone already dared to the partial porting and can give me a brief assistance. Thank you !
See how Symbian port worked (I think it might be removed from recent versions, but it should be still downloadable) - it was also based on non-POSIX sockets. Create your own platform-specific socket file and ioqueue file.

How to find an AF_UNIX socket and see the information passing through it?

There is an application running on a FreeBSD 10.1 release operating system and i need to figure out how to find the sockets it has created and is using. I know that i'm looking for an AF_MAP socket which should be similar to a AF_UNIX socket.
How do i see what sockets are open, and once i find the one im looking for i need to see what information passes through. how is this also done?
Thankyou
I'm not sure on FreeBSD specifically but you can use lsof in a way like:
$ lsof -p $(pidof your-appname)
This will give you all the files it has opened.
For AF_UNIX sockets you may refer to this.
Also, you may want to use netstat -xa to see CONNECTED state sockets (if the application uses stream oriented sockets)

Basic TCP client on Linux, OSX and Windows

I want to make a TCP client that works on Windows, Linux and osx (most important) in C. The code that I've found on SO might work on linux but not on osx and vice versa. So what do I need to make sure that it works on all three?
Thanks!
Assuming you are planning to write your code to use the BSD sockets API, you'll find that most TCP client code that works on Linux will work on MacOS/X with little or no modification, and vice versa.
Getting the code to work under Windows as well is a little bit trickier, as required #includes are different, and there are a number of cases where Windows' TCP stack (aka WinSock) behaves a little bit differently than that the TCP stacks of the other two OS's. That said, Windows does support most of the BSD sockets API, and with a bit of #ifdef-ing you can come up with a program that will compile and run correctly on all three OS's. You'll need to test and debug on all three OS's, of course; never assume that just because something works on one OS that it will work everywhere.
Depending on your program's particular needs (and your interests), it may well make sense to follow Duck's advice and find a networking library that has already done the above-described work for you; but if you prefer to "roll your own", that is doable too. A good approach when writing to the BSD sockets API is: whenever you find a piece of code that you have to write differently for different OS's, hide the implementations of that code snippet inside a function together (with an #ifdef so that the right code gets compiled under each OS), so that the rest of your program doesn't have to remember how to deal with that unpleasant detail anymore. Do that enough times and you'll end up the proud maintainer of your own cross-platform networking library ;)
I'd recommend getting your program working under Linux and/or OS/X first, and once you're happy with it, then porting it over to Windows. Some "gotchas" to watch out for when porting your network code to Windows:
Under Windows, you #include windows.h or winsock2.h to get the network definitions you need. (If you want the newer WinSock2 API, you have to include winsock2.h, and always do it before any #include of windows.h, or you'll get the wrong API version... it's a real circus)
Under Windows you have to call WSAStartup() before doing any networking stuff (if you forget, all your networking calls will error out)
Under MacOS/X and Linux, file descriptors and sockets are largely interchangeable (i.e. you can select() on STDIN_FILENO, etc). Under Windows, they are not.
Under MacOS/X and Linux, you can find out why a call failed by checking errno. Under Windows, you call WSAGetLastError() instead.
Under MacOS/X and Linux, you destroy a socket with close(). Under Windows you do it with closesocket().
Under MacOS/X and Linux, you can (if you choose) call read() and write() on your TCP socket to receive/send data (respectively). Under Windows, that won't work, you must call send() and recv() only. (send() and recv() will work under MacOS/X and Linux too)
To set a socket to non-blocking mode under Windows, you have to call ioctlsocket(fd, FIONBIO, &mode). Under MacOS/X and Linux, you call instead fcntl(fd, F_SETFL, flags).
More fun Windows-networking gotchas can be read at the WinSock Lame List.

/proc/net/tcp Alternative Under Solaris 11

On Most Linux Distributions, i was able to list all tcp connections by reading /proc/net/tcp, but this Doesn't exists on solaris, is there a file that i can read tcp connections from on Solaris 11?
thanks.
EDIT: forgot to mention that i'm coding in c.
If you're trying to rewrite netstat, I suggest looking at the source code for it: https://hg.java.net/hg/solaris~on-src/file/tip/usr/src/cmd/cmd-inet/usr.bin/netstat/netstat.c
The important parts are mibopen, which opens /dev/arp and pushes the tcp STREAMS module onto it, and mibget which actually requests the connection information. The code is a bit complicated, so I suggest stepping through the code in a debugger to understand how it works. The key syscalls are open, ioctl, putmsg, and getmsg.
If you just want to see what sockets a process has open, you can inspect /proc/PID/fd, as in pfiles: https://hg.java.net/hg/solaris~on-src/file/tip/usr/src/cmd/ptools/pfiles/pfiles.c
You should either use netstat -an or pcp

Does posix aio in linux 2.6 support socket file descriptor?

I've seached such question in google and got different answers.I cann't determine whether posix aio in linux 2.6 support socket file descriptor or not.
if it support tcp socket,does the aiocb.aio_offset=0 relative to the first byte readed from the tcp socket fd?
if it doesn't,does any asynchronous io library in linux support socket fd?
A comment above states that aio does not support sockets. You ask for possible alternatives.
The obvious ones are:
use an event driven programming model, either produced by hand using poll(2) or what have you or via a library like Niels Provos' "libevent"
use threads
I generally prefer the event driven way of doing things, and generally use libevent, which is documented here: http://libevent.org/
Bear in mind, however, that event driven programming is rather heavily different from what you may be used to in program organization. Threads are conceptually similar, although often less efficient when handling large numbers of sockets.

Resources