我使用以下代码连接到localhost上运行的tor服务器。由于某些原因,此代码解析了ip并且仅提供IP地址。所以我无法连接到隐藏的服务,因为tor需要DNS名称进行连接。对于连接常规IP,所有工作都按预期工作,仅针对隐藏服务存在问题。
我做错了什么?有什么我想念的吗?我正在使用VisualStudio 2013。
非常感谢任何帮助!!
int fd = socket(AF_INET, SOCK_STREAM, 0);
if (fd < 0)
return 1;
struct sockaddr_in destaddr;
destaddr.sin_addr.s_host = (unsigned char) "6esfckh37teglfi5.onion";
int dest_port = 19452;
struct sockaddr_in saddr;
saddr.sin_port = htons(9150);
saddr.sin_family = AF_INET;
saddr.sin_addr.s_addr = inet_addr("127.0.0.1");
int rv = connect(fd, (struct sockaddr *)&saddr, sizeof(saddr));
if (rv < SOCKET_ERROR)
return 1;
char buf[256], *ptr;
ptr = buf;
PUT_BYTE(ptr++, 5);
PUT_BYTE(ptr++, 1);
PUT_BYTE(ptr++, 0x00);
send(fd, buf, ptr - buf, 0);
recv(fd, buf, 2, 0);
if ((buf[0] != 5) || buf[1] == 0xFF)
{
exit(0);
}
ptr = buf;
PUT_BYTE(ptr++, 5);
PUT_BYTE(ptr++, 1);
PUT_BYTE(ptr++, 0);
PUT_BYTE(ptr++, 1);
memcpy(ptr, &destaddr.sin_addr.s_addr, sizeof(destaddr.sin_addr));
ptr += sizeof(destaddr.sin_addr);
PUT_BYTE(ptr++, dest_port >> 8);
PUT_BYTE(ptr++, dest_port & 0xFF);
send(fd, buf, ptr - buf, 0);
recv(fd, buf, 4, 0);
if (!buf[1] != 0x00)
{
ptr = buf + 4;
switch (buf[3]) {
case 1:
recv(fd, ptr, 4 + 2, 0);
break;
case 3:
recv(fd, ptr, 1, 0);
recv(fd, ptr + 1, *(unsigned char*)ptr + 2, 0);
break;
case 4:
recv(fd, ptr, 16 + 2, 0);
break;
}
}
编辑:
我更正了我的代码并将addr位更改为3,仍然没有成功:
int dest_port = 19452;
struct sockaddr_in saddr;
saddr.sin_port = htons(9150);
saddr.sin_family = AF_INET;
saddr.sin_addr.s_addr = inet_addr("127.0.0.1");
int rv = connect(fd, (struct sockaddr *)&saddr, sizeof(saddr));
if (rv < SOCKET_ERROR)
return 1;
char buf[256], *ptr;
ptr = buf;
PUT_BYTE(ptr++, 5);
PUT_BYTE(ptr++, 1);
PUT_BYTE(ptr++, 0x00);
send(fd, buf, ptr - buf, 0);
recv(fd, buf, 2, 0);
if ((buf[0] != 5) || buf[1] == 0xFF)
{
exit(0);
}
ptr = buf;
PUT_BYTE(ptr++, 5);
PUT_BYTE(ptr++, 1);
PUT_BYTE(ptr++, 0);
PUT_BYTE(ptr++, 3);
//memcpy(ptr, &destaddr.sin_addr.s_addr, sizeof(destaddr.sin_addr));
//ptr += sizeof(destaddr.sin_addr);
memcpy(ptr, "22", 1);
memcpy(ptr, "iszgnywrejvdg2nc.onion", sizeof("iszgnywrejvdg2nc.onion"));
PUT_BYTE(ptr++, dest_port >> 8);
PUT_BYTE(ptr++, dest_port & 0xFF);
send(fd, buf, ptr - buf, 0);
recv(fd, buf, 4, 0);
if (!buf[1] != 0x00)
{
ptr = buf + 4;
switch (buf[3]) {
case 1:
recv(fd, ptr, 4 + 2, 0);
break;
case 3:
recv(fd, ptr, 1, 0);
recv(fd, ptr + 1, *(unsigned char*)ptr + 2, 0);
break;
case 4:
recv(fd, ptr, 16 + 2, 0);
break;
}
}