有什么方法可以省略此 portscanner 代码中 int_to_string 转换函数的使用
Is there any way to omit the usage of int_to_string conversion function in this portscanner code
我试图检查主机的端口从 0 到 1024 是否打开。我使用 getaddrinfo(const char*node, const char *service, const struct addrinfo *hints, struct addrinfo **res)
将端口号从 0 循环到 1024(即第二个参数 const char *service),方法是将 int
转换为字符串,然后传递给 getaddrinfo()
。有什么办法可以避免使用这个转换函数吗?
char *convert_int_to_string(int num)
{
int tmp;
int i = 0;
int j = 0;
static char a[5] = {'0'};
while (num > 0) {
tmp = num % 10;
a[i++] = tmp + '0';
num = num / 10;
}
a[i] = '[=11=]';
for (j = 0; j < i / 2; j++) {
tmp = a[j];
a[j] = a[i - j - 1];
a[i - j - 1] = tmp;
}
return a;
}
int main(int argc, char **argv)
{
int status;
char *node;
char *port_no;
int sock_fd;
int i = 0;
struct addrinfo hints, *serviceinfo;
if (argc != 2)
error(1, errno, "Too many or few arguments\n");
node = argv[1];
memset(&hints, 0, sizeof hints);
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
for (i = 0; i < 1024; i++) {
port_no = convert_int_to_string(i);
status = getaddrinfo(node, port_no, &hints, &serviceinfo);
if (status != 0) {
error(1, errno, "error in getaddrinfo() function call\n");
}
sock_fd = socket(serviceinfo->ai_family, serviceinfo->ai_socktype, serviceinfo->ai_protocol);
if (sock_fd == -1)
error(1, errno, "error in socket() function call\n");
status = connect(sock_fd, serviceinfo->ai_addr, serviceinfo->ai_addrlen);
if (status != -1)
printf("Port : %s is open\n", port_no);
else
printf("Port : %s is closed\n", port_no);
}
}
Is there any way to omit the usage of int_to_string
conversion function in this portscanner code (?)
是用snprintf()
组成port_no
。
for (i = 0; i < 1024; i++) {
// port_no = convert_int_to_string(i);
char port_no[40];
snprintf(port_no, sizeof port_no, "%d", i);
...
我试图检查主机的端口从 0 到 1024 是否打开。我使用 getaddrinfo(const char*node, const char *service, const struct addrinfo *hints, struct addrinfo **res)
将端口号从 0 循环到 1024(即第二个参数 const char *service),方法是将 int
转换为字符串,然后传递给 getaddrinfo()
。有什么办法可以避免使用这个转换函数吗?
char *convert_int_to_string(int num)
{
int tmp;
int i = 0;
int j = 0;
static char a[5] = {'0'};
while (num > 0) {
tmp = num % 10;
a[i++] = tmp + '0';
num = num / 10;
}
a[i] = '[=11=]';
for (j = 0; j < i / 2; j++) {
tmp = a[j];
a[j] = a[i - j - 1];
a[i - j - 1] = tmp;
}
return a;
}
int main(int argc, char **argv)
{
int status;
char *node;
char *port_no;
int sock_fd;
int i = 0;
struct addrinfo hints, *serviceinfo;
if (argc != 2)
error(1, errno, "Too many or few arguments\n");
node = argv[1];
memset(&hints, 0, sizeof hints);
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
for (i = 0; i < 1024; i++) {
port_no = convert_int_to_string(i);
status = getaddrinfo(node, port_no, &hints, &serviceinfo);
if (status != 0) {
error(1, errno, "error in getaddrinfo() function call\n");
}
sock_fd = socket(serviceinfo->ai_family, serviceinfo->ai_socktype, serviceinfo->ai_protocol);
if (sock_fd == -1)
error(1, errno, "error in socket() function call\n");
status = connect(sock_fd, serviceinfo->ai_addr, serviceinfo->ai_addrlen);
if (status != -1)
printf("Port : %s is open\n", port_no);
else
printf("Port : %s is closed\n", port_no);
}
}
Is there any way to omit the usage of
int_to_string
conversion function in this portscanner code (?)
是用snprintf()
组成port_no
。
for (i = 0; i < 1024; i++) {
// port_no = convert_int_to_string(i);
char port_no[40];
snprintf(port_no, sizeof port_no, "%d", i);
...