DNSmasq

DNSmasq 代码分析

代码流程分析

遇到的知识点

  1. od shell命令
  2. /dev/urandom
  3. 实现引导时启动
  4. setjump & longjump jump_buf
  5. SLAAC
  6. getopt & getopt_long 函数
  7. DHCP服务器如何知道 DNS server and route 6.4节
  8. ACD技术
  9. PXE(Preboot Execution Enviroment)预启动执行环境
  10. strtok 函数
  11. uname函数
  12. 实现read write时的注意
  13. 关闭文件描述符的注意
  14. 判断linux版本 (ipset.c line 88 ipset_init 函数)
  15. [AF_NETLINK & IPPROTO_RAW 选项](# 15-> AF_NETLINK & IPPROTO_RAW 选项)
  16. readv & writev(聚合读、散布写)
  17. [SO_BINDTODEVICE socket选项](# 17-> SO_BINDTODEVICE 选项)
  18. inotify 函数
  19. [if_nametodeix 函数](# 19-> if_nametoindex & if_indextoname)
  20. [getpwnam 函数](# 20-> getpwnam函数)
  21. [设置文件描述符 NONBLOCK](# 21-> 设置文件描述符 NONBLOCK)
  22. safe_pip & safe_malloc & wine_malloc Code(详细见dnsmasq源码)
  23. [chown & fchown & lchown 函数](# 23-> chown & fchown & lchown 函数)
  24. difftime 函数

遇到知识点解答

15-> AF_NETLINK & IPPROTO_RAW 选项

IPPROTO_RAW

对于socket(AF_INET, SOCK_RAW, IPPROTO_IP),其原型为
int socket (int domain, int type, int protocol);
1 参数protocol用来指明所要接收的协议包,如果是象IPPROTO_TCP(6)这种非0、非255的协议,当操作系统内核碰到ip头中protocol域和创建socket所使用参数protocol相同的IP包,就会交给这个raw socket来处理,因此,一般来说,要想接收什么样的数据包,就应该在参数protocol里来指定相应的协议。当内核向此raw socket交付数据包的时候,是包括整个IP头的,并且已经是重组好的IP包。
2 如果protocol是IPPROTO_RAW(255),这时候,这个socket只能用来发送IP包,而不能接收任何的数据。发送的数据需要自己填充IP包头,并且自己计算校验和。
3 对于protocol为0(IPPROTO_IP)的raw socket。用于接收任何的IP数据包。其中的校验和和协议分析由程序自己完成。

AF_NETLINK

netlink 凭借其标准的 socket API、模块化实现、异步通信机制、多播机制等等多种优势,成为了内核与越来越多应用程序之间交互的主要方式。在 Linux 的内核中,已经为我们封装了使用 netlink 对特定网络状态变化进行消息通知的功能,这就是著名的 rtnetlink。详细介绍参考IBM学习

17-> SO_BINDTODEVICE 选项

指定套接字数据收发从哪一个设备。

SO_BINDTODEVICE
Bind this socket to a particular device like “eth0”, as specified in the passed interface name. If the
name is an empty string or the option length is zero, the socket device binding is removed.
———— In man 7 socket

19-> if_nametoindex & if_indextoname

The if_nametoindex() function returns the index of the network interface corresponding to the name ifname.
The if_indextoname() function returns the name of the network interface corresponding to the interface index ifindex. The name is placed in the buffer pointed to by ifname. The buffer must allow for the storage of at least IF_NAMESIZE bytes.

​ ———— In man page

20-> getpwnam函数

The getpwnam() function returns a pointer to a structure containing the broken-out fields of the record in the password database (e.g., the local password file /etc/passwd, NIS, and LDAP) that matches the username name.

The getpwuid() function returns a pointer to a structure containing the broken-out fields of the record in the password database that matches the user ID uid.

​ ————[nam page]

21-> 设置文件描述符 NONBLOCK

/* set NONBLOCK bit on fd: See Stevens 16.6 */
int fix_fd(int fd)
{
  int flags;

  if ((flags = fcntl(fd, F_GETFL)) == -1 ||
      fcntl(fd, F_SETFL, flags | O_NONBLOCK) == -1)
    return 0;
  
  return 1;
}

23-> chown & fchown & lchown 函数

  • chown() changes the ownership of the file specified by pathname, which is dereferenced if it is a symbolic link.
  • fchown() changes the ownership of the file referred to by the open file descriptor fd.
  • lchown() is like chown(), but does not dereference symbolic links
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值