{"id":19175,"date":"2022-11-05T18:09:32","date_gmt":"2022-11-05T12:39:32","guid":{"rendered":"https:\/\/jassweb.com\/solved\/solved-ipv6-compatibility-issues-with-low-level-c-apis-closed\/"},"modified":"2022-11-05T18:09:32","modified_gmt":"2022-11-05T12:39:32","slug":"solved-ipv6-compatibility-issues-with-low-level-c-apis-closed","status":"publish","type":"post","link":"https:\/\/jassweb.com\/solved\/solved-ipv6-compatibility-issues-with-low-level-c-apis-closed\/","title":{"rendered":"[Solved] IPv6 Compatibility Issues with Low-Level C API&#8217;s [closed]"},"content":{"rendered":"<p> [ad_1]<br \/>\n<\/p>\n<div id=\"answer-46270106\" class=\"answer js-answer accepted-answer js-accepted-answer\" data-answerid=\"46270106\" data-parentid=\"46267907\" data-score=\"1\" data-position-on-page=\"1\" data-highest-scored=\"1\" data-question-has-accepted-highest-score=\"1\" itemprop=\"acceptedAnswer\" itemscope itemtype=\"https:\/\/schema.org\/Answer\">\n<div class=\"post-layout\">\n<div class=\"votecell post-layout--left\"><\/div>\n<div class=\"answercell post-layout--right\">\n<div class=\"s-prose js-post-body\" itemprop=\"text\">\n<p>You are NOT supposed to use <code>AF_UNSPEC<\/code> with <code>socket()<\/code> at all. You MUST use either <code>AF_INET<\/code> (IPv4) or <code>AF_INET6<\/code> (IPv6).<\/p>\n<p>You can use <code>AF_UNSPEC<\/code> with the <code>hints<\/code> input parameter of <code>getaddrinfo()<\/code> to indicate that you are willing to accept both IPv4 and IPv6 addresses as output. The actual addresses in the output will be either <code>AF_INET<\/code> or <code>AF_INET6<\/code>.  Or, you can set the hints to <code>AF_INET<\/code> for IPv4-only output. Or <code>AF_INET6<\/code> for IPv6-only output. <\/p>\n<p>You are supposed to loop through the list that is returned by <code>getaddrinfo()<\/code>. For each address in the list:<\/p>\n<ul>\n<li>pass its <code>ai_family<\/code>,  <code>ai_socktype<\/code>, and <code>ai_protocol<\/code> fields to <code>socket()<\/code><\/li>\n<li>then pass its <code>ai_addr<\/code> and <code>ai_addrlen<\/code> fields to <code>bind()<\/code> (servers) or <code>connect()<\/code> (clients).<\/li>\n<\/ul>\n<p>Repeat this for all addresses reported for a listening server, and for all addresses reported for a client until one successfully connects.<\/p>\n<p>This way, each socket you create matches the IP version of the address it is working with, and you are passing an appropriate matching <code>sockaddr_in<\/code> (IPv4) or <code>sockaddr_in6<\/code> (IPv6) to <code>bind()<\/code>\/<code>connect()<\/code>.<\/p>\n<p>Once you have successful listening server socket(s), or a successfully connected client socket, if you need to retrieve an IP from a socket using <code>accept()<\/code>, <code>getsockname()<\/code> or <code>getpeername()<\/code>, be sure to pass it a <code>sockaddr_storage<\/code> struct to fill in. <code>sockaddr_storage<\/code> is large enough to hold all defined <code>sockaddr<\/code>-based structures (and there are many).  If successful, you can then type-cast it to a <code>sockaddr_in<\/code> or <code>sockaddr_in6<\/code> based on its <code>sa_family<\/code> field (<code>AF_INET<\/code> or <code>AF_INET6<\/code>, respectively). Same goes for other similar functions, like <code>inet_pton()<\/code> and <code>inet_ntop()<\/code>.  <\/p>\n<hr>\n<p><strong>Update<\/strong>: given the client code you have shown, try this instead:<\/p>\n<pre><code>struct addrinfo hints = {0};\nhints.ai_family   = AF_INET;\nhints.ai_socktype = SOCK_STREAM;\nhints.ai_protocol = IPPROTO_TCP;\n\nNSString* portString = [NSString stringWithFormat:@\"%d\", _port];\n\nstruct addrinfo *ai;\nint sockfd = -1;\n\nint status = getaddrinfo([_hostname UTF8String], [portString cStringUsingEncoding:kCFStringEncodingASCII] , &amp;hints, &amp;ai);\nif (status == 0) {\n    sockfd = socket(ai-&gt;ai_family, ai-&gt;ai_socktype, ai-&gt;ai_protocol);\n    if (sockfd == -1) {\n        status = errno;\n    }\n    else if (connect(sockfd, ai-&gt;ai_addr, ai-&gt;ai_addrlen) &lt; 0) {\n        status = errno;\n        close(sockfd);\n        sockfd = -1;\n    }\n    freeaddrinfo(ai);\n}\n\nif (sockfd == -1) {\n    \/\/ handle status error as needed...\n}\nelse {\n    \/\/ use sockfd as needed...\n    close(sockfd);\n}\n<\/code><\/pre>\n<\/p><\/div>\n<div class=\"mt24\"><\/div>\n<\/div>\n<p>            <span class=\"d-none\" itemprop=\"commentCount\">8<\/span> <\/p><\/div>\n<\/div>\n<p>[ad_2]<\/p>\n<p>solved IPv6 Compatibility Issues with Low-Level C API&#8217;s [closed] <\/p>\n","protected":false},"excerpt":{"rendered":"<p>[ad_1] You are NOT supposed to use AF_UNSPEC with socket() at all. You MUST use either AF_INET (IPv4) or AF_INET6 (IPv6). You can use AF_UNSPEC with the hints input parameter of getaddrinfo() to indicate that you are willing to accept both IPv4 and IPv6 addresses as output. The actual addresses in the output will be &#8230; <a title=\"[Solved] IPv6 Compatibility Issues with Low-Level C API&#8217;s [closed]\" class=\"read-more\" href=\"https:\/\/jassweb.com\/solved\/solved-ipv6-compatibility-issues-with-low-level-c-apis-closed\/\" aria-label=\"More on [Solved] IPv6 Compatibility Issues with Low-Level C API&#8217;s [closed]\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[320],"tags":[324,470,1074],"class_list":["post-19175","post","type-post","status-publish","format-standard","hentry","category-solved","tag-c","tag-objective-c","tag-sockets"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>[Solved] IPv6 Compatibility Issues with Low-Level C API&#039;s [closed] - JassWeb<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/jassweb.com\/solved\/solved-ipv6-compatibility-issues-with-low-level-c-apis-closed\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"[Solved] IPv6 Compatibility Issues with Low-Level C API&#039;s [closed] - JassWeb\" \/>\n<meta property=\"og:description\" content=\"[ad_1] You are NOT supposed to use AF_UNSPEC with socket() at all. You MUST use either AF_INET (IPv4) or AF_INET6 (IPv6). You can use AF_UNSPEC with the hints input parameter of getaddrinfo() to indicate that you are willing to accept both IPv4 and IPv6 addresses as output. The actual addresses in the output will be ... Read more\" \/>\n<meta property=\"og:url\" content=\"https:\/\/jassweb.com\/solved\/solved-ipv6-compatibility-issues-with-low-level-c-apis-closed\/\" \/>\n<meta property=\"og:site_name\" content=\"JassWeb\" \/>\n<meta property=\"article:published_time\" content=\"2022-11-05T12:39:32+00:00\" \/>\n<meta name=\"author\" content=\"Kirat\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Kirat\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-ipv6-compatibility-issues-with-low-level-c-apis-closed\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-ipv6-compatibility-issues-with-low-level-c-apis-closed\/\"},\"author\":{\"name\":\"Kirat\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\"},\"headline\":\"[Solved] IPv6 Compatibility Issues with Low-Level C API&#8217;s [closed]\",\"datePublished\":\"2022-11-05T12:39:32+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-ipv6-compatibility-issues-with-low-level-c-apis-closed\/\"},\"wordCount\":253,\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"keywords\":[\"c++\",\"objective-c\",\"sockets\"],\"articleSection\":[\"Solved\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-ipv6-compatibility-issues-with-low-level-c-apis-closed\/\",\"url\":\"https:\/\/jassweb.com\/solved\/solved-ipv6-compatibility-issues-with-low-level-c-apis-closed\/\",\"name\":\"[Solved] IPv6 Compatibility Issues with Low-Level C API's [closed] - JassWeb\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#website\"},\"datePublished\":\"2022-11-05T12:39:32+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-ipv6-compatibility-issues-with-low-level-c-apis-closed\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/jassweb.com\/solved\/solved-ipv6-compatibility-issues-with-low-level-c-apis-closed\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-ipv6-compatibility-issues-with-low-level-c-apis-closed\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/jassweb.com\/solved\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"[Solved] IPv6 Compatibility Issues with Low-Level C API&#8217;s [closed]\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/jassweb.com\/solved\/#website\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"name\":\"JassWeb\",\"description\":\"Build High-quality Websites\",\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/jassweb.com\/solved\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\",\"name\":\"Jass Web\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"contentUrl\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"width\":693,\"height\":132,\"caption\":\"Jass Web\"},\"image\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\",\"name\":\"Kirat\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1776403586\",\"contentUrl\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1776403586\",\"caption\":\"Kirat\"},\"sameAs\":[\"http:\/\/jassweb.com\"],\"url\":\"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"[Solved] IPv6 Compatibility Issues with Low-Level C API's [closed] - JassWeb","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/jassweb.com\/solved\/solved-ipv6-compatibility-issues-with-low-level-c-apis-closed\/","og_locale":"en_US","og_type":"article","og_title":"[Solved] IPv6 Compatibility Issues with Low-Level C API's [closed] - JassWeb","og_description":"[ad_1] You are NOT supposed to use AF_UNSPEC with socket() at all. You MUST use either AF_INET (IPv4) or AF_INET6 (IPv6). You can use AF_UNSPEC with the hints input parameter of getaddrinfo() to indicate that you are willing to accept both IPv4 and IPv6 addresses as output. The actual addresses in the output will be ... Read more","og_url":"https:\/\/jassweb.com\/solved\/solved-ipv6-compatibility-issues-with-low-level-c-apis-closed\/","og_site_name":"JassWeb","article_published_time":"2022-11-05T12:39:32+00:00","author":"Kirat","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Kirat","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/jassweb.com\/solved\/solved-ipv6-compatibility-issues-with-low-level-c-apis-closed\/#article","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/solved-ipv6-compatibility-issues-with-low-level-c-apis-closed\/"},"author":{"name":"Kirat","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31"},"headline":"[Solved] IPv6 Compatibility Issues with Low-Level C API&#8217;s [closed]","datePublished":"2022-11-05T12:39:32+00:00","mainEntityOfPage":{"@id":"https:\/\/jassweb.com\/solved\/solved-ipv6-compatibility-issues-with-low-level-c-apis-closed\/"},"wordCount":253,"publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"keywords":["c++","objective-c","sockets"],"articleSection":["Solved"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/jassweb.com\/solved\/solved-ipv6-compatibility-issues-with-low-level-c-apis-closed\/","url":"https:\/\/jassweb.com\/solved\/solved-ipv6-compatibility-issues-with-low-level-c-apis-closed\/","name":"[Solved] IPv6 Compatibility Issues with Low-Level C API's [closed] - JassWeb","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/#website"},"datePublished":"2022-11-05T12:39:32+00:00","breadcrumb":{"@id":"https:\/\/jassweb.com\/solved\/solved-ipv6-compatibility-issues-with-low-level-c-apis-closed\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/jassweb.com\/solved\/solved-ipv6-compatibility-issues-with-low-level-c-apis-closed\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/jassweb.com\/solved\/solved-ipv6-compatibility-issues-with-low-level-c-apis-closed\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/jassweb.com\/solved\/"},{"@type":"ListItem","position":2,"name":"[Solved] IPv6 Compatibility Issues with Low-Level C API&#8217;s [closed]"}]},{"@type":"WebSite","@id":"https:\/\/jassweb.com\/solved\/#website","url":"https:\/\/jassweb.com\/solved\/","name":"JassWeb","description":"Build High-quality Websites","publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/jassweb.com\/solved\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/jassweb.com\/solved\/#organization","name":"Jass Web","url":"https:\/\/jassweb.com\/solved\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/","url":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","contentUrl":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","width":693,"height":132,"caption":"Jass Web"},"image":{"@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31","name":"Kirat","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/","url":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1776403586","contentUrl":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1776403586","caption":"Kirat"},"sameAs":["http:\/\/jassweb.com"],"url":"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/"}]}},"_links":{"self":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/19175","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/comments?post=19175"}],"version-history":[{"count":0,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/19175\/revisions"}],"wp:attachment":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/media?parent=19175"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/categories?post=19175"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/tags?post=19175"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}