当前位置:   article > 正文

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

unpv13e if_dl.h

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

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

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

  1. ./configure # try to figure out all implementation differences
  2. cd lib # build the basic library that all programs need
  3. make # use "gmake" everywhere on BSD/OS systems

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

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

错误提示:

  1. gcc -I../lib -g -O2 -D_REENTRANT -Wall -c -o in_cksum.o in_cksum.c
  2. gcc -I../lib -g -O2 -D_REENTRANT -Wall -c -o inet_ntop.o inet_ntop.c
  3. inet_ntop.c: In function ‘inet_ntop’:
  4. inet_ntop.c:60:9: error: argument ‘size’ doesn’t match prototype
  5. size_t size;
  6. ^~~~
  7. In file included from inet_ntop.c:27:
  8. /usr/include/arpa/inet.h:64:20: error: prototype declaration
  9. extern const char *inet_ntop (int __af, const void *__restrict __cp,
  10. ^~~~~~~~~
  11. make: *** [<内置>:inet_ntop.o] 错误 1

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

#define size_t socklen_t 

2、在/usr/include/net路径下添加一个文件:if_dl.h 。if_dl.h的源代码如下:

  1. /*
  2. * Copyright (c) 1990, 1993
  3. * The Regents of the University of California. All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * 3. All advertising materials mentioning features or use of this software
  14. * must display the following acknowledgement:
  15. * This product includes software developed by the University of
  16. * California, Berkeley and its contributors.
  17. * 4. Neither the name of the University nor the names of its contributors
  18. * may be used to endorse or promote products derived from this software
  19. * without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  22. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  25. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  26. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  27. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  28. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  29. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  30. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  31. * SUCH DAMAGE.
  32. *
  33. * @(#)if_dl.h 8.1 (Berkeley) 6/10/93
  34. * $FreeBSD: src/sys/net/if_dl.h,v 1.10 2000/03/01 02:46:25 archie Exp $
  35. */
  36. #ifndef _NET_IF_DL_H_
  37. #define _NET_IF_DL_H_
  38. /*
  39. * A Link-Level Sockaddr may specify the interface in one of two
  40. * ways: either by means of a system-provided index number (computed
  41. * anew and possibly differently on every reboot), or by a human-readable
  42. * string such as "il0" (for managerial convenience).
  43. *
  44. * Census taking actions, such as something akin to SIOCGCONF would return
  45. * both the index and the human name.
  46. *
  47. * High volume transactions (such as giving a link-level ``from'' address
  48. * in a recvfrom or recvmsg call) may be likely only to provide the indexed
  49. * form, (which requires fewer copy operations and less space).
  50. *
  51. * The form and interpretation of the link-level address is purely a matter
  52. * of convention between the device driver and its consumers; however, it is
  53. * expected that all drivers for an interface of a given if_type will agree.
  54. */
  55. /*
  56. * Structure of a Link-Level sockaddr:
  57. */
  58. struct sockaddr_dl {
  59. u_char sdl_len; /* Total length of sockaddr */
  60. u_char sdl_family; /* AF_LINK */
  61. u_short sdl_index; /* if != 0, system given index for interface */
  62. u_char sdl_type; /* interface type */
  63. u_char sdl_nlen; /* interface name length, no trailing 0 reqd. */
  64. u_char sdl_alen; /* link level address length */
  65. u_char sdl_slen; /* link layer selector length */
  66. char sdl_data[12]; /* minimum work area, can be larger;
  67. contains both if name and ll address */
  68. u_short sdl_rcf; /* source routing control */
  69. u_short sdl_route[16]; /* source routing information */
  70. };
  71. #define LLADDR(s) ((caddr_t)((s)->sdl_data + (s)->sdl_nlen))
  72. #ifndef _KERNEL
  73. #include <sys/cdefs.h>
  74. __BEGIN_DECLS
  75. void link_addr __P((const char *, struct sockaddr_dl *));
  76. char *link_ntoa __P((const struct sockaddr_dl *));
  77. __END_DECLS
  78. #endif /* !_KERNEL */
  79. #endif

然后可以正常的执行上面的make了。

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

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

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

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

connect error: Connection refused

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

有两个解决方案:

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

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

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

 service xinetd restart

此时,直接执行,

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

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

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


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

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

  1. make daytimetcpsrv
  2. sudo ./daytimetcpsrv

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

  1. make daytimetcpcli
  2. ./daytimetcpcli 127.0.0.1

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

 

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/很楠不爱3/article/detail/158776
推荐阅读
  

闽ICP备14008679号