2012-09-06 07:06:06 +04:00
|
|
|
/* -*- Mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; tab-width: 40 -*- */
|
|
|
|
/* vim: set ts=2 et sw=2 tw=80: */
|
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
2012-09-26 00:13:15 +04:00
|
|
|
#ifndef mozilla_ipc_UnixSocket_h
|
|
|
|
#define mozilla_ipc_UnixSocket_h
|
|
|
|
|
2013-03-19 21:23:47 +04:00
|
|
|
|
2012-10-01 11:03:16 +04:00
|
|
|
#include <sys/socket.h>
|
2013-03-19 21:23:47 +04:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/un.h>
|
2013-03-27 16:53:24 +04:00
|
|
|
#include <netinet/in.h>
|
2013-10-23 10:12:24 +04:00
|
|
|
#ifdef MOZ_B2G_BT_BLUEZ
|
2013-03-19 21:23:47 +04:00
|
|
|
#include <bluetooth/bluetooth.h>
|
|
|
|
#include <bluetooth/sco.h>
|
2013-04-12 14:45:37 +04:00
|
|
|
#include <bluetooth/l2cap.h>
|
2013-03-19 21:23:47 +04:00
|
|
|
#include <bluetooth/rfcomm.h>
|
|
|
|
#endif
|
2012-09-26 00:13:15 +04:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include "nsString.h"
|
|
|
|
#include "nsAutoPtr.h"
|
|
|
|
#include "mozilla/RefPtr.h"
|
2013-12-09 17:21:18 +04:00
|
|
|
#include "nsThreadUtils.h"
|
2012-09-06 07:06:06 +04:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace ipc {
|
|
|
|
|
2013-03-19 21:23:47 +04:00
|
|
|
union sockaddr_any {
|
|
|
|
sockaddr_storage storage; // address-family only
|
|
|
|
sockaddr_un un;
|
|
|
|
sockaddr_in in;
|
|
|
|
sockaddr_in6 in6;
|
2013-10-23 10:12:24 +04:00
|
|
|
#ifdef MOZ_B2G_BT_BLUEZ
|
2013-03-19 21:23:47 +04:00
|
|
|
sockaddr_sco sco;
|
|
|
|
sockaddr_rc rc;
|
2013-04-12 14:45:37 +04:00
|
|
|
sockaddr_l2 l2;
|
2013-03-19 21:23:47 +04:00
|
|
|
#endif
|
|
|
|
// ... others
|
|
|
|
};
|
|
|
|
|
2012-12-20 14:36:55 +04:00
|
|
|
class UnixSocketRawData
|
2012-09-26 00:13:15 +04:00
|
|
|
{
|
2012-12-20 14:36:55 +04:00
|
|
|
public:
|
2012-09-26 00:13:15 +04:00
|
|
|
// Number of octets in mData.
|
|
|
|
size_t mSize;
|
|
|
|
size_t mCurrentWriteOffset;
|
2013-09-06 12:18:35 +04:00
|
|
|
nsAutoArrayPtr<uint8_t> mData;
|
2012-09-26 00:13:15 +04:00
|
|
|
|
2012-10-01 11:03:16 +04:00
|
|
|
/**
|
2013-09-06 12:18:35 +04:00
|
|
|
* Constructor for situations where only size is known beforehand
|
|
|
|
* (for example, when being assigned strings)
|
2012-09-26 00:13:15 +04:00
|
|
|
*/
|
2013-09-06 12:18:35 +04:00
|
|
|
UnixSocketRawData(size_t aSize) :
|
2012-09-26 00:13:15 +04:00
|
|
|
mSize(aSize),
|
|
|
|
mCurrentWriteOffset(0)
|
|
|
|
{
|
2013-09-06 12:18:35 +04:00
|
|
|
mData = new uint8_t[mSize];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor for situations where size and data is known
|
|
|
|
* beforehand (for example, when being assigned strings)
|
|
|
|
*/
|
|
|
|
UnixSocketRawData(const void* aData, size_t aSize)
|
|
|
|
: mSize(aSize),
|
|
|
|
mCurrentWriteOffset(0)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aData || !mSize);
|
|
|
|
mData = new uint8_t[mSize];
|
|
|
|
memcpy(mData, aData, mSize);
|
2012-09-26 00:13:15 +04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class UnixSocketImpl;
|
|
|
|
|
2012-10-01 11:03:16 +04:00
|
|
|
/**
|
2012-09-26 00:13:15 +04:00
|
|
|
* UnixSocketConnector defines the socket creation and connection/listening
|
|
|
|
* functions for a UnixSocketConsumer. Due to the fact that socket setup can
|
|
|
|
* vary between protocols (unix sockets, tcp sockets, bluetooth sockets, etc),
|
|
|
|
* this allows the user to create whatever connection mechanism they need while
|
|
|
|
* still depending on libevent for non-blocking communication handling.
|
|
|
|
*
|
|
|
|
* FIXME/Bug 793980: Currently only virtual, since we only support bluetooth.
|
|
|
|
* Should make connection functions for other unix sockets so we can support
|
|
|
|
* things like RIL.
|
|
|
|
*/
|
|
|
|
class UnixSocketConnector
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
UnixSocketConnector()
|
|
|
|
{}
|
|
|
|
|
|
|
|
virtual ~UnixSocketConnector()
|
|
|
|
{}
|
|
|
|
|
2012-10-01 11:03:16 +04:00
|
|
|
/**
|
2012-09-26 00:13:15 +04:00
|
|
|
* Establishs a file descriptor for a socket.
|
|
|
|
*
|
|
|
|
* @return File descriptor for socket
|
|
|
|
*/
|
|
|
|
virtual int Create() = 0;
|
|
|
|
|
2013-09-06 12:17:55 +04:00
|
|
|
/**
|
2012-10-01 11:03:16 +04:00
|
|
|
* Since most socket specifics are related to address formation into a
|
|
|
|
* sockaddr struct, this function is defined by subclasses and fills in the
|
|
|
|
* structure as needed for whatever connection it is trying to build
|
2012-09-26 00:13:15 +04:00
|
|
|
*
|
2012-10-01 11:03:16 +04:00
|
|
|
* @param aIsServer True is we are acting as a server socket
|
2013-09-06 12:17:55 +04:00
|
|
|
* @param aAddrSize Size of the struct
|
2012-10-01 11:03:16 +04:00
|
|
|
* @param aAddr Struct to fill
|
|
|
|
* @param aAddress If aIsServer is false, Address to connect to. nullptr otherwise.
|
2013-03-19 21:23:47 +04:00
|
|
|
*
|
|
|
|
* @return True if address is filled correctly, false otherwise
|
2012-09-26 00:13:15 +04:00
|
|
|
*/
|
2013-03-19 21:23:47 +04:00
|
|
|
virtual bool CreateAddr(bool aIsServer,
|
2012-10-01 11:03:16 +04:00
|
|
|
socklen_t& aAddrSize,
|
2013-03-19 21:23:47 +04:00
|
|
|
sockaddr_any& aAddr,
|
2012-10-01 11:03:16 +04:00
|
|
|
const char* aAddress) = 0;
|
|
|
|
|
2013-09-06 12:17:55 +04:00
|
|
|
/**
|
2013-10-25 06:00:22 +04:00
|
|
|
* Does any socket type specific setup that may be needed, only for socket
|
|
|
|
* created by ConnectSocket()
|
2012-09-26 00:13:15 +04:00
|
|
|
*
|
2012-10-01 11:03:16 +04:00
|
|
|
* @param aFd File descriptor for opened socket
|
2012-09-26 00:13:15 +04:00
|
|
|
*
|
2012-10-01 11:03:16 +04:00
|
|
|
* @return true is successful, false otherwise
|
2012-09-26 00:13:15 +04:00
|
|
|
*/
|
2012-10-11 09:48:40 +04:00
|
|
|
virtual bool SetUp(int aFd) = 0;
|
2012-10-18 04:11:05 +04:00
|
|
|
|
2013-10-25 06:00:22 +04:00
|
|
|
/**
|
|
|
|
* Perform socket setup for socket created by ListenSocket(), after listen().
|
|
|
|
*
|
|
|
|
* @param aFd File descriptor for opened socket
|
|
|
|
*
|
|
|
|
* @return true is successful, false otherwise
|
|
|
|
*/
|
|
|
|
virtual bool SetUpListenSocket(int aFd) = 0;
|
|
|
|
|
2013-09-06 12:17:55 +04:00
|
|
|
/**
|
2012-10-18 04:11:05 +04:00
|
|
|
* Get address of socket we're currently connected to. Return null string if
|
|
|
|
* not connected.
|
|
|
|
*
|
|
|
|
* @param aAddr Address struct
|
|
|
|
* @param aAddrStr String to store address to
|
|
|
|
*/
|
2013-03-19 21:23:47 +04:00
|
|
|
virtual void GetSocketAddr(const sockaddr_any& aAddr,
|
2012-10-18 04:11:05 +04:00
|
|
|
nsAString& aAddrStr) = 0;
|
|
|
|
|
2012-10-11 09:48:40 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
enum SocketConnectionStatus {
|
|
|
|
SOCKET_DISCONNECTED = 0,
|
2012-10-26 13:28:34 +04:00
|
|
|
SOCKET_LISTENING = 1,
|
|
|
|
SOCKET_CONNECTING = 2,
|
|
|
|
SOCKET_CONNECTED = 3
|
2012-09-26 00:13:15 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
class UnixSocketConsumer : public RefCounted<UnixSocketConsumer>
|
|
|
|
{
|
|
|
|
public:
|
2014-02-21 18:11:33 +04:00
|
|
|
MOZ_DECLARE_REFCOUNTED_TYPENAME(UnixSocketConsumer)
|
2012-10-11 09:48:40 +04:00
|
|
|
UnixSocketConsumer();
|
2012-09-26 00:13:15 +04:00
|
|
|
|
|
|
|
virtual ~UnixSocketConsumer();
|
2012-10-01 11:03:16 +04:00
|
|
|
|
2013-04-05 04:52:12 +04:00
|
|
|
SocketConnectionStatus GetConnectionStatus() const
|
2012-10-11 09:48:40 +04:00
|
|
|
{
|
2013-12-09 17:21:18 +04:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2012-10-11 09:48:40 +04:00
|
|
|
return mConnectionStatus;
|
|
|
|
}
|
|
|
|
|
2014-02-14 11:03:05 +04:00
|
|
|
int GetSuggestedConnectDelayMs() const
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
return mConnectDelayMs;
|
|
|
|
}
|
|
|
|
|
2012-10-01 11:03:16 +04:00
|
|
|
/**
|
2012-09-26 00:13:15 +04:00
|
|
|
* Function to be called whenever data is received. This is only called on the
|
|
|
|
* main thread.
|
|
|
|
*
|
|
|
|
* @param aMessage Data received from the socket.
|
|
|
|
*/
|
2013-02-01 16:28:15 +04:00
|
|
|
virtual void ReceiveSocketData(nsAutoPtr<UnixSocketRawData>& aMessage) = 0;
|
2012-09-26 00:13:15 +04:00
|
|
|
|
2012-10-01 11:03:16 +04:00
|
|
|
/**
|
2012-09-26 00:13:15 +04:00
|
|
|
* Queue data to be sent to the socket on the IO thread. Can only be called on
|
|
|
|
* originating thread.
|
|
|
|
*
|
|
|
|
* @param aMessage Data to be sent to socket
|
|
|
|
*
|
|
|
|
* @return true if data is queued, false otherwise (i.e. not connected)
|
|
|
|
*/
|
|
|
|
bool SendSocketData(UnixSocketRawData* aMessage);
|
|
|
|
|
2012-10-01 11:03:16 +04:00
|
|
|
/**
|
2012-09-26 00:13:15 +04:00
|
|
|
* Convenience function for sending strings to the socket (common in bluetooth
|
|
|
|
* profile usage). Converts to a UnixSocketRawData struct. Can only be called
|
|
|
|
* on originating thread.
|
|
|
|
*
|
|
|
|
* @param aMessage String to be sent to socket
|
|
|
|
*
|
|
|
|
* @return true if data is queued, false otherwise (i.e. not connected)
|
|
|
|
*/
|
|
|
|
bool SendSocketData(const nsACString& aMessage);
|
|
|
|
|
2012-10-01 11:03:16 +04:00
|
|
|
/**
|
|
|
|
* Starts a task on the socket that will try to connect to a socket in a
|
|
|
|
* non-blocking manner.
|
2012-09-26 00:13:15 +04:00
|
|
|
*
|
|
|
|
* @param aConnector Connector object for socket type specific functions
|
|
|
|
* @param aAddress Address to connect to.
|
2013-02-01 16:28:18 +04:00
|
|
|
* @param aDelayMs Time delay in milli-seconds.
|
2012-09-26 00:13:15 +04:00
|
|
|
*
|
2012-10-01 11:03:16 +04:00
|
|
|
* @return true on connect task started, false otherwise.
|
2012-09-26 00:13:15 +04:00
|
|
|
*/
|
2013-02-01 16:28:18 +04:00
|
|
|
bool ConnectSocket(UnixSocketConnector* aConnector,
|
|
|
|
const char* aAddress,
|
|
|
|
int aDelayMs = 0);
|
2012-09-26 00:13:15 +04:00
|
|
|
|
2013-09-06 12:17:55 +04:00
|
|
|
/**
|
2012-10-01 11:03:16 +04:00
|
|
|
* Starts a task on the socket that will try to accept a new connection in a
|
|
|
|
* non-blocking manner.
|
|
|
|
*
|
|
|
|
* @param aConnector Connector object for socket type specific functions
|
|
|
|
*
|
|
|
|
* @return true on listen started, false otherwise
|
|
|
|
*/
|
|
|
|
bool ListenSocket(UnixSocketConnector* aConnector);
|
|
|
|
|
|
|
|
/**
|
2012-09-26 00:13:15 +04:00
|
|
|
* Queues the internal representation of socket for deletion. Can be called
|
|
|
|
* from main thread.
|
|
|
|
*/
|
|
|
|
void CloseSocket();
|
2012-09-06 07:06:06 +04:00
|
|
|
|
2013-09-06 12:17:55 +04:00
|
|
|
/**
|
2012-10-11 09:48:40 +04:00
|
|
|
* Callback for socket connect/accept success. Called after connect/accept has
|
|
|
|
* finished. Will be run on main thread, before any reads take place.
|
|
|
|
*/
|
|
|
|
virtual void OnConnectSuccess() = 0;
|
|
|
|
|
2013-09-06 12:17:55 +04:00
|
|
|
/**
|
2012-10-11 09:48:40 +04:00
|
|
|
* Callback for socket connect/accept error. Will be run on main thread.
|
|
|
|
*/
|
|
|
|
virtual void OnConnectError() = 0;
|
|
|
|
|
2013-09-06 12:17:55 +04:00
|
|
|
/**
|
2012-10-12 22:38:14 +04:00
|
|
|
* Callback for socket disconnect. Will be run on main thread.
|
|
|
|
*/
|
|
|
|
virtual void OnDisconnect() = 0;
|
2012-10-11 09:48:40 +04:00
|
|
|
|
2013-09-06 12:17:55 +04:00
|
|
|
/**
|
2012-10-11 09:48:40 +04:00
|
|
|
* Called by implementation to notify consumer of success.
|
|
|
|
*/
|
|
|
|
void NotifySuccess();
|
|
|
|
|
2013-09-06 12:17:55 +04:00
|
|
|
/**
|
2012-10-11 09:48:40 +04:00
|
|
|
* Called by implementation to notify consumer of error.
|
|
|
|
*/
|
|
|
|
void NotifyError();
|
|
|
|
|
2013-09-06 12:17:55 +04:00
|
|
|
/**
|
2012-10-12 22:38:14 +04:00
|
|
|
* Called by implementation to notify consumer of disconnect.
|
|
|
|
*/
|
|
|
|
void NotifyDisconnect();
|
2012-10-18 04:10:27 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the current sockaddr for the socket
|
|
|
|
*/
|
2012-10-18 04:11:05 +04:00
|
|
|
void GetSocketAddr(nsAString& aAddrStr);
|
|
|
|
|
2012-09-26 00:13:15 +04:00
|
|
|
private:
|
2014-02-14 11:03:05 +04:00
|
|
|
uint32_t CalculateConnectDelayMs() const;
|
|
|
|
|
2012-10-06 03:05:35 +04:00
|
|
|
UnixSocketImpl* mImpl;
|
2012-10-11 09:48:40 +04:00
|
|
|
SocketConnectionStatus mConnectionStatus;
|
2014-02-14 11:03:05 +04:00
|
|
|
PRIntervalTime mConnectTimestamp;
|
|
|
|
uint32_t mConnectDelayMs;
|
2012-09-26 00:13:15 +04:00
|
|
|
};
|
2012-09-06 07:06:06 +04:00
|
|
|
|
|
|
|
} // namespace ipc
|
|
|
|
} // namepsace mozilla
|
|
|
|
|
|
|
|
#endif // mozilla_ipc_Socket_h
|