diff options
author | Alon Zakai <alonzakai@gmail.com> | 2013-01-26 15:28:51 -0800 |
---|---|---|
committer | Alon Zakai <alonzakai@gmail.com> | 2013-01-26 16:15:55 -0800 |
commit | 4840a62f3733b2f2a2c0fe88957e84d947710ab3 (patch) | |
tree | cc0475e4e07eb07cd0fdf73f539482563750bee6 | |
parent | f5fe8742041429a6504ee6dc535aeb3d8550dd89 (diff) |
fix sockaddr_storage and sockaddr_in
-rw-r--r-- | src/library.js | 5 | ||||
-rw-r--r-- | system/include/net/netinet/in.h | 4 | ||||
-rw-r--r-- | system/include/sys/socket.h | 5 | ||||
-rw-r--r-- | tests/799.cpp | 83 | ||||
-rwxr-xr-x | tests/runner.py | 8 |
5 files changed, 99 insertions, 6 deletions
diff --git a/src/library.js b/src/library.js index 647aa738..cb892251 100644 --- a/src/library.js +++ b/src/library.js @@ -6857,10 +6857,11 @@ LibraryManager.library = { nextFd: 1, fds: {}, sockaddr_in_layout: Runtime.generateStructInfo([ - ['i16', 'sin_family'], + ['i32', 'sin_family'], ['i16', 'sin_port'], ['i32', 'sin_addr'], - ['i64', 'sin_zero'], + ['i32', 'sin_zero'], + ['i16', 'sin_zero_b'], ]), msghdr_layout: Runtime.generateStructInfo([ ['*', 'msg_name'], diff --git a/system/include/net/netinet/in.h b/system/include/net/netinet/in.h index 2ac98dfe..e32bf3ee 100644 --- a/system/include/net/netinet/in.h +++ b/system/include/net/netinet/in.h @@ -25,10 +25,10 @@ struct in_addr { }; struct sockaddr_in { - short sin_family; + int sin_family; unsigned short sin_port; struct in_addr sin_addr; - char sin_zero[8]; + char sin_zero[6]; }; struct in6_addr { diff --git a/system/include/sys/socket.h b/system/include/sys/socket.h index 10ba5ce8..e9b6c770 100644 --- a/system/include/sys/socket.h +++ b/system/include/sys/socket.h @@ -47,7 +47,10 @@ struct sockaddr { }; struct sockaddr_storage { - sa_family_t ss_family; + sa_family_t ss_family; + unsigned short ss_port; + unsigned long ss_addr; + char ss_zero[6]; }; ssize_t recvfrom(int socket, void *buffer, size_t length, int flags, struct sockaddr *address, socklen_t *address_len); diff --git a/tests/799.cpp b/tests/799.cpp new file mode 100644 index 00000000..56032133 --- /dev/null +++ b/tests/799.cpp @@ -0,0 +1,83 @@ +#include <netdb.h> +#include <stdint.h> +#include <string.h> +#include <assert.h> +#include <stdio.h> +#include <sys/socket.h> +#include <netinet/in.h> + +#ifdef EMSCRIPTEN +#include <net/arpa/inet.h> +#endif + +#define uint16 uint16_t + +class NetworkAddress { +private: + sockaddr_storage address; ///< The resolved address +public: + + /** + * Create a network address based on a unresolved host and port + * @param hostname the unresolved hostname + * @param port the port + * @param family the address family + */ + NetworkAddress(const char *hostname = "", uint16 port = 0, int family = AF_UNSPEC) + { + memset(&this->address, 0, sizeof(this->address)); + this->address.ss_family = family; + this->SetPort(port); + } + + uint16 GetPort() const; + void SetPort(uint16 port); +}; + +/** + * Get the port. + * @return the port. + */ +uint16 NetworkAddress::GetPort() const +{ + printf("Get PORT family: %d\n", this->address.ss_family); + switch (this->address.ss_family) { + case AF_UNSPEC: + case AF_INET: + return ntohs(((const struct sockaddr_in *)&this->address)->sin_port); + + case AF_INET6: + return ntohs(((const struct sockaddr_in6 *)&this->address)->sin6_port); + + default: + throw 0; + } +} + +/** + * Set the port. + * @param port set the port number. + */ +void NetworkAddress::SetPort(uint16 port) +{ + printf("Set PORT family: %d, port: %d\n", this->address.ss_family, port); + switch (this->address.ss_family) { + case AF_UNSPEC: + case AF_INET: + ((struct sockaddr_in*)&this->address)->sin_port = htons(port); + break; + + case AF_INET6: + ((struct sockaddr_in6*)&this->address)->sin6_port = htons(port); + break; + + default: + throw 0; + } +} + +int main() { + NetworkAddress na("127.0.0.1", 3979); + printf("PORT: %d\n", na.GetPort()); + return 0; +} diff --git a/tests/runner.py b/tests/runner.py index 1ed063b7..e14a9a2e 100755 --- a/tests/runner.py +++ b/tests/runner.py @@ -6077,13 +6077,19 @@ localhost : 1 : 4 * -84.29.3.0. ''') + def test_799(self): + src = open(path_from_root('tests', '799.cpp'), 'r').read() + self.do_run(src, '''Set PORT family: 100, port: 3979 +Get PORT family: 100 +PORT: 3979 +''') + def test_ctype(self): # The bit fiddling done by the macros using __ctype_b_loc requires this. Settings.CORRECT_SIGNS = 1 src = open(path_from_root('tests', 'ctype', 'src.c'), 'r').read() expected = open(path_from_root('tests', 'ctype', 'output.txt'), 'r').read() self.do_run(src, expected) - CORRECT_SIGNS = 0 def test_atomic(self): src = ''' |