NamedPipeShare.cs 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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. foreach (RemoteService service in this)
  39. {
  40. if (String.Equals(path, service.PipeName, StringComparison.InvariantCultureIgnoreCase))
  41. {
  42. return service;
  43. }
  44. }
  45. return null;
  46. }
  47. public string Name
  48. {
  49. get
  50. {
  51. return NamedPipeShareName;
  52. }
  53. }
  54. }
  55. }