NamedPipeShare.cs 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. /* Copyright (C) 2014-2017 Tal Aloni <tal.aloni.il@gmail.com>. All rights reserved.
  2. *
  3. * You can redistribute this program and/or modify it under the terms of
  4. * the GNU Lesser Public License as published by the Free Software Foundation,
  5. * either version 3 of the License, or (at your option) any later version.
  6. */
  7. using System;
  8. using System.Collections.Generic;
  9. using System.IO;
  10. using SMBLibrary.RPC;
  11. using SMBLibrary.Services;
  12. namespace SMBLibrary.Server
  13. {
  14. public class NamedPipeShare : List<RemoteService>, ISMBShare
  15. {
  16. // A pipe share, as defined by the SMB Protocol, MUST always have the name "IPC$".
  17. public const string NamedPipeShareName = "IPC$";
  18. public NamedPipeShare(List<string> shareList)
  19. {
  20. this.Add(new ServerService(Environment.MachineName, shareList));
  21. this.Add(new WorkstationService(Environment.MachineName, Environment.MachineName));
  22. }
  23. public Stream OpenPipe(string path)
  24. {
  25. // It is possible to have a named pipe that does not use RPC (e.g. MS-WSP),
  26. // However this is not currently needed by our implementation.
  27. RemoteService service = GetService(path);
  28. if (service != null)
  29. {
  30. // All instances of a named pipe share the same pipe name, but each instance has its own buffers and handles,
  31. // and provides a separate conduit for client/server communication.
  32. return new RPCPipeStream(service);
  33. }
  34. return null;
  35. }
  36. private RemoteService GetService(string path)
  37. {
  38. if (path.StartsWith(@"\"))
  39. {
  40. path = path.Substring(1);
  41. }
  42. foreach (RemoteService service in this)
  43. {
  44. if (String.Equals(path, service.PipeName, StringComparison.InvariantCultureIgnoreCase))
  45. {
  46. return service;
  47. }
  48. }
  49. return null;
  50. }
  51. public string Name
  52. {
  53. get
  54. {
  55. return NamedPipeShareName;
  56. }
  57. }
  58. }
  59. }