int lua_apr_socket_create(lua_State *L) { /* Socket types */ const char *proto_options[] = { "tcp", "udp", NULL }; const int proto_values[] = { APR_PROTO_TCP, APR_PROTO_UDP }; lua_apr_socket *object; apr_status_t status; int family, type, protocol; protocol = proto_values[luaL_checkoption(L, 1, "tcp", proto_options)]; family = family_check(L, 2); type = protocol == APR_PROTO_TCP ? SOCK_STREAM : SOCK_DGRAM; /* Create and initialize the socket and its associated memory pool. */ status = socket_alloc(L, &object); object->family = family; object->protocol = protocol; if (status == APR_SUCCESS) status = apr_socket_create(&object->handle, family, type, protocol, object->pool); if (status != APR_SUCCESS) return push_error_status(L, status); socket_init(L, object); return 1; }
net_socket_t udp_open(uint16_t port) { net_socket_t sockid = socket_alloc(SOCKET_TYPE_UDP); if (sockid == SOCKET_INVALID) return sockid; socket_set_port(sockid, port); socket_cmd(sockid, SOCKET_CMD_OPEN); return sockid; }
static int socket_accept(lua_State *L) { lua_apr_socket *server, *client = NULL; apr_status_t status; server = socket_check(L, 1, 1); status = socket_alloc(L, &client); client->family = server->family; client->protocol = server->protocol; if (status == APR_SUCCESS) status = apr_socket_accept(&client->handle, server->handle, client->pool); socket_init(L, client); if (status != APR_SUCCESS) return push_error_status(L, status); return 1; }
int main(int argc,char **argv) { struct netdev *nd; int ret; struct socket *sock; if(argc != 4) banner(argv[0]); ret = -1; nd = xzalloc(sizeof(struct netdev)); if(!nd) { perrx("Couldn't create netdev structure"); goto bad; } sock = socket_alloc(); if(sock == NULL) goto bad; nd->nd_ipaddr = inet_addr(argv[3]); nd->nd_ops = &nd_ops; if(cl_sock_connect(&sock,argv[1],atoi(argv[2])) < 0) goto bad; nd->nd_ipaddr = inet_addr(argv[3]); nd->nd_ops = &nd_ops; nd->sk = sock; if(cl_setup_promisc(nd) < 0) goto bad; return 0; bad: if(sock) free(sock); return ret; }