53
53
sockaddr_in6, PF_INET6,
54
54
SOCK_STREAM, uid_t, gid_t, open(),
56
#include <sys/stat.h> /* open() */
56
#include <sys/stat.h> /* open(), S_ISREG */
57
57
#include <sys/socket.h> /* socket(), struct sockaddr_in6,
58
58
inet_pton(), connect() */
59
59
#include <fcntl.h> /* open() */
1174
1177
* (This function is passed to scandir(3) as a filter function.)
1176
1179
int up_interface(const struct dirent *if_entry){
1178
char *flagname = NULL;
1179
1180
if(if_entry->d_name[0] == '.'){
1182
int ret = asprintf(&flagname, "%s/%s/flags", sys_class_net,
1185
perror_plus("asprintf");
1188
int flags_fd = (int)TEMP_FAILURE_RETRY(open(flagname, O_RDONLY));
1190
perror_plus("open");
1195
typedef short ifreq_flags; /* ifreq.ifr_flags in netdevice(7) */
1196
/* read line from flags_fd */
1197
ssize_t to_read = 2+(sizeof(ifreq_flags)*2)+1; /* "0x1003\n" */
1198
char *flagstring = malloc((size_t)to_read+1); /* +1 for final \0 */
1199
flagstring[(size_t)to_read] = '\0';
1200
if(flagstring == NULL){
1201
perror_plus("malloc");
1206
ssret = (ssize_t)TEMP_FAILURE_RETRY(read(flags_fd, flagstring,
1209
perror_plus("read");
1223
tmpmax = strtoimax(flagstring, &tmp, 0);
1224
if(errno != 0 or tmp == flagstring or (*tmp != '\0'
1225
and not (isspace(*tmp)))
1226
or tmpmax != (ifreq_flags)tmpmax){
1228
fprintf(stderr, "Invalid flags \"%s\" for interface \"%s\"\n",
1229
flagstring, if_entry->d_name);
1235
ifreq_flags flags = (ifreq_flags)tmpmax;
1236
/* Reject the loopback device */
1237
if(flags & IFF_LOOPBACK){
1239
fprintf(stderr, "Rejecting loopback interface \"%s\"\n",
1185
if(not get_flags(if_entry->d_name, &ifr)){
1187
fprintf(stderr, "Failed to get flags for interface \"%s\"\n",
1240
1188
if_entry->d_name);
1245
1193
/* Reject down interfaces */
1246
if(not (flags & IFF_UP)){
1250
/* Accept point-to-point devices only if connect_to is specified */
1251
if(connect_to != NULL and (flags & IFF_POINTOPOINT)){
1253
fprintf(stderr, "Accepting point-to-point interface \"%s\"\n",
1258
/* Otherwise, reject non-broadcast-capable devices */
1259
if(not (flags & IFF_BROADCAST)){
1261
fprintf(stderr, "Rejecting non-broadcast interface \"%s\"\n",
1266
/* Reject non-ARP interfaces (including dummy interfaces) */
1267
if(flags & IFF_NOARP){
1269
fprintf(stderr, "Rejecting non-ARP interface \"%s\"\n",
1274
/* Accept this device */
1276
fprintf(stderr, "Interface \"%s\" is acceptable\n",
1194
if(not (ifr.ifr_flags & IFF_UP)){
1196
fprintf(stderr, "Rejecting down interface \"%s\"\n",
1202
/* Reject non-running interfaces */
1203
if(not (ifr.ifr_flags & IFF_RUNNING)){
1205
fprintf(stderr, "Rejecting non-running interface \"%s\"\n",
1211
if(not good_flags(if_entry->d_name, &ifr)){
1677
1612
setenv("VERBOSE", debug ? "1" : "0", 1);
1678
1613
setenv("MODE", "start", 1);
1679
1614
/* setenv( XXX more here */
1680
ret = execl(fullname, direntry->d_name, "start");
1615
ret = execl(fullname, direntry->d_name, "start", NULL);
1681
1616
perror_plus("execl");
1683
1618
free(fullname);
1695
1630
if(interface[0] == '\0'){
1696
1631
struct dirent **direntries;
1697
ret = scandir(sys_class_net, &direntries, good_interface,
1632
/* First look for interfaces that are up */
1633
ret = scandir(sys_class_net, &direntries, up_interface,
1636
/* No up interfaces, look for any good interfaces */
1638
ret = scandir(sys_class_net, &direntries, good_interface,
1700
/* Pick the first good interface */
1642
/* Pick the first interface returned */
1701
1643
interface = strdup(direntries[0]->d_name);
1703
1645
fprintf(stderr, "Using interface \"%s\"\n", interface);