如何获取网卡的地址?

hyq 2000-07-06 03:24:00
加精
环境BLUEPOINT 1.0.THANKS.
...全文
223 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
hem 2001-05-18
  • 打赏
  • 举报
回复
推荐
ilikevc 2000-07-06
  • 打赏
  • 举报
回复
Linux下,学内核分析时参考了别人的程序照着编的。
和内核有关的做了一点注释,见笑。
redhat6.0
正宗的源码可以从Linux的netkit源码包(rpm)里找到。

/* arp.c

Programed by Terry
Reffered cpu's source code in bbs.gznet.edu.cn socket ban

send arp request, get arp reply
get the mac address of the hosts in a subnet

gcc arp.c -o arp1

interface:default is eth0

Note : run it need root right or root has permitted it.

Example:

[root@46# work]# ./arp1 202.114.67.254
request mac 00:e0:4c:dd:79:e1, request IP 202.114.67.211
replied mac 00:e0:34:f0:18:08, replied IP 202.114.67.254
-----------------------------------------------------------------------------
Total Time Delay : 2314 us

[root@46# work]# ./arp1 202.114.67.254
request mac 00:e0:4c:dd:79:e1, request IP 202.114.67.211

--User Break!--
Total Time Delay : 3294147 us

*/

#include <stdio.h>
#include <string.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <netinet/in.h> /* struct in_addr : /usr/include/netinet/in.h */
#include <sys/types.h>
#include <sys/socket.h> /* /usr/src/linux/include/linux/socket.h */
#include <sys/ioctl.h>
#include <net/if.h> /* use the ifreq */
#include <net/if_arp.h> /* use the arphdr */
#include <net/ethernet.h> /* use the ethernethdr */
#include <signal.h>
#include <netinet/ip.h>
#include <sys/time.h> /* use the timeval */

#define DEF_INTERFACE "eth0"
#define PACKET_SIZE 2048

struct arp_hdr {

/* ehter header from /net/ethernet.h */

u_char ether_dhost[ETH_ALEN]; /* destination eth addr */
u_char ether_shost[ETH_ALEN]; /* source ether addr */
u_short ether_type; /* packet type ID field */

/* arp or rarp header from /net/if_arp.h */

u_short ar_hrd; /* Format of hardware address. */
u_short ar_pro; /* Format of protocol address. */
u_char ar_hln; /* Length of hardware address. */
u_char ar_pln; /* Length of protocol address. */
u_short ar_op; /* ARP opcode (command). */
u_char ar_sha[ETH_ALEN]; /* Sender hardware address. */
u_char ar_sip[4]; /* Sender IP address. */
u_char ar_tha[ETH_ALEN]; /* Target hardware address. */
u_char ar_tip[4]; /* Target IP address. */
u_char padding[18]; /* padding */

};

struct in_addr myself, dstaddr; /* defined in /usr/include/netinet/in.h */
/* uint32_t s_addr;Internet address */

int fd_arp; /* socket fd for receiving packets */

struct ifreq ifr; /* ifreq structure : */
/* defined in /usr/include/net/if.h */

long send_time,recv_time; /* time that send & receive arp packet */

long time_now() /* return time passed by */
/* since 1970.1.1 00:00:00, */
/* in 1/1000000 second */
{
struct timeval now;
long lPassed;
gettimeofday(&now, 0);
lPassed = now.tv_sec * 1000000 + now.tv_usec; /* now.tv_sec in second */
/* now.tv_usec in 1/1000000 second */
return lPassed;
}

void stat() /* signal process : quit */
{
printf( "\n--User Break!--\nTotal Time Delay : %ld us\n",time_now()-send_time);
exit(-1);
}


main(int argc, char *argv[])
{
char device[32]; /* ethernet device name */
struct sockaddr from, to;
int fromlen;
struct sockaddr_in * sin_ptr;
u_char * ptr;
int n;
u_char buf[PACKET_SIZE]; /* buffer for send and recv */
struct arp_hdr * arp = (struct arp_hdr *)buf;



if (argc < 2) {
printf("usage: %s ip [interface]\n", argv[0]);
exit(0);
}

if (argc >= 3) {
strncpy(device, argv[2], 32); /* interface name */
device[31] = '\0'; /* make device a string */
} else
strcpy(device, DEF_INTERFACE);

dstaddr.s_addr = inet_addr(argv[1]); /* convert IP xx.xx.. to binary */

if ((fd_arp = socket(AF_INET, SOCK_PACKET, htons(0x0806))) < 0) {
perror( "arp socket error"); /* htons : 0x0806 -- ARP */
exit(-1);
}

strcpy(ifr.ifr_name, device); /* ifr_name:Interface name */
ifr.ifr_addr.sa_family = AF_INET; /* ifr_addr:sockaddr ifru_addr */

/* get ip address of my interface */
if (ioctl(fd_arp, SIOCGIFADDR, &ifr) < 0) {
/* define SIOCGIFADDR 0x8915 -- socket requestof ioctl:get PA address */
/* in /usr/include/bits/ioctls.h */
perror("ioctl SIOCGIFADDR error");
exit(-1);
}
sin_ptr = (struct sockaddr_in *)&ifr.ifr_addr;
myself = sin_ptr->sin_addr;

/* get mac address of the interface */
if (ioctl(fd_arp, SIOCGIFHWADDR, &ifr) < 0) {
perror("ioctl SIOCGIFHWADDR error");
exit(-1);
}

ptr = (u_char *)&ifr.ifr_ifru.ifru_hwaddr.sa_data[0];
printf( "request mac %02x:%02x:%02x:%02x:%02x:%02x, ",
*ptr, *(ptr + 1), *(ptr + 2), *(ptr + 3),
*(ptr + 4), *(ptr + 5));
printf( "request IP %s\n", inet_ntoa(myself));

/* ether header for arp */
memset(arp->ether_dhost, 0xff, 6);
bcopy(&myself, arp->ether_shost, 4);
arp->ether_type = htons(ETHERTYPE_ARP);

/* arp header */
arp->ar_hrd = htons(ARPHRD_ETHER);
arp->ar_pro = htons(ETHERTYPE_IP);
arp->ar_hln = 6;
arp->ar_pln = 4;
arp->ar_op = htons(1);

bcopy(ptr,arp->ar_sha, 6);
bcopy(&myself, arp->ar_sip, 4);
bzero(arp->ar_tha, 6);
bcopy(&dstaddr, arp->ar_tip, 4);
bzero(arp->padding, 18);

strcpy(to.sa_data, device);
if ((n = sendto(fd_arp, buf, sizeof(struct arp_hdr), 0,
&to, sizeof(to))) < 0) {
perror("send arp request");
exit(-1);
}
send_time=time_now(); /* get send time */

strcpy(from.sa_data, device);
Loop:
if (n = recvfrom(fd_arp, buf, PACKET_SIZE, 0, &from, &fromlen)) {

recv_time=time_now(); /* get receive time */

if (arp->ar_op == 0x0200 || !memcmp(arp->ar_sip, &dstaddr, 4)) {
printf( "replied mac %02x:%02x:%02x:%02x:%02x:%02x, ",
arp->ar_sha[0], arp->ar_sha[1],
arp->ar_sha[2], arp->ar_sha[3],
arp->ar_sha[4], arp->ar_sha[5] );
printf( "replied IP %s\n", inet_ntoa(dstaddr));
printf( "-----------------------------------------------------------------------------\n");
printf( "Total Time Delay : %ld us\n",recv_time-send_time);

return;
} else
sigset(SIGINT, stat); /* when press del or ctrl+c, call stat */
/* to statistic the result , and then exit */

goto Loop;
}

}

hyq 2000-07-06
  • 打赏
  • 举报
回复
ifconfig的源码在那?请指教.
hyq 2000-07-06
  • 打赏
  • 举报
回复
那里可找到arp源码?
ilikevc 2000-07-06
  • 打赏
  • 举报
回复
不是编程实现的话,arp ip
编程实现,可参考arp源码。
但不在一个子网里的话,由于网关转发,得不到真实地址。

19,612

社区成员

发帖
与我相关
我的任务
社区描述
系统使用、管理、维护问题。可以是Ubuntu, Fedora, Unix等等
社区管理员
  • 系统维护与使用区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧