protected override DdnsResponse DecodeUpdateIpResponse(string updateIpResp) { // parse Http response DdnsHttpResponse httpResponse = DdnsHttpResponse.Parse(updateIpResp); string responseCode = null; // extract response code if (httpResponse.Body.LastIndexOf(' ') > 0) { responseCode = httpResponse.Body.Substring(0, httpResponse.Body.LastIndexOf(' ')); } else { responseCode = httpResponse.Body; } DdnsResponse ddnsResponse = new DdnsResponse(responseCode); // if response is "good" or "nochg", No-Ip service provider returns also IP address if ((ddnsResponse.Code == DdnsResponseCode.Good) || (ddnsResponse.Code == DdnsResponseCode.NoChg)) { // ex. good aaa.bbb.ccc.ddd // nochg aaa.bbb.ccc.ddd this.ipAddress = IPAddress.Parse(httpResponse.Body.Substring(httpResponse.Body.LastIndexOf(' ') + 1)); } return(ddnsResponse); }
protected override DdnsResponse DecodeUpdateIpResponse(string updateIpResp) { // parse Http response DdnsHttpResponse httpResponse = DdnsHttpResponse.Parse(updateIpResp); string responseCode = null; // extract response code if (httpResponse.Body.LastIndexOf(' ') > 0) responseCode = httpResponse.Body.Substring(0, httpResponse.Body.LastIndexOf(' ')); else responseCode = httpResponse.Body; DdnsResponse ddnsResponse = new DdnsResponse(responseCode); // if response is "good" or "nochg", No-Ip service provider returns also IP address if ((ddnsResponse.Code == DdnsResponseCode.Good) || (ddnsResponse.Code == DdnsResponseCode.NoChg)) { // ex. good aaa.bbb.ccc.ddd // nochg aaa.bbb.ccc.ddd this.ipAddress = IPAddress.Parse(httpResponse.Body.Substring(httpResponse.Body.LastIndexOf(' ') + 1)); } return ddnsResponse; }
/// <summary> /// Update client Ip address /// </summary> protected virtual void UpdateIpAddress() { // create socket for updating IP address using (this.updateIpSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) { this.updateIpSocket.Connect(this.updateIpEndPoint); // prepare buffers for send and receive this.sendBuffer = Encoding.UTF8.GetBytes(this.updateIpCmd.ToString()); this.receiveBuffer = new byte[RECEIVE_BUFFER_SIZE]; int byteRead = 0; // SSL configured if (this.ddnsConfig.SSL) { // create SSL stream using (Microsoft.SPOT.Net.Security.SslStream sslStream = new Microsoft.SPOT.Net.Security.SslStream(this.updateIpSocket)) { // SSL handshake authentication sslStream.AuthenticateAsClient(this.updateIpHost, Microsoft.SPOT.Net.Security.SslProtocols.TLSv1); // send message sslStream.Write(this.sendBuffer, 0, this.sendBuffer.Length); // cycle for reading from socket int offset = 0; int read = 0; do { read = sslStream.Read(this.receiveBuffer, offset, RECEIVE_BUFFER_SIZE - offset); offset += read; } while (read != 0); byteRead = offset; } } else { // send message and read response this.updateIpSocket.Send(this.sendBuffer); byteRead = this.updateIpSocket.Receive(this.receiveBuffer); } if (byteRead > 0) { this.response = this.DecodeUpdateIpResponse(new String(Encoding.UTF8.GetChars(this.receiveBuffer))); // raise IP address updated event this.OnIpAddressUpdated(new IpAddressUpdatedEventArgs { IpAddress = this.ipAddress, Response = this.response }); } } }