《UNIX网络编程》配置unp.h头文件

0x00 前言

文章中的文字可能存在语法错误以及标点错误,请谅解;

如果在文章中发现代码错误或其它问题请告知,感谢!

系统版本:Ubuntu 14.04_64

最后更新:2022-03-23

0x01配置步骤

1.下载&解压unpv13e

可以通过http://www.unpbook.com/unpv13e.tar.gz下载,也可以通过https://www.ituring.com.cn/book/164下载(本文选择此链接下载)。

下载完成后,解压源码传送到Ubuntu中。

2.查看unpv13e中README文件配置unp.h头文件

README内容如下,按照步骤一步一步配置:

QUICK AND DIRTY
===============

Execute the following from the src/ directory:

    ./configure    # try to figure out all implementation differences

    cd lib         # build the basic library that all programs need
    make           # use "gmake" everywhere on BSD/OS systems

    cd ../libfree  # continue building the basic library
    make

    cd ../libroute # only if your system supports 4.4BSD style routing sockets
    make           # only if your system supports 4.4BSD style routing sockets

    cd ../libxti   # only if your system supports XTI
    make           # only if your system supports XTI

    cd ../intro    # build and test a basic client program
    make daytimetcpcli
    ./daytimetcpcli 127.0.0.1

If all that works, you're all set to start compiling individual programs.

Notice that all the source code assumes tabs every 4 columns, not 8.

MORE DETAILS
============

5.  If you need to make any changes to the "unp.h" header, notice that it
    is a hard link in each directory, so you only need to change it once.

6.  Go into the "lib/" directory and type "make".  This builds the library
    "libunp.a" that is required by almost all of the programs.  There may
    be compiler warnings (see NOTES below).  This step is where you'll find
    all of your system's dependencies, and you must just update your cf/
    files from step 1, rerun "config" and do this step again.

6.  Go into the "libfree/" directory and type "make".  This adds to the
    "libunp.a" library.  The files in this directory do not #include
    the "unp.h" header, as people may want to use these functions
    independent of the book's examples.

8.  Once the library is made from steps 5 and 6, you can then go into any
    of the source code directories and make whatever program you are
    interested in.  Note that the horizontal rules at the beginning and
    end of each program listing in the book contain the directory name and
    filename.

    BEWARE: Not all programs in each directory will compile on all systems
    (e.g., the file src/advio/recvfromflags.c will not compile unless your
    system supports the IP_RECVDSTADDR socket option).  Also, not all files
    in each directory are included in the book.  Beware of any files with
    "test" in the filename: they are probably a quick test program that I
    wrote to check something, and may or may not work.

NOTES
-----

- Many systems do not have correct function prototypes for the socket
  functions, and this can cause many warnings during compilation.
  For example, Solaris 2.5 omits the "const" from the 2nd argument
  to connect().  Lots of systems use "int" for the length of socket
  address structures, while Posix.1g specifies "size_t".  Lots of
  systems still have the pointer argument to [sg]etsockopt() as a
  "char *" instead of a "void *", and this also causes warnings.

- SunOS 4.1.x: If you are using Sun's acc compiler, you need to run
  the configure program as

        CC=acc CFLAGS=-w CPPFLAGS=-w ./configure

  Failure to do this results in numerous system headers (<sys/sockio.h>)
  not being found during configuration, causing compile errors later.

- If your system supports IPv6 and you want to run the examples in the
  book using hostnames, you must install the latest BIND release.  You
  can get it from ftp://ftp.vix.com/pub/bind/release.  All you need from
  this release is a resolver library that you should then add to the
  LDLIBS and LDLIBS_THREADS lines.

- IPv6 support is still in its infancy.  There may be differences
  between the IPv6 sockets API specifications and what the vendor
  provides.  This may require hand tweaking, but should get better
  over time.

- If your system supports an older draft of the Posix pthreads standard,
  but configure detects the support of pthreads, you will have to disable
  this by hand.  Digital Unix V3.2C has this problem, for example, as it
  supports draft 4, not the final draft.

  To fix this, remove wrappthread.o from LIB_OBJS in "Make.defines" and
  don't try to build and run any of the threads programs.

COMMON DIFFERENCES
------------------

These are the common differences that I see in various headers that are
not "yet" at the level of Posix.1g or X/Open XNS Issue 5.

- getsockopt() and setsockopt(): 5th argument is not correct type.

- t_bind(): second argument is missing "const".

- t_connect(): second argument is missing "const".

- t_open(): first argument is missing "const".

- t_optmsmg(): second argument is missing "const".

- If your <xti.h> defines the members of the t_opthdr{} as longs,
  instead of t_uscalar_t, some of the printf formats of these value
  might generate warnings from your compiler, since you are printing
  a long without a corresponding long format specifier.

3.1配置脚本

在unpv13e打开终端,执行:

#./configure

3.2 创建lib库

#cd ./lib
#make

3.3创建libfree库

#cd ../libfree
#make

make时可能会有如下报错:
在这里插入图片描述打开该目录下inet_ntop.c文件,定位60行修改为socklen_t size;,保存后执行make,通过:

const char *
inet_ntop(af, src, dst, size)
	int af;
	const void *src;
	char *dst;
	socklen_t size;

3.4创建libroute库

3.4步骤适用于支持4.4BSD系统,可直接跳到3.5。

#cd ../libroute
#make

make时可能会有如下报错:
在这里插入图片描述新建if_dl.h文件,填写如下内容保存后将该if_dl.h文件移动到/usr/include/net目录下:

/*
 * 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
 */

/* 
 * 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_DLI */
    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 */
};

#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 */

#sudo mv if_dl.h /usr/include/net

3.5创建libxti库

3.5步骤适用于支持XTI系统,若不支持可直接跳到3.6。

#cd ../libxti
#make

3.6测试

#cd ../intro    
#make daytimetcpsrv
#make daytimetcpcli
#./daytimetcpsrv &
#./daytimetcpcli 127.0.0.1

测试结果如下:
在这里插入图片描述

3.将unp.h及静态库添加编译路径

#cp unpv13e/lib/unp.h	/usr/include
#cp unpv13e/config.h	/usr/include
#cp unpv13e/libunp.a	/usr/lib

修改/usr/include路径下unp.h头文件中的第7行为:

#include	<config.h>

测试:

#include<stdio.h>
#include<unp.h>
int main(){
	printf("hello world\n");
	return 0;
}

编译无错误则成功。

以上。

参考文档:
1.https://blog.csdn.net/ASCE_S/article/details/106969129

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值