Preparing for a Session
To communicate, you can open a port for your application and make it
available to receive session requests, to initiate sessions, or both. Applications
that are able to receive session requests can choose to accept or reject
incoming session requests.
Before an application can accept and establish a session with another
application, the PPC Toolbox authenticates the initiating user (unless guest access is enabled or the applications are located on the same computer). Once a
session begins, the two applications can exchange data with each other.
Initiating a PPC Session
Once you have established the name and the location of the port that you want
to communicate with, you can initiate a session. You can use either the
the user's screen to identify each user who requests a session. You may prefer
to use the PPCStart function for low-level code such as that used for drivers, which typically do not provide a user interface. You may also prefer to use
PPCStart when the application you are initiating a session with does not return information about whether a particular port requires authentication.
that is running in the background, since it requires that several dialog boxes
appear on the user's screen.
identify each user who requests a session. This function combines the
processes of prompting for user name and password and initiating a session
into one synchronous procedure call. If authentication fails, the
function to use the default user identity ( described later in this section). If the
function displays a dialog box to allow a user to log on. The following picture
shows the user identity dialog box.
The user identity dialog box
specify a line of text that the dialog box can display. The allowGuest parameter
specifies whether to enable the Guest radio button. If a port requires
authentication, you should set this parameter to FALSE. The userName parameter specifies the name of the user who is attempting to initiate a session. If the user name is not specified, the user identity dialog box
appears on the user's screen with the owner name provided from the Sharing
Setup control panel.
displays the dialog box shown in the next picture:
The incorrect password dialog box
After the user clicks OK, the user identity dialog box reappears in the
foreground so that the user can enter the password again.
dialog box shown in the following picture:
The invalid user name dialog box
After the user clicks OK, the user identity dialog box reappears so that the
user can enter a new user name.
session is initiated or the user clicks Cancel in the user identity dialog box. If a
user reference number in the corresponding field in the PPCStart parameter block. The user reference number represents the user name and
password. A user reference number of 0 indicates that a session has been
initiated with guest access.
Before your application quits, you need to invalidate all user reference
default user reference number and the guest reference number (0).
function to establish an authenticated session. This listing shows only one
session, although your application may conduct multiple sessions at one time.
// Using the StartSecureSession function to establish a session
// Assuming inclusion of macheaders
#include <PPCToolBox.h>
// Prototype code like this prior to calling it:
OSErr MyStartSecureSession
(PortInfoPtr,
OSErr MyStartSecureSession
(PortInfoPtr thePortInfoPtr,
long *theUserRefNum,
long [TOKEN:10868]heRejectInfo,
{
thePPCStartPBRec.ioCompletion = NULL;
thePPCStartPBRec.portRefNum = thePortRefNum;
// from the PPCOpen function
thePPCStartPBRec.serviceType = ppcServiceRealTime;
thePPCStartPBRec.resFlag = 0;
thePPCStartPBRec.portName = &thePortInfoPtr->name;
thePPCStartPBRec. locationName = theLocationNamePtr;
thePPCStartPBRec.userData = 0;
// try to connect with default user identity
// highlight the Guest button appropriately
allowGuest = !thePortInfoPtr->authRequired;
allowGuest, guestSelected, NULL);
if ( err == noErr ) {
*theSessRefNum = thePPCStartPBRec.sessRefNum;
}
else if (err == userRejectErr)
// return rejectInfo from the PPCReject function *theRejectInfo = thePPCStartPBRec.rejectInfo;
return err;
}
For low-level code such as that used for drivers (which typically do not
provide a user interface), you can use the PPCStart function instead of the IPCListPorts function (instead of displaying the program linking dialog box) to obtain a list of ports.
If the authRequired field of the port information record contains FALSE, the port allows guest access. If the authRequired field of the port information
record contains TRUE, use the PPCStart function and the user reference reestablish an authenticated session.
You can also attempt to log on as the default user using the GetDefaultUser function to obtain the default user reference number and the default userName. The default userName is established after the owner starts up the computer. The userRef parameter is a reference number that represents the user name
and password of the default user. The userName parameter contains the owner
name that is specified in the Sharing Setup control panel.
identity does not exist (no name is specified in the Sharing Setup control
panel) or the user is not currently logged on.
The following program illustrates how you use the PPCStart function to initiate a session. The PPCStart function uses the port information record and the location name record to attempt to open a session with the selected PPC
port.
// Initiating a session using the PPCStart function
// Assuming inclusion of MacHeaders
#include <PPCToolBox.h>
// Prototype your function like this prior to calling it
OSErr MyPPCStart (PortInfoPtr thePortInfoPtr, long *theUserRefNum,
long *theRejectInfo)
{
thePPCStartPBRec.ioCompletion = NULL;
thePPCStartPBRec.portRefNum = thePortRefNum;
thePPCStartPBRec.serviceType = ppcServiceRealTime;
thePPCStartPBRec.resFlag [TOKEN:15648]0;
thePPCStartPBRec.portName = &thePortInfoPtr->name;
// destination port
thePPCStartPBRec. locationName = theLocationNamePtr;
// destination location
// application-specific data for PPCInform
thePPCStartPBRec.userData = 0;
if ( err != noErr )
if ( thePortInfoPtr->authRequired && ! thePPCStartPBRec.userRefNum )
// port selected does not allow guests and you do not have a default
// user reference number, so you cannot log on to this port
err = authFailErr;
else
// attempt to log on
if ( err == noErr ) {
*theSessRefNum = thePPCStartPBRec.sessRefNum;
}
else if ( err == userRejectErr )
// return rejectInfo from the PPCReject function
*theRejectInfo = thePPCStartPBRec.rejectInfo;
return err;
}
The port to which you wish to connect must have an outstanding PPCInform function to successfully start a session. You cannot initiate a session with a
port that is not able to receive session requests.
If the port is open, has an outstanding PPCInform posted, and accepts your session request, the PPCStart function returns a noErr result code and a valid session reference number. This session reference number is used to
identify the session during the exchange of data.