Fix mg_socketpair for lwip + UDP

CL: Fix mg_socketpair for lwip + UDP

PUBLISHED_FROM=6dd09136b3837016026523fb34acb5cc893cadbe
This commit is contained in:
Бобби 2018-02-18 16:34:48 +00:00 committed by Cesanta Bot
parent ad8f1d88d4
commit 780077d118
4 changed files with 18 additions and 14 deletions

View File

@ -3910,7 +3910,7 @@ mg_socketpair_accept(sock_t sock, union socket_address *sa, socklen_t sa_len) {
} }
int mg_socketpair(sock_t sp[2], int sock_type) { int mg_socketpair(sock_t sp[2], int sock_type) {
union socket_address sa; union socket_address sa, sa2;
sock_t sock; sock_t sock;
socklen_t len = sizeof(sa.sin); socklen_t len = sizeof(sa.sin);
int ret = 0; int ret = 0;
@ -3919,18 +3919,20 @@ int mg_socketpair(sock_t sp[2], int sock_type) {
(void) memset(&sa, 0, sizeof(sa)); (void) memset(&sa, 0, sizeof(sa));
sa.sin.sin_family = AF_INET; sa.sin.sin_family = AF_INET;
sa.sin.sin_port = htons(0);
sa.sin.sin_addr.s_addr = htonl(0x7f000001); /* 127.0.0.1 */ sa.sin.sin_addr.s_addr = htonl(0x7f000001); /* 127.0.0.1 */
sa2 = sa;
if ((sock = socket(AF_INET, sock_type, 0)) == INVALID_SOCKET) { if ((sock = socket(AF_INET, sock_type, 0)) == INVALID_SOCKET) {
} else if (bind(sock, &sa.sa, len) != 0) { } else if (bind(sock, &sa.sa, len) != 0) {
} else if (sock_type == SOCK_STREAM && listen(sock, 1) != 0) { } else if (sock_type == SOCK_STREAM && listen(sock, 1) != 0) {
} else if (getsockname(sock, &sa.sa, &len) != 0) { } else if (getsockname(sock, &sa.sa, &len) != 0) {
} else if ((sp[0] = socket(AF_INET, sock_type, 0)) == INVALID_SOCKET) { } else if ((sp[0] = socket(AF_INET, sock_type, 0)) == INVALID_SOCKET) {
} else if (connect(sp[0], &sa.sa, len) != 0) { } else if (sock_type == SOCK_STREAM && connect(sp[0], &sa.sa, len) != 0) {
} else if (sock_type == SOCK_DGRAM && } else if (sock_type == SOCK_DGRAM &&
(getsockname(sp[0], &sa.sa, &len) != 0 || (bind(sp[0], &sa2.sa, len) != 0 ||
connect(sock, &sa.sa, len) != 0)) { getsockname(sp[0], &sa2.sa, &len) != 0 ||
connect(sp[0], &sa.sa, len) != 0 ||
connect(sock, &sa2.sa, len) != 0)) {
} else if ((sp[1] = (sock_type == SOCK_DGRAM ? sock : mg_socketpair_accept( } else if ((sp[1] = (sock_type == SOCK_DGRAM ? sock : mg_socketpair_accept(
sock, &sa, len))) == sock, &sa, len))) ==
INVALID_SOCKET) { INVALID_SOCKET) {

View File

@ -4680,9 +4680,9 @@ int mg_http_parse_header2(struct mg_str *hdr, const char *var_name, char **buf,
int mg_http_parse_header(struct mg_str *hdr, const char *var_name, char *buf, int mg_http_parse_header(struct mg_str *hdr, const char *var_name, char *buf,
size_t buf_size) size_t buf_size)
#ifdef __GNUC__ #ifdef __GNUC__
__attribute__((deprecated)); __attribute__((deprecated))
#endif #endif
; ;
/* /*
* Gets and parses the Authorization: Basic header * Gets and parses the Authorization: Basic header

View File

@ -64,9 +64,9 @@ int mg_http_parse_header2(struct mg_str *hdr, const char *var_name, char **buf,
int mg_http_parse_header(struct mg_str *hdr, const char *var_name, char *buf, int mg_http_parse_header(struct mg_str *hdr, const char *var_name, char *buf,
size_t buf_size) size_t buf_size)
#ifdef __GNUC__ #ifdef __GNUC__
__attribute__((deprecated)); __attribute__((deprecated))
#endif #endif
; ;
/* /*
* Gets and parses the Authorization: Basic header * Gets and parses the Authorization: Basic header

View File

@ -652,7 +652,7 @@ mg_socketpair_accept(sock_t sock, union socket_address *sa, socklen_t sa_len) {
} }
int mg_socketpair(sock_t sp[2], int sock_type) { int mg_socketpair(sock_t sp[2], int sock_type) {
union socket_address sa; union socket_address sa, sa2;
sock_t sock; sock_t sock;
socklen_t len = sizeof(sa.sin); socklen_t len = sizeof(sa.sin);
int ret = 0; int ret = 0;
@ -661,18 +661,20 @@ int mg_socketpair(sock_t sp[2], int sock_type) {
(void) memset(&sa, 0, sizeof(sa)); (void) memset(&sa, 0, sizeof(sa));
sa.sin.sin_family = AF_INET; sa.sin.sin_family = AF_INET;
sa.sin.sin_port = htons(0);
sa.sin.sin_addr.s_addr = htonl(0x7f000001); /* 127.0.0.1 */ sa.sin.sin_addr.s_addr = htonl(0x7f000001); /* 127.0.0.1 */
sa2 = sa;
if ((sock = socket(AF_INET, sock_type, 0)) == INVALID_SOCKET) { if ((sock = socket(AF_INET, sock_type, 0)) == INVALID_SOCKET) {
} else if (bind(sock, &sa.sa, len) != 0) { } else if (bind(sock, &sa.sa, len) != 0) {
} else if (sock_type == SOCK_STREAM && listen(sock, 1) != 0) { } else if (sock_type == SOCK_STREAM && listen(sock, 1) != 0) {
} else if (getsockname(sock, &sa.sa, &len) != 0) { } else if (getsockname(sock, &sa.sa, &len) != 0) {
} else if ((sp[0] = socket(AF_INET, sock_type, 0)) == INVALID_SOCKET) { } else if ((sp[0] = socket(AF_INET, sock_type, 0)) == INVALID_SOCKET) {
} else if (connect(sp[0], &sa.sa, len) != 0) { } else if (sock_type == SOCK_STREAM && connect(sp[0], &sa.sa, len) != 0) {
} else if (sock_type == SOCK_DGRAM && } else if (sock_type == SOCK_DGRAM &&
(getsockname(sp[0], &sa.sa, &len) != 0 || (bind(sp[0], &sa2.sa, len) != 0 ||
connect(sock, &sa.sa, len) != 0)) { getsockname(sp[0], &sa2.sa, &len) != 0 ||
connect(sp[0], &sa.sa, len) != 0 ||
connect(sock, &sa2.sa, len) != 0)) {
} else if ((sp[1] = (sock_type == SOCK_DGRAM ? sock : mg_socketpair_accept( } else if ((sp[1] = (sock_type == SOCK_DGRAM ? sock : mg_socketpair_accept(
sock, &sa, len))) == sock, &sa, len))) ==
INVALID_SOCKET) { INVALID_SOCKET) {