I have to get info about services using getservbyname() function, I've checked that _PATH_SERVICES refers to "/etc/services" for the services database file, but when I try to print returned infos port number is another one.
我必须使用getservbyname()函数获取有关服务的信息,我已检查_PATH_SERVICES是否为服务数据库文件引用“/ etc / services”,但是当我尝试打印返回的信息时,端口号是另一个。
It's my code:
这是我的代码:
#include <stdio.h>
#include <stdlib.h>
#include <netdb.h>
void foo(){
//netdb.h is in /usr/include
//CHECK: is not reading from /etc/services
FILE * f;
f = fopen("/etc/services", "r");
char buffer[50000];
int index = 0;
//just try
while(index < 1000){
buffer[index++] = fgetc(f);
}
printf("%s", buffer); //printed buffer corresponds to /etc/services contents
struct servent * serv;
serv = getservbyname("ftp", "tcp");
printf("Service name: %s\n", serv->s_name);
printf("Service port: %u\n", serv->s_port); //it would be 21, instead is 5376
printf("Service protocol: %s\n", serv->s_proto);
}
int main(void){
printf("Reading services from: %s\n", _PATH_SERVICES); //it returns "/etc/services"
foo();
return 0;
}
getservbyname() uses getservent(), and getservent() reads from _PATH_SERVICES path, is it possible that it is not reading from _PATH_SERVICES?
getservbyname()使用getservent(),并且getservent()从_PATH_SERVICES路径读取,是否可能没有从_PATH_SERVICES读取?
Thanks all!
1 个解决方案
#1
0
You need to convert network byte order to host byte order using ntohs(serv->s_port) function call.
您需要使用ntohs(serv-> s_port)函数调用将网络字节顺序转换为主机字节顺序。
printf("Service port: %u\n", ntohs(serv->s_port));
printf(“服务端口:%u \ n”,ntohs(serv-> s_port));
#1
0
You need to convert network byte order to host byte order using ntohs(serv->s_port) function call.
您需要使用ntohs(serv-> s_port)函数调用将网络字节顺序转换为主机字节顺序。
printf("Service port: %u\n", ntohs(serv->s_port));
printf(“服务端口:%u \ n”,ntohs(serv-> s_port));