티스토리 뷰
728x90
#include <iostream>
#include <string>
#ifdef _WIN32
#include <winsock2.h>
#include <ws2tcpip.h>
#pragma comment(lib, "Ws2_32.lib")
#else
#include <arpa/inet.h>
#include <cstring>
#endif
bool convertIPv4ToIPv6(const std::string& ipv4AddrStr, std::string& ipv6AddrStr) {
struct in_addr ipv4Addr;
struct in6_addr ipv6Addr;
// Convert the string IPv4 address to in_addr
if (inet_pton(AF_INET, ipv4AddrStr.c_str(), &ipv4Addr) != 1) {
std::cerr << "Invalid IPv4 address" << std::endl;
return false;
}
// Initialize the IPv6 address to zero
std::memset(&ipv6Addr, 0, sizeof(ipv6Addr));
// Set the IPv4-mapped IPv6 prefix
ipv6Addr.s6_addr[10] = 0xFF;
ipv6Addr.s6_addr[11] = 0xFF;
// Copy the IPv4 address into the last 4 bytes of the IPv6 address
std::memcpy(&ipv6Addr.s6_addr[12], &ipv4Addr, sizeof(ipv4Addr));
char ipv6AddrStrBuf[INET6_ADDRSTRLEN];
// Convert the in6_addr to a string
if (inet_ntop(AF_INET6, &ipv6Addr, ipv6AddrStrBuf, INET6_ADDRSTRLEN) == nullptr) {
std::cerr << "Failed to convert IPv6 address to string" << std::endl;
return false;
}
ipv6AddrStr = ipv6AddrStrBuf;
return true;
}
int main() {
#ifdef _WIN32
// Initialize Winsock
WSADATA wsaData;
if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) {
std::cerr << "WSAStartup failed" << std::endl;
return 1;
}
#endif
std::string ipv4AddrStr = "192.168.1.1";
std::string ipv6AddrStr;
if (convertIPv4ToIPv6(ipv4AddrStr, ipv6AddrStr)) {
std::cout << "IPv6-mapped IPv4 address: " << ipv6AddrStr << std::endl;
} else {
std::cerr << "Failed to convert IPv4 address to IPv6-mapped IPv4 address" << std::endl;
#ifdef _WIN32
WSACleanup();
#endif
return 1;
}
#ifdef _WIN32
WSACleanup();
#endif
return 0;
}
반응형
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- C++
- 현포다이브
- 네트워크 정보
- Windows
- effective
- C#
- 서귀포
- C
- CMake
- 제주도
- Linux
- 암호화
- 블루버블다이빙팀
- DLL
- script
- 성산블루버블
- 울릉도
- C# 고급 기술
- 블루버블
- 패턴
- 블루버블다이브팀
- PowerShell
- 서귀포블루버블
- OpenSource
- 스쿠버다이빙
- Effective c++
- 스쿠버 다이빙
- 외돌개
- Build
- C#.NET
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | |||||
3 | 4 | 5 | 6 | 7 | 8 | 9 |
10 | 11 | 12 | 13 | 14 | 15 | 16 |
17 | 18 | 19 | 20 | 21 | 22 | 23 |
24 | 25 | 26 | 27 | 28 | 29 | 30 |
글 보관함
250x250