/* if the bootloader loaded a different file
 * than what the BOOTP/DHCP server says we have
 * then we want to forge the respective system
 * variables.
 */
static void
my_bootp_intercept(void)
{
int   media;
	/* Interfaces are attached; now see if we should set the media
	 * Note that we treat the case of an empty media string and
	 * 'auto' differently. In the former case, we simply leave the
	 * IF alone, otherwise we enforce autoconfig.
	 */

	if ( boot_my_media && *boot_my_media ) {
		/* Only do something if the string is not empty */
		media = rtems_str2ifmedia(boot_my_media, 0/* only 1 phy supported here */);
		if ( !media ) {
			fprintf(stderr,"Unable to configure IF media - invalid parameter '%s'\n",boot_my_media);
		} else {
			/* network port has already been selected and the IF name fixed */
			struct rtems_bsdnet_ifconfig *ifc;

			ifc = find_first_real_if();

			if ( !ifc ) {
				fprintf(stderr,"Unable to set IF media - no interface found\n");
			} else {
				if ( rtems_bsdnet_ifconfig(ifc->name, SIOCSIFMEDIA, &media )) {
					fprintf(stderr,
							"Setting IF media on %s (SIOCSIFMEDIA) failed: %s\n",
							ifc->name, strerror(errno));
				}
			}
		}
	}

	/* now check if we should do real bootp */
	if ( 'N' != do_bootp() ) {
		/* Do bootp first */
		if (the_apps_bootp) {
			the_apps_bootp();
		} else {
			rtems_bsdnet_do_bootp();
		}
	}

	if ( 'Y' != do_bootp() ) {
		/* override the server/filename parameters */
		fillin_srvrandfile();
	}
}
void mon_ifconfig(int argc, char *argv[],  uint32_t command_arg,
                  boolean verbose)
{
    struct sockaddr_in  ipaddr;
    struct sockaddr_in  dstaddr;
    struct sockaddr_in  netmask;
    struct sockaddr_in  broadcast;
    char               *iface;
    int                 f_ip        = 0;
    int                 f_ptp       = 0;
    int                 f_netmask   = 0;
    int                 f_up        = 0;
    int                 f_down      = 0;
    int                 f_bcast     = 0;
    int                 cur_idx;
    int                 rc;
    int                 flags;

    bzero((void*) &ipaddr, sizeof(ipaddr));
    bzero((void*) &dstaddr, sizeof(dstaddr));
    bzero((void*) &netmask, sizeof(netmask));
    bzero((void*) &broadcast, sizeof(broadcast));
    
    ipaddr.sin_len = sizeof(ipaddr);
    ipaddr.sin_family = AF_INET;
    
    dstaddr.sin_len = sizeof(dstaddr);
    dstaddr.sin_family = AF_INET;
    
    netmask.sin_len = sizeof(netmask);
    netmask.sin_family = AF_INET;
    
    broadcast.sin_len = sizeof(broadcast);
    broadcast.sin_family = AF_INET;
    
    cur_idx = 0;
    if (argc <= 1) {
        /* display all interfaces */
        iface = NULL;
        cur_idx += 1;
    } else {
        iface = argv[1];
        if (isdigit(*argv[2])) {
            if (inet_pton(AF_INET, argv[2], &ipaddr.sin_addr) < 0) {
                printf("bad ip address: %s\n", argv[2]);
                return;
            }
            f_ip = 1;
            cur_idx += 3;
        } else {
            cur_idx += 2;
        }
    }
    
    if ((f_down !=0) && (f_ip != 0)) {
        f_up = 1;
    }
    
    while(argc > cur_idx) {
        if (strcmp(argv[cur_idx], "up") == 0) {
            f_up = 1;
            if (f_down != 0) {
                printf("Can't make interface up and down\n");
            }
        } else if(strcmp(argv[cur_idx], "down") == 0) {
            f_down = 1;
            if (f_up != 0) {
                printf("Can't make interface up and down\n");
            }
        } else if(strcmp(argv[cur_idx], "netmask") == 0) {
            if ((cur_idx + 1) >= argc) {
                printf("No netmask address\n");
                return;
            } 
            if (inet_pton(AF_INET, argv[cur_idx+1], &netmask.sin_addr) < 0) {
                printf("bad netmask: %s\n", argv[cur_idx]);
                return;
            }
            f_netmask = 1;
            cur_idx += 1;
        } else if(strcmp(argv[cur_idx], "broadcast") == 0) {
            if ((cur_idx + 1) >= argc) {
                printf("No broadcast address\n");
                return;
            } 
            if (inet_pton(AF_INET, argv[cur_idx+1], &broadcast.sin_addr) < 0) {
                printf("bad broadcast: %s\n", argv[cur_idx]);
                return;
            }
            f_bcast = 1;
            cur_idx += 1;
        } else if(strcmp(argv[cur_idx], "pointopoint") == 0) {
            if ((cur_idx + 1) >= argc) {
                printf("No pointopoint address\n");
                return;
            } 
            if (inet_pton(AF_INET, argv[cur_idx+1], &dstaddr.sin_addr) < 0) {
                printf("bad pointopoint: %s\n", argv[cur_idx]);
                return;
            }
            
            f_ptp = 1;
            cur_idx += 1;
        } else {
            printf("Bad parameter: %s\n", argv[cur_idx]);
            return;
        }
        
        cur_idx += 1;
    }
    
    printf("ifconfig ");
    if (iface != NULL) {
        printf("%s ", iface);
        if (f_ip != 0) {
            char str[256];
            inet_ntop(AF_INET, &ipaddr.sin_addr, str, 256);
            printf("%s ", str);
        }
        
        if (f_netmask != 0) {
            char str[256];
            inet_ntop(AF_INET, &netmask.sin_addr, str, 256);
            printf("netmask %s ", str);
        }
        
        if (f_bcast != 0) {
            char str[256];
            inet_ntop(AF_INET, &broadcast.sin_addr, str, 256);
            printf("broadcast %s ", str);
        }
        
        if (f_ptp != 0) {
            char str[256];
            inet_ntop(AF_INET, &dstaddr.sin_addr, str, 256);
            printf("pointopoint %s ", str);
        }
        
        if (f_up != 0) {
            printf("up\n");
        } else if (f_down != 0) {
            printf("down\n");
        } else {
            printf("\n");
        }
    }
    
    if ((iface == NULL) || ((f_ip == 0) && (f_down == 0) && (f_up == 0))) {
        rtems_bsdnet_show_if_stats();
        return;
    }
    
    flags = 0;
    if (f_netmask) {
        rc = rtems_bsdnet_ifconfig(iface, SIOCSIFNETMASK, &netmask);
        if (rc < 0) {
            printf("Could not set netmask: %s\n", strerror(errno));
            return;
        }
    }
    
    if (f_bcast) {
        rc = rtems_bsdnet_ifconfig(iface, SIOCSIFBRDADDR, &broadcast);
        if (rc < 0) {
            printf("Could not set broadcast: %s\n", strerror(errno));
            return;
        }
    }
    
    if (f_ptp) {
        rc = rtems_bsdnet_ifconfig(iface, SIOCSIFDSTADDR, &dstaddr);
        if (rc < 0) {
            printf("Could not set destination address: %s\n", strerror(errno));
            return;
        }
        flags |= IFF_POINTOPOINT;
    }
    
    /* This must come _after_ setting the netmask, broadcast addresses */    
    if (f_ip) {
        rc = rtems_bsdnet_ifconfig(iface, SIOCSIFADDR, &ipaddr);
        if (rc < 0) {
            printf("Could not set IP address: %s\n", strerror(errno));
            return;
        }
    }
    
    if (f_up != 0) {
        flags |= IFF_UP;
    }
    
    if (f_down != 0) {
        printf("Warning: taking interfaces down is not supported\n");
    }
    
    rc = rtems_bsdnet_ifconfig(iface, SIOCSIFFLAGS, &flags);
    if (rc < 0) {
        printf("Could not set interface flags: %s\n", strerror(errno));
        return;
    }
}