MCPcopy Create free account
hub / github.com/cppla/ServerStatus / priv_net_create_socket

Function priv_net_create_socket

server/src/system.c:855–909  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

853}
854
855static int priv_net_create_socket(int domain, int type, struct sockaddr *addr, int sockaddrlen)
856{
857 int sock, e;
858
859 /* create socket */
860 sock = socket(domain, type, 0);
861 if(sock < 0)
862 {
863#if defined(CONF_FAMILY_WINDOWS)
864 char buf[128];
865 int error = WSAGetLastError();
866 if(FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_IGNORE_INSERTS, 0, error, 0, buf, sizeof(buf), 0) == 0)
867 buf[0] = 0;
868 dbg_msg("net", "failed to create socket with domain %d and type %d (%d '%s')", domain, type, error, buf);
869#else
870 dbg_msg("net", "failed to create socket with domain %d and type %d (%d '%s')", domain, type, errno, strerror(errno));
871#endif
872 return -1;
873 }
874
875 /* set to IPv6 only if thats what we are creating */
876#if defined(IPV6_V6ONLY) /* windows sdk 6.1 and higher */
877 if(domain == AF_INET6)
878 {
879 int ipv6only = 1;
880 setsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY, (const char*)&ipv6only, sizeof(ipv6only));
881 }
882#endif
883
884 if(type == SOCK_STREAM)
885 {
886 int tmp = 1;
887 setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &tmp, sizeof(tmp));
888 }
889
890 /* bind the socket */
891 e = bind(sock, addr, sockaddrlen);
892 if(e != 0)
893 {
894#if defined(CONF_FAMILY_WINDOWS)
895 char buf[128];
896 int error = WSAGetLastError();
897 if(FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_IGNORE_INSERTS, 0, error, 0, buf, sizeof(buf), 0) == 0)
898 buf[0] = 0;
899 dbg_msg("net", "failed to bind socket with domain %d and type %d (%d '%s')", domain, type, error, buf);
900#else
901 dbg_msg("net", "failed to bind socket with domain %d and type %d (%d '%s')", domain, type, errno, strerror(errno));
902#endif
903 priv_net_close_socket(sock);
904 return -1;
905 }
906
907 /* return the newly created socket */
908 return sock;
909}
910
911NETSOCKET net_udp_create(NETADDR bindaddr)
912{

Callers 2

net_udp_createFunction · 0.85
net_tcp_createFunction · 0.85

Calls 2

dbg_msgFunction · 0.85
priv_net_close_socketFunction · 0.85

Tested by

no test coverage detected