Skip site navigation (1)Skip section navigation (2)
Date:      Mon, 16 Sep 2002 18:51:03 +0000
From:      mm <meadele@nerim.net>
To:        Leslie Jackson <int@softhome.net>
Cc:        freebsd-hackers@FreeBSD.ORG
Subject:   Re: ioctl & SIOCDIFADDR
Message-ID:  <3D862817.8070007@nerim.net>
References:  <20020916114315.7feb4bac.int@softhome.net>

next in thread | previous in thread | raw e-mail | index | archive | help
Leslie Jackson wrote:
[..snip..]
> I can specify an specific IP, say,  "192.168.0.2",  for SIOCDIFADDR to delete.
> But how can i specify this "default address"(said in the netintor(4)) to
> "delete the first address of the interface"?

Hello,

You can try to request the first address of the specified interface via
ioctl call.


#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <net/if.h>
#include <netinet/in.h>
#include <sys/ioctl.h>
#include <arpa/inet.h>

/*
  * retrieve the FIRST address of the specified interface
  * you may want to directly merge it with your code.
  */
in_addr_t get_ifaddr(char *ifname)
{
  int s;
  struct ifreq ifs;

  ifs.ifr_addr.sa_family = AF_INET;
  s = socket(ifs.ifr_addr.sa_family, SOCK_DGRAM, 0);
  if( s < 0 ) {
     perror("get_ifaddr(): socket()");
     exit(1);
     }
  strncpy(ifs.ifr_name, ifname, sizeof(ifs.ifr_name));
  if( ioctl(s, OSIOCGIFADDR, &ifs) == -1 ) {
     perror("get_ifaddr(): ioctl()");
     exit(1);
     }
  close(s);

  return ((struct sockaddr_in *)&(ifs.ifr_addr))->sin_addr.s_addr;
}

/* your code */
int main(int argc, char *argv[])
{
  int s;
  struct ifaliasreq ifaliasreq;
  struct sockaddr_in *in;

  if( argc != 2 ) {
     fprintf(stderr, "%s ifname\n", argv[0]);
     return 1;
     }

  s = socket(PF_INET, SOCK_STREAM, 0);
  if( s == -1 ) {
     perror("socket()");
     return 1;
     }

  memset(&ifaliasreq, 0, sizeof(ifaliasreq));
  strncpy(ifaliasreq.ifra_name, argv[1], sizeof(ifaliasreq.ifra_name));

  in = (struct sockaddr_in *) &ifaliasreq.ifra_addr;
  in->sin_family = AF_INET;
  in->sin_len = sizeof(ifaliasreq.ifra_addr);

  /*
   * let get_ifaddr() find the address of the interface
   */
  in->sin_addr.s_addr = get_ifaddr(ifaliasreq.ifra_name);

  if( ioctl(s, SIOCDIFADDR, &ifaliasreq) == -1 ) {
     perror("SIOCDIFADDR");
     return 1;
     }

return 0;
}
bash-2.05# ifconfig rl0
rl0: flags=8843<UP,BROADCAST,RUNNING,SIMPLEX,MULTICAST> mtu 1500
         inet6 fe80::250:fcff:fe22:dc01%rl0 prefixlen 64 scopeid 0x1
         inet 192.168.0.1 netmask 0xffffff00 broadcast 192.168.0.255
         inet 192.168.0.10 netmask 0xffffffff broadcast 192.168.0.10
         inet 192.168.0.11 netmask 0xffffffff broadcast 192.168.0.11
         ether 00:50:fc:22:dc:01
         media: Ethernet 10baseT/UTP
         status: no carrier
bash-2.05# ./test rl0
bash-2.05# ifconfig rl0
rl0: flags=8843<UP,BROADCAST,RUNNING,SIMPLEX,MULTICAST> mtu 1500
         inet6 fe80::250:fcff:fe22:dc01%rl0 prefixlen 64 scopeid 0x1
         inet 192.168.0.10 netmask 0xffffffff broadcast 192.168.0.10
         inet 192.168.0.11 netmask 0xffffffff broadcast 192.168.0.11
         ether 00:50:fc:22:dc:01
         media: Ethernet 10baseT/UTP
         status: no carrier

the first rl0 address (192.168.0.1) has been removed.


To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-hackers" in the body of the message




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?3D862817.8070007>