UNIX网络编程学前准备

  1. 获取unp源码
    可以从图灵社区获取.

  2. 执行configure
    cd unpv13e
    ./configure
    如果显示权限不够, 修改一下configure文件的权限: chmod 777 configure

  3. 打开README文件, 按说明进行make

    $ cd ../libfree/
    $ make
    gcc -I../lib -g -O2 -D_REENTRANT -Wall   -c -o in_cksum.o in_cksum.c
    gcc -I../lib -g -O2 -D_REENTRANT -Wall   -c -o inet_ntop.o inet_ntop.c
    inet_ntop.c: 在函数‘inet_ntop’中:
    inet_ntop.c:60:9: 错误:实参‘size’与原型不符
      size_t size;
             ^
    In file included from inet_ntop.c:27:0:
    /usr/include/arpa/inet.h:64:20: 错误:原型声明
     extern const char *inet_ntop (int __af, const void *__restrict __cp,
                        ^
    make: *** [inet_ntop.o] 错误 1

size_t size;改为socklen_t size;就可以了

    $ cd ../libroute/
    $ make
    gcc -I../lib -g -O2 -D_REENTRANT -Wall   -c -o get_rtaddrs.o get_rtaddrs.c
    In file included from get_rtaddrs.c:1:0:
    unproute.h:3:45: 致命错误:net/if_dl.h:没有那个文件或目录
    编译中断。
    make: *** [get_rtaddrs.o] 错误 1

在usr/include/net/ 下,添加源代码文件if_dl.h 即可。

if_dl.h源代码

/*
* Copyright (c) 1990, 1993
* The Regents of the University of California.  All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
*    notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
*    notice, this list of conditions and the following disclaimer in the
*    documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
*    must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
*    may be used to endorse or promote products derived from this software
*    without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* @(#)if_dl.h 8.1 (Berkeley) 6/10/93
* $FreeBSD: src/sys/net/if_dl.h,v 1.10 2000/03/01 02:46:25 archie Exp $
*/

#ifndef _NET_IF_DL_H_
#define _NET_IF_DL_H_

/*
* A Link-Level Sockaddr may specify the interface in one of two
* ways: either by means of a system-provided index number (computed
* anew and possibly differently on every reboot), or by a human-readable
* string such as "il0" (for managerial convenience).
*
* Census taking actions, such as something akin to SIOCGCONF would return
* both the index and the human name.
*
* High volume transactions (such as giving a link-level ``from'' address
* in a recvfrom or recvmsg call) may be likely only to provide the indexed
* form, (which requires fewer copy operations and less space).
*
* The form and interpretation  of the link-level address is purely a matter
* of convention between the device driver and its consumers; however, it is
* expected that all drivers for an interface of a given if_type will agree.
*/

/*
* Structure of a Link-Level sockaddr:
*/
struct sockaddr_dl {
u_char sdl_len; /* Total length of sockaddr */
u_char sdl_family; /* AF_LINK */
u_short sdl_index; /* if != 0, system given index for interface */
u_char sdl_type; /* interface type */
u_char sdl_nlen; /* interface name length, no trailing 0 reqd. */
u_char sdl_alen; /* link level address length */
u_char sdl_slen; /* link layer selector length */
char sdl_data[12]; /* minimum work area, can be larger;
   contains both if name and ll address */
u_short sdl_rcf; /* source routing control */
u_short sdl_route[16]; /* source routing information */
};

#define LLADDR(s) ((caddr_t)((s)->sdl_data + (s)->sdl_nlen))

#ifndef _KERNEL

#include <sys/cdefs.h>

__BEGIN_DECLS
void link_addr __P((const char *, struct sockaddr_dl *));
char *link_ntoa __P((const struct sockaddr_dl *));
__END_DECLS

#endif /* !_KERNEL */

#endif

源码地址

$ make
gcc -I../lib -g -O2 -D_REENTRANT -Wall   -c -o get_rtaddrs.o get_rtaddrs.c
get_rtaddrs.c: 在函数‘get_rtaddrs’中:
get_rtaddrs.c:21:18: 错误:‘RTAX_MAX’未声明(在此函数内第一次使用)
  for (i = 0; i < RTAX_MAX; i++) {
                  ^

get_rtaddrs.c 中RTAX_MAX变量未声明,在/libroute/unproute.h 加 #define RTAX_MAX 1024

$ make
gcc -I../lib -g -O2 -D_REENTRANT -Wall   -c -o get_rtaddrs.o get_rtaddrs.c
get_rtaddrs.c: 在函数‘get_rtaddrs’中:
get_rtaddrs.c:13:21: 错误:‘struct sockaddr’没有名为‘sa_len’的成员
  ((caddr_t) ap + (ap->sa_len ? ROUNDUP(ap->sa_len, sizeof (u_long)) : 
                     ^
get_rtaddrs.c:24:4: 附注:in expansion of macro ‘NEXT_SA’
    NEXT_SA(sa);
    ^

struct sockaddr 结构体中没有sa_len变量,不单单是这个问题,主因是only if your system supports 4.4BSD style routing sockets。
BSD之所以要在socket地址结构体里面定义一个长度字段,是因为BSD代码中需要处理socket地址结构体的代码都是统一的,与协议无关的。根据这个字段就可以知道后面需要处理的地址实际有多长。

原文地址:https://www.cnblogs.com/jie828/p/12883723.html