Show
Ignore:
Timestamp:
10/14/08 23:42:18 (5 years ago)
Author:
tokuhirom
Message:

support AF_INET6

Location:
lang/cplusplus/llv8call/trunk
Files:
1 added
3 modified

Legend:

Unmodified
Added
Removed
  • lang/cplusplus/llv8call/trunk/TODO

    r21227 r21332  
    66-- base64.js 
    77-- etc. 
    8 - added unix domain socket support 
    98- add socket.* 
    109- org.coderepos.subprocess module 
  • lang/cplusplus/llv8call/trunk/ext/socket/socket.cc

    r21256 r21332  
    4141typedef union sock_addr { 
    4242    struct sockaddr_in in; 
    43 #ifdef AF_UNIX 
     43#if defined(AF_UNIX) 
    4444    struct sockaddr_un un; 
     45#endif 
     46#if defined(AF_INET6) 
     47    struct sockaddr_in6 in6; 
    4548#endif 
    4649} sock_addr_t; 
     
    8184            sockaddr_un * addr_un = (sockaddr_un*)&addr; 
    8285            ret->Set(Int32::New(0), String::New(addr_un->sun_path)); 
     86        } 
     87        break; 
     88#endif 
     89#if defined(AF_INET6) 
     90    case AF_INET6: 
     91        { 
     92            char buf[NI_MAXHOST]; 
     93            if (getnameinfo(&addr, sizeof(sockaddr_in6), buf, sizeof(buf), NULL, 0, NI_NUMERICHOST)) { 
     94                throw_stderr(__func__); 
     95                return ret; 
     96            } 
     97            sockaddr_in6 * addr_in6 = (sockaddr_in6*)&addr; 
     98            ret->Set(Int32::New(0), String::New(buf)); 
     99            ret->Set(Int32::New(1), Int32::New(ntohs(addr_in6->sin6_port))); 
    83100        } 
    84101        break; 
     
    104121 
    105122// TODO: support unix domain socket 
    106 // TODO: support inet6 
    107123static inline void _gen_sockaddr(Handle<Array> &args, int family, sock_addr_t* addr_ret, socklen_t * len) { 
    108124    switch (family) { 
     
    126142            addr->sun_path[path.length()] = '\0'; 
    127143            *len = path.length() + (sizeof(*addr) - sizeof(addr->sun_path)); 
     144        } 
     145        break; 
     146#endif 
     147#if defined(AF_INET6) 
     148    case AF_INET6: 
     149        { 
     150            if (args->Length() != 2) { 
     151                ThrowException(String::New("invalid args: AF_INET => (host, port)")); 
     152                return; 
     153            } 
     154            String::Utf8Value host(args->Get(Int32::New(0))); 
     155            int32_t port = args->Get(Int32::New(1))->Int32Value(); 
     156 
     157            struct sockaddr_in6 *addr = (struct sockaddr_in6*)addr_ret; 
     158            memset(addr, 0, sizeof(*addr)); // clear 
     159            addr->sin6_family = AF_INET6; 
     160            int pton_ret = inet_pton(AF_INET6, *host, addr->sin6_addr.s6_addr); 
     161            if (pton_ret == 0) { 
     162                ThrowException(String::New("invalid ip form")); 
     163                return; 
     164            } else if (pton_ret == -1) { 
     165                ThrowException(String::New("unknown protocol family")); 
     166                return; 
     167            } 
     168            addr->sin6_port = htons((short)port); 
     169            *len = sizeof(*addr); 
    128170        } 
    129171        break; 
     
    538580#endif 
    539581        BIND_CI("AF_INET",        AF_INET); 
     582#if defined(AF_INET6) 
    540583        BIND_CI("AF_INET6",       AF_INET6); 
     584#endif 
    541585 
    542586        // grep '"SOL_' Modules/socketmodule.c|perl -lne 'print "#if defined($1)\n        BIND_CI(\"$1\", $1)\n#endif" if /"(SOL_\w+)"/'  
  • lang/cplusplus/llv8call/trunk/lib/org/coderepos/test/tcp.js

    r21210 r21332  
    4141            throw "empty port not found"; 
    4242        }, 
    43         "waitPort" : function (port) { 
     43        "waitPort" : function (port, family) { 
     44            if (!family) { family = Socket.AF_INET } 
    4445            var _check_port = function (port) { 
    45                 var sock = new Socket(); 
     46                var sock = new Socket(family); 
    4647                try { 
    47                     sock.connect(["127.0.0.1", port]); 
     48                    sock.connect([family == Socket.AF_INET ? '127.0.0.1' : 'fe80::1%lo0', port]); 
    4849                    sock.close(); 
    4950                    return true; 
     
    6263        }, 
    6364        "testTCP" : function (args) { 
     65            var family = args['family'] ? args['family'] : Socket.AF_INET; 
    6466            var pid = posix.fork(); 
    6567            if (pid) { 
    6668                // parent 
    67                 Test.TCP.waitPort(args.port); 
     69                Test.TCP.waitPort(args.port, family); 
    6870                args.client(args.port); 
    6971            } else if (pid == 0) {