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 */
 
 
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
 
  /* Reject non-ARP interfaces (including dummy interfaces) */
 
1117
 
  if(flags & IFF_NOARP){
 
1119
 
      fprintf(stderr, "Rejecting non-ARP interface \"%s\"\n",
 
1124
 
  /* Accept this device */
 
1126
 
    fprintf(stderr, "Interface \"%s\" is acceptable\n",
 
1132
1023
int main(int argc, char *argv[]){
 
1133
1024
  AvahiSServiceBrowser *sb = NULL;
 
 
1136
1027
  intmax_t tmpmax;
 
1138
1029
  int exitcode = EXIT_SUCCESS;
 
1139
 
  const char *interface = "";
 
 
1030
  const char *interface = "eth0";
 
1140
1031
  struct ifreq network;
 
1142
1033
  bool take_down_interface = false;
 
 
1036
  char *connect_to = NULL;
 
1145
1037
  char tempdir[] = "/tmp/mandosXXXXXX";
 
1146
1038
  bool tempdir_created = false;
 
1147
1039
  AvahiIfIndex if_index = AVAHI_IF_UNSPEC;
 
 
1305
1197
    avahi_set_log_function(empty_log);
 
1308
 
  if(interface[0] == '\0'){
 
1309
 
    struct dirent **direntries;
 
1310
 
    ret = scandir(sys_class_net, &direntries, good_interface,
 
1313
 
      /* Pick the first good interface */
 
1314
 
      interface = strdup(direntries[0]->d_name);
 
1316
 
        fprintf(stderr, "Using interface \"%s\"\n", interface);
 
1318
 
      if(interface == NULL){
 
1321
 
        exitcode = EXIT_FAILURE;
 
1327
 
      fprintf(stderr, "Could not find a network interface\n");
 
1328
 
      exitcode = EXIT_FAILURE;
 
1333
1200
  /* Initialize Avahi early so avahi_simple_poll_quit() can be called
 
1334
1201
     from the signal handler */
 
 
1407
1274
  /* If the interface is down, bring it up */
 
1408
 
  if(strcmp(interface, "none") != 0){
 
 
1275
  if(interface[0] != '\0'){
 
1409
1276
    if_index = (AvahiIfIndex) if_nametoindex(interface);
 
1410
1277
    if(if_index == 0){
 
1411
1278
      fprintf(stderr, "No such interface: \"%s\"\n", interface);
 
 
1634
 
    while(not quit_now){
 
1635
 
      ret = start_mandos_communication(address, port, if_index, af);
 
1636
 
      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;
 
1643
1523
      exitcode = EXIT_SUCCESS;
 
 
1745
1624
        network.ifr_flags &= ~(short)IFF_UP; /* clear flag */
 
1746
1625
        ret = ioctl(sd, SIOCSIFFLAGS, &network);
 
1748
 
          perror("ioctl SIOCSIFFLAGS -IFF_UP");
 
 
1627
          perror("ioctl SIOCSIFFLAGS");
 
1751
1630
      ret = (int)TEMP_FAILURE_RETRY(close(sd));