6 * Copyright (c) 2001-2008 Maksim Yevmenkin <m_evmenkin@yahoo.com>
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * $Id: rfcomm_pppd.c,v 1.5 2003/09/07 18:32:11 max Exp $
34 #include <bluetooth.h>
48 #define RFCOMM_PPPD "rfcomm_pppd"
50 int rfcomm_channel_lookup (bdaddr_t const *local,
51 bdaddr_t const *remote,
52 int service, int *channel, int *error);
54 static void exec_ppp (int s, char *unit, char *label);
55 static void sighandler (int s);
56 static void usage (void);
62 main(int argc, char *argv[])
64 struct sockaddr_rfcomm sock_addr;
65 char *label = NULL, *unit = NULL, *ep = NULL;
67 int s, channel, detach, server, service,
71 memcpy(&addr, NG_HCI_BDADDR_ANY, sizeof(addr));
79 /* Parse command line arguments */
80 while ((s = getopt(argc, argv, "a:cC:dDhl:sSu:")) != -1) {
82 case 'a': /* BDADDR */
83 if (!bt_aton(optarg, &addr)) {
84 struct hostent *he = NULL;
86 if ((he = bt_gethostbyname(optarg)) == NULL)
87 errx(1, "%s: %s", optarg, hstrerror(h_errno));
89 memcpy(&addr, he->h_addr, sizeof(addr));
93 case 'c': /* client */
97 case 'C': /* RFCOMM channel */
98 channel = strtoul(optarg, &ep, 10);
101 switch (tolower(optarg[0])) {
102 case 'd': /* DialUp Networking */
103 service = SDP_SERVICE_CLASS_DIALUP_NETWORKING;
106 case 'l': /* LAN Access Using PPP */
107 service = SDP_SERVICE_CLASS_LAN_ACCESS_USING_PPP;
113 case 'd': /* do not detach */
117 case 'D': /* Register DUN service as well as LAN service */
121 case 'l': /* PPP label */
125 case 's': /* server */
129 case 'S': /* Register SP service as well as LAN service */
133 case 'u': /* PPP -unit option */
134 strtoul(optarg, &ep, 10);
149 /* Check if we got everything we wanted */
151 errx(1, "Must specify PPP label");
154 if (memcmp(&addr, NG_HCI_BDADDR_ANY, sizeof(addr)) == 0)
155 errx(1, "Must specify server BD_ADDR");
157 /* Check channel, if was not set then obtain it via SDP */
158 if (channel == 0 && service != 0)
159 if (rfcomm_channel_lookup(NULL, &addr, service,
161 errc(1, s, "Could not obtain RFCOMM channel");
164 if (channel <= 0 || channel > 30)
165 errx(1, "Invalid RFCOMM channel number %d", channel);
167 openlog(RFCOMM_PPPD, LOG_PID | LOG_PERROR | LOG_NDELAY, LOG_USER);
171 if (pid == (pid_t) -1) {
172 syslog(LOG_ERR, "Could not fork(). %s (%d)",
173 strerror(errno), errno);
180 if (daemon(0, 0) < 0) {
181 syslog(LOG_ERR, "Could not daemon(0, 0). %s (%d)",
182 strerror(errno), errno);
187 s = socket(PF_BLUETOOTH, SOCK_STREAM, BLUETOOTH_PROTO_RFCOMM);
189 syslog(LOG_ERR, "Could not create socket. %s (%d)",
190 strerror(errno), errno);
197 sdp_lan_profile_t lan;
199 /* Install signal handler */
200 memset(&sa, 0, sizeof(sa));
201 sa.sa_handler = sighandler;
203 if (sigaction(SIGTERM, &sa, NULL) < 0) {
204 syslog(LOG_ERR, "Could not sigaction(SIGTERM). %s (%d)",
205 strerror(errno), errno);
209 if (sigaction(SIGHUP, &sa, NULL) < 0) {
210 syslog(LOG_ERR, "Could not sigaction(SIGHUP). %s (%d)",
211 strerror(errno), errno);
215 if (sigaction(SIGINT, &sa, NULL) < 0) {
216 syslog(LOG_ERR, "Could not sigaction(SIGINT). %s (%d)",
217 strerror(errno), errno);
221 sa.sa_handler = SIG_IGN;
222 sa.sa_flags = SA_NOCLDWAIT;
224 if (sigaction(SIGCHLD, &sa, NULL) < 0) {
225 syslog(LOG_ERR, "Could not sigaction(SIGCHLD). %s (%d)",
226 strerror(errno), errno);
230 /* bind socket and listen for incoming connections */
231 sock_addr.rfcomm_len = sizeof(sock_addr);
232 sock_addr.rfcomm_family = AF_BLUETOOTH;
233 memcpy(&sock_addr.rfcomm_bdaddr, &addr,
234 sizeof(sock_addr.rfcomm_bdaddr));
235 sock_addr.rfcomm_channel = channel;
237 if (bind(s, (struct sockaddr *) &sock_addr,
238 sizeof(sock_addr)) < 0) {
239 syslog(LOG_ERR, "Could not bind socket. %s (%d)",
240 strerror(errno), errno);
244 if (listen(s, 10) < 0) {
245 syslog(LOG_ERR, "Could not listen on socket. %s (%d)",
246 strerror(errno), errno);
250 ss = sdp_open_local(NULL);
252 syslog(LOG_ERR, "Unable to create local SDP session");
256 if (sdp_error(ss) != 0) {
257 syslog(LOG_ERR, "Unable to open local SDP session. " \
258 "%s (%d)", strerror(sdp_error(ss)),
263 memset(&lan, 0, sizeof(lan));
264 lan.server_channel = channel;
266 if (sdp_register_service(ss,
267 SDP_SERVICE_CLASS_LAN_ACCESS_USING_PPP,
268 &addr, (void *) &lan, sizeof(lan), NULL) != 0) {
269 syslog(LOG_ERR, "Unable to register LAN service with " \
270 "local SDP daemon. %s (%d)",
271 strerror(sdp_error(ss)), sdp_error(ss));
276 * Register DUN (Dial-Up Networking) service on the same
277 * RFCOMM channel if requested. There is really no good reason
278 * to not to support this. AT-command exchange can be faked
279 * with chat script in ppp.conf
283 sdp_dun_profile_t dun;
285 memset(&dun, 0, sizeof(dun));
286 dun.server_channel = channel;
288 if (sdp_register_service(ss,
289 SDP_SERVICE_CLASS_DIALUP_NETWORKING,
290 &addr, (void *) &dun, sizeof(dun),
292 syslog(LOG_ERR, "Unable to register DUN " \
293 "service with local SDP daemon. " \
294 "%s (%d)", strerror(sdp_error(ss)),
301 * Register SP (Serial Port) service on the same RFCOMM channel
302 * if requested. It appears that some cell phones are using so
303 * called "callback mechanism". In this scenario user is trying
304 * to connect his cell phone to the Internet, and, user's host
305 * computer is acting as the gateway server. It seems that it
306 * is not possible to tell the phone to just connect and start
307 * using the LAN service. Instead the user's host computer must
308 * "jump start" the phone by connecting to the phone's SP
309 * service. What happens next is the phone kills the existing
310 * connection and opens another connection back to the user's
311 * host computer. The phone really wants to use LAN service,
312 * but for whatever reason it looks for SP service on the
313 * user's host computer. This brain damaged behavior was
314 * reported for Nokia 6600 and Sony/Ericsson P900. Both phones
315 * are Symbian-based phones. Perhaps this is a Symbian problem?
321 memset(&sp, 0, sizeof(sp));
322 sp.server_channel = channel;
324 if (sdp_register_service(ss,
325 SDP_SERVICE_CLASS_SERIAL_PORT,
326 &addr, (void *) &sp, sizeof(sp),
328 syslog(LOG_ERR, "Unable to register SP " \
329 "service with local SDP daemon. " \
330 "%s (%d)", strerror(sdp_error(ss)),
336 for (done = 0; !done; ) {
337 socklen_t len = sizeof(sock_addr);
338 int s1 = accept(s, (struct sockaddr *) &sock_addr, &len);
341 syslog(LOG_ERR, "Could not accept connection " \
342 "on socket. %s (%d)", strerror(errno),
348 if (pid == (pid_t) -1) {
349 syslog(LOG_ERR, "Could not fork(). %s (%d)",
350 strerror(errno), errno);
358 /* Reset signal handler */
359 memset(&sa, 0, sizeof(sa));
360 sa.sa_handler = SIG_DFL;
362 sigaction(SIGTERM, &sa, NULL);
363 sigaction(SIGHUP, &sa, NULL);
364 sigaction(SIGINT, &sa, NULL);
365 sigaction(SIGCHLD, &sa, NULL);
371 * XXX Make sure user does not shoot himself
372 * in the foot. Do not pass unit option to the
373 * PPP when operating in the server mode.
376 exec_ppp(s1, NULL, label);
381 sock_addr.rfcomm_len = sizeof(sock_addr);
382 sock_addr.rfcomm_family = AF_BLUETOOTH;
383 memcpy(&sock_addr.rfcomm_bdaddr, NG_HCI_BDADDR_ANY,
384 sizeof(sock_addr.rfcomm_bdaddr));
385 sock_addr.rfcomm_channel = 0;
387 if (bind(s, (struct sockaddr *) &sock_addr,
388 sizeof(sock_addr)) < 0) {
389 syslog(LOG_ERR, "Could not bind socket. %s (%d)",
390 strerror(errno), errno);
394 memcpy(&sock_addr.rfcomm_bdaddr, &addr,
395 sizeof(sock_addr.rfcomm_bdaddr));
396 sock_addr.rfcomm_channel = channel;
398 if (connect(s, (struct sockaddr *) &sock_addr,
399 sizeof(sock_addr)) < 0) {
400 syslog(LOG_ERR, "Could not connect socket. %s (%d)",
401 strerror(errno), errno);
405 exec_ppp(s, unit, label);
412 * Redirects stdin/stdout to s, stderr to /dev/null and exec
413 * 'ppp -direct -quiet [-unit N] label'. Never returns.
417 exec_ppp(int s, char *unit, char *label)
419 char ppp[] = "/usr/sbin/ppp";
420 char *ppp_args[] = { ppp, "-direct", "-quiet",
421 NULL, NULL, NULL, NULL };
425 syslog(LOG_ERR, "Could not dup(0). %s (%d)",
426 strerror(errno), errno);
432 syslog(LOG_ERR, "Could not dup(1). %s (%d)",
433 strerror(errno), errno);
438 open("/dev/null", O_RDWR);
441 ppp_args[3] = "-unit";
447 if (execv(ppp, ppp_args) < 0) {
448 syslog(LOG_ERR, "Could not exec(%s -direct -quiet%s%s %s). " \
449 "%s (%d)", ppp, (unit != NULL)? " -unit " : "",
450 (unit != NULL)? unit : "", label,
451 strerror(errno), errno);
463 /* Display usage and exit */
468 "Usage: %s options\n" \
469 "Where options are:\n" \
470 "\t-a address Address to listen on or connect to (required for client)\n" \
471 "\t-c Act as a clinet (default)\n" \
472 "\t-C channel RFCOMM channel to listen on or connect to (required)\n" \
473 "\t-d Run in foreground\n" \
474 "\t-D Register Dial-Up Networking service (server mode only)\n" \
475 "\t-l label Use PPP label (required)\n" \
476 "\t-s Act as a server\n" \
477 "\t-S Register Serial Port service (server mode only)\n" \
478 "\t-u N Tell PPP to operate on /dev/tunN (client mode only)\n" \
479 "\t-h Display this message\n", RFCOMM_PPPD);