- GError *error = NULL;
- static GStaticMutex getprotobyname_mutex = G_STATIC_MUTEX_INIT;
- gint fd, native_domain, native_type, native_protocol;
-
- switch (domain)
- {
- case G_SOCKET_DOMAIN_INET:
- native_domain = PF_INET;
- break;
-
- case G_SOCKET_DOMAIN_INET6:
- native_domain = PF_INET6;
- break;
-
- case G_SOCKET_DOMAIN_UNIX:
- native_domain = PF_UNIX;
- break;
-
- default:
- g_set_error (&error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED, "unsupported socket domain");
- return NULL;
- }
-
- switch (type)
- {
- case G_SOCKET_TYPE_STREAM:
- native_type = SOCK_STREAM;
- break;
-
- case G_SOCKET_TYPE_DATAGRAM:
- native_type = SOCK_DGRAM;
- break;
-
- case G_SOCKET_TYPE_SEQPACKET:
- native_type = SOCK_SEQPACKET;
- break;
-
- default:
- g_set_error (&error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED, "unsupported socket type");
- return NULL;
- }
-
- if (protocol == NULL)
- native_protocol = 0;
- else
- {
- struct protoent *ent;
- g_static_mutex_lock (&getprotobyname_mutex);
- if (!(ent = getprotobyname (protocol)))
- {
- g_static_mutex_unlock (&getprotobyname_mutex);
- g_set_error (&error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED, "unsupported socket protocol");
- return NULL;
- }
- native_protocol = ent->p_proto;
- g_static_mutex_unlock (&getprotobyname_mutex);
- }
-
- fd = socket(native_domain, native_type, native_protocol);
-
- if (fd < 0)
- {
- g_set_error (&error, G_IO_ERROR, g_io_error_from_errno (errno), "unable to create socket: %s", g_strerror (errno));
- return NULL;
- }
-
- g_print ("finishing socket_new\n");
-
- return G_SOCKET (g_object_new (G_TYPE_SOCKET, "blocking", TRUE, "fd", fd, NULL));