Path: blob/a-new-beginning/SharedDependencies/Sources/libslirp/include/slirp.h
2 views
/* SPDX-License-Identifier: BSD-3-Clause */1#ifndef SLIRP_H2#define SLIRP_H34#ifdef _WIN3256/* as defined in sdkddkver.h */7#ifndef _WIN32_WINNT8#define _WIN32_WINNT 0x0601 /* Windows 7 */9#endif10/* reduces the number of implicitly included headers */11#ifndef WIN32_LEAN_AND_MEAN12#define WIN32_LEAN_AND_MEAN13#endif1415#include <winsock2.h>16#include <windows.h>17#include <ws2tcpip.h>18#include <sys/timeb.h>19#include <iphlpapi.h>2021#else22#define O_BINARY 023#endif2425#ifndef _WIN3226#include <sys/uio.h>27#include <netinet/in.h>28#include <arpa/inet.h>29#include <sys/socket.h>30#include <sys/ioctl.h>31#endif3233#ifdef __APPLE__34#include <sys/filio.h>35#endif3637#include "debug.h"38#include "util.h"3940#include "libslirp.h"41#include <netinet/ip.h>42#include "ip.h"43#include "ip6.h"44#include "tcp.h"45#include "tcp_timer.h"46#include "tcp_var.h"47#include "tcpip.h"48#include <netinet/udp.h>49#include "udp.h"50#include <netinet/ip_icmp.h>51#include "ip_icmp.h"52#include "ip6_icmp.h"53#include "mbuf.h"54#include "sbuf.h"55#include "socket.h"56#include "if.h"57#include "main.h"58#include "misc.h"5960#include "bootp.h"61#include "tftp.h"6263#define ARPOP_REQUEST 1 /* ARP request */64#define ARPOP_REPLY 2 /* ARP reply */6566struct ethhdr {67unsigned char h_dest[ETH_ALEN]; /* destination eth addr */68unsigned char h_source[ETH_ALEN]; /* source ether addr */69unsigned short h_proto; /* packet type ID field */70};7172SLIRP_PACKED_BEGIN73struct slirp_arphdr {74unsigned short ar_hrd; /* format of hardware address */75unsigned short ar_pro; /* format of protocol address */76unsigned char ar_hln; /* length of hardware address */77unsigned char ar_pln; /* length of protocol address */78unsigned short ar_op; /* ARP opcode (command) */7980/*81* Ethernet looks like this : This bit is variable sized however...82*/83uint8_t ar_sha[ETH_ALEN]; /* sender hardware address */84uint32_t ar_sip; /* sender IP address */85uint8_t ar_tha[ETH_ALEN]; /* target hardware address */86uint32_t ar_tip; /* target IP address */87} SLIRP_PACKED_END;8889#define ARP_TABLE_SIZE 169091typedef struct ArpTable {92struct slirp_arphdr table[ARP_TABLE_SIZE];93int next_victim;94} ArpTable;9596/* Add a new ARP entry for the given addresses */97void arp_table_add(Slirp *slirp, uint32_t ip_addr,98const uint8_t ethaddr[ETH_ALEN]);99100/* Look for an ARP entry for the given IP address */101bool arp_table_search(Slirp *slirp, uint32_t ip_addr,102uint8_t out_ethaddr[ETH_ALEN]);103104struct ndpentry {105uint8_t eth_addr[ETH_ALEN]; /* sender hardware address */106struct in6_addr ip_addr; /* sender IP address */107};108109#define NDP_TABLE_SIZE 16110111typedef struct NdpTable {112struct ndpentry table[NDP_TABLE_SIZE];113/*114* The table is a cache with old entries overwritten when the table fills.115* Preserve the first entry: it is the guest, which is needed for lazy116* hostfwd guest address assignment.117*/118struct in6_addr guest_in6_addr;119int next_victim;120} NdpTable;121122/* Add a new NDP entry for the given addresses */123void ndp_table_add(Slirp *slirp, struct in6_addr ip_addr,124uint8_t ethaddr[ETH_ALEN]);125126/* Look for an NDP entry for the given IPv6 address */127bool ndp_table_search(Slirp *slirp, struct in6_addr ip_addr,128uint8_t out_ethaddr[ETH_ALEN]);129130/* Slirp configuration, specified by the application */131struct Slirp {132int cfg_version;133134unsigned time_fasttimo;135unsigned last_slowtimo;136bool do_slowtimo;137138bool in_enabled, in6_enabled;139140/* virtual network configuration */141struct in_addr vnetwork_addr;142struct in_addr vnetwork_mask;143struct in_addr vhost_addr;144struct in6_addr vprefix_addr6;145uint8_t vprefix_len;146struct in6_addr vhost_addr6;147bool disable_dhcp; /* slirp will not reply to any DHCP requests */148struct in_addr vdhcp_startaddr;149struct in_addr vnameserver_addr;150struct in6_addr vnameserver_addr6;151152struct in_addr client_ipaddr;153char client_hostname[33];154155int restricted;156struct gfwd_list *guestfwd_list;157158int if_mtu;159int if_mru;160161bool disable_host_loopback;162163uint32_t mfr_id;164uint8_t oob_eth_addr[ETH_ALEN];165166/* mbuf states */167struct slirp_quehead m_freelist;168struct slirp_quehead m_usedlist;169int mbuf_alloced;170171/* if states */172struct slirp_quehead if_fastq; /* fast queue (for interactive data) */173struct slirp_quehead if_batchq; /* queue for non-interactive data */174bool if_start_busy; /* avoid if_start recursion */175176/* ip states */177struct ipq ipq; /* ip reass. queue */178uint16_t ip_id; /* ip packet ctr, for ids */179180/* bootp/dhcp states */181BOOTPClient bootp_clients[NB_BOOTP_CLIENTS];182char *bootp_filename;183size_t vdnssearch_len;184uint8_t *vdnssearch;185char *vdomainname;186187/* tcp states */188struct socket tcb;189struct socket *tcp_last_so;190tcp_seq tcp_iss; /* tcp initial send seq # */191uint32_t tcp_now; /* for RFC 1323 timestamps */192193/* udp states */194struct socket udb;195struct socket *udp_last_so;196197/* icmp states */198struct socket icmp;199struct socket *icmp_last_so;200201/* tftp states */202char *tftp_prefix;203struct tftp_session tftp_sessions[TFTP_SESSIONS_MAX];204char *tftp_server_name;205206ArpTable arp_table;207NdpTable ndp_table;208209GRand *grand;210void *ra_timer;211212bool enable_emu;213214const SlirpCb *cb;215void *opaque;216217struct sockaddr_in *outbound_addr;218struct sockaddr_in6 *outbound_addr6;219bool disable_dns; /* slirp will not redirect/serve any DNS packet */220};221222/*223* Send one packet from each session.224* If there are packets on the fastq, they are sent FIFO, before225* everything else. Then we choose the first packet from each226* batchq session (socket) and send it.227* For example, if there are 3 ftp sessions fighting for bandwidth,228* one packet will be sent from the first session, then one packet229* from the second session, then one packet from the third.230*/231void if_start(Slirp *);232233/* Get the address of the DNS server on the host side */234int get_dns_addr(struct in_addr *pdns_addr);235236/* Get the IPv6 address of the DNS server on the host side */237int get_dns6_addr(struct in6_addr *pdns6_addr, uint32_t *scope_id);238239/* ncsi.c */240241/* Process NCSI packet coming from the guest */242void ncsi_input(Slirp *slirp, const uint8_t *pkt, int pkt_len);243244#ifndef _WIN32245#include <netdb.h>246#endif247248/* Whether we should send TCP keepalive packets */249extern bool slirp_do_keepalive;250251#define TCP_MAXIDLE (TCPTV_KEEPCNT * TCPTV_KEEPINTVL)252253/* dnssearch.c */254/* Translate from vdnssearch in configuration, into Slirp */255int translate_dnssearch(Slirp *s, const char **names);256257/* cksum.c */258/* Compute the checksum of the mbuf */259int cksum(struct mbuf *m, int len);260/* Compute the checksum of the mbuf which contains an IPv6 packet */261int ip6_cksum(struct mbuf *m);262263/* if.c */264/* Called from slirp_new */265void if_init(Slirp *);266/* Queue packet into an output queue (fast or batch), for sending to the guest */267void if_output(struct socket *, struct mbuf *);268269/* ip_input.c */270/* Called from slirp_new */271void ip_init(Slirp *);272/* Called from slirp_cleanup */273void ip_cleanup(Slirp *);274/* Process IPv4 packet coming from the guest */275void ip_input(struct mbuf *);276/*277* IP timer processing;278* if a timer expires on a reassembly279* queue, discard it.280*/281void ip_slowtimo(Slirp *);282/*283* Strip out IP options, at higher284* level protocol in the kernel.285*/286void ip_stripoptions(register struct mbuf *);287288/* ip_output.c */289/* Send IPv4 packet to the guest */290int ip_output(struct socket *, struct mbuf *);291292/* ip6_input.c */293/* Called from slirp_new, but after other initialization */294void ip6_post_init(Slirp *);295/* Called from slirp_cleanup */296void ip6_cleanup(Slirp *);297/* Process IPv6 packet coming from the guest */298void ip6_input(struct mbuf *);299300/* ip6_output */301/* Send IPv6 packet to the guest */302int ip6_output(struct socket *, struct mbuf *, int fast);303304/* tcp_input.c */305/* Process TCP datagram coming from the guest */306void tcp_input(register struct mbuf *, int, struct socket *, unsigned short af);307/* Determine a reasonable value for maxseg size */308int tcp_mss(register struct tcpcb *, unsigned offer);309310/* tcp_output.c */311/* Send TCP datagram to the guest */312int tcp_output(register struct tcpcb *);313/* Start/restart persistence timer */314void tcp_setpersist(register struct tcpcb *);315316/* tcp_subr.c */317/* Called from slirp_new */318void tcp_init(Slirp *);319/* Called from slirp_cleanup */320void tcp_cleanup(Slirp *);321/*322* Create template to be used to send tcp packets on a connection.323* Call after host entry created, fills324* in a skeletal tcp/ip header, minimizing the amount of work325* necessary when the connection is used.326*/327void tcp_template(struct tcpcb *);328/*329* Send a single message to the TCP at address specified by330* the given TCP/IP header.331*/332void tcp_respond(struct tcpcb *, register struct tcpiphdr *,333register struct mbuf *, tcp_seq, tcp_seq, int, unsigned short);334/*335* Create a new TCP control block, making an336* empty reassembly queue and hooking it to the argument337* protocol control block.338*/339struct tcpcb *tcp_newtcpcb(struct socket *);340/*341* Close a TCP control block:342* discard all space held by the tcp343* discard internet protocol block344* wake up any sleepers345*/346struct tcpcb *tcp_close(register struct tcpcb *);347/* The Internet socket got closed, tell the guest */348void tcp_sockclosed(struct tcpcb *);349/*350* Connect to a host on the Internet351* Called by tcp_input352*/353int tcp_fconnect(struct socket *, unsigned short af);354/* Accept the connection from the Internet, and connect to the guest */355void tcp_connect(struct socket *);356/* Attach a TCPCB to a socket */357void tcp_attach(struct socket *);358/* * Return TOS according to the ports */359uint8_t tcp_tos(struct socket *);360/*361* We received a packet from the guest.362*363* Emulate programs that try and connect to us364* This includes ftp (the data connection is365* initiated by the server) and IRC (DCC CHAT and366* DCC SEND) for now367*/368int tcp_emu(struct socket *, struct mbuf *);369/* Configure the socket, now that the guest completed accepting the connection */370int tcp_ctl(struct socket *);371/*372* Drop a TCP connection, reporting373* the specified error. If connection is synchronized,374* then send a RST to peer.375*/376struct tcpcb *tcp_drop(struct tcpcb *tp, int err);377378/* Find the socket for the guest address and port */379struct socket *slirp_find_ctl_socket(Slirp *slirp, struct in_addr guest_addr,380int guest_port);381382/* Send a frame to the virtual Ethernet board, i.e. call the application send_packet callback */383void slirp_send_packet_all(Slirp *slirp, const void *buf, size_t len);384385/* Create a new timer, i.e. call the application timer_new callback */386void *slirp_timer_new(Slirp *slirp, SlirpTimerId id, void *cb_opaque);387388#endif389390391