private static STUNMessage FormSTUNIndication(String Software) { STUNMessage stunIndicationRequest = new STUNMessage(StunMessageType.BindingIndication); SoftwareAttribute software = SoftwareAttribute.CreateSoftware(Software); stunIndicationRequest.AddAttribute(software); stunIndicationRequest.Create(); return stunIndicationRequest; }
private static STUNMessage FormSTUNIndication() { STUNMessage stunIndicationRequest = new STUNMessage(StunMessageType.BindingIndication); stunIndicationRequest.Create(); return stunIndicationRequest; }
private void ServerMethod() { // get Network Stream NetworkStream ns = m_client.GetStream(); while (IsSocketConnected(m_client.Client)) { // data available? if (ns.DataAvailable) { // 1. Receive STUN Message int bytesRead = 0; // read first 20 bytes (header) byte[] stunHeader = new byte[20]; int read = ns.Read(stunHeader, 0, stunHeader.Length); // parse length of message (3rd and 4th byte) ushort stunMsgLength = NetworkByteArray.ReadUInt16(stunHeader, 2); // Byte Array for request Byte[] stunRequest = new Byte[20 + stunMsgLength]; // copy header to request Array.Copy(stunHeader, 0, stunRequest, 0, stunHeader.Length); // is there more to read? if yes get the rest of the message if (stunMsgLength > 0) bytesRead = ns.Read(stunRequest, 20, stunMsgLength); // received entire message? if (bytesRead != stunMsgLength) Console.WriteLine("Error receiving Stun Message!"); // parse message STUNMessage request = STUNMessage.Parse(stunRequest); // contains Fingerprint? if (request.ContainsFingerprint()) // validate Fingerprint, in error case discard message if (!request.ValidateFingerprint()) return; // 2. Process STUN Message STUNMessage response = null; // Binding Request? if (request.StunMessageType == StunMessageType.BindingRequest) { // public Endpoint of Client IPEndPoint remoteEndPoint = (IPEndPoint)m_client.Client.RemoteEndPoint; // TransactionID of Request also for Response Byte[] reqTransID = request.TransactionID; response = new STUNMessage(StunMessageType.BindingSuccessResponse, reqTransID); // add Attributes and create message XorMappedAddressAttribute xmaa = XorMappedAddressAttribute.CreateXorMappedAddress(response.TransactionID, remoteEndPoint.Address.ToString(), (ushort)remoteEndPoint.Port); response.AddAttribute(xmaa); MappedAddressAttribute maa = MappedAddressAttribute.CreateMappedAddress(remoteEndPoint.Address.ToString(), (ushort)remoteEndPoint.Port); response.AddAttribute(maa); response.Create(); } else if (request.StunMessageType == StunMessageType.BindingIndication) { // No response is generated for an indication (RFC 5389, 7.3.2.) } byte[] stunResponse = response.ToByteArray(); ns.Write(stunResponse, 0, stunResponse.Length); ns.Flush(); } // no data available else Thread.Sleep(10); } }
// Messages erzeugen: private static STUNMessage FormSTUNBindingRequest() { STUNMessage stunBindingRequest = new STUNMessage(StunMessageType.BindingRequest); // TEST Fingerprint FingerprintAttribute fingerprint = FingerprintAttribute.CreateFingerprint(); stunBindingRequest.AddAttribute(fingerprint); stunBindingRequest.Create(); return stunBindingRequest; }
private static IPEndPoint GetPublicMapping(Socket stunSocket) { STUNMessage stunBindingRequest = new STUNMessage(StunMessageType.BindingRequest); stunBindingRequest.Create(); stunSocket.Send(stunBindingRequest.ToByteArray()); // STUN Header 20 Byte byte[] stunHeader = new byte[20]; stunSocket.Receive(stunHeader, 20, SocketFlags.None); ushort stunBodyLength = NetworkByteArray.ReadUInt16(stunHeader, 2); byte[] stunBody = new byte[stunBodyLength]; stunSocket.Receive(stunBody, stunBodyLength, SocketFlags.None); byte[] stunBindResp = new byte[20 + stunBodyLength]; Array.Copy(stunHeader, 0, stunBindResp, 0, 20); Array.Copy(stunBody, 0, stunBindResp, 20, stunBodyLength); STUNMessage stunBindingResponse = STUNMessage.Parse(stunBindResp); // contains XOR Mapped Address? for (int i = 0; i < stunBindingResponse.AttributeList.Count; i++) { if (stunBindingResponse.AttributeList[i].Type == STUNAttribute.StunAttributeType.XorMappedAddress) { XorMappedAddressAttribute xmaa = (XorMappedAddressAttribute)stunBindingResponse.AttributeList[i]; return xmaa.XorMappedAddress; } } // contains Mapped Address for (int i = 0; i < stunBindingResponse.AttributeList.Count; i++) { if (stunBindingResponse.AttributeList[i].Type == STUNAttribute.StunAttributeType.MappedAddress) { MappedAddressAttribute maa = (MappedAddressAttribute)stunBindingResponse.AttributeList[i]; return maa.MappedAddress; } } // no attribute return null; }