msg.go 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. // BSD 3-Clause License
  2. //
  3. // Copyright (c) 2019, Guillaume Ballet
  4. // All rights reserved.
  5. //
  6. // Redistribution and use in source and binary forms, with or without
  7. // modification, are permitted provided that the following conditions are met:
  8. //
  9. // * Redistributions of source code must retain the above copyright notice, this
  10. // list of conditions and the following disclaimer.
  11. //
  12. // * Redistributions in binary form must reproduce the above copyright notice,
  13. // this list of conditions and the following disclaimer in the documentation
  14. // and/or other materials provided with the distribution.
  15. //
  16. // * Neither the name of the copyright holder nor the names of its
  17. // contributors may be used to endorse or promote products derived from
  18. // this software without specific prior written permission.
  19. //
  20. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  21. // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  22. // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  23. // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  24. // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  25. // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  26. // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  27. // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  28. // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  29. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. package pcsc
  31. import (
  32. "encoding/binary"
  33. "net"
  34. )
  35. /**
  36. * @brief Wrapper for the MessageSend() function.
  37. *
  38. * Called by clients to send messages to the server.
  39. * The parameters \p command and \p data are set in the \c sharedSegmentMsg
  40. * struct in order to be sent.
  41. *
  42. * @param[in] command Command to be sent.
  43. * @param[in] dwClientID Client socket handle.
  44. * @param[in] size Size of the message (\p data).
  45. * @param[in] data_void Data to be sent.
  46. *
  47. * @return Same error codes as MessageSend().
  48. */
  49. func messageSendWithHeader(command uint32, conn net.Conn, data []byte) error {
  50. /* Translate header into bytes */
  51. msgData := make([]byte, 8+len(data))
  52. binary.LittleEndian.PutUint32(msgData[4:], command)
  53. binary.LittleEndian.PutUint32(msgData, uint32(len(data)))
  54. /* Copy payload */
  55. copy(msgData[8:], data)
  56. _, err := conn.Write(msgData)
  57. return err
  58. }
  59. // clientSetupSession prepares a communication channel for the client to talk to the server.
  60. // This is called by the application to create a socket for local IPC with the
  61. // server. The socket is associated to the file \c PCSCLITE_CSOCK_NAME.
  62. /*
  63. * @param[out] pdwClientID Client Connection ID.
  64. *
  65. * @retval 0 Success.
  66. * @retval -1 Can not create the socket.
  67. * @retval -1 The socket can not open a connection.
  68. * @retval -1 Can not set the socket to non-blocking.
  69. */
  70. func clientSetupSession(daemonPath string) (net.Conn, error) {
  71. path := PCSCDSockName
  72. if len(daemonPath) > 0 {
  73. path = daemonPath
  74. }
  75. return net.Dial("unix", path)
  76. }