//Initialize the Linux-specific part of the context. All is related to //libmnl/netfilter struct neat_ctx *neat_linux_init_ctx(struct neat_ctx *nc) { //TODO: Consider allocator function if ((nc->mnl_rcv_buf = calloc(MNL_SOCKET_BUFFER_SIZE, 1)) == NULL) { fprintf(stderr, "Failed to allocate netlink buffer\n"); return NULL; } //Configure netlink and start requesting addresses if ((nc->mnl_sock = mnl_socket_open(NETLINK_ROUTE)) == NULL) { fprintf(stderr, "Failed to allocate netlink socket\n"); return NULL; } if (mnl_socket_bind(nc->mnl_sock, (1 << (RTNLGRP_IPV4_IFADDR - 1)) | (1 << (RTNLGRP_IPV6_IFADDR - 1)), 0)) { fprintf(stderr, "Failed to bind netlink socket\n"); return NULL; } //We need to build a list of all available source addresses as soon as //possible. It is started here if (neat_linux_request_addrs(nc->mnl_sock) <= 0) { fprintf(stderr, "Failed to request addresses\n"); return NULL; } //Add socket to event loop if (uv_udp_init(nc->loop, &(nc->uv_nl_handle))) { fprintf(stderr, "Failed to initialize uv UDP handle\n"); return NULL; } //TODO: We could use offsetof, but libuv has a pointer so ... nc->uv_nl_handle.data = nc; if (uv_udp_open(&(nc->uv_nl_handle), mnl_socket_get_fd(nc->mnl_sock))) { fprintf(stderr, "Could not add netlink socket to uv\n"); return NULL; } if (uv_udp_recv_start(&(nc->uv_nl_handle), neat_linux_nl_alloc, neat_linux_nl_recv)) { fprintf(stderr, "Could not start receiving netlink packets\n"); return NULL; } nc->cleanup = neat_linux_cleanup; //Configure netlink socket, add to event loop and start dumping return nc; }
void UDPSocket::init() { if (ptr()) return; TraceS(this) << "Init" << endl; uv_udp_t* udp = new uv_udp_t; udp->data = this; //instance(); _closed = false; _ptr = reinterpret_cast<uv_handle_t*>(udp); int r = uv_udp_init(loop(), udp); if (r) setUVError("Cannot initialize UDP socket", r); }
void io_create(uv_loop_t *loop, uv_handle_t *handle, int type) { if (type == SOCK_DGRAM) { uv_udp_init(loop, (uv_udp_t *)handle); } else { uv_tcp_init(loop, (uv_tcp_t *)handle); uv_tcp_nodelay((uv_tcp_t *)handle, 1); } struct worker_ctx *worker = loop->data; handle->data = session_borrow(worker); assert(handle->data); }
/* * Class: com_oracle_libuv_handles_UDPHandle * Method: _new * Signature: (J)J */ JNIEXPORT jlong JNICALL Java_com_oracle_libuv_handles_UDPHandle__1new__J (JNIEnv *env, jclass cls, jlong loop) { assert(loop); uv_loop_t* lp = reinterpret_cast<uv_loop_t*>(loop); uv_udp_t* udp = new uv_udp_t(); int r = uv_udp_init(lp, udp); if (r) { ThrowException(env, udp->loop, "uv_udp_init"); return (jlong) NULL; } udp->data = new UDPCallbacks(); return reinterpret_cast<jlong>(udp); }
static void udp_sender(void) { struct sockaddr_in server_addr; uv_buf_t buf; int r; r = uv_udp_init(loop, &udp); ASSERT(!r); buf = uv_buf_init("PING", 4); server_addr = uv_ip4_addr("127.0.0.1", server_port); r = uv_udp_send(&send_req, &udp, &buf, 1, server_addr, udp_send); ASSERT(!r); }
/* * * SOCKS5 UDP Request * +----+------+------+----------+----------+----------+ * |RSV | FRAG | ATYP | DST.ADDR | DST.PORT | DATA | * +----+------+------+----------+----------+----------+ * | 2 | 1 | 1 | Variable | 2 | Variable | * +----+------+------+----------+----------+----------+ * */ static void client_recv_cb(uv_udp_t *handle, ssize_t nread, const uv_buf_t *buf, const struct sockaddr *addr, unsigned flags) { struct server_context *server = container_of(handle, struct server_context, udp); if (nread > 0) { uint8_t frag = buf->base[2]; if (frag) { logger_log(LOG_ERR, "don't support udp dgram frag"); goto err; } char key[KEY_BYTES + 1] = {0}; crypto_generickey((uint8_t *)key, sizeof(key) -1, (uint8_t*)addr, sizeof(*addr), NULL, 0); struct client_context *client = NULL; uv_mutex_lock(&mutex); cache_lookup(cache, key, (void *)&client); uv_mutex_unlock(&mutex); if (client == NULL) { client = new_client(); client->addr = *addr; client->local_handle = handle; memcpy(client->key, key, sizeof(key)); uv_timer_init(handle->loop, client->timer); uv_udp_init(handle->loop, &client->server_handle); client->server_handle.data = client; uv_udp_recv_start(&client->server_handle, server_alloc_cb, server_recv_cb); uv_mutex_lock(&mutex); cache_insert(cache, client->key, (void *)client); uv_mutex_unlock(&mutex); } int clen = nread - 3 + PRIMITIVE_BYTES; uint8_t *c = (uint8_t *)buf->base - PRIMITIVE_BYTES; int rc = crypto_encrypt(c, (uint8_t*)buf->base + 3, nread - 3); if (!rc) { reset_timer(client); forward_to_server(server->server_addr, client, c, clen); } } else { goto err; } return; err: free(buf->base - PRIMITIVE_BYTES); }
static int luv_new_udp(lua_State* L) { uv_udp_t* handle = (uv_udp_t*)luv_newuserdata(L, sizeof(*handle)); int ret; if (lua_isnoneornil(L, 1)) { ret = uv_udp_init(luv_loop(L), handle); } else { ret = uv_udp_init_ex(luv_loop(L), handle, lua_tointeger(L, 1)); } if (ret < 0) { lua_pop(L, 1); return luv_error(L, ret); } handle->data = luv_setup_handle(L); return 1; }
int melo_udp_start(melo_udp_t * mudp, uv_loop_t* loop, melo_udp_config_t config) { assert(mudp && loop); mudp->uvloop = loop; memcpy(&mudp->config, &config, sizeof(melo_udp_config_t)); // init udp uv_udp_init(loop, &mudp->uvudp); mudp->uvudp.data = mudp; if (mudp->config.ip) { int r; melo_sockaddr_t sockaddr; melo_get_sock_addr(mudp->config.ip, mudp->config.port, &sockaddr); r = uv_udp_bind(&mudp->uvudp, (const struct sockaddr *)&sockaddr, 0); if(r >= 0) { char timestr[32]; time_t t; time(&t); strftime(timestr, sizeof(timestr), "[%Y-%m-%d %X]", localtime(&t)); // C99 only: %F = %Y-%m-%d MELO_INFO(config.log_out, "[uvx-udp] %s %s bind on %s:%d ...\n", timestr, mudp->config.name, mudp->config.ip, mudp->config.port); } else { MELO_ERR(config.log_err, "\n!!! [uvx-udp] %s bind on %s:%d failed: %s\n", mudp->config.name, mudp->config.ip, mudp->config.port, uv_strerror(r)); uv_close((uv_handle_t*) &mudp->uvudp, NULL); return ERROR; } } else { // if ip == NULL, default bind to local random port number (for UDP client) MELO_INFO(config.log_out, "[uvx-udp] %s bind on local random port ...\n", mudp->config.name); } uv_udp_recv_start(&mudp->uvudp, melo_alloc_buf, _cb_on_udp_recv); return OK; }
static int udp4_echo_start(int port) { int r; server = (uv_handle_t*)&udpServer; serverType = UDP; r = uv_udp_init(loop, &udpServer); if (r) { fprintf(stderr, "uv_udp_init: %s\n", uv_strerror(r)); return 1; } r = uv_udp_recv_start(&udpServer, echo_alloc, on_recv); if (r) { fprintf(stderr, "uv_udp_recv_start: %s\n", uv_strerror(r)); return 1; } return 0; }
static int UDP_tp_init(UDP *self, PyObject *args, PyObject *kwargs) { int r; uv_udp_t *uv_udp_handle = NULL; Loop *loop; PyObject *tmp = NULL; UNUSED_ARG(kwargs); if (UV_HANDLE(self)) { PyErr_SetString(PyExc_UDPError, "Object already initialized"); return -1; } if (!PyArg_ParseTuple(args, "O!:__init__", &LoopType, &loop)) { return -1; } tmp = (PyObject *)((Handle *)self)->loop; Py_INCREF(loop); ((Handle *)self)->loop = loop; Py_XDECREF(tmp); uv_udp_handle = PyMem_Malloc(sizeof(uv_udp_t)); if (!uv_udp_handle) { PyErr_NoMemory(); Py_DECREF(loop); return -1; } r = uv_udp_init(UV_HANDLE_LOOP(self), uv_udp_handle); if (r != 0) { RAISE_UV_EXCEPTION(UV_HANDLE_LOOP(self), PyExc_UDPError); Py_DECREF(loop); return -1; } uv_udp_handle->data = (void *)self; UV_HANDLE(self) = (uv_handle_t *)uv_udp_handle; return 0; }
static int udp_listener(int port) { struct sockaddr_in addr = uv_ip4_addr("0.0.0.0", port); struct sockaddr sockname; int namelen = sizeof(sockname); char ip[20]; int r; r = uv_udp_init(loop, &udpServer); if (r) { fprintf(stderr, "Socket creation error\n"); return 1; } r = uv_udp_bind(&udpServer, addr, 0); if (r) { fprintf(stderr, "Bind error\n"); return 1; } memset(&sockname, -1, sizeof sockname); r = uv_getsockname((uv_handle_t*)&udpServer, &sockname, &namelen); if (r != 0) { fprintf(stderr, "uv_getsockname error (listening) %d\n", uv_last_error(loop).code); } ASSERT(r == 0); r = uv_ip4_name((struct sockaddr_in*)&sockname, ip, 20); ASSERT(r == 0); ASSERT(ip[0] == '0'); ASSERT(ip[1] == '.'); ASSERT(ip[2] == '0'); printf("sockname = %s\n", ip); getsocknamecount++; r = uv_udp_recv_start(&udpServer, alloc, udp_recv); ASSERT(r == 0); return 0; }
int udprelay_start(uv_loop_t *loop, struct server_context *server) { int rc; uv_udp_init(loop, &server->udp); if ((rc = uv_udp_open(&server->udp, server->udp_fd))) { logger_stderr("udp open error: %s", uv_strerror(rc)); return 1; } rc = uv_udp_bind(&server->udp, server->local_addr, UV_UDP_REUSEADDR); if (rc) { logger_stderr("bind error: %s", uv_strerror(rc)); return 1; } uv_udp_recv_start(&server->udp, client_alloc_cb, client_recv_cb); return 0; }
int sg_etp_session_start(sg_etp_session_t * session, int interval_ms, uv_udp_t * udp) { int ret = ERROR; SG_ASSERT_RET(NULL != session, "session pointer is NULL", ret); session->interval = interval_ms; if (NULL == udp) /* self-contained udp, for client */ { /* init udp */ session->udp = &(session->udp_hdl); ret = uv_udp_init(session->loop, session->udp); SG_ASSERT_RET(ret >= 0, "init udp failed", ERROR); session->udp->data = session; ret = uv_udp_recv_start(session->udp, on_uv_alloc_buffer, on_client_recv_udp); SG_ASSERT_RET(ret >= 0, "start udp recv failed", ERROR); } else { session->udp = udp; } ret = ikcp_nodelay(session->kcp, 1, interval_ms, 2, 0); SG_ASSERT_RET(ret >= 0, "ikcp nodelay failed", ERROR); /* start a timer for kcp update and receiving */ ret = uv_timer_init(session->loop, &(session->timer)); SG_ASSERT_RET(ret >= 0, "init timer failed", ERROR); session->timer.data = session; /* link client pointer to timer */ ret = uv_timer_start(&(session->timer), on_uv_timer_cb, session->interval, session->interval); SG_ASSERT_RET(ret >= 0, "start timer failed", ERROR); uv_idle_init(session->loop, &(session->idle)); session->idle.data = session; SG_CALLBACK(session->on_open, session); return OK; }
void utb_udp_init(utb_t *utb,const char *rmt_addr,int port) { int status; // store the udp port utb->rmt_addr = uv_ip4_addr(utb->udp_addr,utb->udp_port); // create the socket status = uv_udp_init(utb->stream->loop,&utb->udp_sock); assert(status == 0); // bind to all interface with a system assigned local port status = uv_udp_bind(&utb->udp_sock,uv_ip4_addr("0.0.0.0",0),0); assert(status == 0); // link this utb with the udp socket utb->udp_sock.data = utb; // start udp reception status = uv_udp_recv_start(&utb->udp_sock,utb_on_buf_alloc,utb_on_udp_recv); assert(status == 0); }
static int UDP_tp_init(UDP *self, PyObject *args, PyObject *kwargs) { int r; Loop *loop; UNUSED_ARG(kwargs); RAISE_IF_HANDLE_INITIALIZED(self, -1); if (!PyArg_ParseTuple(args, "O!:__init__", &LoopType, &loop)) { return -1; } r = uv_udp_init(loop->uv_loop, (uv_udp_t *)UV_HANDLE(self)); if (r != 0) { RAISE_UV_EXCEPTION(loop->uv_loop, PyExc_UDPError); return -1; } initialize_handle(HANDLE(self), loop); return 0; }
int main(int argc, char *argv[]) { helix_session_t session; struct sockaddr_in addr; helix_protocol_t proto; struct config cfg = {}; uv_udp_t socket; int err; program = basename(argv[0]); parse_options(&cfg, argc, argv); if (!cfg.symbol) { fprintf(stderr, "error: symbol is not specified. Use the '-s' option to specify it.\n"); exit(1); } if (!cfg.proto) { fprintf(stderr, "error: multicast protocol is not specified. Use the '-P' option to specify it.\n"); exit(1); } if (!cfg.multicast_addr) { fprintf(stderr, "error: multicast address is not specified. Use the '-a' option to specify it.\n"); exit(1); } if (!cfg.multicast_port) { fprintf(stderr, "error: multicast port is not specified. Use the '-p' option to specify it.\n"); exit(1); } proto = helix_protocol_lookup(cfg.proto); if (!proto) { fprintf(stderr, "error: protocol '%s' is not supported\n", cfg.proto); exit(1); } session = helix_session_create(proto, process_ob_event, process_trade_event, NULL); if (!session) { fprintf(stderr, "error: unable to create new session\n"); exit(1); } helix_session_subscribe(session, cfg.symbol, cfg.max_orders); err = uv_udp_init(uv_default_loop(), &socket); if (err) { libuv_error("uv_udp_init", err); } socket.data = session; err = uv_ip4_addr("0.0.0.0", cfg.multicast_port, &addr); if (err) { libuv_error("uv_ip4_addr", err); } err = uv_udp_bind(&socket, (const struct sockaddr *)&addr, UV_UDP_REUSEADDR); if (err) { libuv_error("uv_udp_bind", err); } err = uv_udp_set_membership(&socket, cfg.multicast_addr, NULL, UV_JOIN_GROUP); if (err) { libuv_error("uv_udp_set_membership", err); } err = uv_udp_recv_start(&socket, alloc_packet, recv_packet); if (err) { libuv_error("uv_udp_recv_start", err); } initscr(); clear(); refresh(); uv_run(uv_default_loop(), UV_RUN_DEFAULT); endwin(); return 0; }
extern "C" int rust_uv_udp_init(uv_loop_t* loop, uv_udp_t* handle) { return uv_udp_init(loop, handle); }
static void poll_cb(uv_poll_t *watcher, int status, int events) { char buffer[1024] = {0}; char control_buffer[64] = {0}; struct iovec iov[1]; struct msghdr msg; struct sockaddr client_addr; struct server_context *server = container_of(watcher, struct server_context, watcher); if (status >= 0) { msg.msg_name = &client_addr; msg.msg_namelen = sizeof(client_addr); msg.msg_control = control_buffer; msg.msg_controllen = sizeof(control_buffer); iov[0].iov_base = buffer; iov[0].iov_len = sizeof(buffer); msg.msg_iov = iov; msg.msg_iovlen = 1; int msglen = recvmsg(watcher->io_watcher.fd, &msg, 0); if (msglen <= 0) { logger_stderr("receive from client error: %s", strerror(errno)); } struct sockaddr dest_addr; if (getdestaddr(&msg, &dest_addr)) { logger_stderr("can not get destination address"); } int addrlen = dest_addr.sa_family == AF_INET ? IPV4_HEADER_LEN : IPV6_HEADER_LEN; int mlen = addrlen + msglen; int clen = PRIMITIVE_BYTES + mlen; uint8_t *c = malloc(clen); uint8_t *m = c + PRIMITIVE_BYTES; /* * * xsocks UDP Request * +------+----------+----------+----------+ * | ATYP | DST.ADDR | DST.PORT | DATA | * +------+----------+----------+----------+ * | 1 | Variable | 2 | Variable | * +------+----------+----------+----------+ * */ if (dest_addr.sa_family == AF_INET) { struct sockaddr_in *addr = (struct sockaddr_in *)&dest_addr; m[0] = ATYP_IPV4; memcpy(m + 1, &addr->sin_addr, 4); memcpy(m + 1 + 4, &addr->sin_port, 2); } else { struct sockaddr_in6 *addr = (struct sockaddr_in6 *)&dest_addr; m[0] = ATYP_IPV6; memcpy(m + 1, &addr->sin6_addr, 16); memcpy(m + 1 + 16, &addr->sin6_port, 2); } memcpy(m + addrlen, buffer, msglen); int rc = crypto_encrypt(c, m, mlen); if (!rc) { char key[KEY_BYTES + 1] = {0}; crypto_generickey((uint8_t *)key, sizeof(key) -1, (uint8_t *)&client_addr, sizeof(client_addr), NULL, 0); struct client_context *client = NULL; uv_mutex_lock(&mutex); cache_lookup(cache, key, (void *)&client); uv_mutex_unlock(&mutex); if (client == NULL) { client = new_client(); client->addr = client_addr; memcpy(client->key, key, sizeof(key)); uv_timer_init(watcher->loop, client->timer); uv_udp_init(watcher->loop, &client->server_handle); client->server_handle.data = client; uv_udp_recv_start(&client->server_handle, server_alloc_cb, server_recv_cb); uv_mutex_lock(&mutex); cache_insert(cache, client->key, (void *)client); uv_mutex_unlock(&mutex); } reset_timer(client); forward_to_server(server->server_addr, client, c, clen); } } }
static void server_recv_cb(uv_udp_t *handle, ssize_t nread, const uv_buf_t *buf, const struct sockaddr *addr, unsigned flags) { if (nread > 0) { struct client_context *client = handle->data; reset_timer(client); int mlen = nread - PRIMITIVE_BYTES; uint8_t *m = (uint8_t *)buf->base; int rc = crypto_decrypt(m, (uint8_t *)buf->base, nread); if (rc) { logger_log(LOG_ERR, "invalid udp packet"); goto err; } /* * * xsocks UDP Response * +------+----------+----------+----------+ * | ATYP | DST.ADDR | DST.PORT | DATA | * +------+----------+----------+----------+ * | 1 | Variable | 2 | Variable | * +------+----------+----------+----------+ * */ union { struct sockaddr addr; struct sockaddr_in addr4; struct sockaddr_in6 addr6; } dest_addr; if (m[0] == ATYP_IPV4) { dest_addr.addr4.sin_family = AF_INET; memcpy(&dest_addr.addr4.sin_addr, m + 1, 4); memcpy(&dest_addr.addr4.sin_port, m + 5, 2); } else { dest_addr.addr6.sin6_family = AF_INET6; memcpy(&dest_addr.addr6.sin6_addr, m + 1, 16); memcpy(&dest_addr.addr6.sin6_port, m + 17, 2); } int addrlen = m[0] == ATYP_IPV4 ? IPV4_HEADER_LEN : IPV6_HEADER_LEN; memmove(m, m + addrlen, mlen - addrlen); mlen -= addrlen; if (!client->bind_server) { uv_os_sock_t sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP); if (sock < 0) { logger_stderr("socket error: %s\n", strerror(errno)); } int yes = 1; if (setsockopt(sock, SOL_IP, IP_TRANSPARENT, &yes, sizeof(int))) { logger_stderr("setsockop IP_TRANSPARENT error: %s)", strerror(errno)); } client->dest_handle = malloc(sizeof(uv_udp_t)); uv_udp_init(handle->loop, client->dest_handle); rc = uv_udp_open(client->dest_handle, sock); if (rc) { logger_stderr("udp open error: %s", uv_strerror(rc)); } rc = uv_udp_bind(client->dest_handle, &dest_addr.addr, UV_UDP_REUSEADDR); if (rc) { logger_stderr("udp server bind error: %s", uv_strerror(rc)); } client->bind_server = 1; } forward_to_client(client, m , mlen); } else { goto err; } return; err: free(buf->base); }
int main(int argc, char *argv[]) { helix_session_t session; struct sockaddr_in addr; helix_protocol_t proto; struct config cfg = {}; uv_udp_t socket; int err; program = basename(argv[0]); parse_options(&cfg, argc, argv); if (!cfg.symbol) { fprintf(stderr, "error: symbol is not specified. Use the '-s' option to specify it.\n"); exit(1); } if (!cfg.multicast_proto) { fprintf(stderr, "error: multicast protocol is not specified. Use the '-c' option to specify it.\n"); exit(1); } if (!cfg.multicast_addr) { fprintf(stderr, "error: multicast address is not specified. Use the '-a' option to specify it.\n"); exit(1); } if (!cfg.multicast_port) { fprintf(stderr, "error: multicast port is not specified. Use the '-p' option to specify it.\n"); exit(1); } if (!strcmp(cfg.format, "pretty")) { fmt_ops = &fmt_pretty_ops; } else if (!strcmp(cfg.format, "csv")) { fmt_ops = &fmt_csv_ops; } else { fprintf(stderr, "error: %s: unsupported format\n", cfg.format); exit(1); } if (cfg.output) { output = fopen(cfg.output, "w"); if (!output) { fprintf(stderr, "error: %s: %s\n", cfg.output, strerror(errno)); exit(1); } } else { output = stdout; } proto = helix_protocol_lookup(cfg.multicast_proto); if (!proto) { fprintf(stderr, "error: protocol '%s' is not supported\n", cfg.multicast_proto); exit(1); } session = helix_session_create(proto, cfg.symbol, process_ob_event, process_trade_event, NULL); if (!session) { fprintf(stderr, "error: unable to create new session\n"); exit(1); } err = uv_udp_init(uv_default_loop(), &socket); if (err) { libuv_error("uv_udp_init", err); } socket.data = session; err = uv_ip4_addr("0.0.0.0", cfg.multicast_port, &addr); if (err) { libuv_error("uv_ip4_addr", err); } err = uv_udp_bind(&socket, (const struct sockaddr *)&addr, UV_UDP_REUSEADDR); if (err) { libuv_error("uv_udp_bind", err); } err = uv_udp_set_membership(&socket, cfg.multicast_addr, NULL, UV_JOIN_GROUP); if (err) { libuv_error("uv_udp_set_membership", err); } err = uv_udp_recv_start(&socket, alloc_packet, recv_packet); if (err) { libuv_error("uv_udp_recv_start", err); } fmt_ops->fmt_header(); uv_run(uv_default_loop(), UV_RUN_DEFAULT); }
int Udp::DoOpen(/* [in] */ Loop &loop, /* [in] */ uv_handle_t *peer) { return uv_udp_init(loop, (uv_udp_t *) peer); }