Author: ArcRiley Date: 2009-01-03 16:50:34 -0500 (Sat, 03 Jan 2009) New Revision: 1412
Modified: trunk/concordance/src/Core.c Log: More network code cleanup, no longer leaks resinfo on bind/listen failure Modified: trunk/concordance/src/Core.c =================================================================== --- trunk/concordance/src/Core.c 2009-01-03 21:21:10 UTC (rev 1411) +++ trunk/concordance/src/Core.c 2009-01-03 21:50:34 UTC (rev 1412) @@ -57,28 +57,29 @@ self->s2s.addr = "0.0.0.0"; self->s2s.port = 5269; - /* parse and check arguments */ + /* parse arguments and keywords */ if (!PyArg_ParseTupleAndKeywords(args, kwds, "|(sH)(sH):Core", kwlist, &self->c2s.addr, &self->c2s.port, &self->s2s.addr, &self->s2s.port)) return -1; - if (self->c2s.port<0 || self->c2s.port>65535) { - PyErr_SetString(PyExc_ValueError, "client port out of range (0-65535)"); - return -1; - } - - if (self->s2s.port<0 || self->s2s.port>65535) { - PyErr_SetString(PyExc_ValueError, "server port out of range (0-65535)"); - return -1; - } - + /* open client-to-server listening socket (self->c2s->sock) */ if (!opensocket(self->c2s)) return -1; + /* open server-to-server listening socket (self->s2s->sock) */ if (!opensocket(self->s2s)) return -1; +/* +#ifdef MS_WINDOWS + sock.chan = g_io_channel_win32_new_socket(sock.sock); +#else + sock.chan = g_io_channel_unix_new(sock.sock); +#endif + g_io_add_watch(sock.chan, G_IO_IN, callback, NULL); +*/ + return 0; } @@ -98,10 +99,15 @@ int ret; struct addrinfo* resinfo = NULL; + /* Get address family and struct + + int getaddrinfo(const char *node, const char *service, + const struct addrinfo *hints, + struct addrinfo **res); + getaddrinfo() returns 0 if it succeeds, or non-zero on failure. + */ ret = getaddrinfo(sock.addr, NULL, NULL, &resinfo); - - /* Handle address lookup errors */ - if (ret<0) { + if (ret != 0) { switch (ret) { case EAI_AGAIN : /* EAI_AGAIN @@ -131,43 +137,52 @@ return 0; } - /* int socket(int domain, int type, int protocol); */ + /* Copy port into sockaddr struct + + uint16_t htons(uint16_t hostshort); + The htons() function converts the unsigned short integer hostshort + from host byte order to network byte order. + */ + ((struct sockaddr_in*) resinfo->ai_addr)->sin_port = htons(sock.port); + + /* Open new socket of the appropriate family (AF_INET or AF_INET6) + + int socket(int domain, int type, int protocol); + */ sock.sock = socket(resinfo->ai_family, SOCK_STREAM, 0); - if (sock.sock == -1) { - if (resinfo) - /* prevent leaking resinfo on err, see freeaddrinfo below */ - freeaddrinfo(resinfo); - PyErr_SetString(PyExc_OSError, "unable to create listening socket"); - return 0; - } + /* If successful continue to bind/listen, else set error message */ + if (sock.sock > -1) { + /* Bind and Listen to opened socket - /* The htons() function converts the unsigned short integer hostshort from - host byte order to network byte order. */ - ((struct sockaddr_in*) resinfo->ai_addr)->sin_port = htons(sock.port); + int bind(int sockfd, const struct sockaddr *addr, + socklen_t addrlen); + On success, zero is returned. On error, -1 is returned. - /* int bind(int sockfd, const struct sockaddr *addr, - socklen_t addrlen); - On success, zero is returned. On error, -1 is returned. */ - if (bind(sock.sock, resinfo->ai_addr, resinfo->ai_addrlen) == -1 || - listen(sock.sock, 16) == -1 ) { + int listen(int sockfd, int backlog); + On success, zero is returned. On error, -1 is returned. + */ + if (bind(sock.sock, resinfo->ai_addr, resinfo->ai_addrlen) == 0 && + listen(sock.sock, 16) == 0 ) + ret = 1; + else { + PyErr_SetString(PyExc_OSError, "unable to bind listening socket"); + ret = 0; + } + } + else { PyErr_SetString(PyExc_OSError, "unable to bind listening socket"); - return 0; + ret = 0; } + /* before returning, free the previously created resinfo struct + + void freeaddrinfo(struct addrinfo *res); + */ if (resinfo) - /* The freeaddrinfo() function frees the memory that was allocated for - the dynamically allocated linked list res. */ freeaddrinfo(resinfo); -/* -#ifdef MS_WINDOWS - sock.chan = g_io_channel_win32_new_socket(sock.sock); -#else - sock.chan = g_io_channel_unix_new(sock.sock); -#endif - g_io_add_watch(sock.chan, G_IO_IN, callback, NULL); -*/ - return 1; + + return ret; } _______________________________________________ PySoy-SVN mailing list PySoy-SVN@pysoy.org http://www.pysoy.org/mailman/listinfo/pysoy-svn