Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
remjey committed Aug 8, 2018
0 parents commit 822c26e
Show file tree
Hide file tree
Showing 13 changed files with 378 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
target
*.swp
20 changes: 20 additions & 0 deletions LICENCE
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
Copyright (c) 2018 Jérémy Farnaud

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# bt-tether

## Description

**bt-tether** is a small script that enables Bluetooth Tethering on your Jolla Phone. I could not find any software that did that realiably so I tried my hand at it.

It is **quite experimental** and a little hacky. It’s only been tested on my Jolla Phone in developer mode. I’m putting it here without any warranty, however I’d be glad to receive some feedback and I’ll do my best to improve it.

**bt-tether** can connect several devices at once to whatever is the current connection of the phone (Mobile or WLAN). It uses the `pand` daemon that should be installed already, along with specially crafted scripts and an adhoc DNS relay.

## Licence

This software is distributed under the terms of the MIT Lincence.

11 changes: 11 additions & 0 deletions files/etc/systemd/system/bt-tether.service
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[Unit]
Description=Bluetooth Tethering

[Service]
Type=oneshot
ExecStart=/usr/local/bin/bt-tether
RemainAfterExit=yes

[Install]
WantedBy=bluetooth.target

5 changes: 5 additions & 0 deletions files/usr/local/bin/bt-tether
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/bin/sh

mkdir /var/tmp/bt-tether
pand --listen --role NAP --master --devup /usr/local/lib/bt-tether/devup --devdown /usr/local/lib/bt-tether/devdown

16 changes: 16 additions & 0 deletions files/usr/local/lib/bt-tether/bluetooth-config
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/bin/sh

if [ "install" = "$1" ]; then

echo -e "##bt-tether##begin##\nDisablePlugins = network\n##bt-tether##end##" >> /etc/bluetooth/main.conf

elif [ "uninstall" = "$1" ]; then

sed -i -e "/##bt-tether##begin##/,/##bt-tether##end##/ d" /etc/bluetooth/main.conf

else
echo "Usage: $0 install | uninstall"
exit 1
fi


7 changes: 7 additions & 0 deletions files/usr/local/lib/bt-tether/common
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
IF=$1
SUBNET=10.64.$(( 100 + ${IF#bnep} ))
DNSRELAY_PID=/var/tmp/bt-tether/$IF.dns-relay
UDHCPD_CONF=/var/tmp/bt-tether/$IF.udhcpd
UDHCPD_PID=$UDHCPD_CONF.pid
UDHCPD_LEASE=$UDHCPD_CONF.lease

11 changes: 11 additions & 0 deletions files/usr/local/lib/bt-tether/devdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/bin/sh

. "$( dirname $0 )"/common

kill -- "$( cat $UDHCPD_PID )"
kill -- "$( cat $DNSRELAY_PID )"

rm $DNSRELAY_PID $UDHCPD_PID $UDHCPD_LEASE $UDHCPD_CONF

iptables -t nat -D POSTROUTING -s $SUBNET.0/24 -j MASQUERADE

26 changes: 26 additions & 0 deletions files/usr/local/lib/bt-tether/devup
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#!/bin/sh

. "$( dirname $0 )"/common

ifconfig $IF $SUBNET.1 netmask 255.255.255.0 broadcast $SUBNET.255 up

cat >$UDHCPD_CONF <<EOF
interface $IF
pidfile $UDHCPD_PID
leasefile $UDHCPD_LEASE
start $SUBNET.20
end $SUBNET.254
opt dns $SUBNET.1
opt subnet 255.255.255.0
opt router $SUBNET.1
EOF

echo 1 > /proc/sys/net/ipv4/ip_forward
iptables -t nat -A POSTROUTING -s $SUBNET.0/24 -j MASQUERADE

udhcpd $UDHCPD_CONF
/usr/local/lib/bt-tether/dns-relay -f $SUBNET.1 > $DNSRELAY_PID

51 changes: 51 additions & 0 deletions rpm/bt-tether.spec.gen
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#!/bin/sh

VERSION="$1"
RELEASE="${2:-1}"

cat <<__EOF__
Name: bt-tether
Version: ${VERSION}
Release: 1
Summary: Bluetooth Tethering
Group: Miscellaneous
License: MIT
URL: https://jf.almel.fr
Source: bt-tether-${VERSION}-${RELEASE}.tgz
%description
This package will enable BlueTooth tethering on SailfishOS.
%prep
%setup -n bt-tether-${VERSION}-${RELEASE}
%build
cd src
make
%install
mkdir -p %{buildroot}
cp -r files/* %{buildroot}
cp src/dns-relay %{buildroot}/usr/local/lib/bt-tether
%files
/etc
/usr
%clean
%post
/usr/local/lib/bt-tether/bluetooth-config install
systemctl enable bt-tether.service
systemctl start bt-tether.service
systemctl restart bluetooth.service
%preun
/usr/local/lib/bt-tether/bluetooth-config uninstall
systemctl stop bt-tether.service
systemctl disable bt-tether.service
systemctl restart bluetooth.service
__EOF__

26 changes: 26 additions & 0 deletions rpm/prep
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#!/bin/sh -e

NAME=bt-tether
VERSION="$1"
RELEASE=${2:-1}
DST="$NAME-$VERSION-$RELEASE"

if [ -z "$VERSION" ]; then
printf "Usage: $0 version [release]\n"
exit 1
fi

cd "$(dirname "$0")"/..

mkdir -p target

rm -rf "target/$DST" "target/$DST.tgz"
mkdir -p "target/$DST"
cp -r files src "target/$DST"
mkdir -p "target/$DST/files/usr/local/share/doc/bt-tether"
cp LICENCE README.md "target/$DST/files/usr/local/share/doc/bt-tether"
tar -C target -zcf "target/$DST.tgz" "$DST"
rm -rf "target/$DST"

rpm/bt-tether.spec.gen $VERSION $RELEASE > "target/$DST.spec"

3 changes: 3 additions & 0 deletions src/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
dns-relay: dns-relay.c
$(CC) -std=c99 -Wall -Wextra -pedantic -o $@ $^

186 changes: 186 additions & 0 deletions src/dns-relay.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,186 @@
/*
* Copyright (c) 2018 Jérémy Farnaud
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <poll.h>
#include <string.h>
#include <time.h>

#define MAX_REQ_SIZE 2048
#define MAX_SOCKETS 256
#define REQ_TIMEOUT 10

struct ret_info_t {
struct sockaddr_in addr;
time_t timeout;
};

struct sockaddr_in relay_addr;
struct sockaddr_in server_addr;

struct pollfd fds[MAX_SOCKETS];
struct ret_info_t ret_info[MAX_SOCKETS];
int nfds = 0;

void usage(char *name) {
printf("Usage: %s [-f] <ip address>\n", name);
exit(1);
}

int fds_add(int fd, short events) {
if (nfds == MAX_SOCKETS) return -1;

fds[nfds].fd = fd;
fds[nfds].events = events;
fds[nfds].revents = 0;
return nfds++;
}

int fds_add_with_ri(int fd, short events, struct sockaddr_in addr, time_t timeout) {
int i = fds_add(fd, events);
if (i != -1) {
ret_info[i].addr = addr;
ret_info[i].timeout = timeout;
}
return i;
}

void fds_remove(int i) {
memmove(&fds[i], &fds[i + 1], sizeof(struct pollfd) * (nfds - i - 1));
memmove(&ret_info[i], &ret_info[i + 1], sizeof(struct ret_info_t) * (nfds - i - 1));
--nfds;
}

int main(int argc, char **argv) {
int ip_arg_n = 1;
int do_fork = 0;

if (argc > ip_arg_n && strcmp("-f", argv[1]) == 0) {
do_fork = 1;
++ip_arg_n;
}

relay_addr.sin_family = AF_INET;
relay_addr.sin_port = htons(53);

if (argc <= ip_arg_n || !inet_pton(AF_INET, argv[ip_arg_n], &relay_addr.sin_addr)) {
usage(argv[0]);
}

int relay_fd = socket(AF_INET, SOCK_DGRAM, 0);
if (relay_fd == -1) {
perror("relay socket");
exit(10);
}
if (bind(relay_fd, (struct sockaddr*)&relay_addr, sizeof(relay_addr))) {
perror("relay bind");
exit(11);
}

if (do_fork) {
pid_t pid = fork();
if (pid != 0) {
printf("%d\n", pid);
exit(0);
}

close(STDIN_FILENO);
close(STDOUT_FILENO);
close(STDERR_FILENO);
}

server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(53);
server_addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);

fds_add(relay_fd, POLLIN);

for (;;) {
if (poll(fds, nfds, REQ_TIMEOUT * 1000) == -1) {
if (!do_fork) perror("poll");
exit(201);
}

time_t now = time(NULL);

for (int i = 1; i < nfds;) {
if (fds[i].revents == 0 && ret_info[i].timeout > now) {
// We are still waiting for an answer and timeout is still later
++i;
continue;
}

if (fds[i].revents == POLLIN) {
char buf[MAX_REQ_SIZE];
ssize_t len = recv(fds[i].fd, buf, sizeof(buf), MSG_WAITALL);
if (len > 0) {
ssize_t sent_len = sendto(
relay_fd, buf, len, 0,
(struct sockaddr*)&ret_info[i].addr, sizeof(struct sockaddr_in));
if (sent_len < 0) {
if (!do_fork) perror("relay sendto");
}
}
}

// Either we had an answer or the answer has timed out, remove fd from list
close(fds[i].fd);
fds_remove(i);
}

// Check on relay_fd if we received a request
if ((fds[0].revents & POLLERR) != 0) exit(200);
if ((fds[0].revents & POLLIN) != 0) {
struct sockaddr_in from;
socklen_t from_len = sizeof(from);
char buf[MAX_REQ_SIZE];
ssize_t len = recvfrom(
relay_fd, buf, sizeof(buf), MSG_WAITALL, (struct sockaddr*)&from, &from_len);

// We’ll ignore empty packets and packets received when the fds is full
if (len > 0 && nfds < MAX_SOCKETS) {
// Forward request to server
int server_fd = socket(AF_INET, SOCK_DGRAM, 0);
connect(server_fd, (struct sockaddr*)&server_addr, sizeof(server_addr));
ssize_t send_len = send(server_fd, buf, len, 0);
if (send_len < 0) {
if (!do_fork) perror("server sendto");
close(server_fd);
} else {
fds_add_with_ri(server_fd, POLLIN, from, now + REQ_TIMEOUT);
}
} else if (len < 0) {
if (!do_fork) perror("recvfrom");
}
}
}

return 0;
}

0 comments on commit 822c26e

Please sign in to comment.