All of lore.kernel.org
 help / color / mirror / Atom feed
From: "Flávio de Ayra Mendes" <flv@globax.com.br>
To: Massimiliano Cialdi <cialdi@firenze.net>
Cc: linux-c-programming@vger.kernel.org
Subject: Re: retriving a dhcp assigned ip
Date: Sat, 22 Mar 2003 19:06:11 -0300	[thread overview]
Message-ID: <3E7CDE53.60109@globax.com.br> (raw)
In-Reply-To: <20030322214216.45bc5681.cialdi@firenze.net>

[-- Attachment #1: Type: text/plain, Size: 221 bytes --]

Massimiliano Cialdi wrote:

>How can I retrive the ip address assigned to my host by a dhcp server?
>I think I need to use ioctl with SIOCGIFADDR parameter, but I don't know
>where the address is saved.
>
>thanks
>
>  
>

[-- Attachment #2: discoverip.c --]
[-- Type: text/x-csrc, Size: 1203 bytes --]

/* 
 * Wed Mar  3 16:21:48 EST 1999 - 
 * h4 <h4@locked.org>
 */

#include <stdio.h>
#include <errno.h>  
#include <string.h>

#include <sys/stat.h> /* socket() */
#include <sys/types.h> /* socket() */
#include <sys/ioctl.h> /* socket() */
#include <sys/socket.h> /* socket() */
#include <linux/if.h> /* ifreq */
#include <linux/in.h> /* sockaddr_in */

int main(int argc, char **argv)
{
  struct sockaddr_in *sk8;
  struct ifreq        r0x;

  int sockfd;

  if (argc != 2) {
    fprintf(stderr, "usage: %s <interface>\n", argv[0]);
    exit(1);
  }
  
  /* 
   * linux/if.h
   * #define ifr_name   ifr_ifrn.ifrn_name
   */
  strncpy(r0x.ifr_name, argv[1], sizeof(r0x.ifr_name));

  if ((sockfd = socket(AF_INET, SOCK_STREAM, IPPROTO_IP)) == -1) {
    fprintf(stderr, "socket(): %s\n", strerror(errno));
    exit(1);
  }

  if (ioctl(sockfd, SIOCGIFADDR, &r0x) != 0) {
    fprintf(stderr, "ioctl(): %s\n", strerror(errno));
    exit(1);
  }

  /*
   * linux/if.h
   * #define ifr_addr        ifr_ifru.ifru_addr
   */
  sk8 = (struct sockaddr_in *)&r0x.ifr_addr;

  fprintf(stdout, "interface : %s\n", r0x.ifr_name);
  fprintf(stdout, "address: %s\n", inet_ntoa(sk8->sin_addr.s_addr));

  return(0);
}


      parent reply	other threads:[~2003-03-22 22:06 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2003-03-22 20:42 retriving a dhcp assigned ip Massimiliano Cialdi
2003-03-22 20:46 ` Jan-Benedict Glaw
2003-03-22 22:06 ` Flávio de Ayra Mendes [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=3E7CDE53.60109@globax.com.br \
    --to=flv@globax.com.br \
    --cc=cialdi@firenze.net \
    --cc=linux-c-programming@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.