21.03.2013 Views

Problem - Kevin Tafuro

Problem - Kevin Tafuro

Problem - Kevin Tafuro

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

so. Likewise, any process with the proper permissions may write to the message<br />

queue. Message queues require cooperation among the processes that will use<br />

the queues. A malicious program can easily subvert that cooperation and steal<br />

messages away from the queue. Message queues are also limited such that they<br />

can only handle small amounts of data.<br />

Mailslots (Windows)<br />

Windows mailslots can be named just as named pipes can be, though there are<br />

two separate and distinct namespaces. Mailslots are a one-way communication<br />

mechanism. Only the process that creates the mailslot can read from it; other<br />

processes can only write to it. Mailslots work well when there is a single process<br />

that needs to receive information from other processes but does not need to send<br />

anything back.<br />

Sockets<br />

It is difficult these days to find an operating system that does not support the<br />

Berkeley socket interface for TCP/IP sockets. While most TCP/IP connections<br />

are established over a network between two different machines, it is also possible<br />

to connect two processes running on the same machine without ever touching<br />

a network using TCP/IP. On Unix, the same interface can also be used for<br />

Unix Domain sockets, which are faster, can be used to exchange file descriptors,<br />

and can also be used to exchange credentials (see Recipe 9.8).<br />

Using TCP/IP sockets for interprocess communication (IPC) is not very different<br />

from using them for network communications. In fact, you could use them in<br />

exactly the same way and it would work just fine, but if your intent is to use the<br />

sockets strictly for local IPC, there are a couple of additional things that you<br />

should do, which we discuss in the following paragraphs.<br />

If you are using TCP/IP sockets for local IPC, the most important thing for you to<br />

know is that you should always use the loopback address. When you bind a socket,<br />

do not bind to INADDR_ANY, but instead use 127.0.0.1. If you do this, you will only be<br />

able to connect to the port using the 127.0.0.1 address. This means that the server<br />

will be unreachable from any other machine, whether or not you have blocked the<br />

port in your firewall.<br />

On Windows systems, the following code will strictly use TCP/IP sockets, but on<br />

Unix systems, we have made an optimization to use Unix sockets if the loopback<br />

address of 127.0.0.1 is used. We have created a wrapper around the socket descriptor<br />

that keeps track of the type of socket (Unix or TCP/IP) and the address to which<br />

it is bound. This information is then used in spc_socket_accept( ), spc_socket_<br />

sendto( ), and spc_socket_recvfrom( ), which act as wrappers around accept( ),<br />

sendto( ), and recvfrom( ), respectively.<br />

Remember that on Windows you must call WSAStartup( ) before you can use any<br />

socket functions. You should also be sure to call WSACleanup( ) when you have finished<br />

using sockets in your program.<br />

Performing Interprocess Communication Using Sockets | 477<br />

This is the Title of the Book, eMatter Edition<br />

Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!