private ObexStatusCode Connect_Obex() { //do obex negotiation byte[] connectPacket; if (uri.Scheme == SchemeNames.Ftp) { connectPacket = new byte[] { 0x80, 0x00, 26, 0x10, 0x00, 0x20, 0x00, 0x46, 0x00, 19, 0xF9, 0xEC, 0x7B, 0xC4, 0x95, 0x3C, 0x11, 0xD2, 0x98, 0x4E, 0x52, 0x54, 0x00, 0xDC, 0x9E, 0x09 }; } else { connectPacket = new byte[7] { 0x80, 0x00, 0x07, 0x10, 0x00, 0x20, 0x00 }; } ns.Write(connectPacket, 0, connectPacket.Length); byte[] receivePacket = new byte[3]; StreamReadBlockMust(ns, receivePacket, 0, 3); if (receivePacket[0] == (byte)(ObexStatusCode.OK | ObexStatusCode.Final)) { //get length short len = (short)(IPAddress.NetworkToHostOrder(BitConverter.ToInt16(receivePacket, 1)) - 3); byte[] receivePacket2 = new byte[3 + len]; Buffer.BlockCopy(receivePacket, 0, receivePacket2, 0, 3); StreamReadBlockMust(ns, receivePacket2, 3, len); ObexParser.ParseHeaders(receivePacket2, true, ref remoteMaxPacket, null, headers); if (headers["CONNECTIONID"] != null) { connectionId = int.Parse(headers["CONNECTIONID"]); } //ParseHeaders(receivePacket2, headers, null); } return((ObexStatusCode)receivePacket[0]); }
private byte[] HandleAndMakeResponse(ref bool moretoreceive, ref bool putCompleted, ObexMethod method) { byte[] responsePacket; switch (method) { case ObexMethod.Connect: ObexParser.ParseHeaders(buffer, true, ref remoteMaxPacket, bodyStream, headers); responsePacket = new byte[7] { 0xA0, 0x00, 0x07, 0x10, 0x00, 0x20, 0x00 }; break; case ObexMethod.Put: if (putCompleted) // Don't allow another PUT to append to the previous content! { goto case ObexMethod.PutFinal; } ObexParser.ParseHeaders(buffer, false, ref remoteMaxPacket, bodyStream, headers); responsePacket = new byte[3] { (byte)(ObexStatusCode.Continue | ObexStatusCode.Final), 0x00, 0x03 }; break; case ObexMethod.PutFinal: if (putCompleted) // Don't allow another PUT to append to the previous content! { ObexStatusCode code = ObexStatusCode.Forbidden; // Any better one? responsePacket = new byte[3] { (byte)(code | ObexStatusCode.Final), 0x00, 0x03 }; moretoreceive = false; break; } ObexParser.ParseHeaders(buffer, false, ref remoteMaxPacket, bodyStream, headers); responsePacket = new byte[3] { (byte)(ObexStatusCode.OK | ObexStatusCode.Final), 0x00, 0x03 }; // Shouldn't return an object if the sender didn't send it all. putCompleted = true; // (Need to just assume that it does contains EndOfBody) break; case ObexMethod.Disconnect: ObexParser.ParseHeaders(buffer, false, ref remoteMaxPacket, bodyStream, headers); responsePacket = new byte[3] { (byte)(ObexStatusCode.OK | ObexStatusCode.Final), 0x00, 0x03 }; moretoreceive = false; break; default: //Console.WriteLine(method.ToString() + " " + received.ToString()); responsePacket = new byte[3] { (byte)ObexStatusCode.NotImplemented, 0x00, 0x03 }; break; } return(responsePacket); }
private ObexStatusCode DoGet(MemoryStream ms, WebHeaderCollection headers) { ObexStatusCode sc; const byte ObexMethod_GetFinal = (byte)ObexMethod.Get | 0x80; byte[] buffer = new byte[remoteMaxPacket]; buffer[0] = ObexMethod_GetFinal; int bufferlen = 3; //build the packet based on the available headers //connectionid (must be first header) if (connectionId != 0) { buffer[bufferlen] = (byte)ObexHeader.ConnectionID; BitConverter.GetBytes(IPAddress.HostToNetworkOrder(connectionId)).CopyTo(buffer, bufferlen + 1); bufferlen += 5; } //name string filename = uri.PathAndQuery.TrimStart(new char[] { '/' }); if (filename.Length > 0) { const int NullTerminatorLen = 2; int filenameLength = filename.Length * 2 + NullTerminatorLen; buffer[bufferlen] = (byte)ObexHeader.Name; int filenameheaderlen = IPAddress.HostToNetworkOrder((short)(filenameLength + 3)); BitConverter.GetBytes(filenameheaderlen).CopyTo(buffer, bufferlen + 1); System.Text.Encoding.BigEndianUnicode.GetBytes(filename).CopyTo(buffer, bufferlen + 3); bufferlen += filenameLength + 3; } //content type string type = this.headers["TYPE"]; if (type != null) { buffer[bufferlen] = (byte)ObexHeader.Type; int typeheaderlen = IPAddress.HostToNetworkOrder((short)((type.Length + 1) + 3)); BitConverter.GetBytes(typeheaderlen).CopyTo(buffer, bufferlen + 1); System.Text.Encoding.ASCII.GetBytes(type).CopyTo(buffer, bufferlen + 3); bufferlen += type.Length + 4; } //write total packet size BitConverter.GetBytes(IPAddress.HostToNetworkOrder((short)bufferlen)).CopyTo(buffer, 1); //send packet with name header //then loop until end do { // Send then Receive ns.Write(buffer, 0, bufferlen); // StreamReadBlockMust(ns, buffer, 0, 3); int bytesread = 3; //get code sc = (ObexStatusCode)buffer[0]; //get length short len = (short)IPAddress.NetworkToHostOrder(BitConverter.ToInt16(buffer, 1)); Debug.Assert(len > 0, "not got len!"); //read all of packet StreamReadBlockMust(ns, buffer, bytesread, len - bytesread); ObexParser.ParseHeaders(buffer, false, ref remoteMaxPacket, ms, headers); //prepare the next request buffer[0] = ObexMethod_GetFinal; BitConverter.GetBytes(IPAddress.HostToNetworkOrder((short)3)).CopyTo(buffer, 1); bufferlen = 3; }while (sc == ObexStatus_Continue); if (sc != ObexStatus_OK) { throw new WebException("GET failed with code: " + sc, WebExceptionStatus.ProtocolError); } return(sc); }
private ObexStatusCode Connect_Obex() { // do obex negotiation byte[] connectPacket = new byte[remoteMaxPacket]; short connectPacketLength = 7; if (uri.Scheme == SchemeNames.Ftp) { connectPacket = new byte[] { 0x80, 0x00, 26, 0x10, 0x00, 0x20, 0x00, 0x46, 0x00, 19, 0xF9, 0xEC, 0x7B, 0xC4, 0x95, 0x3C, 0x11, 0xD2, 0x98, 0x4E, 0x52, 0x54, 0x00, 0xDC, 0x9E, 0x09 }; connectPacketLength = (short)connectPacket.Length; } else { new byte[7] { 0x80, 0x00, 0x07, 0x10, 0x00, 0x20, 0x00 }.CopyTo(connectPacket, 0); // target if (Guid.TryParse(Headers["TARGET"], out var targetGuid)) { var targetBytes = ToBigEndian(targetGuid).ToByteArray(); connectPacket[connectPacketLength] = (byte)ObexHeader.Target; short targetHeaderLen = IPAddress.HostToNetworkOrder((short)(targetBytes.Length + 3)); BitConverter.GetBytes(targetHeaderLen).CopyTo(connectPacket, connectPacketLength + 1); targetBytes.CopyTo(connectPacket, connectPacketLength + 3); connectPacketLength += 19; } if (!string.IsNullOrEmpty(Headers["MapSupportedFeatures"])) { connectPacket[connectPacketLength] = 0x27; short targetHeaderLen = IPAddress.HostToNetworkOrder((short)7); BitConverter.GetBytes(targetHeaderLen).CopyTo(connectPacket, connectPacketLength + 1); int featureMap = IPAddress.HostToNetworkOrder(0x1c); BitConverter.GetBytes(featureMap).CopyTo(connectPacket, connectPacketLength + 3); connectPacketLength += 7; } } BitConverter.GetBytes(IPAddress.HostToNetworkOrder(connectPacketLength)).CopyTo(connectPacket, 1); ns.Write(connectPacket, 0, connectPacketLength); byte[] receivePacket = new byte[3]; StreamReadBlockMust(ns, receivePacket, 0, 3); if (receivePacket[0] == (byte)(ObexStatusCode.OK | ObexStatusCode.Final)) { // get length short len = (short)(IPAddress.NetworkToHostOrder(BitConverter.ToInt16(receivePacket, 1)) - 3); byte[] receivePacket2 = new byte[3 + len]; Buffer.BlockCopy(receivePacket, 0, receivePacket2, 0, 3); StreamReadBlockMust(ns, receivePacket2, 3, len); ObexParser.ParseHeaders(receivePacket2, true, ref remoteMaxPacket, null, Headers); if (Headers["CONNECTIONID"] != null) { connectionId = int.Parse(Headers["CONNECTIONID"]); } //ParseHeaders(receivePacket2, headers, null); } return((ObexStatusCode)receivePacket[0]); }