2009-05-21 18:44:01 +04:00
|
|
|
/* $NetBSD: rfcomm_sppd.c,v 1.11 2009/05/21 14:44:01 plunky Exp $ */
|
2006-06-19 19:44:33 +04:00
|
|
|
|
|
|
|
/*-
|
|
|
|
* Copyright (c) 2006 Itronix Inc.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. The name of Itronix Inc. may not be used to endorse
|
|
|
|
* or promote products derived from this software without specific
|
|
|
|
* prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY ITRONIX INC. ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
|
|
|
|
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
|
|
|
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ITRONIX INC. BE LIABLE FOR ANY
|
|
|
|
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
|
|
|
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
|
|
|
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
|
|
|
* ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
/*
|
2009-05-12 22:43:35 +04:00
|
|
|
* Copyright (c) 2009 The NetBSD Foundation, Inc.
|
2007-01-31 11:12:21 +03:00
|
|
|
* Copyright (c) 2007 Iain Hibbert
|
2006-06-19 19:44:33 +04:00
|
|
|
* Copyright (c) 2003 Maksim Yevmenkin <m_evmenkin@yahoo.com>
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/cdefs.h>
|
2009-05-12 22:43:35 +04:00
|
|
|
__COPYRIGHT("@(#) Copyright (c) 2009 The NetBSD Foundation, Inc.\
|
|
|
|
Copyright (c) 2007 Iain Hibbert.\
|
2008-07-21 18:19:20 +04:00
|
|
|
Copyright (c) 2006 Itronix, Inc.\
|
|
|
|
Copyright (c) 2003 Maksim Yevmenkin m_evmenkin@yahoo.com.\
|
|
|
|
All rights reserved.");
|
2009-05-21 18:44:01 +04:00
|
|
|
__RCSID("$NetBSD: rfcomm_sppd.c,v 1.11 2009/05/21 14:44:01 plunky Exp $");
|
|
|
|
|
|
|
|
#include <sys/param.h>
|
2006-06-19 19:44:33 +04:00
|
|
|
|
|
|
|
#include <bluetooth.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <err.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <grp.h>
|
|
|
|
#include <limits.h>
|
|
|
|
#include <paths.h>
|
|
|
|
#include <sdp.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <syslog.h>
|
|
|
|
#include <termios.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2007-04-21 10:15:22 +04:00
|
|
|
#include <netbt/rfcomm.h>
|
|
|
|
|
2007-01-31 11:12:21 +03:00
|
|
|
int open_tty(const char *);
|
2007-04-21 10:15:22 +04:00
|
|
|
int open_client(bdaddr_t *, bdaddr_t *, int, const char *);
|
|
|
|
int open_server(bdaddr_t *, uint8_t, int, const char *);
|
2007-01-31 11:12:21 +03:00
|
|
|
void copy_data(int, int);
|
2009-05-12 22:43:35 +04:00
|
|
|
int channel_lookup(const bdaddr_t *, const bdaddr_t *, uint16_t, uintmax_t *);
|
2007-01-31 11:12:21 +03:00
|
|
|
void sighandler(int);
|
|
|
|
void usage(void);
|
|
|
|
void reset_tio(void);
|
|
|
|
|
|
|
|
int done; /* got a signal */
|
|
|
|
struct termios tio; /* stored termios for reset on exit */
|
|
|
|
|
|
|
|
struct service {
|
2009-05-12 22:43:35 +04:00
|
|
|
const char * name;
|
|
|
|
const char * description;
|
2007-01-31 11:12:21 +03:00
|
|
|
uint16_t class;
|
|
|
|
} services[] = {
|
|
|
|
{ "DUN", "Dialup Networking",
|
2009-05-12 22:43:35 +04:00
|
|
|
SDP_SERVICE_CLASS_DIALUP_NETWORKING },
|
|
|
|
{ "LAN", "LAN access using PPP",
|
|
|
|
SDP_SERVICE_CLASS_LAN_ACCESS_USING_PPP },
|
2007-01-31 11:12:21 +03:00
|
|
|
{ "SP", "Serial Port",
|
2009-05-12 22:43:35 +04:00
|
|
|
SDP_SERVICE_CLASS_SERIAL_PORT },
|
|
|
|
{ NULL, NULL, 0 }
|
2007-01-31 11:12:21 +03:00
|
|
|
};
|
2006-06-19 19:44:33 +04:00
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc, char *argv[])
|
|
|
|
{
|
2007-01-31 11:12:21 +03:00
|
|
|
struct termios t;
|
|
|
|
bdaddr_t laddr, raddr;
|
|
|
|
fd_set rdset;
|
2007-03-31 11:14:44 +04:00
|
|
|
const char *service;
|
|
|
|
char *ep, *tty;
|
2007-04-21 10:15:22 +04:00
|
|
|
int lm, n, rfcomm, tty_in, tty_out;
|
2007-01-31 11:12:21 +03:00
|
|
|
uint8_t channel;
|
2006-06-19 19:44:33 +04:00
|
|
|
|
|
|
|
bdaddr_copy(&laddr, BDADDR_ANY);
|
|
|
|
bdaddr_copy(&raddr, BDADDR_ANY);
|
2007-03-02 00:44:30 +03:00
|
|
|
service = "SP";
|
|
|
|
tty = NULL;
|
2007-01-31 11:12:21 +03:00
|
|
|
channel = 0;
|
2007-04-21 10:15:22 +04:00
|
|
|
lm = 0;
|
2006-06-19 19:44:33 +04:00
|
|
|
|
|
|
|
/* Parse command line options */
|
2007-04-21 10:15:22 +04:00
|
|
|
while ((n = getopt(argc, argv, "a:c:d:hm:s:t:")) != -1) {
|
2006-06-19 19:44:33 +04:00
|
|
|
switch (n) {
|
2007-01-31 11:12:21 +03:00
|
|
|
case 'a': /* remote device address */
|
2006-06-19 19:44:33 +04:00
|
|
|
if (!bt_aton(optarg, &raddr)) {
|
|
|
|
struct hostent *he = NULL;
|
|
|
|
|
|
|
|
if ((he = bt_gethostbyname(optarg)) == NULL)
|
2007-01-31 11:12:21 +03:00
|
|
|
errx(EXIT_FAILURE, "%s: %s", optarg,
|
|
|
|
hstrerror(h_errno));
|
2006-06-19 19:44:33 +04:00
|
|
|
|
|
|
|
bdaddr_copy(&raddr, (bdaddr_t *)he->h_addr);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'c': /* RFCOMM channel */
|
|
|
|
channel = strtoul(optarg, &ep, 10);
|
2007-03-02 00:44:30 +03:00
|
|
|
if (*ep != '\0' || channel < 1 || channel > 30)
|
|
|
|
errx(EXIT_FAILURE, "Invalid channel: %s", optarg);
|
|
|
|
|
2006-06-19 19:44:33 +04:00
|
|
|
break;
|
|
|
|
|
2007-01-31 11:12:21 +03:00
|
|
|
case 'd': /* local device address */
|
2006-06-19 19:44:33 +04:00
|
|
|
if (!bt_devaddr(optarg, &laddr))
|
|
|
|
err(EXIT_FAILURE, "%s", optarg);
|
2007-01-31 11:12:21 +03:00
|
|
|
|
|
|
|
break;
|
|
|
|
|
2007-04-21 10:15:22 +04:00
|
|
|
case 'm': /* Link Mode */
|
|
|
|
if (strcasecmp(optarg, "auth") == 0)
|
|
|
|
lm = RFCOMM_LM_AUTH;
|
|
|
|
else if (strcasecmp(optarg, "encrypt") == 0)
|
|
|
|
lm = RFCOMM_LM_ENCRYPT;
|
|
|
|
else if (strcasecmp(optarg, "secure") == 0)
|
|
|
|
lm = RFCOMM_LM_SECURE;
|
|
|
|
else
|
|
|
|
errx(EXIT_FAILURE, "%s: unknown mode", optarg);
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
2007-03-02 00:44:30 +03:00
|
|
|
case 's': /* service class */
|
2007-01-31 11:12:21 +03:00
|
|
|
service = optarg;
|
2006-06-19 19:44:33 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 't': /* Slave TTY name */
|
|
|
|
if (optarg[0] != '/')
|
|
|
|
asprintf(&tty, "%s%s", _PATH_DEV, optarg);
|
|
|
|
else
|
|
|
|
tty = optarg;
|
2007-01-31 11:12:21 +03:00
|
|
|
|
2006-06-19 19:44:33 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'h':
|
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
/* NOT REACHED */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-01-31 11:12:21 +03:00
|
|
|
/*
|
|
|
|
* validate options:
|
2007-03-02 00:44:30 +03:00
|
|
|
* must have channel or remote address but not both
|
2007-01-31 11:12:21 +03:00
|
|
|
*/
|
2007-03-02 00:44:30 +03:00
|
|
|
if ((channel == 0 && bdaddr_any(&raddr))
|
|
|
|
|| (channel != 0 && !bdaddr_any(&raddr)))
|
2006-06-19 19:44:33 +04:00
|
|
|
usage();
|
|
|
|
|
2007-01-31 11:12:21 +03:00
|
|
|
/*
|
|
|
|
* grab ttys before we start the bluetooth
|
|
|
|
*/
|
2006-06-19 19:44:33 +04:00
|
|
|
if (tty == NULL) {
|
2007-01-31 11:12:21 +03:00
|
|
|
tty_in = STDIN_FILENO;
|
|
|
|
tty_out = STDOUT_FILENO;
|
2006-06-19 19:44:33 +04:00
|
|
|
} else {
|
2007-01-31 11:12:21 +03:00
|
|
|
tty_in = open_tty(tty);
|
|
|
|
tty_out = tty_in;
|
2006-06-19 19:44:33 +04:00
|
|
|
}
|
|
|
|
|
2007-01-31 11:12:21 +03:00
|
|
|
/* open RFCOMM */
|
2007-03-02 00:44:30 +03:00
|
|
|
if (channel == 0)
|
2007-04-21 10:15:22 +04:00
|
|
|
rfcomm = open_client(&laddr, &raddr, lm, service);
|
2007-01-31 11:12:21 +03:00
|
|
|
else
|
2007-04-21 10:15:22 +04:00
|
|
|
rfcomm = open_server(&laddr, channel, lm, service);
|
2006-06-19 19:44:33 +04:00
|
|
|
|
2007-01-31 11:12:21 +03:00
|
|
|
/*
|
2007-03-08 22:10:49 +03:00
|
|
|
* now we are ready to go, so either detach or maybe turn
|
2007-01-31 11:12:21 +03:00
|
|
|
* off some input processing, so that rfcomm_sppd can
|
|
|
|
* be used directly with stdio
|
|
|
|
*/
|
|
|
|
if (tty == NULL) {
|
|
|
|
if (tcgetattr(tty_in, &t) < 0)
|
|
|
|
err(EXIT_FAILURE, "tcgetattr");
|
2006-06-19 19:44:33 +04:00
|
|
|
|
2007-01-31 11:12:21 +03:00
|
|
|
memcpy(&tio, &t, sizeof(tio));
|
|
|
|
t.c_lflag &= ~(ECHO | ICANON);
|
|
|
|
t.c_iflag &= ~(ICRNL);
|
2006-06-19 19:44:33 +04:00
|
|
|
|
2007-03-08 22:10:49 +03:00
|
|
|
if (memcmp(&tio, &t, sizeof(tio))) {
|
|
|
|
if (tcsetattr(tty_in, TCSANOW, &t) < 0)
|
|
|
|
err(EXIT_FAILURE, "tcsetattr");
|
2006-06-19 19:44:33 +04:00
|
|
|
|
2007-03-08 22:10:49 +03:00
|
|
|
atexit(reset_tio);
|
|
|
|
}
|
2007-01-31 11:12:21 +03:00
|
|
|
} else {
|
|
|
|
if (daemon(0, 0) < 0)
|
|
|
|
err(EXIT_FAILURE, "daemon() failed");
|
2006-06-19 19:44:33 +04:00
|
|
|
}
|
|
|
|
|
2007-01-31 11:12:21 +03:00
|
|
|
/* catch signals */
|
|
|
|
done = 0;
|
|
|
|
(void)signal(SIGHUP, sighandler);
|
|
|
|
(void)signal(SIGINT, sighandler);
|
|
|
|
(void)signal(SIGPIPE, sighandler);
|
|
|
|
(void)signal(SIGTERM, sighandler);
|
2006-06-19 19:44:33 +04:00
|
|
|
|
2007-01-31 11:12:21 +03:00
|
|
|
openlog(getprogname(), LOG_PERROR | LOG_PID, LOG_DAEMON);
|
|
|
|
syslog(LOG_INFO, "Starting on %s...", (tty ? tty : "stdio"));
|
2006-06-19 19:44:33 +04:00
|
|
|
|
2009-05-21 18:44:01 +04:00
|
|
|
n = MAX(tty_in, rfcomm) + 1;
|
2007-01-31 11:12:21 +03:00
|
|
|
while (!done) {
|
|
|
|
FD_ZERO(&rdset);
|
|
|
|
FD_SET(tty_in, &rdset);
|
|
|
|
FD_SET(rfcomm, &rdset);
|
|
|
|
|
|
|
|
if (select(n, &rdset, NULL, NULL, NULL) < 0) {
|
2006-06-19 19:44:33 +04:00
|
|
|
if (errno == EINTR)
|
|
|
|
continue;
|
|
|
|
|
2007-01-31 11:12:21 +03:00
|
|
|
syslog(LOG_ERR, "select error: %m");
|
2006-06-19 19:44:33 +04:00
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
2007-01-31 11:12:21 +03:00
|
|
|
if (FD_ISSET(tty_in, &rdset))
|
|
|
|
copy_data(tty_in, rfcomm);
|
2006-06-19 19:44:33 +04:00
|
|
|
|
2007-01-31 11:12:21 +03:00
|
|
|
if (FD_ISSET(rfcomm, &rdset))
|
|
|
|
copy_data(rfcomm, tty_out);
|
|
|
|
}
|
2006-06-19 19:44:33 +04:00
|
|
|
|
2007-01-31 11:12:21 +03:00
|
|
|
syslog(LOG_INFO, "Completed on %s", (tty ? tty : "stdio"));
|
|
|
|
exit(EXIT_SUCCESS);
|
|
|
|
}
|
2006-06-19 19:44:33 +04:00
|
|
|
|
2007-01-31 11:12:21 +03:00
|
|
|
int
|
|
|
|
open_tty(const char *tty)
|
2006-06-19 19:44:33 +04:00
|
|
|
{
|
|
|
|
char pty[PATH_MAX], *slash;
|
|
|
|
struct group *gr = NULL;
|
|
|
|
gid_t ttygid;
|
2007-01-31 11:12:21 +03:00
|
|
|
int master;
|
2006-06-19 19:44:33 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Construct master PTY name. The slave tty name must be less then
|
|
|
|
* PATH_MAX characters in length, must contain '/' character and
|
|
|
|
* must not end with '/'.
|
|
|
|
*/
|
2007-01-31 11:12:21 +03:00
|
|
|
if (strlen(tty) >= sizeof(pty))
|
|
|
|
errx(EXIT_FAILURE, ": tty name too long");
|
2006-06-19 19:44:33 +04:00
|
|
|
|
|
|
|
strlcpy(pty, tty, sizeof(pty));
|
|
|
|
slash = strrchr(pty, '/');
|
2007-01-31 11:12:21 +03:00
|
|
|
if (slash == NULL || slash[1] == '\0')
|
|
|
|
errx(EXIT_FAILURE, "%s: invalid tty", tty);
|
2006-06-19 19:44:33 +04:00
|
|
|
|
|
|
|
slash[1] = 'p';
|
2007-01-31 11:12:21 +03:00
|
|
|
if (strcmp(pty, tty) == 0)
|
|
|
|
errx(EXIT_FAILURE, "Master and slave tty are the same (%s)", tty);
|
2006-06-19 19:44:33 +04:00
|
|
|
|
2007-01-31 11:12:21 +03:00
|
|
|
if ((master = open(pty, O_RDWR, 0)) < 0)
|
|
|
|
err(EXIT_FAILURE, "%s", pty);
|
2006-06-19 19:44:33 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Slave TTY
|
|
|
|
*/
|
|
|
|
|
|
|
|
if ((gr = getgrnam("tty")) != NULL)
|
|
|
|
ttygid = gr->gr_gid;
|
|
|
|
else
|
|
|
|
ttygid = (gid_t)-1;
|
|
|
|
|
2007-01-31 11:12:21 +03:00
|
|
|
(void)chown(tty, getuid(), ttygid);
|
|
|
|
(void)chmod(tty, S_IRUSR | S_IWUSR | S_IWGRP);
|
|
|
|
(void)revoke(tty);
|
2006-06-19 19:44:33 +04:00
|
|
|
|
2007-01-31 11:12:21 +03:00
|
|
|
return master;
|
|
|
|
}
|
2006-06-19 19:44:33 +04:00
|
|
|
|
2007-01-31 11:12:21 +03:00
|
|
|
int
|
2007-04-21 10:15:22 +04:00
|
|
|
open_client(bdaddr_t *laddr, bdaddr_t *raddr, int lm, const char *service)
|
2007-01-31 11:12:21 +03:00
|
|
|
{
|
|
|
|
struct sockaddr_bt sa;
|
2007-03-02 00:44:30 +03:00
|
|
|
struct service *s;
|
2007-01-31 11:12:21 +03:00
|
|
|
struct linger l;
|
2007-03-02 00:44:30 +03:00
|
|
|
char *ep;
|
2009-05-12 22:43:35 +04:00
|
|
|
int fd, error;
|
|
|
|
uintmax_t channel;
|
2007-03-02 00:44:30 +03:00
|
|
|
|
|
|
|
for (s = services ; ; s++) {
|
|
|
|
if (s->name == NULL) {
|
2007-03-08 22:13:14 +03:00
|
|
|
channel = strtoul(service, &ep, 10);
|
2009-05-12 22:43:35 +04:00
|
|
|
if (*ep != '\0')
|
|
|
|
errx(EXIT_FAILURE, "Unknown service: %s", service);
|
2007-03-02 00:44:30 +03:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strcasecmp(s->name, service) == 0) {
|
2009-05-12 22:43:35 +04:00
|
|
|
error = channel_lookup(laddr, raddr, s->class, &channel);
|
|
|
|
if (error != 0)
|
|
|
|
errx(EXIT_FAILURE, "%s: %s", s->name, strerror(error));
|
2007-03-02 00:44:30 +03:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2006-06-19 19:44:33 +04:00
|
|
|
|
2009-05-12 22:43:35 +04:00
|
|
|
if (channel < RFCOMM_CHANNEL_MIN || channel > RFCOMM_CHANNEL_MAX)
|
|
|
|
errx(EXIT_FAILURE, "Invalid channel %"PRIuMAX, channel);
|
|
|
|
|
2007-01-31 11:12:21 +03:00
|
|
|
memset(&sa, 0, sizeof(sa));
|
|
|
|
sa.bt_len = sizeof(sa);
|
|
|
|
sa.bt_family = AF_BLUETOOTH;
|
|
|
|
bdaddr_copy(&sa.bt_bdaddr, laddr);
|
2006-06-19 19:44:33 +04:00
|
|
|
|
2007-01-31 11:12:21 +03:00
|
|
|
fd = socket(PF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM);
|
|
|
|
if (fd < 0)
|
|
|
|
err(EXIT_FAILURE, "socket()");
|
2006-06-19 19:44:33 +04:00
|
|
|
|
2007-01-31 11:12:21 +03:00
|
|
|
if (bind(fd, (struct sockaddr *)&sa, sizeof(sa)) < 0)
|
|
|
|
err(EXIT_FAILURE, "bind(%s)", bt_ntoa(laddr, NULL));
|
2006-06-19 19:44:33 +04:00
|
|
|
|
2007-01-31 11:12:21 +03:00
|
|
|
memset(&l, 0, sizeof(l));
|
|
|
|
l.l_onoff = 1;
|
|
|
|
l.l_linger = 5;
|
|
|
|
if (setsockopt(fd, SOL_SOCKET, SO_LINGER, &l, sizeof(l)) < 0)
|
|
|
|
err(EXIT_FAILURE, "linger()");
|
2006-06-19 19:44:33 +04:00
|
|
|
|
2007-04-21 10:15:22 +04:00
|
|
|
if (setsockopt(fd, BTPROTO_RFCOMM, SO_RFCOMM_LM, &lm, sizeof(lm)) < 0)
|
|
|
|
err(EXIT_FAILURE, "link mode");
|
|
|
|
|
2007-01-31 11:12:21 +03:00
|
|
|
sa.bt_channel = channel;
|
|
|
|
bdaddr_copy(&sa.bt_bdaddr, raddr);
|
2006-06-19 19:44:33 +04:00
|
|
|
|
2007-01-31 11:12:21 +03:00
|
|
|
if (connect(fd, (struct sockaddr *)&sa, sizeof(sa)) < 0)
|
2009-05-12 22:43:35 +04:00
|
|
|
err(EXIT_FAILURE, "connect(%s, %"PRIuMAX")", bt_ntoa(raddr, NULL),
|
2007-01-31 11:12:21 +03:00
|
|
|
channel);
|
2006-06-19 19:44:33 +04:00
|
|
|
|
2007-01-31 11:12:21 +03:00
|
|
|
return fd;
|
|
|
|
}
|
2006-06-19 19:44:33 +04:00
|
|
|
|
2007-01-31 11:12:21 +03:00
|
|
|
int
|
2007-04-21 10:15:22 +04:00
|
|
|
open_server(bdaddr_t *laddr, uint8_t channel, int lm, const char *service)
|
2006-06-19 19:44:33 +04:00
|
|
|
{
|
2009-05-12 22:43:35 +04:00
|
|
|
uint8_t buffer[256];
|
2007-01-31 11:12:21 +03:00
|
|
|
struct sockaddr_bt sa;
|
2009-05-12 22:43:35 +04:00
|
|
|
struct service *s;
|
2007-01-31 11:12:21 +03:00
|
|
|
struct linger l;
|
|
|
|
socklen_t len;
|
2009-05-12 22:43:35 +04:00
|
|
|
sdp_session_t ss;
|
|
|
|
sdp_data_t rec;
|
|
|
|
int sv, fd;
|
2006-06-19 19:44:33 +04:00
|
|
|
|
2009-05-12 22:43:35 +04:00
|
|
|
for (s = services; ; s++) {
|
|
|
|
if (s->name == NULL)
|
|
|
|
usage();
|
2006-06-19 19:44:33 +04:00
|
|
|
|
2009-05-12 22:43:35 +04:00
|
|
|
if (strcasecmp(s->name, service) == 0)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Open server socket */
|
2007-01-31 11:12:21 +03:00
|
|
|
sv = socket(PF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM);
|
|
|
|
if (sv < 0)
|
|
|
|
err(EXIT_FAILURE, "socket()");
|
2006-06-19 19:44:33 +04:00
|
|
|
|
2009-05-12 22:43:35 +04:00
|
|
|
memset(&sa, 0, sizeof(sa));
|
|
|
|
sa.bt_len = sizeof(sa);
|
|
|
|
sa.bt_family = AF_BLUETOOTH;
|
|
|
|
sa.bt_channel = channel;
|
|
|
|
bdaddr_copy(&sa.bt_bdaddr, laddr);
|
2007-01-31 11:12:21 +03:00
|
|
|
if (bind(sv, (struct sockaddr *)&sa, sizeof(sa)) < 0)
|
|
|
|
err(EXIT_FAILURE, "bind(%s, %d)", bt_ntoa(laddr, NULL),
|
|
|
|
channel);
|
|
|
|
|
2007-04-21 10:15:22 +04:00
|
|
|
if (setsockopt(sv, BTPROTO_RFCOMM, SO_RFCOMM_LM, &lm, sizeof(lm)) < 0)
|
|
|
|
err(EXIT_FAILURE, "link mode");
|
|
|
|
|
2007-01-31 11:12:21 +03:00
|
|
|
if (listen(sv, 1) < 0)
|
|
|
|
err(EXIT_FAILURE, "listen()");
|
|
|
|
|
2009-05-12 22:43:35 +04:00
|
|
|
/* Build SDP record */
|
|
|
|
rec.next = buffer;
|
|
|
|
rec.end = buffer + sizeof(buffer);
|
|
|
|
|
|
|
|
sdp_put_uint16(&rec, SDP_ATTR_SERVICE_RECORD_HANDLE);
|
|
|
|
sdp_put_uint32(&rec, 0x00000000);
|
|
|
|
|
|
|
|
sdp_put_uint16(&rec, SDP_ATTR_SERVICE_CLASS_ID_LIST);
|
|
|
|
sdp_put_seq(&rec, 3);
|
|
|
|
sdp_put_uuid16(&rec, s->class);
|
|
|
|
|
|
|
|
sdp_put_uint16(&rec, SDP_ATTR_PROTOCOL_DESCRIPTOR_LIST);
|
|
|
|
sdp_put_seq(&rec, 12);
|
|
|
|
sdp_put_seq(&rec, 3);
|
|
|
|
sdp_put_uuid16(&rec, SDP_UUID_PROTOCOL_L2CAP);
|
|
|
|
sdp_put_seq(&rec, 5);
|
|
|
|
sdp_put_uuid16(&rec, SDP_UUID_PROTOCOL_RFCOMM);
|
|
|
|
sdp_put_uint8(&rec, channel);
|
|
|
|
|
|
|
|
sdp_put_uint16(&rec, SDP_ATTR_BROWSE_GROUP_LIST);
|
|
|
|
sdp_put_seq(&rec, 3);
|
|
|
|
sdp_put_uuid16(&rec, SDP_SERVICE_CLASS_PUBLIC_BROWSE_GROUP);
|
|
|
|
|
|
|
|
sdp_put_uint16(&rec, SDP_ATTR_LANGUAGE_BASE_ATTRIBUTE_ID_LIST);
|
|
|
|
sdp_put_seq(&rec, 9);
|
|
|
|
sdp_put_uint16(&rec, 0x656e); /* "en" */
|
|
|
|
sdp_put_uint16(&rec, 106); /* UTF-8 */
|
|
|
|
sdp_put_uint16(&rec, SDP_ATTR_PRIMARY_LANGUAGE_BASE_ID);
|
|
|
|
|
|
|
|
if (s->class == SDP_SERVICE_CLASS_LAN_ACCESS_USING_PPP) {
|
|
|
|
sdp_put_uint16(&rec, SDP_ATTR_SERVICE_AVAILABILITY);
|
|
|
|
sdp_put_uint8(&rec, 0x00);
|
|
|
|
}
|
2007-01-31 11:12:21 +03:00
|
|
|
|
2009-05-12 22:43:35 +04:00
|
|
|
sdp_put_uint16(&rec, SDP_ATTR_BLUETOOTH_PROFILE_DESCRIPTOR_LIST);
|
|
|
|
sdp_put_seq(&rec, 8);
|
|
|
|
sdp_put_seq(&rec, 6);
|
|
|
|
sdp_put_uuid16(&rec, s->class);
|
|
|
|
sdp_put_uint16(&rec, 0x0100); /* v1.0 */
|
|
|
|
|
|
|
|
sdp_put_uint16(&rec, SDP_ATTR_PRIMARY_LANGUAGE_BASE_ID
|
|
|
|
+ SDP_ATTR_SERVICE_NAME_OFFSET);
|
|
|
|
sdp_put_str(&rec, s->description, -1);
|
|
|
|
|
|
|
|
if (s->class == SDP_SERVICE_CLASS_DIALUP_NETWORKING) {
|
|
|
|
sdp_put_uint16(&rec, SDP_ATTR_AUDIO_FEEDBACK_SUPPORT);
|
|
|
|
sdp_put_bool(&rec, false);
|
2007-01-31 11:12:21 +03:00
|
|
|
}
|
|
|
|
|
2009-05-12 22:43:35 +04:00
|
|
|
#if 0
|
|
|
|
if (s->class == SDP_SERVICE_CLASS_LAN_ACCESS_USING_PPP) {
|
|
|
|
sdp_put_uint16(&rec, SDP_ATTR_IP_SUBNET); /* TODO */
|
|
|
|
sdp_put_str(&rec, "0.0.0.0/0", -1);
|
|
|
|
}
|
|
|
|
#endif
|
2007-01-31 11:12:21 +03:00
|
|
|
|
2009-05-12 22:43:35 +04:00
|
|
|
rec.end = rec.next;
|
|
|
|
rec.next = buffer;
|
|
|
|
|
|
|
|
/* Register service with SDP server */
|
2007-01-31 11:12:21 +03:00
|
|
|
ss = sdp_open_local(NULL);
|
2009-05-12 22:43:35 +04:00
|
|
|
if (ss == NULL)
|
2007-01-31 11:12:21 +03:00
|
|
|
err(EXIT_FAILURE, "sdp_open_local");
|
|
|
|
|
2009-05-12 22:43:35 +04:00
|
|
|
if (!sdp_record_insert(ss, laddr, NULL, &rec))
|
|
|
|
err(EXIT_FAILURE, "sdp_record_insert");
|
2007-01-31 11:12:21 +03:00
|
|
|
|
2009-05-12 22:43:35 +04:00
|
|
|
/* Accept client connection */
|
2007-01-31 11:12:21 +03:00
|
|
|
len = sizeof(sa);
|
|
|
|
fd = accept(sv, (struct sockaddr *)&sa, &len);
|
|
|
|
if (fd < 0)
|
|
|
|
err(EXIT_FAILURE, "accept");
|
|
|
|
|
|
|
|
memset(&l, 0, sizeof(l));
|
|
|
|
l.l_onoff = 1;
|
|
|
|
l.l_linger = 5;
|
|
|
|
if (setsockopt(fd, SOL_SOCKET, SO_LINGER, &l, sizeof(l)) < 0)
|
|
|
|
err(EXIT_FAILURE, "linger()");
|
|
|
|
|
|
|
|
close(sv);
|
|
|
|
return fd;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
copy_data(int src, int dst)
|
|
|
|
{
|
|
|
|
static char buf[BUFSIZ];
|
|
|
|
ssize_t nr, nw, off;
|
|
|
|
|
|
|
|
while ((nr = read(src, buf, sizeof(buf))) == -1) {
|
|
|
|
if (errno != EINTR) {
|
|
|
|
syslog(LOG_ERR, "read failed: %m");
|
|
|
|
exit(EXIT_FAILURE);
|
2006-06-19 19:44:33 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-03-02 00:44:30 +03:00
|
|
|
if (nr == 0) /* reached EOF */
|
|
|
|
done++;
|
|
|
|
|
2007-01-31 11:12:21 +03:00
|
|
|
for (off = 0 ; nr ; nr -= nw, off += nw) {
|
|
|
|
if ((nw = write(dst, buf + off, (size_t)nr)) == -1) {
|
|
|
|
syslog(LOG_ERR, "write failed: %m");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2006-06-19 19:44:33 +04:00
|
|
|
|
2009-05-12 22:43:35 +04:00
|
|
|
int
|
|
|
|
channel_lookup(bdaddr_t const *laddr, bdaddr_t const *raddr,
|
|
|
|
uint16_t class, uintmax_t *channel)
|
|
|
|
{
|
|
|
|
uint8_t buffer[6]; /* SSP (3 bytes) + AIL (3 bytes) */
|
|
|
|
sdp_session_t ss;
|
|
|
|
sdp_data_t ail, ssp, rsp, rec, value, pdl, seq;
|
|
|
|
uint16_t attr;
|
|
|
|
bool rv;
|
|
|
|
|
|
|
|
seq.next = buffer;
|
|
|
|
seq.end = buffer + sizeof(buffer);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* build ServiceSearchPattern (3 bytes)
|
|
|
|
*/
|
|
|
|
ssp.next = seq.next;
|
|
|
|
sdp_put_uuid16(&seq, class);
|
|
|
|
ssp.end = seq.next;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* build AttributeIDList (3 bytes)
|
|
|
|
*/
|
|
|
|
ail.next = seq.next;
|
|
|
|
sdp_put_uint16(&seq, SDP_ATTR_PROTOCOL_DESCRIPTOR_LIST);
|
|
|
|
ail.end = seq.next;
|
|
|
|
|
|
|
|
ss = sdp_open(laddr, raddr);
|
|
|
|
if (ss == NULL)
|
|
|
|
return errno;
|
|
|
|
|
|
|
|
rv = sdp_service_search_attribute(ss, &ssp, &ail, &rsp);
|
|
|
|
if (!rv) {
|
|
|
|
sdp_close(ss);
|
|
|
|
return errno;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The response will be a list of records that matched our
|
|
|
|
* ServiceSearchPattern, where each record is a sequence
|
|
|
|
* containing a single ProtocolDescriptorList attribute and
|
|
|
|
* value
|
|
|
|
*
|
|
|
|
* seq
|
|
|
|
* uint16 ProtocolDescriptorList
|
|
|
|
* value
|
|
|
|
* seq
|
|
|
|
* uint16 ProtocolDescriptorList
|
|
|
|
* value
|
|
|
|
*
|
|
|
|
* If the ProtocolDescriptorList describes a single stack,
|
|
|
|
* the attribute value takes the form of a single Data Element
|
|
|
|
* Sequence where each member is a protocol descriptor.
|
|
|
|
*
|
|
|
|
* seq
|
|
|
|
* list
|
|
|
|
*
|
|
|
|
* If it is possible for more than one kind of protocol
|
|
|
|
* stack to be used to gain access to the service, the
|
|
|
|
* ProtocolDescriptorList takes the form of a Data Element
|
|
|
|
* Alternative where each member is a Data Element Sequence
|
|
|
|
* describing an alternative protocol stack.
|
|
|
|
*
|
|
|
|
* alt
|
|
|
|
* seq
|
|
|
|
* list
|
|
|
|
* seq
|
|
|
|
* list
|
|
|
|
*
|
|
|
|
* Each protocol stack description contains a sequence for each
|
|
|
|
* protocol, where each sequence contains the protocol UUID as
|
|
|
|
* the first element, and any ProtocolSpecificParameters. We are
|
|
|
|
* interested in the RFCOMM channel number, stored as parameter#1.
|
|
|
|
*
|
|
|
|
* seq
|
|
|
|
* uuid L2CAP
|
|
|
|
* uint16 psm
|
|
|
|
* seq
|
|
|
|
* uuid RFCOMM
|
|
|
|
* uint8 channel
|
|
|
|
*/
|
|
|
|
|
|
|
|
rv = false;
|
|
|
|
while (!rv && sdp_get_seq(&rsp, &rec)) {
|
|
|
|
if (!sdp_get_attr(&rec, &attr, &value)
|
|
|
|
|| attr != SDP_ATTR_PROTOCOL_DESCRIPTOR_LIST)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
sdp_get_alt(&value, &value); /* strip any alt container */
|
|
|
|
while (!rv && sdp_get_seq(&value, &pdl)) {
|
|
|
|
if (sdp_get_seq(&pdl, &seq)
|
|
|
|
&& sdp_match_uuid16(&seq, SDP_UUID_PROTOCOL_L2CAP)
|
|
|
|
&& sdp_get_seq(&pdl, &seq)
|
|
|
|
&& sdp_match_uuid16(&seq, SDP_UUID_PROTOCOL_RFCOMM)
|
|
|
|
&& sdp_get_uint(&seq, channel))
|
|
|
|
rv = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sdp_close(ss);
|
|
|
|
return (rv) ? 0 : ENOATTR;
|
|
|
|
}
|
|
|
|
|
2007-01-31 11:12:21 +03:00
|
|
|
void
|
|
|
|
sighandler(int s)
|
2006-06-19 19:44:33 +04:00
|
|
|
{
|
|
|
|
|
2007-01-31 11:12:21 +03:00
|
|
|
done++;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
reset_tio(void)
|
|
|
|
{
|
|
|
|
|
|
|
|
tcsetattr(STDIN_FILENO, TCSAFLUSH, &tio);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2006-06-19 19:44:33 +04:00
|
|
|
usage(void)
|
|
|
|
{
|
2007-04-21 10:15:22 +04:00
|
|
|
const char *cmd = getprogname();
|
2007-01-31 11:12:21 +03:00
|
|
|
struct service *s;
|
|
|
|
|
2007-04-21 10:15:22 +04:00
|
|
|
fprintf(stderr, "Usage: %s [-d device] [-m mode] [-s service] [-t tty]\n"
|
|
|
|
" %*s {-a bdaddr | -c channel}\n"
|
2007-01-31 11:12:21 +03:00
|
|
|
"\n"
|
|
|
|
"Where:\n"
|
|
|
|
"\t-a bdaddr remote device address\n"
|
2007-03-02 00:44:30 +03:00
|
|
|
"\t-c channel local RFCOMM channel\n"
|
2007-01-31 11:12:21 +03:00
|
|
|
"\t-d device local device address\n"
|
2007-04-21 10:15:22 +04:00
|
|
|
"\t-m mode link mode\n"
|
2007-03-02 00:44:30 +03:00
|
|
|
"\t-s service service class\n"
|
2007-01-31 11:12:21 +03:00
|
|
|
"\t-t tty run in background using pty\n"
|
2007-04-21 14:39:30 +04:00
|
|
|
"\n", cmd, (int)strlen(cmd), "");
|
2007-01-31 11:12:21 +03:00
|
|
|
|
2007-03-02 00:44:30 +03:00
|
|
|
fprintf(stderr, "Known service classes:\n");
|
2007-01-31 11:12:21 +03:00
|
|
|
for (s = services ; s->name != NULL ; s++)
|
2007-03-02 00:44:30 +03:00
|
|
|
fprintf(stderr, "\t%-13s%s\n", s->name, s->description);
|
2006-06-19 19:44:33 +04:00
|
|
|
|
|
|
|
exit(EXIT_FAILURE);
|
2007-01-31 11:12:21 +03:00
|
|
|
}
|