我很困惑什么时候使用ntohs和ntohl。我知道你什么时候用unt16_t和ntohl uint32_t使用ntohs。但那些unsigned int或那些指定了特定位数的函数(例如u_int16_t doff:4;)呢?什么时候在C中使用ntohs和ntohl?

这是我工作的代码在于向世人证明问题:

// Utility/Debugging method for dumping raw packet data

void dump(const unsigned char *data, int length) {

unsigned int i;

static unsigned long pcount = 0;

// Decode Packet Header

struct ether_header *eth_header = (struct ether_header *) data;

printf("\n\n === PACKET %ld HEADER ===\n", pcount);

printf("\nSource MAC: ");

for (i = 0; i < 6; ++i) {

printf("%02x", eth_header->ether_shost[i]);

if (i < 5) {

printf(":");

}

}

printf("\nDestination MAC: ");

unsigned short ethernet_type = ntohs(eth_header->ether_type);

printf("\nType: %hu\n", ethernet_type); //Why not nthos?

if (ethernet_type == ETHERTYPE_IP) { //IP Header

printf("\n == IP HEADER ==\n");

struct ip *ip_hdr = (struct ip*) (data + sizeof(struct ether_header));

unsigned int size_ip = ip_hdr->ip_hl * 4; //why no nthos or nthol

printf("\nip_hdr->ip_hl: %u", ip_hdr->ip_hl); //why no nthos or nthol

printf("\nIP Version: %u", ip_hdr->ip_v); //why no nthos or nthol

printf("\nHeader Length: %u", ip_hdr->ip_hl); //why no nthos or nthol

printf("\nTotal Length: %hu", ntohs(ip_hdr->ip_len)); //?is this right?

// TCP Header

printf("\n== TCP HEADER ==\n");

struct tcphdr *tcp_hdr = (struct tcphdr*) (data + sizeof(struct ether_header) + size_ip);

unsigned int size_tcp = tcp_hdr->doff * 4; //why no nthos or nthol

printf("\n Source Port: %" PRIu16, ntohs(tcp_hdr->th_sport));

printf("\n Destination Port: %" PRIu16, ntohs(tcp_hdr->th_dport));

printf("\n fin: %" PRIu16, tcp_hdr->fin); //As this is 1 bit, both nthos or nthol will work

printf("\n urg: %" PRIu16, tcp_hdr->urg); //As this is 1 bit, both nthos or nthol will work

printf("\n ack_seq: %" PRIu32, ntohl(tcp_hdr->ack_seq));

u_int16_t sourcePort = ntohs(tcp_hdr->th_sport);

u_int16_t destinationPort = ntohs(tcp_hdr->th_sport);

if (sourcePort == 80 || destinationPort == 80){

printf("\n\nPORT 80!!!\n");

//Transport payload!

printf("\n\ === TCP PAYLOAD DATA == \n");

// Decode Packet Data (Skipping over the header)

unsigned int headers_size = ETH_HLEN + size_ip + size_tcp;

unsigned int data_bytes = length - headers_size;

const unsigned char *payload = data + headers_size;

const static int output_sz = 500; // Output this many bytes at a time

while (data_bytes > 0) {

int output_bytes = data_bytes < output_sz ? data_bytes : output_sz;

// Print data in raw hexadecimal form

printf("| ");

// Print data in ascii form

for (i = 0; i < output_bytes; ++i) {

char byte = payload[i];

if ((byte > 31 && byte < 127) || byte == '\n') {

// Byte is in printable ascii range

printf("%c", byte); //why no nthos or nthol

} else {

printf(".");

}

}

payload += output_bytes;

data_bytes -= output_bytes;

}

}

}

pcount++;

}

正如你可以看到有次我用ntohs和/和再用ntohl有次我都不使用。我不明白何时使用哪个。

ntohl c 语言函数,什么时候在C中使用ntohs和ntohl?相关推荐

  1. c语言复制粘贴源码,c语言函数memccpy()如何复制内存中的内容实例源码介绍

    c语言函数memccpy()如何复制内存中的内容实例源码介绍.引入的头文件:#include memccpy()函数定义:void * memccpy(void *dest, const void * ...

  2. c语言 函数的参数传递示例_C ++中带有示例的nearint()函数

    c语言 函数的参数传递示例 C ++附近的int()函数 (C++ nearbyint() function) nearbyint() function is a library function o ...

  3. c语言函数官网,c语言函数

    c语言函数[编辑] 概述 c语言函数包括多种函数.它的出现方便了人们对很多数据的计算,节省了大量的时间,方便了人们的计算,为人们提供了一个简便的,时用的函数运算.也在一定的领域中有一个突破性进步,但是 ...

  4. C语言函数大全-- q 开头的函数

    C语言函数大全 本篇介绍C语言函数大全-- q 开头的函数 1. qsort 1.1 函数说明 函数声明 函数功能 void qsort(void *base, size_t nmemb, size_ ...

  5. Matlab mod函数 对应C语言 函数

    Matlab mod函数 对应C语言 函数 2010-09-10 23:43 Matlab中的mod函数,不同于C语言中的"%",在C中没有专门对应的函数. Matlab的mod函 ...

  6. c语言函数返回值类型必须,C++中函数类型与定义的函数返回值类型必须相同么?...

    C++中函数类型与定义的函数返回值类型必须相同么?以下文字资料是由(历史新知网www.lishixinzhi.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧! C++中函数类型与定义 ...

  7. C语言函数大全-- n 开头的函数

    C语言函数大全 本篇介绍C语言函数大全-- n 开头的函数 1. nan 1.1 函数说明 函数声明 函数功能 double nan(const char *tagp); 用于返回一个表示 NaN(非 ...

  8. C语言字符串哪个头文件,如果在程序中要使用C语言的字符串处理函数,应在程序中包含这哪个头文件。...

    程程下列不是K均值聚类的缺点的是() 神--申--电,序中序中这三个字是从一个词分化出来的."神"写作"申",也就是"电",是让人恐怖,让人 ...

  9. c语言exit在哪个头文件_C语言函数执行成功时,返回1和返回0,究竟哪个好?

    基本上,没有人会将大段的C语言代码全部塞入 main() 函数,更好的做法是按照复用率高,耦合性低的原则,尽可能的将代码拆分不同的功能模块,并封装成函数.C语言代码的组合千变万化,因此函数的功能可能会 ...

最新文章

  1. 英国熊孩子频繁攻击学校网络,政府被逼爹味儿下场:当白帽黑客吧!年薪45万...
  2. My first App EncryptWheel is in WAITING FOR REVIEW status
  3. glib中的GList ,GSList,GString,GTree,GTimer
  4. [C#]关于override和new在重写方法时的区别
  5. EDM数据库营销是什么?-EDM数据库营销的概念
  6. python文件生成电脑exe文件
  7. day055056Django之多表操作,多表查询
  8. Java中Properties类的操作配置文件
  9. AceAdmin In MVC之控件
  10. JUCE框架教程(7)——AudioBuffer基础如何生成音频Output
  11. 常用的颜色色值(转)
  12. 亚利桑那州立大学计算机专业,亚利桑那州立大学计算机专业排名
  13. CSS Flexbox 青蛙游戏
  14. java使用jol打印对象信息
  15. 哈工大2021 形式语言与自动机期末
  16. 记录一个多表查询的问题 #1093 - You can‘t specify target table ‘xxx‘ for update in FROM clause
  17. vue3动态路由addRoute
  18. FFMPEG之 Ubuntu系统上配置MP3和AMR编解码工具
  19. java map增加值_Java程序以创建HashMap并添加键值对
  20. 多元函数第五:拓扑初步(1):开集,闭集,内部,边界,外部

热门文章

  1. 2021-06-20-刘铁猛C#语言入门详解-学习笔记P20、21、22事件详解
  2. C++程序员之路深入探讨QueryInterface
  3. 程序员的前途,我们真的要打一辈子工吗?
  4. 人工智能与深度神经网络,人工智能人工神经网络
  5. ssh 代理详细解释
  6. 软件测试--测试过程模型(V,W,H,X)
  7. CSS cursor: pointer; 设置鼠标移动上方变为手指图标
  8. JavaScript:添加与删除数组的方法详解
  9. Spring Boot框架搭建
  10. 最代码5月第3周回顾(5.12-5.18@2014)