Exemple #1
0
int CSocket::Connect (CIPAddress &rForeignIP, u16 nForeignPort)
{
	if (nForeignPort == 0)
	{
		return -1;
	}

	assert (m_pTransportLayer != 0);

	if (m_hConnection >= 0)
	{
		if (m_nProtocol != IPPROTO_UDP)
		{
			return -1;
		}

		m_pTransportLayer->Disconnect (m_hConnection);
		m_hConnection = -1;
	}

	assert (m_pNetConfig != 0);
	if (   m_pNetConfig->GetIPAddress ()->IsNull ()		// from null source address
	    && !(   m_nProtocol == IPPROTO_UDP			// only UDP broadcasts are allowed
		 && rForeignIP.IsBroadcast ()))
	{
		return -1;
	}

	m_hConnection = m_pTransportLayer->Connect (rForeignIP, nForeignPort, m_nOwnPort, m_nProtocol);

	return m_hConnection >= 0 ? 0 : m_hConnection;
}
Exemple #2
0
int CUDPConnection::SendTo (const void *pData, unsigned nLength, int nFlags,
			    CIPAddress	&rForeignIP, u16 nForeignPort)
{
	if (m_nErrno < 0)
	{
		int nErrno = m_nErrno;
		m_nErrno = 0;

		return nErrno;
	}

	if (m_bActiveOpen)
	{
		// ignore rForeignIP and nForeignPort
		return Send (pData, nLength, nFlags);
	}

	if (   nFlags != 0
	    && nFlags != MSG_DONTWAIT)
	{
		return -1;
	}

	unsigned nPacketLength = sizeof (TUDPHeader) + nLength;		// may wrap
	if (   nPacketLength <= sizeof (TUDPHeader)
	    || nPacketLength > FRAME_BUFFER_SIZE)
	{
		return -1;
	}

	assert (m_pNetConfig != 0);
	if (   !m_bBroadcastsAllowed
	    && (   rForeignIP.IsBroadcast ()
	        || rForeignIP == *m_pNetConfig->GetBroadcastAddress ()))
	{
		return -1;
	}

	u8 PacketBuffer[nPacketLength];
	TUDPHeader *pHeader = (TUDPHeader *) PacketBuffer;

	pHeader->nSourcePort = le2be16 (m_nOwnPort);
	pHeader->nDestPort   = le2be16 (nForeignPort);
	pHeader->nLength     = le2be16 (nPacketLength);
	pHeader->nChecksum   = 0;
	
	assert (pData != 0);
	assert (nLength > 0);
	memcpy (PacketBuffer+sizeof (TUDPHeader), pData, nLength);

	m_Checksum.SetSourceAddress (*m_pNetConfig->GetIPAddress ());
	m_Checksum.SetDestinationAddress (rForeignIP);
	pHeader->nChecksum = m_Checksum.Calculate (PacketBuffer, nPacketLength);

	assert (m_pNetworkLayer != 0);
	boolean bOK = m_pNetworkLayer->Send (rForeignIP, PacketBuffer, nPacketLength, IPPROTO_UDP);
	
	return bOK ? nLength : -1;
}
CHostSocket::CHostSocket(CIPAddress& sdlIpAddress ) 
	: log("TcpSocketHost", CXmlSettings::Inst()->GetLoggerSettings()->logLevel)
{
	IPaddress hostIp = sdlIpAddress.GetIPAddress();
	if(!(_socket = SDLNet_TCP_Open(&hostIp)))
	{
		SDLNet_FreeSocketSet(_socketSet);
	}
}
Exemple #4
0
int CUDPConnection::PacketReceived (const void *pPacket, unsigned nLength, CIPAddress &rSenderIP, int nProtocol)
{
	if (nProtocol != IPPROTO_UDP)
	{
		return 0;
	}

	m_Checksum.SetSourceAddress (rSenderIP);

	// TODO: may not work with some UDP based protocol, would need receiver IP from network layer here
	if (   m_bActiveOpen
	    && m_ForeignIP.IsBroadcast ())
	{
		m_Checksum.SetDestinationAddress (m_ForeignIP);
	}
	else
	{
		if (   m_bActiveOpen
		    && m_ForeignIP != rSenderIP)
		{
			return 0;
		}

		assert (m_pNetConfig != 0);
		m_Checksum.SetDestinationAddress (*m_pNetConfig->GetIPAddress ());
	}
	
	if (nLength <= sizeof (TUDPHeader))
	{
		return 0;
	}
	TUDPHeader *pHeader = (TUDPHeader *) pPacket;

	if (m_nOwnPort != be2le16 (pHeader->nDestPort))
	{
		return 0;
	}

	u16 nSourcePort = be2le16 (pHeader->nSourcePort);
	if (   m_bActiveOpen
	    && m_nForeignPort != nSourcePort)
	{
		return 0;
	}

	if (nLength < be2le16 (pHeader->nLength))
	{
		return -1;
	}
	
	if (   pHeader->nChecksum != UDP_CHECKSUM_NONE
	    && m_Checksum.Calculate (pPacket, nLength) != CHECKSUM_OK)
	{
		return -1;
	}

	nLength -= sizeof (TUDPHeader);
	assert (nLength > 0);

	TUDPPrivateData *pData = new TUDPPrivateData;
	assert (pData != 0);
	rSenderIP.CopyTo (pData->SourceAddress);
	pData->nSourcePort = nSourcePort;

	m_RxQueue.Enqueue ((u8 *) pPacket + sizeof (TUDPHeader), nLength, pData);

	return 1;
}
Exemple #5
0
void CMQTTClient::Connect (const char *pHost, u16 usPort, const char *pClientIdentifier,
			   const char *pUsername, const char *pPassword,
			   u16 usKeepAliveSeconds, boolean bCleanSession,
			   const char *pWillTopic, u8 uchWillQoS, boolean bWillRetain,
			   const u8 *pWillPayload, size_t nWillPayloadLength)
{
	assert (m_ConnectStatus == MQTTStatusDisconnected);

	if (m_pTopicBuffer == 0)
	{
		OnDisconnect (MQTTDisconnectInsufficientResources);

		return;
	}

	assert (m_pNetSubSystem != 0);
	CDNSClient DNSClient (m_pNetSubSystem);

	CIPAddress IPServer;
	if (!DNSClient.Resolve (pHost, &IPServer))
	{
		CLogger::Get ()->Write (FromMQTTClient, LogError, "Cannot resolve: %s", pHost);

		OnDisconnect (MQTTDisconnectDNSError);

		return;
	}

	IPServer.Format (&m_IPServerString);
	CLogger::Get ()->Write (FromMQTTClient, LogDebug, "%s: Opening connection",
				(const char *) m_IPServerString);

	assert (m_pSocket == 0);
	m_pSocket = new CSocket (m_pNetSubSystem, IPPROTO_TCP);
	assert (m_pSocket != 0);

	if (m_pSocket->Connect (IPServer, usPort) != 0)
	{
		CLogger::Get ()->Write (FromMQTTClient, LogError, "Cannot connect: %s", pHost);

		delete m_pSocket;
		m_pSocket = 0;

		OnDisconnect (MQTTDisconnectConnectFailed);

		return;
	}

	m_nKeepAliveSeconds = usKeepAliveSeconds;
	m_bTimerRunning = FALSE;
	m_usNextPacketIdentifier = 1;
	m_ReceivePacket.Reset ();
	m_ConnectStatus = MQTTStatusConnectPending;

	CString ClientIdentifier;
	if (pClientIdentifier != 0)
	{
		ClientIdentifier = pClientIdentifier;

		if (ClientIdentifier.GetLength () > 23)
		{
			CLogger::Get ()->Write (FromMQTTClient, LogWarning,
						"Client ID exceeds 23 characters");
		}
	}
	else
	{
		CBcmPropertyTags Tags;
		TPropertyTagSerial Serial;
		if (!Tags.GetTag (PROPTAG_GET_BOARD_SERIAL, &Serial, sizeof Serial))
		{
			Serial.Serial[0] = 0;
			Serial.Serial[1] = 0;
		}

		ClientIdentifier.Format ("raspi%08x%08x", Serial.Serial[1], Serial.Serial[0]);
	}

	u8 uchConnectFlags = 0;
	if (bCleanSession)
	{
		uchConnectFlags |= MQTT_CONNECT_FLAG_CLEAN_SESSION;
	}

	if (pWillTopic != 0)
	{
		uchConnectFlags |= MQTT_CONNECT_FLAG_WILL;

		assert (uchWillQoS <= MQTT_QOS_EXACTLY_ONCE);
		uchConnectFlags |= uchWillQoS << MQTT_CONNECT_FLAG_WILL_QOS__SHIFT;

		if (bWillRetain)
		{
			uchConnectFlags |= MQTT_CONNECT_FLAG_WILL_RETAIN;
		}
	}

	if (pUsername != 0)
	{
		uchConnectFlags |= MQTT_CONNECT_FLAG_USER_NAME;

		if (pPassword != 0)
		{
			uchConnectFlags |= MQTT_CONNECT_FLAG_PASSWORD;
		}
	}

	CMQTTSendPacket Packet (MQTTConnect, m_nMaxPacketSize);
	Packet.AppendString ("MQTT");
	Packet.AppendByte (MQTT_PROTOCOL_LEVEL);
	Packet.AppendByte (uchConnectFlags);
	Packet.AppendWord (usKeepAliveSeconds);
	Packet.AppendString (ClientIdentifier);

	if (pWillTopic != 0)
	{
		Packet.AppendString (pWillTopic);

		assert (nWillPayloadLength < 0x10000);
		Packet.AppendWord (nWillPayloadLength);
		if (nWillPayloadLength > 0)
		{
			assert (pWillPayload != 0);
			Packet.AppendData (pWillPayload, nWillPayloadLength);
		}
	}

	if (pUsername != 0)
	{
		Packet.AppendString (pUsername);

		if (pPassword != 0)
		{
			Packet.AppendString (pPassword);
		}
	}

	if (!SendPacket (&Packet))
	{
		CloseConnection (MQTTDisconnectSendFailed);

		return;
	}
}
Exemple #6
0
int CUDPConnection::PacketReceived (const void *pPacket, unsigned nLength,
				    CIPAddress &rSenderIP, CIPAddress &rReceiverIP, int nProtocol)
{
	if (nProtocol != IPPROTO_UDP)
	{
		return 0;
	}

	if (nLength <= sizeof (TUDPHeader))
	{
		return -1;
	}
	TUDPHeader *pHeader = (TUDPHeader *) pPacket;

	if (m_nOwnPort != be2le16 (pHeader->nDestPort))
	{
		return 0;
	}

	assert (m_pNetConfig != 0);

	u16 nSourcePort = be2le16 (pHeader->nSourcePort);
	if (m_bActiveOpen)
	{
		if (m_nForeignPort != nSourcePort)
		{
			return 0;
		}

		if (   m_ForeignIP != rSenderIP
		    && !m_ForeignIP.IsBroadcast ()
		    && m_ForeignIP != *m_pNetConfig->GetBroadcastAddress ())
		{
			return 0;
		}
	}

	if (nLength < be2le16 (pHeader->nLength))
	{
		return -1;
	}
	
	if (pHeader->nChecksum != UDP_CHECKSUM_NONE)
	{
		m_Checksum.SetSourceAddress (rSenderIP);
		m_Checksum.SetDestinationAddress (rReceiverIP);

		if (m_Checksum.Calculate (pPacket, nLength) != CHECKSUM_OK)
		{
			return -1;
		}
	}

	if (   !m_bBroadcastsAllowed
	    && (   rReceiverIP.IsBroadcast ()
	        || rReceiverIP == *m_pNetConfig->GetBroadcastAddress ()))
	{
		return 1;
	}

	nLength -= sizeof (TUDPHeader);
	assert (nLength > 0);

	TUDPPrivateData *pData = new TUDPPrivateData;
	assert (pData != 0);
	rSenderIP.CopyTo (pData->SourceAddress);
	pData->nSourcePort = nSourcePort;

	m_RxQueue.Enqueue ((u8 *) pPacket + sizeof (TUDPHeader), nLength, pData);

	m_Event.Set ();

	return 1;
}