UNIX网络编程:unpv13e源代码包的环境配置及intro的执行范例

狠心买下正版《UNIX网络编程》后,喵哥开始了网络编程的道路。在书中介绍的第一个例子:输出服务器端的时间信息。

官方提供的源代码都是基于自己的头文件写的,所以要使用这些源代码需要使用他们的头文件,这个不是自己靠复制一两个头文件到目的文件夹那么简单,幸好,官方提供了README,按照README基本上不会有啥问题,除了接下来要解决的几个。

终端进入unpv13e目录下,执行前面的命令比较顺利:

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

在执行第二个make的时候就报错了。

     cd ../libfree  # continue building the basic library
     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: In function ‘inet_ntop’:
inet_ntop.c:60:9: error: argument ‘size’ doesn’t match prototype
  size_t size;
         ^~~~
In file included from inet_ntop.c:27:
/usr/include/arpa/inet.h:64:20: error: prototype declaration
 extern const char *inet_ntop (int __af, const void *__restrict __cp,
                    ^~~~~~~~~
make: *** [<内置>:inet_ntop.o] 错误 1

解决方法:1、在inet_ntop.c中加入

#define size_t socklen_t 

2、在/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了。

README接下来的两个make是针对BSD系统的,而喵哥用的是ubuntu(GNU),所以不需要执行它们,事实上直接执行是会报错的。。。

接下来,就可以进入intro,开始执行第一个程序了,至少README是这么说的:)

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

果真如此么?通常,你这么执行会报如下错误:

connect error: Connection refused

这个错误的意思是: daytime 服务程序没开(为了安全默认是关闭的)。

有两个解决方案:

1、安装xinetd,然后允许访问daytime服务。

sudo apt-get install xinetd
cd /etc/xinetd.d/
sudo vim daytime

文件中有两个 disable = yes ,全部修改成 disable = no,并保存退出,然后重启 xinetd

 service xinetd restart

此时,直接执行,

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

即可得到本机的时间信息。

附:关于如何使用Xinetd:Xinetd服务的安装与配置、xinetd不太详的详解


2、开启系统原本禁止访问的daytime服务可能会导致无法预料的危险,所以不是太提倡按照方法1解决。

实际上在intro文件夹下有相应的服务端源代码,只要用它开启一个服务器即可。

make daytimetcpsrv.c
sudo ./daytimetcpsrv

然后在另外一个终端中输入:

    make daytimetcpcli
    ./daytimetcpcli 127.0.0.1

就可以得到想要的结果了。

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章