Exemple #1
0
long SDMmessage::SendTCP(long ip_addr,long port)
{
	int sock;
	long i;
	long result;
	char buf[3*BUFSIZE];
	char ack[16];
	short error = 0;

	//fill buffer
	i = Marshal(buf);
	if(i < 0)
		return SDM_MESSAGE_SEND_ERROR;
	//send message
	sock = TCPconnect(ip_addr,port);
  if (sock != IP_SOCK_INVALID)
  {
	  result = TCPsend(sock,buf,i);
	  if(buf[0] == SDM_xTEDS || buf[0] == SDM_CancelxTEDS)
	  {
		  TCPrecv(sock,ack,13);
		  error = GET_SHORT(&ack[HEADER_SIZE]);
		  if(error < 0)
      {
			  result = error;
      }
	  }
	  TCPclose (sock);
#ifdef BUILD_WITH_MESSAGE_LOGGING
	  Logger.MessageSent(*this);
#endif
  }
	return result;
}
Exemple #2
0
/*
    Forward a previously received message unchanged (i.e. without changing the timestamp).

    Implementation notes:  Currently, only messages of maximum size BUFSIZE can be forwarded.
    The only message that won't send as expected is SDMxTEDS.  The reason for this is that
    the DM is the only application that should ever receive SDMxTEDS, and it will never use
    this function.

    Params:
    	destination - The component id to which to forward the message
    Returns:
    	long - The length of the message sent or -1 to indicate failure
*/
long SDMmessage::Forward(const SDMComponent_ID& destination)
{
	char buf[BUFSIZE];

	if (MsgName == SDM_xTEDS)
		return -1;

	// Save the old timestamp
	long OldSeconds = sec;
	long OldSubSeconds = subsec;

	// Marshal the message, this call will assign a new timestamp
	const long MessageLength = Marshal(buf);
	if (MessageLength < 0)
		return MessageLength;

	// Reset the old timestamp
	sec = OldSeconds;
	subsec = OldSubSeconds;

	// Remarshal the header, overwriting previous data, body of message is still intact
	MarshalHeaderOldTimeStamp(buf);

	// Now send the message to the destination component id
	int sock = UDPconnect(destination.getAddress(), destination.getPort());
	if (sock < 0)
		return -1;
	long SendResult = UDPsend(sock, buf, MessageLength);
	UDPclose(sock);

#ifdef BUILD_WITH_MESSAGE_LOGGING
	Logger.MessageSent(*this);
#endif
	return SendResult;
}
Exemple #3
0
long SDMmessage::SendReplyTo(int socket, const struct sockaddr_in* sin, bool tcp)
{
	long i;
	long result;
	char buf[BUFSIZE];

	if(socket == IP_SOCK_INVALID) //This is a bad socket descriptor and no message should be sent
		return 0;

	i = Marshal(buf);
	if (i < 0)
		return SDM_MESSAGE_SEND_ERROR;

	if(tcp)
		result = TCPserv_replyto(socket,buf,i,sin);
	else
		result = UDPserv_replyto(socket,buf,i,sin);

	if(result < 0)
	{
#ifndef WIN32
		perror("Unable to send reply message: ");
#else
		printf("Unable to send reply message: %d\n",WSAGetLastError());
#endif

		return SDM_MESSAGE_SEND_ERROR;
	}
#ifdef BUILD_WITH_MESSAGE_LOGGING
	Logger.MessageSent(*this);
#endif
	return i;
}
Exemple #4
0
int
main( int argc, char *argv[] )
{
  tUint8         value[4]  = { 0x42, 0X43, 0X44, 0X45 };
  tUint8         result[4] = { 0x11, 0x12, 0x13, 0x14 };
  unsigned char  buffer[256];

  unsigned int s1 = Marshal( &UserDefinedType, value, buffer );

  if ( s1 != 3 )
       return 1;

  unsigned int s2 = Demarshal( MarshalByteOrder(),
                               &UserDefinedType, result, buffer );

  if ( s2 != 3 )
       return 1;

  if ( value[0] != result[0] )
       return 1;

  if ( value[1] != result[1] )
       return 1;

  if ( value[2] != result[2] )
       return 1;

  if ( result[3] != 0x14 )
       return 1;

  return 0;
}
Exemple #5
0
AJ_Status AJ_MarshalArg(AJ_Message* msg, AJ_Arg* arg)
{
    AJ_Status status;
    AJ_IOBuffer* ioBuf = &msg->bus->sock.tx;
    uint8_t* argStart = ioBuf->writePtr;

    if (msg->varOffset) {
        /*
         * Marshaling a variant - get the signature from the I/O buffer
         */
        const char* sig = (const char*)(argStart - msg->varOffset);
        msg->varOffset = 0;
        status = Marshal(msg, &sig, arg);
    } else if (msg->outer) {
        /*
         * Marshaling a component of a container use the container's signature
         */
        const char* sig = msg->outer->sigPtr;
        if (!*sig) {
            return AJ_ERR_END_OF_DATA;
        }
        status = Marshal(msg, &sig, arg);
        /*
         * Only advance the signature for struct elements
         */
        if (msg->outer->typeId != AJ_ARG_ARRAY) {
            msg->outer->sigPtr = sig;
        }
    } else {
        const char* sig = msg->signature + msg->sigOffset;
        /*
         * Marshalling anything else use the message signature
         */
        if (!*sig) {
            return AJ_ERR_END_OF_DATA;
        }
        status = Marshal(msg, &sig, arg);
        msg->sigOffset = (uint8_t)(sig - msg->signature);
    }
    if (status == AJ_OK) {
        msg->bodyBytes += (uint16_t)(ioBuf->writePtr - argStart);
    } else {
        AJ_ReleaseReplyContext(msg);
    }
    return status;
}
Exemple #6
0
int
HpiMarshalReply( cHpiMarshal *m, void *buffer, const void **params )
{
  // the first value is the result.
  SaErrorT err = *(const SaErrorT *)params[0];

  if ( err == SA_OK )
       return MarshalArray( m->m_reply, params, buffer );

  return Marshal( &SaErrorType, &err, buffer );
}
Exemple #7
0
/*
 * Manual page at process.def
 */
INT32 CGEN_PUBLIC CProcess::Start()
{
  const SMic* pMic    = NULL;                                                   // Method invocation context of Start()
  CFunction*  iCaller = NULL;                                                   // Function calling Start()
  CFunction*  iFnc    = NULL;                                                   // Process function
  StkItm*     pStkItm = NULL;                                                   // Stack item
  INT32       nArgs   = 0;                                                      // Number of process function arguments

  // Validate and initialize                                                    // ------------------------------------
  if (m_nState!=0)                                                              // Not virginal
    return IERROR(this,PRC_CANTSTART,"multiple starts not allowed",0,0);        //   Forget it!
  if (!(pMic = CDlpObject_MicGet(_this))) return -1;                            // Get method invocation context
  iCaller = (CFunction*)CDlpObject_OfKind("function",pMic->iCaller);            // Get calling CFunction
  if (!iCaller) return -1;                                                      // Must be a function!

  // Initialize process                                                         // ------------------------------------
  sprintf(m_psTmpFile,"%s%ld",dlp_tempnam(NULL,"~dLabPro#process#"),(long)dlp_time());// Initialize temp. file name prefix

  // Marshal arguments                                                          // ------------------------------------
  if (!(pStkItm=iCaller->StackGet(0))) return IERROR(this,PRC_TOOFEWARGS,0,0,0);// Get stack top
  if (pStkItm->nType==T_INSTANCE)                                               // Stack top is an instance
    iFnc = (CFunction*)CDlpObject_OfKind("function",pStkItm->val.i);            //   Get function to be called
  if (iFnc)                                                                     // This process is a function call
  {                                                                             // >>
    IFIELD_RESET(CDlpObject,"dto");                                             //   Create data transfer object
    nArgs = CData_GetNRecs(iFnc->m_idArg);                                      //   Get number of function arguments
    Marshal(m_iDto,iCaller,nArgs);                                              //   Marshal arguments for transfer
  }                                                                             // <<
  else                                                                          // This process is a program call
    dlp_strcpy(m_psCmdLine,iCaller->PopString(0));                              //   Get program command line

#ifdef USE_FORK
  if (iFnc)                                                                     // This process is a function call
  {                                                                             // >>
    m_hPid=fork();                                                              //   Fork the process
    if(m_hPid>0){                                                               //   Parent process >>
      m_nState |= PRC_DATASENT;                                                 //     Remember data have been sent
      m_nState |= PRC_RUNNING;                                                  //     Set running flag
      m_hThread = 0;                                                            //     Clear thread handle
      return O_K;                                                               //     Everything is fine
    }                                                                           //   <<
    if(m_hPid==0) return DoJobFork(iCaller,iFnc);                               //   The child process runs the function
    return IERROR(this,PRC_CANTSTART,"fork() failed",0,0);                      //   On error (fid<0) we return
  }                                                                             // <<
#endif
  // Start job in watcher thread                                                // ------------------------------------
  m_hPid = 0;                                                                   // Reset process id
  SendData();                                                                   // Send transfer data
  m_hThread = dlp_create_thread(DoJob,this);                                    // Do the job and watch it

  return O_K;                                                                   // Yo!
}
Exemple #8
0
void CachedObjectMgr::GetCacheFileName(PyRep *key, std::string &into)
{
    Buffer data;
    Marshal( key, data );

    Base64::encode( &data[0], data.size(), into, false );

    std::string::size_type epos = into.find('=');
    if(epos != std::string::npos)
        into.resize(epos);

    into += ".cache";
}
Exemple #9
0
int CMarshalled::Send(DPNID idTo, DWORD flags)
{
	CDarkMarshalBuffer buff;

	buff.UseDefaultBuffer();
	Marshal(buff);

	int bytes = buff.GetBytesWritten();

	if (g_pDarkNet)
		g_pDarkNet->Send(idTo, buff.GetStartBuffer(), bytes, NULL, flags);

	return bytes;
}
void RunThreadedConsole(HWND excel)
{
	if (hWndConsole)
	{
		::ShowWindow(hWndConsole, SW_SHOWDEFAULT);
		::SetWindowPos(hWndConsole, HWND_TOP, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE);
	}
	else
	{
		DWORD dwID;

		Marshal(); // FIXME: do this elsewhere, and once
		UpdateWordList();
		::CreateThread(0, 0, ThreadProc, excel, 0, &dwID);
	}

}
Exemple #11
0
Error SNMPVarbindList::Marshal(std::vector
																	 <Byte> &to) {
	to.push_back(type());

	// get the length of the elements
	int len = 0;
	for (auto it = varbinds_.begin(); it != varbinds_.end(); it++) {
		len += it->length();
	}
	to.push_back(len);

	for (auto it = varbinds_.begin(); it != varbinds_.end(); it++) {
		it->Marshal(to);
	}

	return Error::None;
}
Exemple #12
0
int
MarshalArray( const cMarshalType **types, 
	      const void **data, void *b )
{
  int            i;
  int            size = 0;
  unsigned char *buffer = b;

  for( i = 0; types[i]; i++ )
     {
       int s = Marshal( types[i], data[i], buffer );

       if ( s < 0 )
	    return -1;

       size   += s;
       buffer += s;
     }

  return size;
}
Exemple #13
0
int
main( int argc, char *argv[] )
{
  tInt8         value = 0xfa;
  tInt8         result;
  unsigned char buffer[256];

  unsigned int s1 = Marshal( &Marshal_Int8Type, &value, buffer );

  if ( s1 != sizeof( tInt8 ) )
       return 1;

  unsigned int s2 = Demarshal( G_BYTE_ORDER, &Marshal_Int8Type, &result, buffer );

  if ( s2 != sizeof( tInt8 ) )
       return 1;

  if ( value != result )
       return 1;

  return 0;
}
Exemple #14
0
int
main( int argc, char *argv[] )
{
  tUint64       value = 0xfedc12345678abcdLL;
  tUint64       result;
  unsigned char buffer[256];

  unsigned int s1 = Marshal( &Marshal_Uint64Type, &value, buffer );

  if ( s1 != sizeof( tUint64 ) )
       return 1;

  unsigned int s2 = Demarshal( MarshalByteOrder(), &Marshal_Uint64Type, &result, buffer );

  if ( s2 != sizeof( tUint64 ) )
       return 1;

  if ( value != result )
       return 1;

  return 0;
}
Exemple #15
0
int
main( int argc, char *argv[] )
{
  tFloat64      value = -47.345566;
  tFloat64      result;
  unsigned char buffer[256];

  unsigned int s1 = Marshal( &Marshal_Float64Type, &value, buffer );

  if ( s1 != sizeof( tFloat64 ) )
       return 1;

  unsigned int s2 = Demarshal( MarshalByteOrder(), &Marshal_Float64Type, &result, buffer );

  if ( s2 != sizeof( tFloat64 ) )
       return 1;

  if ( value != result )
       return 1;

  return 0;
}
Exemple #16
0
int
MarshalArray( const cMarshalType **types, const void **data, void *b )
{
  int            size = 0;
  unsigned char *buffer = b;

  int i;
  for( i = 0; types[i]; i++ )
     {
       int cc = Marshal( types[i], data[i], buffer );
       if ( cc < 0 )
	  {
	    CRIT( "MarshalArray[%d]: %s: failure, cc = %d!", i, types[i]->m_name, cc );
	    return cc;
	  }

       size   += cc;
       buffer += cc;
     }

  return size;
}
QStatus NativeAboutObject::GetAnnouncedAboutData(
    /* [in] */ ajn::MsgArg* msgArg)
{
    QStatus status = ER_FAIL;
    if (mAboutDataListenerRef != NULL) {
        AutoPtr<IMap> announceArg;
        ECode ec = mAboutDataListenerRef->GetAnnouncedAboutData((IMap**)&announceArg);
        // check for ErrorReplyBusException exception
        status = CheckForThrownException(ec);
        if (ER_OK == status) {
            if (Marshal("a{sv}", announceArg.Get(), msgArg) == NULL) {
                Logger::E("NativeAboutObject", "GetMsgArgAnnounce() marshaling error");
                return ER_FAIL;
            }
        }
        else {
            Logger::E("NativeAboutObject", "GetAnnouncedAboutData exception with status %s", QCC_StatusText(status));
            return status;
        }
    }
    return status;
}
Exemple #18
0
int
main( int argc, char *argv[] )
{
    tInt64        value = 0x1234567890abcdefLL;
    tInt64        result;
    unsigned char buffer[256];

    unsigned int s1 = Marshal( &Marshal_Int64Type, &value, buffer );

    if ( s1 != sizeof( tInt64 ) )
        return 1;

    unsigned int s2 = Demarshal( G_BYTE_ORDER, &Marshal_Int64Type, &result, buffer );

    if ( s2 != sizeof( tInt64 ) )
        return 1;

    if ( value != result )
        return 1;

    return 0;
}
Exemple #19
0
int
main( int argc, char *argv[] )
{
  tInt32         value = 0x42aa1234;
  tInt32         result;
  unsigned char  buffer[256];

  unsigned int s1 = Marshal( &Marshal_Int32Type, &value, buffer );

  if ( s1 != sizeof( tInt32 ) )
       return 1;

  unsigned int s2 = Demarshal( MarshalByteOrder(), &Marshal_Int32Type, &result, buffer );

  if ( s2 != sizeof( tInt32 ) )
       return 1;

  if ( value != result )
       return 1;

  return 0;
}
Exemple #20
0
//If addr is supplied, it must be in network byte order
long SDMmessage::SendBroadcast(long address, unsigned short dest_port)
{
	int i, result;
	char buf[BUFSIZE];
	long b_cast_address;
	if (address == 0)		//If no address was supplied
		b_cast_address = DataManager.getAddress();
	else
		b_cast_address = address;

	// DANGEROUS: assumes a little-endian machine and a /24 subnet!
	b_cast_address |= 0xFF000000;		//Set the broadcast address's last octet to 255 as xxx.xxx.xxx.255
	i = Marshal(buf);
	if (i < 0)
		return SDM_MESSAGE_SEND_ERROR;

	result = UDPsend_broadcast(b_cast_address, dest_port, buf, i);
	if (result < 0)
		return SDM_MESSAGE_SEND_ERROR;
#ifdef BUILD_WITH_MESSAGE_LOGGING
	Logger.MessageSent(*this);
#endif
	return i;
}
Exemple #21
0
static int
CheckValue( cTest *v, cTest *r )
{
  unsigned char buffer[256];
  unsigned int s1 = Marshal( &TestType, v, buffer );
  unsigned int s2 = Demarshal( MarshalByteOrder(), &TestType, r, buffer );

  if ( s1 != s2 )
       return 1;

  if ( v->m_pad1 != r->m_pad1 )
       return 1;

  if ( v->m_mod != r->m_mod )
       return 1;

  if ( v->m_pad2 != r->m_pad2 )
       return 1;

  if ( v->m_pad3 != r->m_pad3 )
       return 1;

  return 0;
}
Exemple #22
0
int
Marshal( const cMarshalType *type, const void *d, void *b )
{
  if ( IsSimpleType( type->m_type ) )
       return MarshalSimpleTypes( type->m_type, d, b );

  int                  size   = 0;
  const unsigned char *data   = d;
  unsigned char       *buffer = b;

  switch( type->m_type )
     {
       case eMtArray:
            {
              int i;

              //assert( IsSimpleType( type->m_u.m_array.m_type->m_type ) );

              for( i = 0; i < type->m_u.m_array.m_size; i++ )
                 {
                   int s = Marshal( type->m_u.m_array.m_type, data, buffer );

		   if ( s < 0 )
			return -1;

                   data   += s;
                   buffer += s;
                   size   += s;
                 }
            }
            break;

       case eMtStruct:
	    {
	      int i;
 
	      for( i = 0; type->m_u.m_struct.m_elements[i].m_type == eMtStructElement; i++ )
		 {
		   cMarshalType *struct_element = &type->m_u.m_struct.m_elements[i];
                   assert( struct_element->m_type == eMtStructElement );

                   cMarshalType *st_type = struct_element->m_u.m_struct_element.m_type;

                   int s = 0;

                   if ( st_type->m_type == eMtUnion )
                      {
                        // the mod must be before this entry.
                        // this is a limitation of demarshaling of unions
                        assert( st_type->m_u.m_union.m_offset < i );
			const cMarshalType *mod = FindUnionModifierType( type, st_type, data );

			if ( mod )
			   {
			     s = Marshal( mod, data + struct_element->m_u.m_struct_element.m_offset, buffer );

			     if ( s < 0 )
				  return -1;
			   }
			else
			     return -1;
                      }
                   else if ( st_type->m_type == eMtVarArray )
                      {
                        // the array size must be before this entry.
                        // this is a limitation of demarshaling of var arrays
                        assert( st_type->m_u.m_var_array.m_size < i );

			int array_size = FindArraySize( type, st_type, data );

                        // only simple types can be array elements
                        assert( IsSimpleType( st_type->m_u.m_var_array.m_type->m_type ) );

                        unsigned char *bb = buffer;
                        const unsigned char *dd = data + struct_element->m_u.m_struct_element.m_offset;
                        tUint32 j;

                        for( j = 0; j < array_size; j++ )
                           {
                             int si = Marshal( st_type->m_u.m_var_array.m_type,
					       dd, bb );

			     if ( si < 0 )
				  return -1;

                             bb += si;
                             dd += si;
                             s  += si;
                           }
                      }
                   else
		      {
                        s = Marshal( st_type, data + struct_element->m_u.m_struct_element.m_offset,
                                     buffer );

			if ( s < 0 )
			     return -1;
		      }

		   buffer += s;
                   size   += s;
		 }
	    }
	    break;

       case eMtUnion:
            assert( 0 );
	    return -1;

       case eMtUserDefined:
	    assert( type->m_u.m_user_defined.m_marshal );
	    size = type->m_u.m_user_defined.m_marshal( type, d, b, type->m_u.m_user_defined.m_user_data );
	    break;

       default:
            assert( 0 );
            return -1;
     }

  return size;
}
    .m_v1.Persistent       = TRUE,
    .m_v1.Oem              = 0,
    .m_pad2                = 48,
    .m_v2.IdrId            = 2,
    .m_v2.Persistent       = FALSE,
    .m_v2.Oem              = 3,
    .m_v3.IdrId            = 3,
    .m_v3.Persistent       = TRUE,
    .m_v3.Oem              = 2,
    .m_pad3                = 49
  };

  unsigned char *buffer = (unsigned char *)malloc(sizeof(value));
  cTest          result;

  unsigned int s1 = Marshal( &TestType, &value, buffer );
  unsigned int s2 = Demarshal( G_BYTE_ORDER, &TestType, &result, buffer );

  if ( s1 != s2 )
       return 1;

  if ( value.m_pad1 != result.m_pad1 )
       return 1;

  if ( !cmp_inventoryrec( &value.m_v1, &result.m_v1 ) )
       return 1;

  if ( value.m_pad2 != result.m_pad2 )
       return 1;

  if ( !cmp_inventoryrec( &value.m_v2, &result.m_v2 ) )
Exemple #24
0
static AJ_Status MarshalMsg(AJ_Message* msg, uint8_t msgType, uint32_t msgId, uint8_t flags)
{
    AJ_Status status = AJ_OK;
    AJ_IOBuffer* ioBuf = &msg->bus->sock.tx;
    uint8_t fieldId;
    uint8_t secure = FALSE;

    /*
     * Use the msgId to lookup information in the object and interface descriptions to
     * initialize the message header fields.
     */
    status = AJ_InitMessageFromMsgId(msg, msgId, msgType, &secure);
    if (status != AJ_OK) {
        return status;
    }

    AJ_IO_BUF_RESET(ioBuf);

    msg->hdr = (AJ_MsgHeader*)ioBuf->bufStart;
    memset(msg->hdr, 0, sizeof(AJ_MsgHeader));
    ioBuf->writePtr += sizeof(AJ_MsgHeader);

    msg->hdr->endianess = HOST_ENDIANESS;
    msg->hdr->msgType = msgType;
    msg->hdr->flags = flags;
    if (secure) {
        msg->hdr->flags |= AJ_FLAG_ENCRYPTED;
    }

    /*
     * The wire-protocol calls this flag NO_AUTO_START we toggle the meaning in the API
     * so the default flags value can be zero.
     */
    msg->hdr->flags ^= AJ_FLAG_AUTO_START;
    /*
     * Serial number cannot be zero (wire-spec wierdness)
     */
    do { msg->hdr->serialNum = msg->bus->serial++; } while (msg->bus->serial == 1);
    /*
     * Marshal the header fields
     */
    for (fieldId = AJ_HDR_OBJ_PATH; fieldId <= AJ_HDR_SESSION_ID; ++fieldId) {
        char typeId = TypeForHdr[fieldId];
        char buf[4];
        const char* fieldSig = &buf[2];
        AJ_Arg hdrVal;
        /*
         * Skip field id's that are not currently used.
         */
        if (typeId == AJ_ARG_INVALID) {
            continue;
        }
        InitArg(&hdrVal, typeId, NULL);
        switch (fieldId) {
        case AJ_HDR_OBJ_PATH:
            if ((msgType == AJ_MSG_METHOD_CALL) || (msgType == AJ_MSG_SIGNAL)) {
                hdrVal.val.v_objPath = msg->objPath;
            }
            break;

        case AJ_HDR_INTERFACE:
            hdrVal.val.v_string = msg->iface;
            break;

        case AJ_HDR_MEMBER:
            if (msgType != AJ_MSG_ERROR) {
                int32_t len = AJ_StringFindFirstOf(msg->member, " ");
                hdrVal.val.v_string = msg->member;
                hdrVal.len = (len >= 0) ? len : 0;
            }
            break;

        case AJ_HDR_ERROR_NAME:
            if (msgType == AJ_MSG_ERROR) {
                hdrVal.val.v_string = msg->error;
            }
            break;

        case AJ_HDR_REPLY_SERIAL:
            if ((msgType == AJ_MSG_METHOD_RET) || (msgType == AJ_MSG_ERROR)) {
                hdrVal.val.v_uint32 = &msg->replySerial;
            }
            break;

        case AJ_HDR_DESTINATION:
            hdrVal.val.v_string = msg->destination;
            break;

        case AJ_HDR_SENDER:
            hdrVal.val.v_string = AJ_GetUniqueName(msg->bus);
            break;

        case AJ_HDR_SIGNATURE:
            hdrVal.val.v_signature = msg->signature;
            break;

        case AJ_HDR_TIMESTAMP:
            if (msg->ttl) {
                AJ_Time timer;
                timer.seconds = 0;
                timer.milliseconds = 0;
                msg->timestamp = AJ_GetElapsedTime(&timer, FALSE);
                hdrVal.val.v_uint32 = &msg->timestamp;
            }
            break;

        case AJ_HDR_TIME_TO_LIVE:
            if (msg->ttl) {
                hdrVal.val.v_uint32 = &msg->ttl;
            }
            break;

        case AJ_HDR_SESSION_ID:
            if (msg->sessionId) {
                hdrVal.val.v_uint32 = &msg->sessionId;
            }
            break;

        case AJ_HDR_HANDLES:
        case AJ_HDR_COMPRESSION_TOKEN:
        default:
            continue;
        }
        /*
         * Ignore empty fields.
         */
        if (!hdrVal.val.v_data) {
            continue;
        }
        /*
         * Custom marshal the header field - signature is "(yv)" so starts off with STRUCT aligment.
         */
        buf[0] = fieldId;
        buf[1] = 1;
        buf[2] = typeId;
        buf[3] = 0;
        WriteBytes(msg, buf, 4, PadForType(AJ_ARG_STRUCT, ioBuf));
        /*
         * Now marshal the field value
         */
        Marshal(msg, &fieldSig, &hdrVal);
    }
    if (status == AJ_OK) {
        /*
         * Write the header length
         */
        msg->hdr->headerLen = (uint32_t)((ioBuf->writePtr - ioBuf->bufStart) - sizeof(AJ_MsgHeader));
        /*
         * Header must be padded to an 8 byte boundary
         */
        status = WritePad(msg, (8 - msg->hdr->headerLen) & 7);
    }
    return status;
}
Exemple #25
0
int
Marshal( const cMarshalType *type, const void *d, void *b )
{
  if ( IsSimpleType( type->m_type ) )
     {
       return MarshalSimpleType( type->m_type, d, b );
     }

  int                  size   = 0;
  const unsigned char *data   = d;
  unsigned char       *buffer = b;

  switch( type->m_type )
     {
       case eMtArray:
	    {
	      const size_t nelems = type->u.m_array.m_nelements;

	      size_t i;
	      for( i = 0; i < nelems; i++ )
		 {
	           const cMarshalType *elem = type->u.m_array.m_element;
	           const size_t elem_sizeof = type->u.m_array.m_element_sizeof;

		   int cc = Marshal( elem, data, buffer );
		   if ( cc < 0 )
		      {
			   CRIT( "Marshal: %s[%d]: failure, cc = %d!", type->m_name, i, cc );
			   return cc;
		      }

		   data   += elem_sizeof;
		   buffer += cc;
		   size   += cc;
		 }
	    }
	    break;

       case eMtStruct:
	    {
	      const cMarshalType *elems = &type->u.m_struct.m_elements[0];
	      size_t i;
	      for( i = 0; elems[i].m_type == eMtStructElement; i++ )
		 {
		   const cMarshalType *elem = elems[i].u.m_struct_element.m_element;
		   const size_t offset      = elems[i].u.m_struct_element.m_offset;
		   int cc = 0;

		   if ( elem->m_type == eMtUnion )
		      {
			const size_t mod2_idx = elem->u.m_union.m_mod_idx;
			const size_t mod2 = GetStructElementIntegerValue( type, mod2_idx, data );
			const cMarshalType *elem2 = GetUnionElement( elem, mod2 );
			if ( !elem2 ) {
 	                    CRIT( "Marshal: %s:%s: invalid mod value %u!",
			          type->m_name, elems[i].m_name, (unsigned int)mod2 );
			    return -EINVAL;
			}

			cc = Marshal( elem2, data + offset, buffer );
			if ( cc < 0 )
			   {
			     CRIT( "Marshal: %s:%s, mod %u: failure, cc = %d!",
			           type->m_name, elems[i].m_name, (unsigned int)mod2, cc );
			    return -EINVAL;
			   }
		      }
		   else if ( elem->m_type == eMtVarArray )
		      {
			const size_t nelems2_idx   = elem->u.m_var_array.m_nelements_idx;
			const cMarshalType *elem2  = elem->u.m_var_array.m_element;
			const size_t elem2_sizeof  = elem->u.m_var_array.m_element_sizeof;
			const size_t nelems2 = GetStructElementIntegerValue( type, nelems2_idx, data );

			// (data + offset ) points to pointer to var array content
			const unsigned char *data2;
			memcpy(&data2, data + offset, sizeof(void *));

			unsigned char *buffer2 = buffer;
			size_t i2;
			for( i2 = 0; i2 < nelems2; i2++ )
			   {
			     int cc2 = Marshal( elem2, data2, buffer2 );
			     if ( cc2 < 0 )
				{
			          CRIT( "Marshal: %s:%s[%d]: failure, cc = %d!",
			                 type->m_name, elems[i].m_name, i2, cc2 );
				  return cc2;
				}

			     data2   += elem2_sizeof;
			     buffer2 += cc2;
			     cc      += cc2;
			   }
		      }
		   else
		      {
			cc = Marshal( elem, data + offset, buffer );
			if ( cc < 0 )
			   {
			     CRIT( "Marshal: %s:%s: failure, cc = %d!",
			           type->m_name, elems[i].m_name, cc );
			     return cc;
			   }
		      }

		   buffer += cc;
		   size   += cc;
		 }
	    }
	    break;

       case eMtUserDefined:
	    {
	      tMarshalFunction marshaller = type->u.m_user_defined.m_marshaller;
	      void * user_data = type->u.m_user_defined.m_user_data;
	      size = marshaller ? marshaller( type, d, b, user_data ) : 0;
            }
	    break;

       default:
	    return -ENOSYS;
     }

  return size;
}
Exemple #26
0
long SDMmessage::SendTo(const SDMComponent_ID& destination)
{
	int sock;
	long i;
	long result;
	char buf[LARGE_MSG_BUFSIZE];
	char ack[16];
	long address = destination.getAddress();
	int timeout = 2000;   // 2 sec
	int count = 0;
	short error = 0;

	//fill buffer
	i = Marshal(buf);
	if(i < 0)
		return SDM_MESSAGE_SEND_ERROR;
#ifdef TCP_TRANSMIT_OF_LARGE_XTEDS
	if(i>BUFSIZE)
		return SendTCP(destination.getAddress(),destination.getPort());
#endif
	//send message
	sock = UDPconnect(address,destination.getPort());
  if (sock != IP_SOCK_INVALID) // cleanup issue 26
  {
	  result = UDPsend(sock,buf,i);
	  //Look for SDM_ACK from DM for certain messages
	  if(buf[0] == SDM_xTEDS || buf[0] == SDM_CancelxTEDS)
	  {
		  if(buf[0] == SDM_xTEDS)
		  {
			  timeout = 5000;   // 5 sec
		  }
		  UDPset_recv_timeout(sock, timeout);

		  UDPrecv(sock,&ack,13);
		  while(ack[0]!=SDM_ACK && count < NUMRETRIES)
		  {
			  count++;

			  result = UDPsend(sock,buf,i);
			  UDPrecv(sock,&ack,13);
		  }
		  //remove timeout on sdm_dm_sock
		  UDPset_recv_timeout(sock, 0);

      if(count == NUMRETRIES && ack[0]!=SDM_ACK)
      {
		    result = SDM_UNABLE_TO_REGISTER;
      }
	    else
      {
		    error = GET_SHORT(&ack[11]); 
        if(error < 0)
        {
			    result = error;
        }
      }
    }
	  UDPclose (sock);
#ifdef BUILD_WITH_MESSAGE_LOGGING
	  Logger.MessageSent(*this);
#endif
  }
	return result;
}