Chapter 4

Helper Applications and Network Communications


CONTENTS


Introduction

Netscape helper applications are the father to Netscape plug-ins. Helper applications are small stand-alone programs that enable you to use media types not handled by the Netscape browser. Helpers, like plug-ins, are started based on file extension and MIME type. You can configure which helper application is used for each MIME type within the Netscape Navigator.

Netscape network-based helpers use Berkeley sockets, Dynamic Data Exchange (DDE), Object Linking and Embedding (OLE), or a combination of the three to interact with the Internet. Some of these types of helpers are unaware of Netscape and use sockets directly, while others use DDE or OLE to communicate with the Navigator.

Development of a Navigator plug-in rarely requires the direct use of sockets, DDE, or OLE. This chapter is an overview of these techniques and is not meant as a reference.

Netscape Helper Applications

Netscape helper applications work in much the same way as Netscape plug-ins. When the Navigator encounters an unknown media type, it first consults an internal plug-in list. Finding no plug-ins, it turns to the Helpers configuration. If a helper application is associated with the given media type, it is started by the Navigator.

A helper application can be any software program that runs on your system. Many helpers have nothing to do with networking. Consider using Microsoft Paint as a bitmap viewer. Navigator encounters a bitmap file, downloads it, and opens Paint with the given filename. Paint operates on the local file normally.

Other helpers have little, if any, communication with the Netscape Browser and use the network sockets API. A good example of a sockets-based helper application is Progressive Networks' RealAudio Player, which is shown in Figure 4.1. The RealAudio Player plays real-time sound over the Internet.

Figure 4.1 : The RealAudio Player from Progressive Networks.

There are also helpers that rely heavily on the Navigator through the use of Netscape's Dynamic Data Exchange (DDE) or Object Linking and Embedding (OLE). These helpers can perform all network duties, such as loading URLs and writing to Web servers, strictly through the Navigator.

Windows Sockets

The Transmission Control Protocol (TCP) and Internet Protocol (IP) (known together as TCP/IP) were adopted in 1983 to network unlike computer architectures across the world. This network is, of course, the Internet. Although TCP/IP does provide an application layer, it proved to be complex and inconsistent across TCP/IP vendors. Enter Berkeley Software Distribution and its implementation of UNIX sockets, which simplified and standardized network programming. Berkeley socket implementations spread quickly to other platforms including Windows 3.x/95/NT.

The Windows socket interface is commonly known as WinSock. WinSock is implemented by dynamically linking to WINSOCK.DLL for 16-bit applications or WSOCK32.DLL for 32-bit applications. WinSock is a superset of the Berkeley socket API. The Windows extensions are prefaced with the letters WSA. Many of today's Windows Internet applications use WinSock for Internet communication. One good example is Netscape's Navigator.

Although this is not a WinSock programming book, it is interesting to take a look at the socket API names and brief descriptions. The WinSock APIs are divided into three sections. Table 4.1 shows the socket routines. The socket routines are used for basic socket communications including opening, closing, reading, and writing. Information such as host names and protocols can be determined with one of the database routines shown in Table 4.2. In Table 4.3, you can see that many of the Windows extensions (prefaced by WSA) were added to handle asynchronous calls and callbacks for Windows. These were needed to overcome the limitations of single-threaded Windows 3.1.

Table 4.1. Socket routines.

Socket
Description
accept Accept a connection on a socket.
bind Associate a local address with a socket.
closesocket Close a socket.
connect Establish a connection to a peer.
getpeername Get the address of the peer to which a socket is connected.
getsockname Get the local name for a socket.
getsockopt Retrieve a socket option.
htonl Convert a u_long from host to network byte order.
htons Convert a u_short from host to network byte order.
inet_addr Convert a string containing a dotted address into an in_addr.
inet_ntoa Convert a network address into a string in dotted format.
ioctlsocket Control the mode of a socket.
listen Establish a socket to listen for incoming connection.
ntohl Convert a u_long from network to host byte order.
ntohs Convert a u_short from network to host byte order.
recv Receive data from a socket.
recvfrom Receive a datagram and store the source address.
select Determine the status of one or more sockets, waiting if necessary.
send Send data on a connected socket.
sendto Send data to a specific destination.
setsockopt Set a socket option.
shutdown Disable sends and/or receives on a socket.
socket Create a socket.

Table 4.2. Database routines.

Routine
Description
gethostbyaddr Get host information corresponding to an address.
gethostbyname Get host information corresponding to a hostname.
gethostname Return the standard host name for the local machine.
getprotobyname Get protocol information corresponding to a protocol name.
getprotobynumber Get protocol information corresponding to a protocol number.
getservbyname Get service information corresponding to a service name and protocol.
getservbyport Get service information corresponding to a port and protocol.

Table 4.3. Windows extensions.

Extension
Description
WSAAsyncGetHostByAddr Get host information corresponding to an address (asynchronous version).
WSAAsyncGetHostByName Get host information corresponding to a hostname (asynchronous version).
WSAAsyncGetProtoByNumber Get protocol information corresponding to a protocol number (asynchronous version).
WSAAsyncGetProtoByName Get protocol information corresponding to a protocol name (asynchronous version).
WSAAsyncGetServByPort Get service information corresponding to a port and protocol (asynchronous version).
WSAAsyncGetServByName Get service information corresponding to a service name and port (asynchronous version).
WSAAsyncSelect Request event notification for a socket.
WSACancelAsyncRequest Cancel an incomplete asynchronous operation.
WSACancelBlockingCall Cancel a blocking call that is currently in progress.
WSASetBlockingHook Establish an application-specific blocking hook function.
WSAUnhookBlockingHook Restore the default blocking hook function.
WSAGetLastError Get the error status for the last operation that failed.
WSASetLastError Set the error code, which can be retrieved by WSAGetLastError().
WSAIsBlocking Determine whether a blocking call is in progress.
WSAStartup Start a Windows sockets session.
WSACleanup Terminate use of the Windows Sockets DLL.

These APIs are well documented in the Windows Sockets Specification found in the Microsoft Development Library. Although, as a plug-in developer, you might never need to make a single socket call, Netscape Navigator is using sockets on your plug-in's behalf.

Tip
The Socket Spy application (which is covered in Chapter 24, "Debugging with the Socket Spy") enables you to see data sent via sockets from any socket-based application. This can be helpful while debugging your plug-in.

Communication with Netscape

In many cases, socket-based helper applications have no communication with Netscape's Navigator. They are simply started by the Navigator and run as a totally separate program. Helpers that want tighter integration with Navigator use Dynamic Data Exchange (DDE) or Object Linking and Embedding (OLE). Applications using these interfaces might be able to avoid direct socket programming. Both interfaces are known as the Netscape Client API (NCAPI).

Note
This is not part of the Netscape plug-in API that you probably have bought this book to learn about. Just glance over this material, and you'll see the similarities to the plug-in API covered in Chapters 8 through 15.

Netscape's DDE Implementation

Navigator 1.x and above support DDE for communication with external programs. The external application must be both a DDE server and DDE client to take advantage of all DDE topics supported by Navigator. The DDE interface provides things such as getting URLs, progress notification, Navigator window information, and viewer registration. These DDE topics are shown in Table 4.4.

Table 4.4. Navigator DDE topics.

Topic
Description
WWW_Activate Activate a Navigator window.
WWW_Alert Display an alert box.
WWW_BeginProgress Start sending progress topics.
WWW_CancelProgress Notify Navigator to cancel download associated with the transaction.
WWW_EndProgress Indicate that loading of the URL is complete.
WWW_Exit Cause Navigator to attempt to exit.
WWW_GetWindowInfo Get information about a Navigator window.
WWW_ListWindows List all Navigator windows.
WWW_MakingProgress Describe current progress with a range value.
WWW_OpenURL Open a URL.
WWW_ParseAnchor Resolve a relative URL.
WWW_QueryURLFile Return the URL from which a file was loaded.
WWW_QueryViewer Query a viewer with a filename.
WWW_RegisterProtocol Register a custom protocol.
WWW_RegisterURLEcho Register a notification for URL loading.
WWW_RegisterViewer Register a viewer with a MIME type.
WWW_RegisterWindowChange Register a notification when a given Navigator window changes.
WWW_SetProgressRange Set the progress range for progress messages.
WWW_ShowFile Have Navigator load a file.
WWW_UnRegisterProtocol Unregister the protocol registered with WWW_RegiesterProtocol.
WWW_UnRegisterURLEcho Unregister echo registered with WWW_RegisterURLEcho.
WWW_UnRegisterViewer Unregister viewer registered with WWW_RegisterViewer.
WWW_UnRegisterWindowChange Unregister window changes registered with WWW_RegisterWindowChange.
WWW_URLEcho Navigator loaded a URL.
WWW_Version Check Navigator DDE version.
WWW_ViewDocFile A file for a registered viewer.
WWW_WindowChange A given Navigator window has changed.

Full documentation for Navigator DDE implementation can be found at the following address:

http://home.netscape.com/newsref/std/ddeapi.html

OLE Automation in Netscape

For Windows interprocess communication between Netscape's Navigator and an external application, OLE is the preferred mechanism. OLE is a bit more complicated to understand than DDE, but it provides tighter integration and a more object-oriented approach.

OLE automation is implemented in network, registry, viewer, and protocol objects. Of these, network and registry are implemented by the Navigator, while viewer and protocol are handled by the external application. Looking at the methods of these automation objects (shown in Tables 4.5 through 4.8), you see a superset of the DDE implementation.

Table 4.5. Network methods.

Method
Description
BytesReady Inform the caller of the number of bytes prepared to be read.
Close Disconnect any active connection and reset the Netscape.Network.1 object.
GetContentEncoding Return the MIME encoding of the current load.
GetContentLength Return the content length (total amount of bytes) of the current load.
GetContentType Return the MIME type of the current load.
GetErrorMessage Provide the caller with the Netscape internally generated error message.
GetExpires Return when the data retrieved by this load is no longer considered valid.
GetFlagFancyFTP Return whether or not FTP output will contain listing information such as file size, file type, and so on.
GetFlagFancyNews Return whether or not Newsgroup listings will contain only newsgroups with an informative description.
GetFlagShowAllNews Return whether or not all news articles will be listed.
GetLastModified Return when the data retrieved by this load was last modified.
GetPassword Determine the currently set password.
GetServerStatus Determine the error status reported by the server.
GetStatus Query the status of the current load.
GetUsername Determine the currently set username.
IsFinished Determine whether a load is complete.
Open Initiate the retrieval of a URL from the network.
Read Read data retrieved by Netscape.
Resolve Generate an absolute (fully qualified) URL.
SetFlagFancyFTP Inform Netscape whether or not FTP file listings will contain information such as file size, file type, and so on.
SetFlagFancyNews Inform Netscape whether or not Newsgroup listings should also contain descriptions.
SetFlagShowAllNews Inform Netscape whether or not all news articles will be listed when invoking Open with a news URL.
SetPassword Set the current password in the Netscape.Network.1 object.
SetUsername Set the current username in the Netscape.Network.1 object.

Table 4.6. Registry methods.

Method
Description
RegisterViewer Register an OLE automation server to act as a streaming viewer for a particular MIME type.
RegisterProtocol Register an OLE automation server to act as a protocol handler for a particular protocol type.

Table 4.7. Viewer methods.

Method
Description
Close Inform the viewer that the download is now complete.
Initialize Initialize the external streaming viewer.
Ready Query the external streaming viewer about how much data it is ready to handle.
Write Stream data to the external streaming viewer.

Table 4.8. Protocol methods.

Method
Description
Initialize Initialize an external protocol handler to receive a URL.
Open Inform the external protocol handler that it should open a URL.

What All of This Means to Plug-Ins

That is a lot of APIs, topics, and methods, but how do plug-ins fit into this picture? Plug-ins are code modules that become part of Netscape's Navigator. They are not separate applications such as helpers. Therefore, a large percentage of the subjects covered in this chapter are not used by a plug-in. That doesn't mean you can't use these techniques, but it will be a rare occasion when you do.

Conclusion

That was sure a quick tour of some serious information. Keep in mind that the techniques presented in this chapter are mainly for Netscape helper applications, not plug-ins. Plug-ins, a newer method of handling unknown media types within Netscape's Navigator, use a direct binary interface through the Navigator plug-in API.

What's Next?

The next chapter examines using a plug-in in a Web page with the appropriate HTML code. The RealAudio plug-in is used to show how the HTML EMBED tag works with a plug-in and how to extend EMBED tag attributes. This plug-in makes good use of a number of attributes to customize the look and feel of the Web page user interface.