9
9
* "browse_callback", and parts of "main".
11
11
* Everything else is
12
* Copyright © 2008-2011 Teddy Hogeborn
13
* Copyright © 2008-2011 Björn Påhlsson
12
* Copyright © 2008,2009 Teddy Hogeborn
13
* Copyright © 2008,2009 Björn Påhlsson
15
15
* This program is free software: you can redistribute it and/or
16
16
* modify it under the terms of the GNU General Public License as
64
64
#include <assert.h> /* assert() */
65
65
#include <errno.h> /* perror(), errno */
66
#include <time.h> /* nanosleep(), time(), sleep() */
66
#include <time.h> /* nanosleep(), time() */
67
67
#include <net/if.h> /* ioctl, ifreq, SIOCGIFFLAGS, IFF_UP,
68
68
SIOCSIFFLAGS, if_indextoname(),
69
69
if_nametoindex(), IF_NAMESIZE */
127
127
static const char mandos_protocol_version[] = "1";
128
128
const char *argp_program_version = "mandos-client " VERSION;
129
129
const char *argp_program_bug_address = "<mandos@fukt.bsnet.se>";
130
static const char sys_class_net[] = "/sys/class/net";
131
char *connect_to = NULL;
133
131
/* Used for passing in values through the Avahi callback functions */
1024
1020
errno = old_errno;
1028
* This function determines if a directory entry in /sys/class/net
1029
* corresponds to an acceptable network device.
1030
* (This function is passed to scandir(3) as a filter function.)
1032
int good_interface(const struct dirent *if_entry){
1034
char *flagname = NULL;
1035
if(if_entry->d_name[0] == '.'){
1038
int ret = asprintf(&flagname, "%s/%s/flags", sys_class_net,
1044
int flags_fd = (int)TEMP_FAILURE_RETRY(open(flagname, O_RDONLY));
1051
typedef short ifreq_flags; /* ifreq.ifr_flags in netdevice(7) */
1052
/* read line from flags_fd */
1053
ssize_t to_read = (sizeof(ifreq_flags)*2)+3; /* "0x1003\n" */
1054
char *flagstring = malloc((size_t)to_read+1); /* +1 for final \0 */
1055
flagstring[(size_t)to_read] = '\0';
1056
if(flagstring == NULL){
1062
ssret = (ssize_t)TEMP_FAILURE_RETRY(read(flags_fd, flagstring,
1079
tmpmax = strtoimax(flagstring, &tmp, 0);
1080
if(errno != 0 or tmp == flagstring or (*tmp != '\0'
1081
and not (isspace(*tmp)))
1082
or tmpmax != (ifreq_flags)tmpmax){
1084
fprintf(stderr, "Invalid flags \"%s\" for interface \"%s\"\n",
1085
flagstring, if_entry->d_name);
1091
ifreq_flags flags = (ifreq_flags)tmpmax;
1092
/* Reject the loopback device */
1093
if(flags & IFF_LOOPBACK){
1095
fprintf(stderr, "Rejecting loopback interface \"%s\"\n",
1100
/* Accept point-to-point devices only if connect_to is specified */
1101
if(connect_to != NULL and (flags & IFF_POINTOPOINT)){
1103
fprintf(stderr, "Accepting point-to-point interface \"%s\"\n",
1108
/* Otherwise, reject non-broadcast-capable devices */
1109
if(not (flags & IFF_BROADCAST)){
1111
fprintf(stderr, "Rejecting non-broadcast interface \"%s\"\n",
1116
/* Accept this device */
1118
fprintf(stderr, "Interface \"%s\" is acceptable\n",
1124
1023
int main(int argc, char *argv[]){
1125
1024
AvahiSServiceBrowser *sb = NULL;
1128
1027
intmax_t tmpmax;
1130
1029
int exitcode = EXIT_SUCCESS;
1131
const char *interface = "";
1030
const char *interface = "eth0";
1132
1031
struct ifreq network;
1134
1033
bool take_down_interface = false;
1036
char *connect_to = NULL;
1137
1037
char tempdir[] = "/tmp/mandosXXXXXX";
1138
1038
bool tempdir_created = false;
1139
1039
AvahiIfIndex if_index = AVAHI_IF_UNSPEC;
1297
1197
avahi_set_log_function(empty_log);
1300
if(interface[0] == '\0'){
1301
struct dirent **direntries;
1302
ret = scandir(sys_class_net, &direntries, good_interface,
1305
/* Pick the first good interface */
1306
interface = strdup(direntries[0]->d_name);
1308
fprintf(stderr, "Using interface \"%s\"\n", interface);
1310
if(interface == NULL){
1313
exitcode = EXIT_FAILURE;
1319
fprintf(stderr, "Could not find a network interface\n");
1320
exitcode = EXIT_FAILURE;
1325
1200
/* Initialize Avahi early so avahi_simple_poll_quit() can be called
1326
1201
from the signal handler */
1399
1274
/* If the interface is down, bring it up */
1400
if(strcmp(interface, "none") != 0){
1275
if(interface[0] != '\0'){
1401
1276
if_index = (AvahiIfIndex) if_nametoindex(interface);
1402
1277
if(if_index == 0){
1403
1278
fprintf(stderr, "No such interface: \"%s\"\n", interface);
1626
while(not quit_now){
1627
ret = start_mandos_communication(address, port, if_index, af);
1628
if(quit_now or ret == 0){
1501
ret = start_mandos_communication(address, port, if_index, af);
1507
exitcode = EX_NOHOST;
1510
exitcode = EX_USAGE;
1513
exitcode = EX_IOERR;
1516
exitcode = EX_PROTOCOL;
1519
exitcode = EX_OSERR;
1635
1523
exitcode = EXIT_SUCCESS;
1737
1624
network.ifr_flags &= ~(short)IFF_UP; /* clear flag */
1738
1625
ret = ioctl(sd, SIOCSIFFLAGS, &network);
1740
perror("ioctl SIOCSIFFLAGS -IFF_UP");
1627
perror("ioctl SIOCSIFFLAGS");
1743
1630
ret = (int)TEMP_FAILURE_RETRY(close(sd));