125
125
static const char mandos_protocol_version[] = "1";
126
126
const char *argp_program_version = "mandos-client " VERSION;
127
127
const char *argp_program_bug_address = "<mandos@fukt.bsnet.se>";
128
static const char sys_class_net[] = "/sys/class/net";
129
char *connect_to = NULL;
129
131
/* Used for passing in values through the Avahi callback functions */
974
976
errno = old_errno;
980
* This function determines if a directory entry in /sys/class/net
981
* corresponds to an acceptable network device.
982
* (This function is passed to scandir(3) as a filter function.)
984
int good_interface(const struct dirent *if_entry){
986
char *flagname = NULL;
987
int ret = asprintf(&flagname, "%s/%s/flags", sys_class_net,
993
if(if_entry->d_name[0] == '.'){
996
int flags_fd = (int)TEMP_FAILURE_RETRY(open(flagname, O_RDONLY));
1001
typedef short ifreq_flags; /* ifreq.ifr_flags in netdevice(7) */
1002
/* read line from flags_fd */
1003
ssize_t to_read = (sizeof(ifreq_flags)*2)+3; /* "0x1003\n" */
1004
char *flagstring = malloc((size_t)to_read);
1005
if(flagstring == NULL){
1011
ssret = (ssize_t)TEMP_FAILURE_RETRY(read(flags_fd, flagstring,
1028
tmpmax = strtoimax(flagstring, &tmp, 0);
1029
if(errno != 0 or tmp == flagstring or (*tmp != '\0'
1030
and not (isspace(*tmp)))
1031
or tmpmax != (ifreq_flags)tmpmax){
1036
ifreq_flags flags = (ifreq_flags)tmpmax;
1037
/* Reject the loopback device */
1038
if(flags & IFF_LOOPBACK){
1041
/* Accept point-to-point devices only if connect_to is specified */
1042
if(connect_to != NULL and (flags & IFF_POINTOPOINT)){
1045
/* Otherwise, reject non-broadcast-capable devices */
1046
if(not (flags & IFF_BROADCAST)){
1049
/* Accept this device */
977
1053
int main(int argc, char *argv[]){
978
1054
AvahiSServiceBrowser *sb = NULL;
981
1057
intmax_t tmpmax;
983
1059
int exitcode = EXIT_SUCCESS;
984
const char *interface = "eth0";
1060
const char *interface = "";
985
1061
struct ifreq network;
987
1063
bool take_down_interface = false;
990
char *connect_to = NULL;
991
1066
char tempdir[] = "/tmp/mandosXXXXXX";
992
1067
bool tempdir_created = false;
993
1068
AvahiIfIndex if_index = AVAHI_IF_UNSPEC;
1124
1199
avahi_set_log_function(empty_log);
1202
if(interface[0] == '\0'){
1203
struct dirent **direntries;
1204
ret = scandir(sys_class_net, &direntries, good_interface,
1207
/* Pick the first good interface */
1208
interface = strdup(direntries[0]->d_name);
1209
if(interface == NULL){
1212
exitcode = EXIT_FAILURE;
1218
fprintf(stderr, "Could not find a network interface\n");
1219
exitcode = EXIT_FAILURE;
1127
1224
/* Initialize Avahi early so avahi_simple_poll_quit() can be called
1128
1225
from the signal handler */
1201
1298
/* If the interface is down, bring it up */
1202
if(interface[0] != '\0'){
1299
if(strcmp(interface, "none") != 0){
1203
1300
if_index = (AvahiIfIndex) if_nametoindex(interface);
1204
1301
if(if_index == 0){
1205
1302
fprintf(stderr, "No such interface: \"%s\"\n", interface);