/*-------------------------------------------------------------------------*\ * \*-------------------------------------------------------------------------*/ int socket_listen(p_socket ps, int backlog) { int err = IO_DONE; socket_setblocking(ps); if (listen(*ps, backlog) < 0) err = WSAGetLastError(); socket_setnonblocking(ps); return err; }
/*-------------------------------------------------------------------------*\ * \*-------------------------------------------------------------------------*/ int socket_listen(p_socket ps, int backlog) { int err = IO_DONE; socket_setblocking(ps); if (listen(*ps, backlog)) err = errno; socket_setnonblocking(ps); return err; }
/*-------------------------------------------------------------------------*\ * Waits for and returns a client object attempting connection to the * server object \*-------------------------------------------------------------------------*/ static int meth_accept(lua_State *L) { p_tcp server = (p_tcp) auxiliar_checkclass(L, "tcp{server}", 1); p_timeout tm = timeout_markstart(&server->tm); t_socket sock; const char *err = inet_tryaccept(&server->sock, server->family, &sock, tm); /* if successful, push client socket */ if (err == NULL) { p_tcp clnt = (p_tcp) lua_newuserdata(L, sizeof(t_tcp)); auxiliar_setclass(L, "tcp{client}", -1); /* initialize structure fields */ memset(clnt, 0, sizeof(t_tcp)); socket_setnonblocking(&sock); clnt->sock = sock; io_init(&clnt->io, (p_send) socket_send, (p_recv) socket_recv, (p_error) socket_ioerror, &clnt->sock); timeout_init(&clnt->tm, -1, -1); buffer_init(&clnt->buf, &clnt->io, &clnt->tm); clnt->family = server->family; return 1; } else { lua_pushnil(L); lua_pushstring(L, err); return 2; } }
/*-------------------------------------------------------------------------*\ * Creates a master tcp object \*-------------------------------------------------------------------------*/ static int tcp_create(lua_State *L, int family) { t_socket sock; const char *err = inet_trycreate(&sock, family, SOCK_STREAM); /* try to allocate a system socket */ if (!err) { /* allocate tcp object */ p_tcp tcp = (p_tcp) lua_newuserdata(L, sizeof(t_tcp)); /* set its type as master object */ auxiliar_setclass(L, "tcp{master}", -1); /* initialize remaining structure fields */ socket_setnonblocking(&sock); if (family == PF_INET6) { int yes = 1; setsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY, (void *)&yes, sizeof(yes)); } tcp->sock = sock; io_init(&tcp->io, (p_send) socket_send, (p_recv) socket_recv, (p_error) socket_ioerror, &tcp->sock); timeout_init(&tcp->tm, -1, -1); buffer_init(&tcp->buf, &tcp->io, &tcp->tm); tcp->family = family; return 1; } else { lua_pushnil(L); lua_pushstring(L, err); return 2; } }
static int socket_listen_setup(master_server *master_srv, int fd) { /* disable / enable the Nagle (TCP No Delay) algorithm */ if (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char*)&master_srv->config->tcp_nodelay, sizeof(master_srv->config->tcp_nodelay)) == -1) { perror ("ERROR setsockopt(TCP_NODELAY)"); return -1; } /* set send buffer size */ if (setsockopt(fd, SOL_SOCKET, SO_SNDBUF, (char*)&master_srv->config->write_buffer_size, sizeof(master_srv->config->write_buffer_size)) == -1) { perror ("ERROR setsockopt(SO_SNDBUF)"); return -1; } /* set read buffer size */ if (setsockopt(fd, SOL_SOCKET, SO_RCVBUF, (char*)&master_srv->config->read_buffer_size, sizeof(master_srv->config->read_buffer_size)) == -1) { perror ("ERROR setsockopt(SO_RCVBUF)"); return -1; } /* set defer accept to 5 sec */ int val = 5; if (setsockopt(fd, IPPROTO_TCP, TCP_DEFER_ACCEPT, (char*)&val, sizeof(int)) == -1) { perror ("ERROR setsockopt(TCP_DEFER_ACCEPT)"); return -1; } /* non-blocking socket */ socket_setnonblocking (fd); return 0; }
/*-------------------------------------------------------------------------*\ * Creates a master tcp object \*-------------------------------------------------------------------------*/ static int tcp_create(lua_State *L, int family) { p_tcp tcp = (p_tcp) lua_newuserdata(L, sizeof(t_tcp)); memset(tcp, 0, sizeof(t_tcp)); /* set its type as master object */ auxiliar_setclass(L, "tcp{master}", -1); /* if family is AF_UNSPEC, we leave the socket invalid and * store AF_UNSPEC into family. This will allow it to later be * replaced with an AF_INET6 or AF_INET socket upon first use. */ tcp->sock = SOCKET_INVALID; tcp->family = family; io_init(&tcp->io, (p_send) socket_send, (p_recv) socket_recv, (p_error) socket_ioerror, &tcp->sock); timeout_init(&tcp->tm, -1, -1); buffer_init(&tcp->buf, &tcp->io, &tcp->tm); if (family != AF_UNSPEC) { const char *err = inet_trycreate(&tcp->sock, family, SOCK_STREAM, 0); if (err != NULL) { lua_pushnil(L); lua_pushstring(L, err); return 2; } socket_setnonblocking(&tcp->sock); } return 1; }
/*-------------------------------------------------------------------------*\ * Creates a master tcp object \*-------------------------------------------------------------------------*/ static int global_create(lua_State *L) { short family; const char *af_opts[] = {"AF_INET", "AF_INET6"}; const char *def_af = "AF_INET"; const char *err; t_socket sock; switch(luaL_checkoption(L, 1, def_af, af_opts)) { case 0 : family = PF_INET ; break; case 1 : family = PF_INET6 ; break; default: family = PF_INET ; break; } /* try to allocate a system socket */ err = inet_trycreate(&sock, SOCK_STREAM, family); if (!err) { /* allocate tcp object */ p_tcp tcp = (p_tcp) lua_newuserdata(L, sizeof(t_tcp)); /* set its type as master object */ auxiliar_setclass(L, "tcp{master}", -1); /* initialize remaining structure fields */ socket_setnonblocking(&sock); tcp->sock = sock; io_init(&tcp->io, (p_send) socket_send, (p_recv) socket_recv, (p_error) socket_ioerror, &tcp->sock); timeout_init(&tcp->tm, -1, -1); buffer_init(&tcp->buf, &tcp->io, &tcp->tm); return 1; } else { lua_pushnil(L); lua_pushstring(L, err); return 2; } }
/*-------------------------------------------------------------------------*\ * Binds or returns error message \*-------------------------------------------------------------------------*/ int socket_bind(p_socket ps, SA *addr, socklen_t len) { int err = IO_DONE; socket_setblocking(ps); if (bind(*ps, addr, len) < 0) err = errno; socket_setnonblocking(ps); return err; }
/*-------------------------------------------------------------------------*\ * Creates a serial object \*-------------------------------------------------------------------------*/ static int global_create(lua_State *L) { const char* path = luaL_checkstring(L, 1); /* allocate unix object */ p_unix un = (p_unix) lua_newuserdata(L, sizeof(t_unix)); /* open serial device */ t_socket sock = open(path, O_NOCTTY|O_RDWR, 00700); /*printf("open %s on %d\n", path, sock);*/ if (sock < 0) { lua_pushnil(L); lua_pushstring(L, socket_strerror(errno)); lua_pushnumber(L, errno); return 3; } /* set its type as client object */ auxiliar_setclass(L, "serial{client}", -1); /* initialize remaining structure fields */ socket_setnonblocking(&sock); un->sock = sock; io_init(&un->io, (p_send) socket_write, (p_recv) socket_read, (p_error) socket_ioerror, &un->sock); timeout_init(&un->tm, -1, -1); buffer_init(&un->buf, &un->io, &un->tm); return 1; }
T_ERRCODE socket_bind(p_socket sock, p_sa addr, int addr_len) { int ret = SUCCESS; socket_setblocking(sock); if (bind(*sock, addr, addr_len)) { ret = errno; } socket_setnonblocking(sock); return ret; }
T_ERRCODE socket_listen(p_socket sock, int backlog) { int ret = SUCCESS; socket_setblocking(sock); if (listen(*sock, backlog)) { ret = errno; } socket_setnonblocking(sock); return ret; }
int http_reconnect(HTTP_CONNECTION *connection) { if(connection == NULL) { return HT_INVALID_ARGUMENT; } if(connection->socketd != INVALID_SOCKET && connection->status == HT_OK) { if(http_check_socket(connection) == HT_OK) { return HT_OK; } } if(!connection->persistent) { return HT_ILLEGAL_OPERATION; } if(connection->socketd != INVALID_SOCKET) { close(connection->socketd); } connection->socketd = socket(AF_INET, SOCK_STREAM, 0); if(connection->socketd == INVALID_SOCKET) { return HT_RESOURCE_UNAVAILABLE; } { int rcvsize = 128 * 1024; setsockopt(connection->socketd, SOL_SOCKET, SO_RCVBUF, (char *)&rcvsize, (int)sizeof(rcvsize)); } { int flag = 1; setsockopt(connection->socketd, IPPROTO_TCP, TCP_NODELAY, (char*)&flag, sizeof(flag)); } if (connection->lazy) { while (connect(connection->socketd, (struct sockaddr *) &connection->address, sizeof(struct sockaddr_in)) != 0) { #if defined(WIN32) Sleep(100); #endif // WIN32 } } else { if(connect(connection->socketd, (struct sockaddr *) &connection->address, sizeof(struct sockaddr_in)) != 0) { close(connection->socketd); return HT_NETWORK_ERROR; } } socket_setnonblocking(connection->socketd); connection->status = HT_OK; return HT_OK; }
/** * Set the TLS/SSL file descriptor. * This is done *before* the handshake. */ static int meth_setfd(lua_State *L) { p_ssl ssl = (p_ssl) luaL_checkudata(L, 1, "SSL:Connection"); if (ssl->state != ST_SSL_NEW) luaL_argerror(L, 1, "invalid SSL object state"); ssl->sock = luaL_checkint(L, 2); socket_setnonblocking(&ssl->sock); SSL_set_fd(ssl->ssl, (int)ssl->sock); return 0; }
/*-------------------------------------------------------------------------*\ * Send data through unconnected udp socket \*-------------------------------------------------------------------------*/ static int meth_sendto(lua_State *L) { p_udp udp = (p_udp) auxiliar_checkclass(L, "udp{unconnected}", 1); size_t count, sent = 0; const char *data = luaL_checklstring(L, 2, &count); const char *ip = luaL_checkstring(L, 3); const char *port = luaL_checkstring(L, 4); p_timeout tm = &udp->tm; int err; struct addrinfo aihint; struct addrinfo *ai; memset(&aihint, 0, sizeof(aihint)); aihint.ai_family = udp->family; aihint.ai_socktype = SOCK_DGRAM; aihint.ai_flags = AI_NUMERICHOST | AI_NUMERICSERV; err = getaddrinfo(ip, port, &aihint, &ai); if (err) { lua_pushnil(L); lua_pushstring(L, gai_strerror(err)); return 2; } /* create socket if on first sendto if AF_UNSPEC was set */ if (udp->family == AF_UNSPEC && udp->sock == SOCKET_INVALID) { struct addrinfo *ap; const char *errstr = NULL; for (ap = ai; ap != NULL; ap = ap->ai_next) { errstr = inet_trycreate(&udp->sock, ap->ai_family, SOCK_DGRAM, 0); if (errstr == NULL) { socket_setnonblocking(&udp->sock); udp->family = ap->ai_family; break; } } if (errstr != NULL) { lua_pushnil(L); lua_pushstring(L, errstr); freeaddrinfo(ai); return 2; } } timeout_markstart(tm); err = socket_sendto(&udp->sock, data, count, &sent, ai->ai_addr, (socklen_t) ai->ai_addrlen, tm); freeaddrinfo(ai); if (err != IO_DONE) { lua_pushnil(L); lua_pushstring(L, udp_strerror(err)); return 2; } lua_pushnumber(L, (lua_Number) sent); return 1; }
/*------------------------------------------------------------------------------*/ int L3_xface_init(void) { /*------------------------------------------------------------------------------*/ int ret = 0; #ifdef USER_MODE int sock; LOG_D(RRC, "[L3_XFACE] init de l'interface \n"); if (open_socket (&S_rrc, RRC_RRM_SOCK_PATH, RRM_RRC_SOCK_PATH, 0) == -1) return (-1); if (S_rrc.s == -1) { return (-1); } socket_setnonblocking (S_rrc.s); msg ("Interface Connected... RRM-RRC\n"); return 0; #else ret=rtf_create(RRC2RRM_FIFO,32768); if (ret < 0) { msg("[openair][MAC][INIT] Cannot create RRC2RRM fifo %d (ERROR %d)\n",RRC2RRM_FIFO,ret); return(-1); } else { msg("[openair][MAC][INIT] Created RRC2RRM fifo %d\n",RRC2RRM_FIFO); rtf_reset(RRC2RRM_FIFO); } ret=rtf_create(RRM2RRC_FIFO,32768); if (ret < 0) { msg("[openair][MAC][INIT] Cannot create RRM2RRC fifo %d (ERROR %d)\n",RRM2RRC_FIFO,ret); return(-1); } else { msg("[openair][MAC][INIT] Created RRC2RRM fifo %d\n",RRM2RRC_FIFO); rtf_reset(RRM2RRC_FIFO); } return(0); #endif }
//------------------------------------------------------------------------------ void multicast_link_init () { //------------------------------------------------------------------------------ int group; int multicast_loop; int reuse_addr = 1; /* Used so we can re-bind to our port while a previous connection is still in TIME_WAIT state. */ static struct ip_mreq command; struct sockaddr_in sin; for (group = 0; group < MULTICAST_LINK_NUM_GROUPS; group++) { strcpy (group_list[group].host_addr, multicast_group_list[group]); group_list[group].port = 46014 + group; group_list[group].socket = make_socket_inet (SOCK_DGRAM, &group_list[group].port, &sin); if (setsockopt (group_list[group].socket, SOL_SOCKET, SO_REUSEADDR, &reuse_addr, sizeof (reuse_addr)) < 0) { msg ("[MULTICAST] ERROR : setsockopt:SO_REUSEADDR, exiting ..."); exit (EXIT_FAILURE); } socket_setnonblocking (group_list[group].socket); // multicast_loop = 0; if (setsockopt (group_list[group].socket, IPPROTO_IP, IP_MULTICAST_LOOP, &multicast_loop, sizeof (multicast_loop)) < 0) { msg ("[MULTICAST] ERROR: %s line %d multicast_link_main_loop() IP_MULTICAST_LOOP %m", __FILE__, __LINE__); exit (EXIT_FAILURE); } // Join the broadcast group: command.imr_multiaddr.s_addr = inet_addr (group_list[group].host_addr); command.imr_interface.s_addr = htonl (INADDR_ANY); if (command.imr_multiaddr.s_addr == -1) { msg ("[MULTICAST] ERROR: %s line %d NO MULTICAST", __FILE__, __LINE__); exit (EXIT_FAILURE); } if (setsockopt (group_list[group].socket, IPPROTO_IP, IP_ADD_MEMBERSHIP, &command, sizeof (command)) < 0) { msg ("[MULTICAST] ERROR: %s line %d IP_ADD_MEMBERSHIP %m", __FILE__, __LINE__); exit (EXIT_FAILURE); } memset (&group_list[group].sock_remote_addr, 0, sizeof (struct sockaddr_in)); group_list[group].sock_remote_addr.sin_family = AF_INET; group_list[group].sock_remote_addr.sin_addr.s_addr = inet_addr (multicast_group_list[group]); group_list[group].sock_remote_addr.sin_port = htons (group_list[group].port); } }
/*-------------------------------------------------------------------------*\ * Tries to connect to remote address (address, port) \*-------------------------------------------------------------------------*/ const char *inet_tryconnect(p_socket ps, int *family, const char *address, const char *serv, p_timeout tm, struct addrinfo *connecthints) { #ifdef LUASOCKET_SECURITY_SANDBOX if (luasocket_ip_allowed(address)) return "connect restricted"; #endif // LUASOCKET_SECURITY_SANDBOX struct addrinfo *iterator = NULL, *resolved = NULL; const char *err = NULL; int current_family = *family; /* try resolving */ err = socket_gaistrerror(getaddrinfo(address, serv, connecthints, &resolved)); if (err != NULL) { if (resolved) freeaddrinfo(resolved); return err; } for (iterator = resolved; iterator; iterator = iterator->ai_next) { timeout_markstart(tm); /* create new socket if necessary. if there was no * bind, we need to create one for every new family * that shows up while iterating. if there was a * bind, all families will be the same and we will * not enter this branch. */ if (current_family != iterator->ai_family || *ps == SOCKET_INVALID) { socket_destroy(ps); err = inet_trycreate(ps, iterator->ai_family, iterator->ai_socktype, iterator->ai_protocol); if (err) continue; current_family = iterator->ai_family; /* set non-blocking before connect */ socket_setnonblocking(ps); } /* try connecting to remote address */ err = socket_strerror(socket_connect(ps, (SA *) iterator->ai_addr, (socklen_t) iterator->ai_addrlen, tm)); /* if success or timeout is zero, break out of loop */ if (err == NULL || timeout_iszero(tm)) { *family = current_family; break; } } freeaddrinfo(resolved); /* here, if err is set, we failed */ return err; }
/*-------------------------------------------------------------------------*\ * Binds or returns error message \*-------------------------------------------------------------------------*/ int socket_bind(p_socket ps, SA *addr, socklen_t len) { int err = IO_DONE; /* char buf[256]; SOCKADDR_IN * pAddr = (SOCKADDR_IN*)(addr); sprintf(buf, "expected size of SOCKADDR_IN: %d sa_family: %d sin_addr.S_un.S_addr: %d\n", (int)(sizeof(SOCKADDR_IN)), (int)(pAddr->sin_family), (int)(pAddr->sin_addr.S_un.S_addr)); OutputDebugStringA(buf); sprintf(buf, "Trying to bind socket %d with addrlen %d\n", (int)(*ps), len); OutputDebugStringA(buf); */ socket_setblocking(ps); if (bind(*ps, addr, len) < 0) err = WSAGetLastError(); socket_setnonblocking(ps); return err; }
/*-------------------------------------------------------------------------*\ * Tries to bind socket to (address, port) \*-------------------------------------------------------------------------*/ const char *inet_trybind(p_socket ps, int *family, const char *address, const char *serv, struct addrinfo *bindhints) { #ifdef LUASOCKET_SECURITY_SANDBOX if (luasocket_ip_allowed(address)) return "bind restricted"; #endif // LUASOCKET_SECURITY_SANDBOX struct addrinfo *iterator = NULL, *resolved = NULL; const char *err = NULL; int current_family = *family; /* translate luasocket special values to C */ if (strcmp(address, "*") == 0) address = NULL; if (!serv) serv = "0"; /* try resolving */ err = socket_gaistrerror(getaddrinfo(address, serv, bindhints, &resolved)); if (err) { if (resolved) freeaddrinfo(resolved); return err; } /* iterate over resolved addresses until one is good */ for (iterator = resolved; iterator; iterator = iterator->ai_next) { if (current_family != iterator->ai_family || *ps == SOCKET_INVALID) { socket_destroy(ps); err = inet_trycreate(ps, iterator->ai_family, iterator->ai_socktype, iterator->ai_protocol); if (err) continue; current_family = iterator->ai_family; } /* try binding to local address */ err = socket_strerror(socket_bind(ps, (SA *) iterator->ai_addr, (socklen_t) iterator->ai_addrlen)); /* keep trying unless bind succeeded */ if (err == NULL) { *family = current_family; /* set to non-blocking after bind */ socket_setnonblocking(ps); break; } } /* cleanup and return error */ freeaddrinfo(resolved); /* here, if err is set, we failed */ return err; }
/*-------------------------------------------------------------------------*\ * Creates a master udp object \*-------------------------------------------------------------------------*/ static int global_create(lua_State *L) { t_socket sock; const char *err = inet_trycreate(&sock, SOCK_DGRAM); /* try to allocate a system socket */ if (!err) { /* allocate tcp object */ p_udp udp = (p_udp) lua_newuserdata(L, sizeof(t_udp)); auxiliar_setclass(L, "udp{unconnected}", -1); /* initialize remaining structure fields */ socket_setnonblocking(&sock); udp->sock = sock; timeout_init(&udp->tm, -1, -1); return 1; } else { lua_pushnil(L); lua_pushstring(L, err); return 2; } }
/*-------------------------------------------------------------------------*\ * Tries to connect to remote address (address, port) \*-------------------------------------------------------------------------*/ const char *inet_tryconnect(p_socket ps, int *family, const char *address, const char *serv, p_timeout tm, struct addrinfo *connecthints) { struct addrinfo *iterator = NULL, *resolved = NULL; const char *err = NULL; /* try resolving */ err = socket_gaistrerror(getaddrinfo(address, serv, connecthints, &resolved)); if (err != NULL) { if (resolved) freeaddrinfo(resolved); return err; } for (iterator = resolved; iterator; iterator = iterator->ai_next) { timeout_markstart(tm); /* create new socket if necessary. if there was no * bind, we need to create one for every new family * that shows up while iterating. if there was a * bind, all families will be the same and we will * not enter this branch. */ if (*family != iterator->ai_family) { socket_destroy(ps); err = socket_strerror(socket_create(ps, iterator->ai_family, iterator->ai_socktype, iterator->ai_protocol)); if (err != NULL) { freeaddrinfo(resolved); return err; } *family = iterator->ai_family; /* all sockets initially non-blocking */ socket_setnonblocking(ps); } /* try connecting to remote address */ err = socket_strerror(socket_connect(ps, (SA *) iterator->ai_addr, (socklen_t) iterator->ai_addrlen, tm)); /* if success, break out of loop */ if (err == NULL) break; } freeaddrinfo(resolved); /* here, if err is set, we failed */ return err; }
/*-------------------------------------------------------------------------*\ * Creates a master udp object \*-------------------------------------------------------------------------*/ static int udp_create(lua_State *L, int family) { /* allocate udp object */ p_udp udp = (p_udp) lua_newuserdata(L, sizeof(t_udp)); auxiliar_setclass(L, "udp{unconnected}", -1); /* if family is AF_UNSPEC, we leave the socket invalid and * store AF_UNSPEC into family. This will allow it to later be * replaced with an AF_INET6 or AF_INET socket upon first use. */ udp->sock = SOCKET_INVALID; timeout_init(&udp->tm, -1, -1); udp->family = family; if (family != AF_UNSPEC) { const char *err = inet_trycreate(&udp->sock, family, SOCK_DGRAM, 0); if (err != NULL) { lua_pushnil(L); lua_pushstring(L, err); return 2; } socket_setnonblocking(&udp->sock); } return 1; }
static const char *tryconnect6(const char *remoteaddr, const char *remoteserv, struct addrinfo *connecthints, p_tcp tcp) { struct addrinfo *iterator = NULL, *resolved = NULL; const char *err = NULL; /* try resolving */ err = socket_gaistrerror(getaddrinfo(remoteaddr, remoteserv, connecthints, &resolved)); if (err != NULL) { if (resolved) freeaddrinfo(resolved); return err; } /* iterate over all returned addresses trying to connect */ for (iterator = resolved; iterator; iterator = iterator->ai_next) { p_timeout tm = timeout_markstart(&tcp->tm); /* create new socket if one wasn't created by the bind stage */ if (tcp->sock == SOCKET_INVALID) { err = socket_strerror(socket_create(&tcp->sock, iterator->ai_family, iterator->ai_socktype, iterator->ai_protocol)); if (err != NULL) { freeaddrinfo(resolved); return err; } tcp->family = iterator->ai_family; /* all sockets initially non-blocking */ socket_setnonblocking(&tcp->sock); } /* finally try connecting to remote address */ err = socket_strerror(socket_connect(&tcp->sock, (SA *) iterator->ai_addr, (socklen_t) iterator->ai_addrlen, tm)); /* if success, break out of loop */ if (err == NULL) break; } freeaddrinfo(resolved); /* here, if err is set, we failed */ return err; }
/*-------------------------------------------------------------------------*\ * Creates a master unix object \*-------------------------------------------------------------------------*/ static int global_create(lua_State *L) { t_socket sock; int err = socket_create(&sock, AF_UNIX, SOCK_STREAM, 0); /* try to allocate a system socket */ if (err == IO_DONE) { /* allocate unix object */ p_unix un = (p_unix) lua_newuserdata(L, sizeof(t_unix)); /* set its type as master object */ auxiliar_setclass(L, "unix{master}", -1); /* initialize remaining structure fields */ socket_setnonblocking(&sock); un->sock = sock; io_init(&un->io, (p_send) socket_send, (p_recv) socket_recv, (p_error) socket_ioerror, &un->sock); timeout_init(&un->tm, -1, -1); buffer_init(&un->buf, &un->io, &un->tm); return 1; } else { lua_pushnil(L); lua_pushstring(L, socket_strerror(err)); return 2; } }
/*-------------------------------------------------------------------------*\ * Waits for and returns a client object attempting connection to the * server object \*-------------------------------------------------------------------------*/ static int meth_accept(lua_State *L) { p_unix server = (p_unix) auxiliar_checkclass(L, "unix{server}", 1); p_timeout tm = timeout_markstart(&server->tm); t_socket sock; int err = socket_accept(&server->sock, &sock, NULL, NULL, tm); /* if successful, push client socket */ if (err == IO_DONE) { p_unix clnt = (p_unix) lua_newuserdata(L, sizeof(t_unix)); auxiliar_setclass(L, "unix{client}", -1); /* initialize structure fields */ socket_setnonblocking(&sock); clnt->sock = sock; io_init(&clnt->io, (p_send)socket_send, (p_recv)socket_recv, (p_error) socket_ioerror, &clnt->sock); timeout_init(&clnt->tm, -1, -1); buffer_init(&clnt->buf, &clnt->io, &clnt->tm); return 1; } else { lua_pushnil(L); lua_pushstring(L, socket_strerror(err)); return 2; } }
/*-------------------------------------------------------------------------*\ * Creates a master tcp object \*-------------------------------------------------------------------------*/ static int global_create(lua_State *L) { t_socket sock; const char *err = inet_trycreate(&sock, SOCK_STREAM); /* try to allocate a system socket */ if (!err) { /* allocate tcp object */ p_tcp tcp = (p_tcp) lua_newuserdata(L, sizeof(t_tcp)); /* set its type as master object */ auxiliar_setclass(L, "tcp{master}", -1); /* initialize remaining structure fields */ socket_setnonblocking(&sock); tcp->sock = sock; io_init(&tcp->io, (p_send) socket_send, (p_recv) socket_recv, (p_error) socket_ioerror, &tcp->sock); timeout_init(&tcp->tm, -1, -1); buffer_init(&tcp->buf, &tcp->io, &tcp->tm); return 1; } else { lua_pushnil(L); lua_pushstring(L, err); return 2; } }
/*-------------------------------------------------------------------------*\ * \*-------------------------------------------------------------------------*/ void socket_shutdown(p_socket ps, int how) { socket_setblocking(ps); shutdown(*ps, how); socket_setnonblocking(ps); }
static int http_connect_helper(HTTP_CONNECTION **connection, const char *host, short port, const char *username, const char *password, int lazy) { unsigned int ipaddr = 0; struct hostent *hostinfo = NULL; HTTP_CONNECTION *new_connection = NULL; if(connection == NULL) { return HT_INVALID_ARGUMENT; } *connection = NULL; if(connection == NULL || host == NULL) { return HT_INVALID_ARGUMENT; } new_connection = (HTTP_CONNECTION *) _http_allocator(_http_allocator_user_data, 0, sizeof(HTTP_CONNECTION)); memset(new_connection, 0, sizeof(HTTP_CONNECTION)); new_connection->read_count = new_connection->read_index = 0; new_connection->address.sin_family = AF_INET; new_connection->address.sin_port = htons(port); if(username != NULL && password != NULL) { new_connection->auth_info = (HTTP_AUTH_INFO *) _http_allocator(_http_allocator_user_data, 0, sizeof(HTTP_AUTH_INFO)); if(new_connection->auth_info == NULL) { http_disconnect(&new_connection); return HT_MEMORY_ERROR; } memset(new_connection->auth_info, 0, sizeof(HTTP_AUTH_INFO)); http_add_auth_parameter(new_connection->auth_info, "username", username); http_add_auth_parameter(new_connection->auth_info, "password", password); } if((ipaddr = inet_addr(host)) != INADDR_NONE) { memcpy(&new_connection->address.sin_addr, &ipaddr, sizeof(struct in_addr)); } else { hostinfo = (struct hostent *) gethostbyname(host); if(hostinfo == NULL) { return HT_HOST_UNAVAILABLE; } memcpy(&new_connection->address.sin_addr, hostinfo->h_addr, 4); } new_connection->host = wd_strdup(host); if(new_connection->host == NULL) { http_disconnect(&new_connection); return HT_MEMORY_ERROR; } if (!lazy) { new_connection->socketd = socket(AF_INET, SOCK_STREAM, 0); if(new_connection->socketd == INVALID_SOCKET) { http_disconnect(&new_connection); return HT_RESOURCE_UNAVAILABLE; } if(connect(new_connection->socketd, (struct sockaddr *) &new_connection->address, sizeof(struct sockaddr_in)) != 0) { http_disconnect(&new_connection); return HT_NETWORK_ERROR; } socket_setnonblocking(new_connection->socketd); } else { new_connection->socketd = INVALID_SOCKET; } new_connection->lazy = lazy; new_connection->persistent = HT_TRUE; new_connection->status = HT_OK; *connection = new_connection; return HT_OK; }