socket = (GSocket *)malloc(sizeof(GSocket));
+ if (socket == NULL)
+ return NULL;
+
socket->m_fd = -1;
for (i=0;i<GSOCK_MAX_EVENT;i++) {
socket->m_fbacks[i] = NULL;
}
address = GAddress_new();
- _GAddress_translate_from(address, &addr, size);
+ if (address == NULL) {
+ socket->m_error = GSOCK_MEMERR;
+ return NULL;
+ }
+ if (!_GAddress_translate_from(address, &addr, size)) {
+ socket->m_error = GSOCK_MEMERR;
+ GAddress_destroy(address);
+ return NULL;
+ }
return address;
}
/*
* GSocket_SetTimeout()
*/
+
+#ifndef LINUX
+# define CAN_USE_TIMEOUT
+#elif defined(__GLIBC__) && defined(__GLIBC_MINOR__)
+# if (__GLIBC__ == 2) && (__GLIBC_MINOR__ == 1)
+# define CAN_USE_TIMEOUT
+# endif
+#endif
+
void GSocket_SetTimeout(GSocket *socket, unsigned long millisec)
{
assert(socket != NULL);
+ /* Neither GLIBC 2.0 nor the kernel 2.0.36 define SO_SNDTIMEO or
+ SO_RCVTIMEO. The man pages, that these flags should exist but
+ are read only. RR. */
+ /* OK, restrict this to GLIBC 2.1. GL. */
+#ifdef CAN_USE_TIMEOUT
socket->m_timeout = millisec;
if (socket->m_fd != -1) {
struct timeval tval;
setsockopt(socket->m_fd, SOL_SOCKET, SO_SNDTIMEO, &tval, sizeof(tval));
setsockopt(socket->m_fd, SOL_SOCKET, SO_RCVTIMEO, &tval, sizeof(tval));
}
+#endif
}
/*
if ((event & (1 << count)) != 0) {
socket->m_fbacks[count] = fallback;
socket->m_data[count] = cdata;
-
- _GSocket_Install_Callback(socket, count);
_GSocket_Enable(socket, count);
}
}
if ((event & (1 << count)) != 0) {
_GSocket_Disable(socket, count);
socket->m_fbacks[count] = NULL;
- _GSocket_Uninstall_Callback(socket, count);
}
}
}
}
/* Translate a system address into a GSocket address */
- if (!socket->m_peer)
+ if (!socket->m_peer) {
socket->m_peer = GAddress_new();
- _GAddress_translate_from(socket->m_peer, &from, fromlen);
+ if (!socket->m_peer) {
+ socket->m_error = GSOCK_MEMERR;
+ return -1;
+ }
+ }
+ if (!_GAddress_translate_from(socket->m_peer, &from, fromlen)) {
+ socket->m_error = GSOCK_MEMERR;
+ return -1;
+ }
return ret;
}
return -1;
}
- _GAddress_translate_to(socket->m_peer, &addr, &len);
+ if (!_GAddress_translate_to(socket->m_peer, &addr, &len)) {
+ socket->m_error = GSOCK_MEMERR;
+ return -1;
+ }
MASK_SIGNAL();
ret = sendto(socket->m_fd, buffer, size, 0, addr, len);
#define CHECK_ADDRESS(address, family, retval) \
{ \
if (address->m_family == GSOCK_NOFAMILY) \
- _GAddress_Init_##family(address); \
+ if (!_GAddress_Init_##family(address)) {\
+ address->m_error = GSOCK_MEMERR; \
+ return retval; \
+ }\
if (address->m_family != GSOCK_##family) {\
address->m_error = GSOCK_INVADDR; \
return retval; \
address = (GAddress *)malloc(sizeof(GAddress));
+ if (address == NULL)
+ return NULL;
+
address->m_family = GSOCK_NOFAMILY;
address->m_addr = NULL;
address->m_len = 0;
assert(address != NULL);
addr2 = (GAddress *)malloc(sizeof(GAddress));
+
+ if (addr2 == NULL)
+ return NULL;
+
memcpy(addr2, address, sizeof(GAddress));
if (address->m_addr) {
addr2->m_addr = (struct sockaddr *)malloc(addr2->m_len);
+ if (addr2->m_addr == NULL) {
+ free(addr2);
+ return NULL;
+ }
memcpy(addr2->m_addr, address->m_addr, addr2->m_len);
}
return address->m_family;
}
-void _GAddress_translate_from(GAddress *address, struct sockaddr *addr, int len){
+bool _GAddress_translate_from(GAddress *address, struct sockaddr *addr, int len){
address->m_realfamily = addr->sa_family;
switch (addr->sa_family) {
case AF_INET:
address->m_len = len;
address->m_addr = (struct sockaddr *)malloc(len);
+ if (address->m_addr == NULL)
+ return FALSE;
memcpy(address->m_addr, addr, len);
+
+ return TRUE;
}
-void _GAddress_translate_to(GAddress *address,
+bool _GAddress_translate_to(GAddress *address,
struct sockaddr **addr, int *len)
{
if (!address->m_addr) {
*len = address->m_len;
*addr = (struct sockaddr *)malloc(address->m_len);
+ if (*addr == NULL)
+ return FALSE;
+
memcpy(*addr, address->m_addr, address->m_len);
+ return TRUE;
}
/*
* -------------------------------------------------------------------------
*/
-void _GAddress_Init_INET(GAddress *address)
+bool _GAddress_Init_INET(GAddress *address)
{
+ address->m_addr = (struct sockaddr *)malloc(sizeof(struct sockaddr_in));
+ if (address->m_addr == NULL)
+ return FALSE;
+
address->m_len = sizeof(struct sockaddr_in);
- address->m_addr = (struct sockaddr *)malloc(address->m_len);
+
address->m_family = GSOCK_INET;
address->m_realfamily = PF_INET;
((struct sockaddr_in *)address->m_addr)->sin_family = AF_INET;
((struct sockaddr_in *)address->m_addr)->sin_addr.s_addr = INADDR_ANY;
+
+ return TRUE;
}
GSocketError GAddress_INET_SetHostName(GAddress *address, const char *hostname)
* -------------------------------------------------------------------------
*/
-void _GAddress_Init_UNIX(GAddress *address)
+bool _GAddress_Init_UNIX(GAddress *address)
{
- address->m_len = sizeof(struct sockaddr_un);
address->m_addr = (struct sockaddr *)malloc(address->m_len);
+ if (address->m_addr == NULL)
+ return FALSE;
+
+ address->m_len = sizeof(struct sockaddr_un);
address->m_family = GSOCK_UNIX;
address->m_realfamily = PF_UNIX;
((struct sockaddr_un *)address->m_addr)->sun_family = AF_UNIX;
((struct sockaddr_un *)address->m_addr)->sun_path[0] = 0;
+
+ return TRUE;
}
GSocketError GAddress_UNIX_SetPath(GAddress *address, const char *path)