/// <summary> /// Constructor /// </summary> /// <exception cref="System.InvalidOperationException">This is thrown /// if <see cref="Curl"/> hasn't bee properly initialized.</exception> /// <exception cref="System.NullReferenceException">This is thrown if /// the native <c>CURL*</c> handle wasn't created successfully.</exception> public Easy() { Curl.EnsureCurl(); m_pCURL = External.curl_easy_init(); EnsureHandle(); External.curl_easy_setopt_int(m_pCURL, CURLoption.CURLOPT_NOPROGRESS, 1); m_pMyStrings = External.curl_shim_alloc_strings(); m_pfWrite = null; m_privateData = null; m_writeData = null; m_pfRead = null; m_readData = null; m_pfProgress = null; m_progressData = null; m_pfDebug = null; m_debugData = null; m_pfHeader = null; m_headerData = null; m_pfSSLContext = null; m_sslContextData = null; m_pfIoctl = null; m_ioctlData = null; InstallDelegates(); }
private Easy(Easy from) { m_pCURL = External.curl_easy_duphandle(from.m_pCURL); EnsureHandle(); m_pMyStrings = External.curl_shim_alloc_strings(); m_pfWrite = null; m_privateData = null; m_writeData = null; m_pfRead = null; m_readData = null; m_pfProgress = null; m_progressData = null; m_pfDebug = null; m_debugData = null; m_pfHeader = null; m_headerData = null; m_pfSSLContext = null; m_sslContextData = null; m_pfIoctl = null; m_ioctlData = null; InstallDelegates(); }
/// <summary> /// Set options for this object. See the <c>EasyGet</c> sample for /// basic usage. /// </summary> /// <param name="option">This should be a valid <see cref="CURLoption"/>.</param> /// <param name="parameter">This should be a parameter of a varying /// type based on the value of the <c>option</c> parameter.</param> /// <exception cref="System.NullReferenceException">This is thrown if /// the native <c>CURL*</c> handle wasn't created successfully.</exception> /// <returns>A <see cref="CURLcode"/>, typically obtained from /// <c>cURL</c> internally, but sometimes a /// <see cref="CURLcode.CURLE_BAD_FUNCTION_ARGUMENT"/> /// will be returned if the type of value of <c>parameter</c> is invalid. /// </returns> public CURLcode SetOpt(CURLoption option, Object parameter) { EnsureHandle(); CURLcode retCode = CURLcode.CURLE_OK; // numeric cases if ((int)option < CURLOPTTYPE_OBJECTPOINT) { int i = 0; if (option == CURLoption.CURLOPT_DNS_USE_GLOBAL_CACHE || option == CURLoption.CURLOPT_SOURCE_PORT) { return(CURLcode.CURLE_BAD_FUNCTION_ARGUMENT); } else if (option == CURLoption.CURLOPT_TIMEVALUE) { // unboxing may throw class cast exception //DateTime d = (DateTime)parameter; DateTime startTime = Main.DateTimeBegin; TimeSpan currTime = new TimeSpan(DateTime.Now.Ticks - startTime.Ticks); i = Convert.ToInt32(currTime.TotalSeconds); } else { i = Convert.ToInt32(parameter); } retCode = External.curl_easy_setopt_int(m_pCURL, option, i); } // object cases: the majority else if ((int)option < CURLOPTTYPE_FUNCTIONPOINT) { switch (option) { // various data items case CURLoption.CURLOPT_PRIVATE: m_privateData = parameter; break; case CURLoption.CURLOPT_WRITEDATA: m_writeData = parameter; break; case CURLoption.CURLOPT_READDATA: m_readData = parameter; break; case CURLoption.CURLOPT_PROGRESSDATA: m_progressData = parameter; break; case CURLoption.CURLOPT_DEBUGDATA: m_debugData = parameter; break; case CURLoption.CURLOPT_HEADERDATA: m_headerData = parameter; break; case CURLoption.CURLOPT_SSL_CTX_DATA: m_sslContextData = parameter; break; case CURLoption.CURLOPT_IOCTLDATA: m_ioctlData = parameter; break; // items that can't be set externally or // obsolete items case CURLoption.CURLOPT_ERRORBUFFER: case CURLoption.CURLOPT_STDERR: case CURLoption.CURLOPT_SOURCE_HOST: case CURLoption.CURLOPT_SOURCE_PATH: case CURLoption.CURLOPT_PASV_HOST: return(CURLcode.CURLE_BAD_FUNCTION_ARGUMENT); // singular case for share case CURLoption.CURLOPT_SHARE: { Share share = parameter as Share; if (share == null) { return(CURLcode.CURLE_BAD_FUNCTION_ARGUMENT); } retCode = External.curl_easy_setopt_ptr(m_pCURL, option, share.GetHandle()); break; } // multipart HTTP post case CURLoption.CURLOPT_HTTPPOST: { MultiPartForm mf = parameter as MultiPartForm; if (mf == null) { return(CURLcode.CURLE_BAD_FUNCTION_ARGUMENT); } retCode = External.curl_easy_setopt_ptr(m_pCURL, option, mf.GetHandle()); break; } // items curl wants as a curl_slist case CURLoption.CURLOPT_HTTPHEADER: case CURLoption.CURLOPT_PREQUOTE: case CURLoption.CURLOPT_QUOTE: case CURLoption.CURLOPT_POSTQUOTE: case CURLoption.CURLOPT_SOURCE_QUOTE: case CURLoption.CURLOPT_TELNETOPTIONS: case CURLoption.CURLOPT_HTTP200ALIASES: { Slist slist = parameter as Slist; if (slist == null) { retCode = External.curl_easy_setopt_ptr(m_pCURL, option, IntPtr.Zero); } else { retCode = External.curl_easy_setopt_ptr(m_pCURL, option, slist.GetHandle()); } break; } // string items default: { string s = parameter as string; if (s == null) { retCode = External.curl_easy_setopt_ptr(m_pCURL, option, IntPtr.Zero); } else { IntPtr pCurlStr = External.curl_shim_add_string( m_pMyStrings, s); if (pCurlStr != IntPtr.Zero) { retCode = External.curl_easy_setopt_ptr(m_pCURL, option, pCurlStr); } } break; } } } // FUNCTIONPOINT args, for delegates else if ((int)option < CURLOPTTYPE_OFF_T) { switch (option) { case CURLoption.CURLOPT_WRITEFUNCTION: { WriteFunction wf = parameter as WriteFunction; if (wf == null) { return(CURLcode.CURLE_BAD_FUNCTION_ARGUMENT); } m_pfWrite = wf; break; } case CURLoption.CURLOPT_READFUNCTION: { ReadFunction rf = parameter as ReadFunction; if (rf == null) { return(CURLcode.CURLE_BAD_FUNCTION_ARGUMENT); } m_pfRead = rf; break; } case CURLoption.CURLOPT_PROGRESSFUNCTION: { ProgressFunction pf = parameter as ProgressFunction; if (pf == null) { return(CURLcode.CURLE_BAD_FUNCTION_ARGUMENT); } m_pfProgress = pf; break; } case CURLoption.CURLOPT_DEBUGFUNCTION: { DebugFunction pd = parameter as DebugFunction; if (pd == null) { return(CURLcode.CURLE_BAD_FUNCTION_ARGUMENT); } m_pfDebug = pd; break; } case CURLoption.CURLOPT_HEADERFUNCTION: { HeaderFunction hf = parameter as HeaderFunction; if (hf == null) { return(CURLcode.CURLE_BAD_FUNCTION_ARGUMENT); } m_pfHeader = hf; break; } case CURLoption.CURLOPT_SSL_CTX_FUNCTION: { SSLContextFunction sf = parameter as SSLContextFunction; if (sf == null) { return(CURLcode.CURLE_BAD_FUNCTION_ARGUMENT); } m_pfSSLContext = sf; break; } case CURLoption.CURLOPT_IOCTLFUNCTION: { IoctlFunction iof = parameter as IoctlFunction; if (iof == null) { return(CURLcode.CURLE_BAD_FUNCTION_ARGUMENT); } m_pfIoctl = iof; break; } default: return(CURLcode.CURLE_BAD_FUNCTION_ARGUMENT); } } // otherwise, it's one of those 64-bit off_t guys! else { Int64 i = Convert.ToInt64(parameter); retCode = External.curl_easy_setopt_int64(m_pCURL, option, i); } return(retCode); }
/// <summary> /// to create an IoctlSecondary request packet. /// </summary> /// <param name="uid">This field SHOULD identify the authenticated instance of the user.</param> /// <param name="treeId">This field identifies the subdirectory (or tree) on the server that the client is /// accessing.</param> /// <param name="fid">The Fid of the device or file to which the IOCTL is to be sent.</param> /// <param name="category">The implementation dependent device category for the request.</param> /// <param name="function">The implementation dependent device function for the request.</param> /// <param name="parameters">IOCTL parameter bytes. The contents are implementation dependent.</param> /// <param name="data">Transaction data bytes. The contents are implementation dependent.</param> /// <returns>a IoctlSecondary request packet</returns> public SmbIoctlSecondaryRequestPacket CreateIoctlSecondaryRequest( ushort uid, ushort treeId, ushort fid, IoctlCategory category, IoctlFunction function, byte[] parameters, byte[] data) { return this.CreateIoctlSecondaryRequest(this.Context.GetMessageId(this.connectionId), uid, treeId, this.defaultParameters.Flag, this.defaultParameters.Flag2, fid, category, function, this.defaultParameters.MaxParameterCount, this.defaultParameters.MaxDataCount, this.defaultParameters.Timeout, parameters, data); }
/// <summary> /// to create an IoctlSecondary request packet. /// </summary> /// <param name="messageId">This field SHOULD be the multiplex ID that is used to associate a response with a /// request.</param> /// <param name="uid">This field SHOULD identify the authenticated instance of the user.</param> /// <param name="treeId">This field identifies the subdirectory (or tree) on the server that the client is /// accessing.</param> /// <param name="flags">An 8-bit field of 1-bit flags describing various features in effect for the /// message</param> /// <param name="flags2">A 16-bit field of 1-bit flags that represent various features in effect for the /// message. Unspecified bits are reserved and MUST be zero.</param> /// <param name="fid">The Fid of the device or file to which the IOCTL is to be sent.</param> /// <param name="category">The implementation dependent device category for the request.</param> /// <param name="function">The implementation dependent device function for the request.</param> /// <param name="maxParameterCount">The maximum number of SMB_Data.Parameters bytes that the client accepts in /// the IOCTL response. The server MUST NOT return more than this number of bytes in the SMB_Data.Parameter /// field of the response.</param> /// <param name="maxDataCount">The maximum number of SMB_Data.Data bytes that the client accepts in the IOCTL /// response. The server MUST NOT return more than this number of bytes in the SMB_Data.Data field.</param> /// <param name="timeout"> the maximum number of milliseconds the server SHOULD wait for completion of the /// transaction before generating a timeout and returning a response to the client. The client SHOULD set /// this to 0 to indicate that no time-out is expected.</param> /// <param name="parameters">IOCTL parameter bytes. The contents are implementation dependent.</param> /// <param name="data">Transaction data bytes. The contents are implementation dependent.</param> /// <returns>a IoctlSecondary request packet</returns> public SmbIoctlSecondaryRequestPacket CreateIoctlSecondaryRequest( ushort messageId, ushort uid, ushort treeId, SmbFlags flags, SmbFlags2 flags2, ushort fid, IoctlCategory category, IoctlFunction function, ushort maxParameterCount, ushort maxDataCount, uint timeout, byte[] parameters, byte[] data) { if (parameters == null) { parameters = new byte[0]; } if (data == null) { data = new byte[0]; } SmbIoctlSecondaryRequestPacket packet = new SmbIoctlSecondaryRequestPacket(); packet.SmbHeader = CifsMessageUtils.CreateSmbHeader(SmbCommand.SMB_COM_IOCTL_SECONDARY, messageId, uid, treeId, flags, flags2); SMB_COM_IOCTL_Request_SMB_Parameters smbParameters = new SMB_COM_IOCTL_Request_SMB_Parameters(); smbParameters.FID = fid; smbParameters.Category = category; smbParameters.Function = function; smbParameters.MaxParameterCount = maxParameterCount; smbParameters.TotalParameterCount = (ushort)parameters.Length; smbParameters.ParameterCount = (ushort)parameters.Length; smbParameters.MaxDataCount = maxDataCount; smbParameters.TotalDataCount = (ushort)data.Length; smbParameters.DataCount = (ushort)data.Length; smbParameters.Timeout = timeout; smbParameters.Reserved = 0; smbParameters.WordCount = (byte)(Marshal.SizeOf(smbParameters) / NumBytesOfWord); SMB_COM_IOCTL_Request_SMB_Data smbData = new SMB_COM_IOCTL_Request_SMB_Data(); // The size of the preceding SmbParameters part plus Header part is an odd number for all cifs messages // If the format is Unicode, needs to add one 16 bits align pad if ((flags2 & SmbFlags2.SMB_FLAGS2_UNICODE) == SmbFlags2.SMB_FLAGS2_UNICODE) { // pad 1 byte for 16-bits align: smbData.Pad1 = new byte[1]; } else { smbData.Pad1 = new byte[0]; } smbData.Parameters = parameters; // pad 1 byte for 16-bits align of needed: smbData.Pad2 = new byte[smbData.Parameters.Length % twoBytesAlign]; smbData.Data = data; smbData.ByteCount = (ushort)(smbData.Pad1.Length + smbData.Parameters.Length + smbData.Pad2.Length + smbData.Data.Length); smbParameters.ParameterOffset = (ushort)(Marshal.SizeOf(packet.SmbHeader) + Marshal.SizeOf(smbParameters) + Marshal.SizeOf(smbData.ByteCount) + smbData.Pad1.Length); smbParameters.DataOffset = (ushort)(smbParameters.ParameterOffset + smbData.Parameters.Length + smbData.Pad2.Length); packet.SmbParameters = smbParameters; packet.SmbData = smbData; return packet; }
/// <summary> /// Set options for this object. See the <c>EasyGet</c> sample for /// basic usage. /// </summary> /// <param name="option">This should be a valid <see cref="CURLoption"/>.</param> /// <param name="parameter">This should be a parameter of a varying /// type based on the value of the <c>option</c> parameter.</param> /// <exception cref="System.NullReferenceException">This is thrown if /// the native <c>CURL*</c> handle wasn't created successfully.</exception> /// <returns>A <see cref="CURLcode"/>, typically obtained from /// <c>cURL</c> internally, but sometimes a /// <see cref="CURLcode.CURLE_BAD_FUNCTION_ARGUMENT"/> /// will be returned if the type of value of <c>parameter</c> is invalid. /// </returns> public CURLcode SetOpt(CURLoption option, Object parameter) { EnsureHandle(); CURLcode retCode = CURLcode.CURLE_OK; // numeric cases if ((int)option < CURLOPTTYPE_OBJECTPOINT) { int i = 0; if (option == CURLoption.CURLOPT_DNS_USE_GLOBAL_CACHE || option == CURLoption.CURLOPT_SOURCE_PORT) { return CURLcode.CURLE_BAD_FUNCTION_ARGUMENT; } else if (option == CURLoption.CURLOPT_TIMEVALUE) { // unboxing may throw class cast exception DateTime d = (DateTime)parameter; DateTime startTime = new DateTime(1970, 1, 1); TimeSpan currTime = new TimeSpan(DateTime.Now.Ticks - startTime.Ticks); i = Convert.ToInt32(currTime.TotalSeconds); } else i = Convert.ToInt32(parameter); retCode = External.curl_easy_setopt(m_pCURL, option, (IntPtr)i); } // object cases: the majority else if ((int)option < CURLOPTTYPE_FUNCTIONPOINT) { switch(option) { // various data items case CURLoption.CURLOPT_PRIVATE: m_privateData = parameter; break; case CURLoption.CURLOPT_WRITEDATA: m_writeData = parameter; break; case CURLoption.CURLOPT_READDATA: m_readData = parameter; break; case CURLoption.CURLOPT_PROGRESSDATA: m_progressData = parameter; break; case CURLoption.CURLOPT_DEBUGDATA: m_debugData = parameter; break; case CURLoption.CURLOPT_HEADERDATA: m_headerData = parameter; break; case CURLoption.CURLOPT_SSL_CTX_DATA: m_sslContextData = parameter; break; case CURLoption.CURLOPT_IOCTLDATA: m_ioctlData = parameter; break; // items that can't be set externally or // obsolete items case CURLoption.CURLOPT_ERRORBUFFER: case CURLoption.CURLOPT_STDERR: case CURLoption.CURLOPT_SOURCE_HOST: case CURLoption.CURLOPT_SOURCE_PATH: case CURLoption.CURLOPT_PASV_HOST: return CURLcode.CURLE_BAD_FUNCTION_ARGUMENT; // singular case for share case CURLoption.CURLOPT_SHARE: { Share share = parameter as Share; if (share == null) return CURLcode.CURLE_BAD_FUNCTION_ARGUMENT; retCode = External.curl_easy_setopt(m_pCURL, option, share.GetHandle()); break; } // multipart HTTP post case CURLoption.CURLOPT_HTTPPOST: { MultiPartForm mf = parameter as MultiPartForm; if (mf == null) return CURLcode.CURLE_BAD_FUNCTION_ARGUMENT; retCode = External.curl_easy_setopt(m_pCURL, option, mf.GetHandle()); break; } // items curl wants as a curl_slist case CURLoption.CURLOPT_HTTPHEADER: case CURLoption.CURLOPT_PREQUOTE: case CURLoption.CURLOPT_QUOTE: case CURLoption.CURLOPT_POSTQUOTE: case CURLoption.CURLOPT_SOURCE_QUOTE: case CURLoption.CURLOPT_TELNETOPTIONS: case CURLoption.CURLOPT_HTTP200ALIASES: { Slist slist = parameter as Slist; if (slist == null) { retCode = External.curl_easy_setopt(m_pCURL, option, IntPtr.Zero); } else { retCode = External.curl_easy_setopt(m_pCURL, option, slist.GetHandle()); } break; } // string items default: { string s = parameter as string; if (s == null) { retCode = External.curl_easy_setopt(m_pCURL, option, IntPtr.Zero); } else { IntPtr pCurlStr = External.curl_shim_add_string( m_pMyStrings, s); if (pCurlStr != IntPtr.Zero) retCode = External.curl_easy_setopt(m_pCURL, option, pCurlStr); } break; } } } // FUNCTIONPOINT args, for delegates else if ((int)option < CURLOPTTYPE_OFF_T) { switch(option) { case CURLoption.CURLOPT_WRITEFUNCTION: { WriteFunction wf = parameter as WriteFunction; if (wf == null) return CURLcode.CURLE_BAD_FUNCTION_ARGUMENT; m_pfWrite = wf; break; } case CURLoption.CURLOPT_READFUNCTION: { ReadFunction rf = parameter as ReadFunction; if (rf == null) return CURLcode.CURLE_BAD_FUNCTION_ARGUMENT; m_pfRead = rf; break; } case CURLoption.CURLOPT_PROGRESSFUNCTION: { ProgressFunction pf = parameter as ProgressFunction; if (pf == null) return CURLcode.CURLE_BAD_FUNCTION_ARGUMENT; m_pfProgress = pf; break; } case CURLoption.CURLOPT_DEBUGFUNCTION: { DebugFunction pd = parameter as DebugFunction; if (pd == null) return CURLcode.CURLE_BAD_FUNCTION_ARGUMENT; m_pfDebug = pd; break; } case CURLoption.CURLOPT_HEADERFUNCTION: { HeaderFunction hf = parameter as HeaderFunction; if (hf == null) return CURLcode.CURLE_BAD_FUNCTION_ARGUMENT; m_pfHeader = hf; break; } case CURLoption.CURLOPT_SSL_CTX_FUNCTION: { SSLContextFunction sf = parameter as SSLContextFunction; if (sf == null) return CURLcode.CURLE_BAD_FUNCTION_ARGUMENT; m_pfSSLContext = sf; break; } case CURLoption.CURLOPT_IOCTLFUNCTION: { IoctlFunction iof = parameter as IoctlFunction; if (iof == null) return CURLcode.CURLE_BAD_FUNCTION_ARGUMENT; m_pfIoctl = iof; break; } default: return CURLcode.CURLE_BAD_FUNCTION_ARGUMENT; } } // otherwise, it's one of those 64-bit off_t guys! else { Int64 i = Convert.ToInt64(parameter); retCode = External.curl_easy_setopt_64(m_pCURL, option, i); } return retCode; }
private Easy(Easy from) { m_pCURL = External.curl_easy_duphandle(from.m_pCURL); EnsureHandle(); m_pMyStrings = External.curl_shim_alloc_strings(); m_pfWrite = null; m_privateData = null; m_writeData = null; m_pfRead = null; m_readData = null; m_pfProgress = null; m_progressData = null; m_pfDebug = null; m_debugData = null; m_pfHeader = null; m_headerData = null; m_pfSSLContext = null; m_sslContextData = null; m_pfIoctl = null; m_ioctlData = null; InstallDelegates(); }
/// <summary> /// Constructor /// </summary> /// <exception cref="System.InvalidOperationException">This is thrown /// if <see cref="Curl"/> hasn't bee properly initialized.</exception> /// <exception cref="System.NullReferenceException">This is thrown if /// the native <c>CURL*</c> handle wasn't created successfully.</exception> public Easy() { Curl.EnsureCurl(); m_pCURL = External.curl_easy_init(); EnsureHandle(); External.curl_easy_setopt(m_pCURL, CURLoption.CURLOPT_NOPROGRESS, IntPtr.Zero); m_pMyStrings = External.curl_shim_alloc_strings(); m_pfWrite = null; m_privateData = null; m_writeData = null; m_pfRead = null; m_readData = null; m_pfProgress = null; m_progressData = null; m_pfDebug = null; m_debugData = null; m_pfHeader = null; m_headerData = null; m_pfSSLContext = null; m_sslContextData = null; m_pfIoctl = null; m_ioctlData = null; InstallDelegates(); }