* [Bluez-devel] [Patch] Extending the serial service to export a TCP socket.
@ 2007-12-13 15:01 Vinicius Gomes
2007-12-13 16:00 ` Marcel Holtmann
0 siblings, 1 reply; 4+ messages in thread
From: Vinicius Gomes @ 2007-12-13 15:01 UTC (permalink / raw)
To: bluez-devel
[-- Attachment #1: Type: text/plain, Size: 381 bytes --]
Hi Marcel,
It only exports a local (127.0.0.1) port, but could be easily modified
to export a port from any host. It is really simple as all the hard
work was already done for the Unix socket proxy. My primary interest
is export the internal N810 GPS so other devices can use it.
It is based in the latest git tree.
--
Vinicius Costa Gomes
INdT - Instituto Nokia de Tecnologia
[-- Attachment #2: tcp-proxy.patch --]
[-- Type: application/octet-stream, Size: 6811 bytes --]
diff --git a/serial/manager.c b/serial/manager.c
index b45958b..b326975 100644
--- a/serial/manager.c
+++ b/serial/manager.c
@@ -105,22 +105,24 @@ static struct {
typedef enum {
TTY_PROXY,
UNIX_SOCKET_PROXY,
+ TCP_SOCKET_PROXY,
UNKNOWN_PROXY_TYPE = 0xFF
} proxy_type_t;
struct proxy {
- bdaddr_t src;
- bdaddr_t dst;
- char *uuid128; /* UUID 128 */
- char *address; /* TTY or Unix socket name */
- proxy_type_t type; /* TTY or Unix socket */
- struct termios sys_ti; /* Default TTY setting */
- struct termios proxy_ti; /* Proxy TTY settings */
- uint8_t channel; /* RFCOMM channel */
- uint32_t record_id; /* Service record id */
- guint listen_watch; /* Server listen watch */
- guint rfcomm_watch; /* RFCOMM watch: Remote */
- guint local_watch; /* Local watch: TTY or Unix socket */
+ bdaddr_t src;
+ bdaddr_t dst;
+ char *uuid128; /* UUID 128 */
+ char *address; /* TTY, Unix or TCP socket name */
+ unsigned short int port; /* TCP port */
+ proxy_type_t type; /* TTY, Unix or TCP socket */
+ struct termios sys_ti; /* Default TTY setting */
+ struct termios proxy_ti; /* Proxy TTY settings */
+ uint8_t channel; /* RFCOMM channel */
+ uint32_t record_id; /* Service record id */
+ guint listen_watch; /* Server listen watch */
+ guint rfcomm_watch; /* RFCOMM watch: Remote */
+ guint local_watch; /* Local watch: TTY or Unix socket */
};
static DBusConnection *connection = NULL;
@@ -586,7 +588,7 @@ static void record_reply(DBusPendingCall *call, void *data)
err = rfcomm_connect(pc);
if (err < 0) {
error("RFCOMM connection failed");
- error_connection_attempt_failed(pc->conn,
+ error_connection_attempt_failed(pc->conn,
pc->msg, -err);
goto fail;
}
@@ -657,7 +659,7 @@ static void handles_reply(DBusPendingCall *call, void *data)
/* FIXME : forward error as is */
if (dbus_error_has_name(&derr,
"org.bluez.Error.ConnectionAttemptFailed"))
- error_connection_attempt_failed(pc->conn,
+ error_connection_attempt_failed(pc->conn,
pc->msg, EIO);
else
error_not_supported(pc->conn, pc->msg);
@@ -667,7 +669,7 @@ static void handles_reply(DBusPendingCall *call, void *data)
dbus_error_free(&derr);
goto fail;
}
-
+
if (!dbus_message_get_args(reply, &derr,
DBUS_TYPE_ARRAY, DBUS_TYPE_UINT32, &phandle,
&len, DBUS_TYPE_INVALID)) {
@@ -1287,6 +1289,49 @@ static inline int unix_socket_connect(const char *address)
return sk;
}
+static int tcp_socket_connect(const char *address)
+{
+ struct sockaddr_in addr;
+ int err, sk;
+ unsigned short int port;
+
+ memset(&addr, 0, sizeof(addr));
+
+ if (strncmp(address, "localhost", 9) != 0) {
+ error("Address should have the form localhost:'port'.");
+ return -1;
+ }
+
+ port = atoi(strchr(address, ':') + 1);
+ if (port <= 0) {
+ error("Invalid port '%d'.", port);
+ return -1;
+ }
+
+ addr.sin_family = AF_INET;
+ addr.sin_addr.s_addr = inet_addr("127.0.0.1");
+ addr.sin_port = htons(port);
+
+ sk = socket(PF_INET, SOCK_STREAM, 0);
+ if (sk < 0) {
+ err = errno;
+ error("TCP socket(%s) create failed %s(%d)", address,
+ strerror(err), err);
+ return -err;
+ }
+
+ if (connect(sk, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
+ err = errno;
+ error("TCP socket(%s) connect failed: %s(%d)",
+ address, strerror(err), err);
+ close(sk);
+ errno = err;
+ return -err;
+ }
+
+ return sk;
+}
+
static inline int tty_open(const char *tty, struct termios *ti)
{
int err, sk;
@@ -1337,10 +1382,19 @@ static gboolean connect_event(GIOChannel *chan,
bacpy(&prx->dst, &raddr.rc_bdaddr);
- if (prx->type == UNIX_SOCKET_PROXY)
+ switch (prx->type) {
+ case UNIX_SOCKET_PROXY:
lsk = unix_socket_connect(prx->address);
- else
+ break;
+ case TTY_PROXY:
lsk = tty_open(prx->address, &prx->proxy_ti);
+ break;
+ case TCP_SOCKET_PROXY:
+ lsk = tcp_socket_connect(prx->address);
+ break;
+ default:
+ lsk = -1;
+ }
if (lsk < 0) {
close(rsk);
@@ -1753,6 +1807,25 @@ static int proxy_socket_register(bdaddr_t *src, const char *uuid128,
return ret;
}
+static int proxy_tcp_register(bdaddr_t *src, const char *uuid128,
+ const char *address, char *outpath, size_t size)
+{
+ struct proxy *prx;
+ int ret;
+
+ prx = g_new0(struct proxy, 1);
+ prx->address = g_strdup(address);
+ prx->uuid128 = g_strdup(uuid128);
+ prx->type = TCP_SOCKET_PROXY;
+ bacpy(&prx->src, src);
+
+ ret = register_proxy_object(prx, outpath, size);
+ if (ret < 0)
+ proxy_free(prx);
+
+ return ret;
+}
+
static proxy_type_t addr2type(const char *address)
{
struct stat st;
@@ -1763,10 +1836,14 @@ static proxy_type_t addr2type(const char *address)
* it refers to abstract namespace. 'x00' will be used
* to represent the null byte.
*/
+ if (strncmp("localhost:", address, 10) == 0)
+ return TCP_SOCKET_PROXY;
+
if (strncmp("x00", address, 3) != 0)
return UNKNOWN_PROXY_TYPE;
else
return UNIX_SOCKET_PROXY;
+
} else {
/* Filesystem: char device or unix socket */
if (S_ISCHR(st.st_mode) && strncmp("/dev/", address, 4) == 0)
@@ -1833,12 +1910,22 @@ static DBusHandlerResult create_proxy(DBusConnection *conn,
if (!reply)
return DBUS_HANDLER_RESULT_NEED_MEMORY;
- if (type != TTY_PROXY)
+ switch (type) {
+ case UNIX_SOCKET_PROXY:
ret = proxy_socket_register(&src, uuid128,
- address, path, sizeof(path));
- else
+ address, path, sizeof(path));
+ break;
+ case TTY_PROXY:
ret = proxy_tty_register(&src, uuid128,
address, NULL, path, sizeof(path));
+ break;
+ case TCP_SOCKET_PROXY:
+ ret = proxy_tcp_register(&src, uuid128, address,
+ path, sizeof(path));
+ break;
+ default:
+ ret = -1;
+ }
if (ret < 0) {
dbus_message_unref(reply);
@@ -2065,7 +2152,7 @@ static DBusHandlerResult cancel_connect_service(DBusConnection *conn,
pending = find_pending_connect_by_pattern(bda, pattern);
if (!pending)
- return error_does_not_exist(conn, msg,
+ return error_does_not_exist(conn, msg,
"No such connection request");
reply = dbus_message_new_method_return(msg);
@@ -2087,7 +2174,7 @@ static void proxy_path_free(gpointer data, gpointer udata)
if (dbus_connection_get_object_user_data(conn,
path, (void *) &prx) && prx) {
struct termios *ti;
-
+
ti = (prx->type == TTY_PROXY ? &prx->proxy_ti : NULL);
proxy_store(&prx->src, prx->uuid128, prx->address, NULL,
prx->channel, 0, ti);
@@ -2244,6 +2331,9 @@ static void parse_proxy(char *key, char *value, void *data)
case UNIX_SOCKET_PROXY:
proxy_socket_register(&src, uuid128, key, NULL, 0);
break;
+ case TCP_SOCKET_PROXY:
+ proxy_tcp_register(&src, uuid128, key, NULL, 0);
+ break;
default:
return;
}
[-- Attachment #3: Type: text/plain, Size: 308 bytes --]
-------------------------------------------------------------------------
SF.Net email is sponsored by:
Check out the new SourceForge.net Marketplace.
It's the best place to buy or sell services
for just about anything Open Source.
http://ad.doubleclick.net/clk;164216239;13503038;w?http://sf.net/marketplace
[-- Attachment #4: Type: text/plain, Size: 164 bytes --]
_______________________________________________
Bluez-devel mailing list
Bluez-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/bluez-devel
^ permalink raw reply related [flat|nested] 4+ messages in thread
* Re: [Bluez-devel] [Patch] Extending the serial service to export a TCP socket.
2007-12-13 15:01 [Bluez-devel] [Patch] Extending the serial service to export a TCP socket Vinicius Gomes
@ 2007-12-13 16:00 ` Marcel Holtmann
2007-12-13 18:41 ` Vinicius Gomes
0 siblings, 1 reply; 4+ messages in thread
From: Marcel Holtmann @ 2007-12-13 16:00 UTC (permalink / raw)
To: BlueZ development
Hi Vinicius,
> It only exports a local (127.0.0.1) port, but could be easily modified
> to export a port from any host. It is really simple as all the hard
> work was already done for the Unix socket proxy. My primary interest
> is export the internal N810 GPS so other devices can use it.
patch looks good, but it contains to many unneeded hunks. Which
indicates that you only changed whitespace into tabs or vise versa.
Please use an editor that visually shows whitespaces and tabs.
First thing it is "unsigned short port" and not some "short int" or
something. This also means you can skip all reformatting in struct
proxy.
Don't introduce extra empty lines in your if or case statements. They
are not needed.
Regards
Marcel
-------------------------------------------------------------------------
SF.Net email is sponsored by:
Check out the new SourceForge.net Marketplace.
It's the best place to buy or sell services
for just about anything Open Source.
http://ad.doubleclick.net/clk;164216239;13503038;w?http://sf.net/marketplace
_______________________________________________
Bluez-devel mailing list
Bluez-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/bluez-devel
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [Bluez-devel] [Patch] Extending the serial service to export a TCP socket.
2007-12-13 16:00 ` Marcel Holtmann
@ 2007-12-13 18:41 ` Vinicius Gomes
2007-12-13 19:09 ` Marcel Holtmann
0 siblings, 1 reply; 4+ messages in thread
From: Vinicius Gomes @ 2007-12-13 18:41 UTC (permalink / raw)
To: BlueZ development
[-- Attachment #1: Type: text/plain, Size: 1558 bytes --]
On 12/13/07, Marcel Holtmann <marcel@holtmann.org> wrote:
> Hi Vinicius,
>
> > It only exports a local (127.0.0.1) port, but could be easily modified
> > to export a port from any host. It is really simple as all the hard
> > work was already done for the Unix socket proxy. My primary interest
> > is export the internal N810 GPS so other devices can use it.
>
> patch looks good, but it contains to many unneeded hunks. Which
> indicates that you only changed whitespace into tabs or vise versa.
> Please use an editor that visually shows whitespaces and tabs.
Many of these hunks were showing whitespace being removed from the end
of some lines. Also there was a line with just a tab on it.
> First thing it is "unsigned short port" and not some "short int" or
> something. This also means you can skip all reformatting in struct
> proxy.
Much better, done.
> Don't introduce extra empty lines in your if or case statements. They
> are not needed.
Also done.
> Regards
>
> Marcel
>
>
>
> -------------------------------------------------------------------------
> SF.Net email is sponsored by:
> Check out the new SourceForge.net Marketplace.
> It's the best place to buy or sell services
> for just about anything Open Source.
> http://ad.doubleclick.net/clk;164216239;13503038;w?http://sf.net/marketplace
> _______________________________________________
> Bluez-devel mailing list
> Bluez-devel@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/bluez-devel
>
Cheers,
--
Vinicius Gomes
INdT - Instituto Nokia de Tecnologia
[-- Attachment #2: tcp-proxy.patch --]
[-- Type: application/octet-stream, Size: 5784 bytes --]
diff --git a/serial/manager.c b/serial/manager.c
index b45958b..25693f9 100644
--- a/serial/manager.c
+++ b/serial/manager.c
@@ -105,6 +105,7 @@ static struct {
typedef enum {
TTY_PROXY,
UNIX_SOCKET_PROXY,
+ TCP_SOCKET_PROXY,
UNKNOWN_PROXY_TYPE = 0xFF
} proxy_type_t;
@@ -113,6 +114,7 @@ struct proxy {
bdaddr_t dst;
char *uuid128; /* UUID 128 */
char *address; /* TTY or Unix socket name */
+ short int port; /* TCP port */
proxy_type_t type; /* TTY or Unix socket */
struct termios sys_ti; /* Default TTY setting */
struct termios proxy_ti; /* Proxy TTY settings */
@@ -586,7 +588,7 @@ static void record_reply(DBusPendingCall *call, void *data)
err = rfcomm_connect(pc);
if (err < 0) {
error("RFCOMM connection failed");
- error_connection_attempt_failed(pc->conn,
+ error_connection_attempt_failed(pc->conn,
pc->msg, -err);
goto fail;
}
@@ -657,7 +659,7 @@ static void handles_reply(DBusPendingCall *call, void *data)
/* FIXME : forward error as is */
if (dbus_error_has_name(&derr,
"org.bluez.Error.ConnectionAttemptFailed"))
- error_connection_attempt_failed(pc->conn,
+ error_connection_attempt_failed(pc->conn,
pc->msg, EIO);
else
error_not_supported(pc->conn, pc->msg);
@@ -667,7 +669,7 @@ static void handles_reply(DBusPendingCall *call, void *data)
dbus_error_free(&derr);
goto fail;
}
-
+
if (!dbus_message_get_args(reply, &derr,
DBUS_TYPE_ARRAY, DBUS_TYPE_UINT32, &phandle,
&len, DBUS_TYPE_INVALID)) {
@@ -1287,6 +1289,45 @@ static inline int unix_socket_connect(const char *address)
return sk;
}
+static int tcp_socket_connect(const char *address)
+{
+ struct sockaddr_in addr;
+ int err, sk;
+ unsigned short int port;
+
+ memset(&addr, 0, sizeof(addr));
+
+ if (strncmp(address, "localhost", 9) != 0) {
+ error("Address should have the form localhost:port.");
+ return -1;
+ }
+ port = atoi(strchr(address, ':') + 1);
+ if (port <= 0) {
+ error("Invalid port '%d'.", port);
+ return -1;
+ }
+ addr.sin_family = AF_INET;
+ addr.sin_addr.s_addr = inet_addr("127.0.0.1");
+ addr.sin_port = htons(port);
+
+ sk = socket(PF_INET, SOCK_STREAM, 0);
+ if (sk < 0) {
+ err = errno;
+ error("TCP socket(%s) create failed %s(%d)", address,
+ strerror(err), err);
+ return -err;
+ }
+ if (connect(sk, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
+ err = errno;
+ error("TCP socket(%s) connect failed: %s(%d)",
+ address, strerror(err), err);
+ close(sk);
+ errno = err;
+ return -err;
+ }
+ return sk;
+}
+
static inline int tty_open(const char *tty, struct termios *ti)
{
int err, sk;
@@ -1337,10 +1378,19 @@ static gboolean connect_event(GIOChannel *chan,
bacpy(&prx->dst, &raddr.rc_bdaddr);
- if (prx->type == UNIX_SOCKET_PROXY)
+ switch (prx->type) {
+ case UNIX_SOCKET_PROXY:
lsk = unix_socket_connect(prx->address);
- else
+ break;
+ case TTY_PROXY:
lsk = tty_open(prx->address, &prx->proxy_ti);
+ break;
+ case TCP_SOCKET_PROXY:
+ lsk = tcp_socket_connect(prx->address);
+ break;
+ default:
+ lsk = -1;
+ }
if (lsk < 0) {
close(rsk);
@@ -1753,6 +1803,25 @@ static int proxy_socket_register(bdaddr_t *src, const char *uuid128,
return ret;
}
+static int proxy_tcp_register(bdaddr_t *src, const char *uuid128,
+ const char *address, char *outpath, size_t size)
+{
+ struct proxy *prx;
+ int ret;
+
+ prx = g_new0(struct proxy, 1);
+ prx->address = g_strdup(address);
+ prx->uuid128 = g_strdup(uuid128);
+ prx->type = TCP_SOCKET_PROXY;
+ bacpy(&prx->src, src);
+
+ ret = register_proxy_object(prx, outpath, size);
+ if (ret < 0)
+ proxy_free(prx);
+
+ return ret;
+}
+
static proxy_type_t addr2type(const char *address)
{
struct stat st;
@@ -1763,6 +1832,8 @@ static proxy_type_t addr2type(const char *address)
* it refers to abstract namespace. 'x00' will be used
* to represent the null byte.
*/
+ if (strncmp("localhost:", address, 10) == 0)
+ return TCP_SOCKET_PROXY;
if (strncmp("x00", address, 3) != 0)
return UNKNOWN_PROXY_TYPE;
else
@@ -1833,13 +1904,22 @@ static DBusHandlerResult create_proxy(DBusConnection *conn,
if (!reply)
return DBUS_HANDLER_RESULT_NEED_MEMORY;
- if (type != TTY_PROXY)
+ switch (type) {
+ case UNIX_SOCKET_PROXY:
ret = proxy_socket_register(&src, uuid128,
- address, path, sizeof(path));
- else
+ address, path, sizeof(path));
+ break;
+ case TTY_PROXY:
ret = proxy_tty_register(&src, uuid128,
address, NULL, path, sizeof(path));
-
+ break;
+ case TCP_SOCKET_PROXY:
+ ret = proxy_tcp_register(&src, uuid128, address,
+ path, sizeof(path));
+ break;
+ default:
+ ret = -1;
+ }
if (ret < 0) {
dbus_message_unref(reply);
return error_failed(conn, msg, "Create object path failed");
@@ -2065,7 +2145,7 @@ static DBusHandlerResult cancel_connect_service(DBusConnection *conn,
pending = find_pending_connect_by_pattern(bda, pattern);
if (!pending)
- return error_does_not_exist(conn, msg,
+ return error_does_not_exist(conn, msg,
"No such connection request");
reply = dbus_message_new_method_return(msg);
@@ -2087,7 +2167,7 @@ static void proxy_path_free(gpointer data, gpointer udata)
if (dbus_connection_get_object_user_data(conn,
path, (void *) &prx) && prx) {
struct termios *ti;
-
+
ti = (prx->type == TTY_PROXY ? &prx->proxy_ti : NULL);
proxy_store(&prx->src, prx->uuid128, prx->address, NULL,
prx->channel, 0, ti);
@@ -2244,6 +2324,9 @@ static void parse_proxy(char *key, char *value, void *data)
case UNIX_SOCKET_PROXY:
proxy_socket_register(&src, uuid128, key, NULL, 0);
break;
+ case TCP_SOCKET_PROXY:
+ proxy_tcp_register(&src, uuid128, key, NULL, 0);
+ break;
default:
return;
}
[-- Attachment #3: Type: text/plain, Size: 308 bytes --]
-------------------------------------------------------------------------
SF.Net email is sponsored by:
Check out the new SourceForge.net Marketplace.
It's the best place to buy or sell services
for just about anything Open Source.
http://ad.doubleclick.net/clk;164216239;13503038;w?http://sf.net/marketplace
[-- Attachment #4: Type: text/plain, Size: 164 bytes --]
_______________________________________________
Bluez-devel mailing list
Bluez-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/bluez-devel
^ permalink raw reply related [flat|nested] 4+ messages in thread
* Re: [Bluez-devel] [Patch] Extending the serial service to export a TCP socket.
2007-12-13 18:41 ` Vinicius Gomes
@ 2007-12-13 19:09 ` Marcel Holtmann
0 siblings, 0 replies; 4+ messages in thread
From: Marcel Holtmann @ 2007-12-13 19:09 UTC (permalink / raw)
To: BlueZ development
Hi Vinicius,
> > > It only exports a local (127.0.0.1) port, but could be easily modified
> > > to export a port from any host. It is really simple as all the hard
> > > work was already done for the Unix socket proxy. My primary interest
> > > is export the internal N810 GPS so other devices can use it.
> >
> > patch looks good, but it contains to many unneeded hunks. Which
> > indicates that you only changed whitespace into tabs or vise versa.
> > Please use an editor that visually shows whitespaces and tabs.
>
> Many of these hunks were showing whitespace being removed from the end
> of some lines. Also there was a line with just a tab on it.
and your new patch introduced as many as it fixes. I am serious about
this and once you look at the diff file, I can even tell without
whitespace visualization. I fixed it up for you, but make sure in the
future this is all sane and sound. Patch is in the CVS.
Regards
Marcel
-------------------------------------------------------------------------
SF.Net email is sponsored by:
Check out the new SourceForge.net Marketplace.
It's the best place to buy or sell services
for just about anything Open Source.
http://ad.doubleclick.net/clk;164216239;13503038;w?http://sf.net/marketplace
_______________________________________________
Bluez-devel mailing list
Bluez-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/bluez-devel
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2007-12-13 19:09 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2007-12-13 15:01 [Bluez-devel] [Patch] Extending the serial service to export a TCP socket Vinicius Gomes
2007-12-13 16:00 ` Marcel Holtmann
2007-12-13 18:41 ` Vinicius Gomes
2007-12-13 19:09 ` Marcel Holtmann
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).