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

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

 

 

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