servname.c

来自「一个网络流量分析的完整的程序」· C语言 代码 · 共 47 行

C
47
字号
/***servname.c	- lookup module for TCP and UDP service names based on		  port numbersCopyright (c) Gerard Paul Java 1998This software is open source; you can redistribute it and/or modifyit under the terms of the GNU General Public License as published bythe Free Software Foundation; either version 2 of the License, or(at your option) any later version.This program is distributed WITHOUT ANY WARRANTY; without even theimplied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the GNU General Public License in the included COPYING file fordetails.***/#include <netdb.h>#include <stdio.h>#include <string.h>#include <netinet/in.h>void servlook(int servnames, unsigned int port, unsigned int protocol,	      char *target, int maxlen){    static struct servent *sve;    bzero(target, maxlen + 1);    if (servnames) {	if (protocol == IPPROTO_TCP)	    sve = getservbyport(port, "tcp");	else	    sve = getservbyport(port, "udp");	if (sve != NULL) {	    strncpy(target, sve->s_name, maxlen);	} else {	    sprintf(target, "%u", ntohs(port));	}    } else {	sprintf(target, "%u", ntohs(port));    }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?