void ip_handle() { int * data = ethernet_rx_data + ETHERNET_HDR_LEN; // not IPv4 or header is longer than 20bit if(data[IP_VERSION] != IP_VERSION_VAL) return; if (data[IP_DST] != IP_ADDR[0] || data[IP_DST + 1] != IP_ADDR[1] || data[IP_DST + 2] != IP_ADDR[2] || data[IP_DST + 3] != IP_ADDR[3]) { //cprintf("IP destination not correct.\n"); return; } int length = (data[IP_TOTAL_LEN] << 8) | data[IP_TOTAL_LEN + 1]; length -= 20; // ip header if(data[IP_PROTOCAL] == IP_PROTOCAL_ICMP) icmp_handle(length); else if(data[IP_PROTOCAL] == IP_PROTOCAL_TCP) tcp_handle(length); else if (data[IP_PROTOCAL] == IP_PROTOCAL_UDP) { //cprintf("udp\n"); udp_handle(length); } else cprintf("unknown protocal %x\n", data[IP_PROTOCAL]); }
void ip_handle() { int * data = ethernet_rx_data + ETHERNET_HDR_LEN; // not IPv4 or header is longer than 20bit if(data[IP_VERSION] != IP_VERSION_VAL) return; int length = (data[IP_TOTAL_LEN] << 8) | data[IP_TOTAL_LEN + 1]; length -= 20; // ip header if(data[IP_PROTOCAL] == IP_PROTOCAL_ICMP) icmp_handle(length); if(data[IP_PROTOCAL] == IP_PROTOCAL_TCP) tcp_handle(length); }
void ip_handle(int *dataHead, int length) { // kprintf("handle ip\n"); int * data = dataHead + ETHERNET_HDR_LEN; // not IPv4 or header is longer than 20bit if(data[IP_VERSION] != IP_VERSION_VAL) return; int IPlength = (data[IP_TOTAL_LEN] << 8) | data[IP_TOTAL_LEN + 1]; IPlength -= 20; // ip header if(data[IP_PROTOCAL] == IP_PROTOCAL_ICMP) icmp_handle(dataHead, IPlength); if(data[IP_PROTOCAL] == IP_PROTOCAL_TCP) tcp_handle(dataHead, IPlength); }
static int tcp_event_serve(tcp_context_t *tcp, unsigned i) { int fd = tcp->set.pfd[i].fd; int ret = tcp_handle(tcp, fd, &tcp->iov[0], &tcp->iov[1]); /* Flush per-query memory. */ mp_flush(tcp->overlay.mm->ctx); if (ret == KNOT_EOK) { /* Update socket activity timer. */ rcu_read_lock(); fdset_set_watchdog(&tcp->set, i, conf()->max_conn_idle); rcu_read_unlock(); } return ret; }
int main(int _argc, char *_argv[]) { // getaddrinfo DNS request uv::getaddrinfo gai_req; // set a getaddrinfo request callback function that will be called after the DNS resolving request complete gai_req.on_request() = [](uv::getaddrinfo _gai_req) { if (!_gai_req) { PRINT_UV_ERR("getaddrinfo", _gai_req.uv_status()); return; }; // connect request uv::connect connect_req; // set a connect request callback function that will be called when the connection is established connect_req.on_request() = [](uv::connect _connect_req) { if (!_connect_req) { PRINT_UV_ERR("connect", _connect_req.uv_status()); return; }; // the tcp handle this connect request has been run on uv::tcp tcp_handle = static_cast< uv::tcp&& >(_connect_req.handle()); // start reading from the tcp stream tcp_handle.read_start( // the callback function that will be called when a new input buffer get needed to be allocated for data read [](uv::handle, std::size_t _suggested_size) { return uv::buffer{_suggested_size}; }, // the callback function that will be called when data has been read from the stream [](uv::io _io, ssize_t _nread, uv::buffer _buf, int64_t, void*) { if (_nread < 0) { _io.read_stop(); if (_nread != UV_EOF) PRINT_UV_ERR("read", _nread); } else if (_nread > 0) { fwrite(_buf.base(), 1, _nread, stdout); fflush(stdout); }; } ); if (!tcp_handle) { PRINT_UV_ERR("read_start", tcp_handle.uv_status()); return; }; // fill in a buffer with HTTP request data uv::buffer buf; buf.base() = const_cast< char* >( "GET / HTTP/1.0\r\n" "Host: www.nyan.cat\r\n" "\r\n" ); buf.len() = std::strlen(buf.base()); // write HTTP request to the tcp stream uv::write wr; wr.on_request() = [](uv::write _wr, uv::buffer) { if (!_wr) PRINT_UV_ERR("write", _wr.uv_status()); }; wr.run(tcp_handle, buf); }; // tcp handle uv::tcp tcp_handle(uv::loop::Default()); // run the connect request on tcp handle connect_req.run(tcp_handle, *_gai_req.addrinfo()->ai_addr); }; // run getaddrinfo request gai_req.run(uv::loop::Default(), "www.nyan.cat", "80"); return uv::loop::Default().run(UV_RUN_DEFAULT); }