home *** CD-ROM | disk | FTP | other *** search
-
- /* C O P Y R I G H T N O T I C E : */
- /* Copyright 1986 Eric Jul. May not be used for any */
- /* purpose without written permission from the author. */
-
- /* File: /usr/em/Kernel/KmdOps/kmdMM.c Date: 1986-05-01 */
-
- /*
- * $Header$
- * INTERFACE:
- *
- * FUNCTION: Takes care of sending messages for KMD.
- * Messages are sent using reliable byte streams using the
- * TCP/IP protocol.
- *
- * IMPORTS: malloc, free, sys/types.h, sys/cmuipc.h, errno.h,
- * QueueTask, PutEdenMsg, HOTSSearchPtr,
- * /u1/Eden/ErrCodes/MMcodes.h,
- * /u1/Eden/ErrCodes/SYScodes.h,
- * /u1/Eden/Kernel/MsgOps/Types.h,
- * /u1/Eden/Kernel/MsgOps/BufferTypes.h,
- * /u1/Eden/Kernel/MsgOps/FifoTypes.h,
- * /u1/Eden/Kernel/MsgOps/mmMsgTypes.h,
- * /u1/Eden/Kernel/ProcessA/Events.h,
- *
- * EXPORTS: KMDReceive, KMDSend, KMDBuildMsg, KMDSendBlock
- *
- * DESIGN: A lot of code copied from the Message Module, (msgCode.c).
- *
- * $Log$
- * 1986-05-01 Rewritten to use TCP/IP for ULTRIX 32m (and Berkeley 4.2bsd).
- * 1985-02-21 Changed pack and unpack to allow for a string of bytes
- * instead of just a string of char.
- * Added KMDSendBlock.
- * 1983-06-08 Initial Implementation, Eric Jul.
- */
- #include <sys/types.h>
-
- #undef integer
- #include <errno.h>
-
- extern errno; /* IPC status if call returns -1; NB: not set upon success!*/
-
- extern char *malloc();
- extern int free();
- extern void perror();
-
- extern int DisplayEdenMsg(),
- GetEdenMsg(),
- PutEdenMsg();
-
- #include "Kernel/h/mmCodes.h"
- #include "Kernel/h/unixCodes.h"
-
- #include "Kernel/h/mmTypes.h"
- #include "Kernel/h/mmMsgDefs.h"
- #include "Kernel/h/mmMsgTypes.h"
- #include "Kernel/h/kmdDefs.h"
-
- /* Define KMD test output flag. */
- int KMDTest = 0;
-
- /* InvByteOrder is a macro that given a byte index produces a corresponding
- index for grouping into 4-byte integers. */
- #define InvByteOrder(i) (i+3-2*(i%4))
-
-
-
- /************************************************************/
- /* */
- /* KMDPack */
- /* */
- /* KMDPack takes three data items and packs them into a */
- /* KMDData packet. */
- /* */
- /************************************************************/
-
- void KMDPack(fType, fInt, fString, fMsgPtr)
-
- int fType;
- int fInt;
- char *fString;
- KMDMsgPtr fMsgPtr;
-
- {
- register char *src, *dest;
- if (KMDTest) printf("KMDPack (%d, %d, %s)\n", fType, fInt, fString);
- fMsgPtr->data.MsgType = fType;
- fMsgPtr->data.MsgInt = fInt;
- src = fString;
- dest = &fMsgPtr->data.MsgString[0];
- while (*dest++ = *src++);
- fMsgPtr->totalSize = dest - (char *) (& fMsgPtr->totalSize);
-
- if ( KMDTest )
- printf(" Packed total size %d\n", fMsgPtr->totalSize);
- }
-
- #ifdef NOTIMPLEMENTED
-
- /************************************************************/
- /* */
- /* KMDPackBlock */
- /* */
- /* KMDPack packs a block into a KMDData packet. */
- /* */
- /************************************************************/
-
- void KMDPackBlock(fType, fSize, fBlock, fMsgPtr)
-
- int fType;
- int fSize;
- char *fBlock;
- KMDDataPtr fMsgPtr;
- {
- if (KMDTest) { register int i;
- printf("KMDPackBlock (%d, %d, %d)", fType, fSize, fMsgPtr);
- for (i=0; i < fSize; i++){
- printf("%s%02x", (i % (8*4) ? (i % 4 ? "" : " ") : "\n"),
- (unsigned char) fBlock[InvByteOrder(i)]);
- };
- printf("\n");
- }
- fMsgPtr -> MsgType = fType;
- fMsgPtr -> MsgInt = fSize;
- bcopy(fBlock, (char *) fMsgPtr->MsgString, fSize);
- }
- #endif NOTIMPLEMENTED
-
- /************************************************************/
- /* */
- /* KMDSend */
- /* */
- /* KMDSend packages and sends the three data via IPC to */
- /* specified port. */
- /* */
- /************************************************************/
-
-
- KKStatus KMDSend(fDestSock, fType, fInt, fString)
- int fDestSock, fType, fInt;
- char *fString;
- {
- KMDMsg msg;
- int err;
- if(KMDSTDOUTSOCK == fDestSock) {
- printf("%s", fString);
- (void) fflush(stdout);
- return(MMSS_Success);
- }
- KMDPack(fType, fInt, fString, &msg);
- do {
- err = send(fDestSock, (char *) &msg, msg.totalSize, 0);
- } while ((err < 0) && (errno == EINTR));
- if (err < 0){
- if (KMDTest || 1) perror("KMDShip: send");
- return ( mSystemError( errno ) );
- };
- return MMSS_Success;
- }
-
- #ifdef NOTIMPLEMENTED
- /************************************************************/
- /* */
- /* KMDSendBlock */
- /* */
- /* KMDSend packages and sends a block of bytes via IPC to */
- /* the specified port. */
- /* */
- /************************************************************/
-
-
- KKStatus KMDSendBlock(fSrcPort, fDestPort, fType, fSize, fBlock, fMsgPrio)
-
- localport fSrcPort;
- localport fDestPort;
- int fType;
- int fSize;
- char *fBlock;
- int fMsgPrio;
-
- {
- int Size;
- Message(MAXMESSAGESIZE) Msg;
- MessageHeaderPtr MsgHdrPtr;
- KMDDataType Data1;
- KMDDataPtr Data1Ptr;
-
-
- Data1Ptr = &Data1;
-
- if (KMDTest)
- printf("KMDSendBlock\n");
-
- KMDPackBlock(fType, fSize, fBlock, Data1Ptr);
- /*
- KMDBuildMsg(fSrcPort, fDestPort, Data1Ptr, fSize+2*(sizeof (int)), &Msg);
- */
- return ());
- }
-
- #endif NOTIMPLEMENTED
-
- /************************************************************/
- /* */
- /* KMDUnpack */
- /* */
- /* Unpack a KMD data packet into the components. */
- /* */
- /************************************************************/
-
- void KMDUnpack(fMsgPtr, fType, fInt, fString)
-
- KMDMsgPtr fMsgPtr;
- int *fType;
- int *fInt;
- char fString[];
-
- {
- register char *src, *dest;
- if(KMDTest) printf("Unpack total length %d\n", fMsgPtr->totalSize);
- *fType = fMsgPtr->data.MsgType;
- *fInt = fMsgPtr->data.MsgInt;
- src = &fMsgPtr->data.MsgString[0];
- dest = fString;
- while (*dest++ = *src++);
- if ( KMDTest) {
- printf("KMDUnpack: type=%d, Int=%d\n", *fType, *fInt);
- #ifdef NOTIMPLEMENTED
- if (fMsgPtr->MsgType >= KMDCBYTES) {
- /* Contains raw bytes instead of a string. */
- register int i;
- printf("Block:");
- for (i=0; i < *fInt; i++) {
- printf("%s%02x", i % (8*4) ? i % 4 ? "" : " " : "\n",
- (unsigned char) (fMsgPtr -> MsgString[InvByteOrder(i)]));
- };
- printf("\n");
-
- } else
- #endif NOTIMPLEMENTED
- printf(" String=#%s#\n", fString);
- }
- }
-
- /************************************************************/
- /* */
- /* KMDReceive */
- /* */
- /* Synchronously receive a KMD message on the DATAPORT */
- /* of the process. Unpack it and return the components. */
- /* */
- /************************************************************/
-
- KKStatus KMDReceive(fRemotePort, fType, fInt, fString)
-
- int fRemotePort;
- int *fType;
- int *fInt;
- char fString[];
- {
- KMDMsg msg;
- int noerror;
- KKStatus status;
-
- do {
- noerror = recv(fRemotePort, (char *) &msg.totalSize, 4, 0);
- } while ( (noerror < 0 ) && (errno == EINTR) );
- if (noerror < 0) {
- if (KMDTest) perror("KMDReceive: recv");
- return (mSystemError(errno));
- };
- if (KMDTest) printf("KMDReceive msgSize = %d\n", msg.totalSize);
- if ((msg.totalSize >= 5000) || (msg.totalSize < 5) ) return(MMSF_MsgOvfl);
- do {
- noerror = recv( fRemotePort, (char *) &msg.data, msg.totalSize-4, 0);
- if ( KMDTest ) printf("Receive result: %d %d\n", noerror,
- noerror < 0 ? errno : 0);
- if ( noerror < 0 ) {
- if (KMDTest) perror("KMDReceive: recv");
- status = mSystemError(errno);
- } else if ( noerror < 8 ) {
- /* Too few bytes read */
- status = MMSF_MsgOvfl;
- } else status = MMSS_Success;
-
- if ( mSUCCESS(status) ) {
- KMDUnpack(&msg, fType, fInt, fString);
- }
- } while (status == SYSK_EINTR);
- return (status);
- }
-
- /* C O P Y R I G H T N O T I C E : */
- /* Copyright 1986 Eric Jul. May not be used for any */
- /* purpose without written permission from the author. */
-
-