![]() |
io6Library
WIZnet Dual Stack TCP/IP Ethernet Controller Driver
|
WIZnet socket APIs are based on Berkeley socket APIs, thus it has much similar name and interface. But there is a little bit of difference. More...
Functions | |
| int8_t | socket (uint8_t sn, uint8_t protocol, uint16_t port, uint8_t flag) |
| Open a socket. More... | |
| int8_t | close (uint8_t sn) |
| Close a SOCKET. More... | |
| int8_t | listen (uint8_t sn) |
| Listen to a connection request from a TCP CLIENT. More... | |
| int8_t | connect (uint8_t sn, uint8_t *addr, uint16_t port, uint8_t addrlen) |
| Try to connect to a TCP SERVER. More... | |
| int8_t | disconnect (uint8_t sn) |
| Try to disconnect to the connected peer. More... | |
| datasize_t | send (uint8_t sn, uint8_t *buf, datasize_t len) |
| Send data to the connected peer. More... | |
| datasize_t | recv (uint8_t sn, uint8_t *buf, datasize_t len) |
| Receive data from the connected peer. More... | |
| datasize_t | sendto (uint8_t sn, uint8_t *buf, datasize_t len, uint8_t *addr, uint16_t port, uint8_t addrlen) |
| Send datagram to the peer specifed by destination IP address and port number passed as parameter. More... | |
| datasize_t | recvfrom (uint8_t sn, uint8_t *buf, datasize_t len, uint8_t *addr, uint16_t *port, uint8_t *addrlen) |
| Receive datagram from a peer. More... | |
| int8_t | ctlsocket (uint8_t sn, ctlsock_type cstype, void *arg) |
| Control SOCKETn. More... | |
| int8_t | setsockopt (uint8_t sn, sockopt_type sotype, void *arg) |
| Set SOCKETn options. More... | |
| int8_t | getsockopt (uint8_t sn, sockopt_type sotype, void *arg) |
| get SOCKETn options More... | |
| int16_t | peeksockmsg (uint8_t sn, uint8_t *submsg, uint16_t subsize) |
| Peeks a sub-message in SOCKETn RX buffer. More... | |
WIZnet socket APIs are based on Berkeley socket APIs, thus it has much similar name and interface. But there is a little bit of difference.
Comparison between WIZnet and Berkeley SOCKET APIs
| API | WIZnet | Berkeley |
| socket() | O | O |
| bind() | X | O |
| listen() | O | O |
| connect() | O | O |
| accept() | X | O |
| recv() | O | O |
| send() | O | O |
| recvfrom() | O | O |
| sendto() | O | O |
| closesocket() | O close() & disconnect() | O |
There are bind() and accept() functions in Berkeley SOCKET API but, not in WIZnet SOCKET API.
Because socket() of WIZnet is not only creating a SOCKET but also binding a local port number,
and listen() of WIZnet is not only listening to connection request from client but also accepting the connection request.
When you program "TCP SERVER" with Berkeley SOCKET API, you can use only one listen port.
When the listen SOCKET accepts a connection request from a client, it keeps listening.
After accepting the connection request, a new SOCKET is created and the new SOCKET is used in communication with the client.
Following figure shows network flow diagram by Berkeley SOCKET API.
<Berkeley SOCKET API> |
But, When you program "TCP SERVER" with WIZnet SOCKET API, you can use as many as 8 listen SOCKET with same port number.
Because there's no accept() in WIZnet SOCKET APIs, when the listen SOCKET accepts a connection request from a client,
it is changed in order to communicate with the client.
And the changed SOCKET is not listening any more and is dedicated for communicating with the client.
If there're many listen SOCKET with same listen port number and a client requests a connection,
the SOCKET which has the smallest SOCKET number accepts the request and is changed as communication SOCKET.
Following figure shows network flow diagram by WIZnet SOCKET API.
<WIZnet SOCKET API> |
| int8_t socket | ( | uint8_t | sn, |
| uint8_t | protocol, | ||
| uint16_t | port, | ||
| uint8_t | flag | ||
| ) |
Open a socket.
Initializes the SOCKET have the sn number open it.
| sn | SOCKET number. It should be 0 ~ _WIZCHIP_SOCK_NUM_. |
| protocol | Protocol type to operate such as TCP, UDP IPRAW, and MACRAW.
|
| port | Source port number to be binded. |
| flag | SOCKET flags are as following. It is used together with OR operation. |
Definition at line 101 of file socket.c.
References CHECK_IPZERO, CHECK_SOCKNUM, close(), getLLAR, getSIPR, setSn_MR, setSn_MR2, SF_DHA_MANUAL, SF_FORCE_ARP, SF_MULTI_ENABLE, SF_UNI_BLOCK, Sn_MR_IPRAW4, Sn_MR_IPRAW6, Sn_MR_MACRAW, Sn_MR_TCP4, Sn_MR_TCP6, Sn_MR_TCPD, Sn_MR_UDP, Sn_MR_UDP6, Sn_MR_UDPD, SOCKERR_SOCKFLAG, and SOCKERR_SOCKMODE.
Referenced by DHCP_run(), DHCP_run2(), DHCPv4_run(), loopback_tcpc(), loopback_tcps(), and loopback_udps().
| int8_t close | ( | uint8_t | sn | ) |
Close a SOCKET.
It closes the SOCKET with 'sn' passed as parameter.
| sn | SOCKET number. It should be 0 ~ _WIZCHIP_SOCK_NUM_. |
Definition at line 186 of file socket.c.
References CHECK_SOCKNUM, getSn_CR, setSn_CR, setSn_IRCLR, and Sn_CR_CLOSE.
Referenced by DHCP_stop(), DHCPv4_stop(), listen(), loopback_tcpc(), loopback_tcps(), recv(), send(), and socket().
| int8_t listen | ( | uint8_t | sn | ) |
Listen to a connection request from a TCP CLIENT.
It is listening to a connection request from a client. If connection request is accepted successfully, the connection is established.
SOCKET sn is used as passive(TCP SERVER) mode.
| sn | SOCKET number. It should be 0 ~ _WIZCHIP_SOCK_NUM_. |
Definition at line 204 of file socket.c.
References CHECK_SOCKINIT, CHECK_SOCKNUM, close(), getSn_CR, getSn_SR, setSn_CR, Sn_CR_LISTEN, SOCK_LISTEN, SOCK_OK, and SOCKERR_SOCKCLOSED.
Referenced by loopback_tcps().
| int8_t connect | ( | uint8_t | sn, |
| uint8_t * | addr, | ||
| uint16_t | port, | ||
| uint8_t | addrlen | ||
| ) |
Try to connect to a TCP SERVER.
It sends a connection-reqeust message to the server with destination IP address and port number passed as parameter.
SOCKET sn is used as active(TCP CLIENT) mode.
| sn | SOCKET number. It should be 0 ~ _WIZCHIP_SOCK_NUM_. |
| addr | Pointer variable of destination IPv6 or IPv4 address. |
| port | Destination port number. |
| addrlen | the length of addr. If addr is IPv6 address it should be 16,else if addr is IPv4 address it should be 4. Otherwize, return SOCKERR_IPINVALID. |
Definition at line 219 of file socket.c.
References CHECK_IPZERO, CHECK_SOCKINIT, CHECK_SOCKNUM, CHECK_TCPMODE, getSn_CR, getSn_MR, setSn_CR, setSn_DIP6R, setSn_DIPR, setSn_DPORTR, Sn_CR_CONNECT, Sn_CR_CONNECT6, Sn_MR_TCP6, SOCKERR_PORTZERO, and SOCKERR_SOCKMODE.
Referenced by loopback_tcpc().
| int8_t disconnect | ( | uint8_t | sn | ) |
Try to disconnect to the connected peer.
It sends disconnect-request message to the connected peer which is TCP SERVER or TCP CLIENT.
| sn | SOCKET number. It should be 0 ~ _WIZCHIP_SOCK_NUM_. |
Definition at line 266 of file socket.c.
References CHECK_SOCKNUM, CHECK_TCPMODE, getSn_CR, getSn_SR, setSn_CR, Sn_CR_DISCON, and SOCK_CLOSED.
Referenced by loopback_tcpc(), and loopback_tcps().
| datasize_t send | ( | uint8_t | sn, |
| uint8_t * | buf, | ||
| datasize_t | len | ||
| ) |
Send data to the connected peer.
It sends data to the connected peer by using TCP mode SOCKET sn.
| sn | SOCKET number. It should be 0 ~ _WIZCHIP_SOCK_NUM_. |
| buf | Pointer of data buffer to be sent. |
| len | The byte length of data in buf. |
Definition at line 289 of file socket.c.
References close(), getSn_SR, getSn_TX_FSR(), getSn_TxMAX, SOCK_CLOSE_WAIT, SOCK_CLOSED, SOCK_ESTABLISHED, and SOCKERR_SOCKSTATUS.
Referenced by loopback_tcpc(), and loopback_tcps().
| datasize_t recv | ( | uint8_t | sn, |
| uint8_t * | buf, | ||
| datasize_t | len | ||
| ) |
Receive data from the connected peer.
It can read data received from the connected peer by using TCP mode SOCKET sn.
| sn | SOCKET number. It should be 0 ~ _WIZCHIP_SOCK_NUM_. |
| buf | Pointer buffer to read the received data. |
| len | The max data length of data in buf. |
Definition at line 338 of file socket.c.
References close(), getSn_RX_RSR(), getSn_RxMAX, getSn_SR, SOCK_CLOSE_WAIT, SOCK_CLOSED, SOCK_ESTABLISHED, and SOCKERR_SOCKSTATUS.
Referenced by loopback_tcpc(), and loopback_tcps().
| datasize_t sendto | ( | uint8_t | sn, |
| uint8_t * | buf, | ||
| datasize_t | len, | ||
| uint8_t * | addr, | ||
| uint16_t | port, | ||
| uint8_t | addrlen | ||
| ) |
Send datagram to the peer specifed by destination IP address and port number passed as parameter.
It sends datagram data by using UDP,IPRAW, or MACRAW mode SOCKET.
| sn | SOCKET number. It should be 0 ~ _WIZCHIP_SOCK_NUM_. |
| buf | Pointer of data buffer to be sent. |
| len | The byte length of data in buf. |
| addr | Pointer variable of destination IPv6 or IPv4 address. |
| port | Destination port number. |
| addrlen | the length of addr. If addr is IPv6 address it should be 16,else if addr is IPv4 address it should be 4. Otherwize, return SOCKERR_IPINVALID. |
Definition at line 372 of file socket.c.
References getSn_MR, getSn_SR, getSn_TX_FSR(), getSn_TxMAX, setSn_DIP6R, setSn_DIPR, setSn_DPORTR, Sn_CR_SEND, Sn_CR_SEND6, Sn_MR_IPRAW6, Sn_MR_MACRAW, Sn_MR_UDP6, SOCK_CLOSED, SOCKERR_IPINVALID, SOCKERR_PORTZERO, SOCKERR_SOCKCLOSED, and SOCKERR_SOCKMODE.
Referenced by check_DHCPv4_leasedIP(), loopback_udps(), send_DHCP_INFOREQ(), send_DHCP_REQUEST(), send_DHCP_SOLICIT(), send_DHCPv4_DECLINE(), send_DHCPv4_DISCOVER(), and send_DHCPv4_REQUEST().
| datasize_t recvfrom | ( | uint8_t | sn, |
| uint8_t * | buf, | ||
| datasize_t | len, | ||
| uint8_t * | addr, | ||
| uint16_t * | port, | ||
| uint8_t * | addrlen | ||
| ) |
Receive datagram from a peer.
It can read a data received from a peer by using UDP, IPRAW, or MACRAW mode SOCKET.
| sn | SOCKET number. It should be 0 ~ _WIZCHIP_SOCK_NUM_. |
| buf | Pointer buffer to be saved the received data. |
| len | The max read data length. When the received packet size <= len, it can read data as many as the packet size. When others, it can read data as many as len and remain to the rest data of the packet. |
| addr | Pointer variable of destination IP address. It is valid only when recvfrom() is first called for receiving the datagram packet. You can check it valid or not through PACK_FIRST. You can get it through getsockopt(sn, SO_PACKINFO, &packinfo). In UDP4, IPRAW mode SOCKET, it should be allocated over 4bytes. In UDP6, UDPD mode SOCKET, it should be allocated over 16bytes. |
| port | Pointer variable of destination port number. It is valid only when recvfrom() is first called for receiving the datagram packet, same as port case. |
| addrlen | The byte length of destination IP address. It is valid only when recvfrom() is first called for receiving the datagram packet, same as port case. When the destination has a IPv4 address, it is set to 4. when the destination has a IPv6 address, it is set to 16. |
Definition at line 441 of file socket.c.
Referenced by loopback_udps(), parseDHCPMSG(), and parseDHCPv4MSG().
| int8_t ctlsocket | ( | uint8_t | sn, |
| ctlsock_type | cstype, | ||
| void * | arg | ||
| ) |
Control SOCKETn.
Control IO mode, Interrupt & Mask of SOCKETn and get the SOCKETn buffer information. Refer to ctlsock_type.
| sn | socket number | |||||||||||||||
| cstype | type of control SOCKETn. refer to ctlsock_type. | |||||||||||||||
| arg | Data type and value is determined according to ctlsock_type.
|
Definition at line 528 of file socket.c.
References CHECK_SOCKNUM, CS_SET_IOMODE, and SOCK_IO_NONBLOCK.
Referenced by loopback_tcpc(), and loopback_tcps().
| int8_t setsockopt | ( | uint8_t | sn, |
| sockopt_type | sotype, | ||
| void * | arg | ||
| ) |
Set SOCKETn options.
Set SOCKETn option like as TTL, MSS, TOS, and so on. Refer to sockopt_type.
| sn | SOCKET number | ||||||||||||||||||||||||
| sotype | SOCKET option type. refer to sockopt_type | ||||||||||||||||||||||||
| arg | Data type and value is determined according to sotype.
|
Definition at line 576 of file socket.c.
References CHECK_SOCKNUM, CHECK_TCPMODE, getSn_CR, getSn_IR, getSn_KPALVTR, setSn_CR, setSn_DIP6R, setSn_DIPR, setSn_DPORTR, setSn_IRCLR, setSn_KPALVTR, setSn_MSSR, setSn_TOSR, setSn_TTLR, Sn_CR_SEND_KEEP, Sn_IR_TIMEOUT, SO_DESTIP, SO_DESTPORT, SO_KEEPALIVEAUTO, SO_KEEPALIVESEND, SO_MSS, SO_TOS, SO_TTL, SOCK_OK, SOCKERR_ARG, SOCKERR_SOCKOPT, and SOCKERR_TIMEOUT.
| int8_t getsockopt | ( | uint8_t | sn, |
| sockopt_type | sotype, | ||
| void * | arg | ||
| ) |
get SOCKETn options
Get SOCKETn option like as FLAG, TTL, MSS, and so on. Refer to sockopt_type
| sn | SOCKET number | |||||||||||||||||||||||||||||||||||||||
| sotype | SOCKET option type. refer to sockopt_type | |||||||||||||||||||||||||||||||||||||||
| arg | Data type and value is determined according to sotype.
|
Definition at line 620 of file socket.c.
References CHECK_SOCKNUM, getSn_MR, getSn_MR2, and SO_FLAG.
Referenced by loopback_tcpc(), loopback_tcps(), and loopback_udps().
| int16_t peeksockmsg | ( | uint8_t | sn, |
| uint8_t * | submsg, | ||
| uint16_t | subsize | ||
| ) |
Peeks a sub-message in SOCKETn RX buffer.
It peeks the incoming message of SOCKETn RX buffer.
It can find the specified sub-message in the incoming message and return the length of incoming message before the sub-message.
It is useful when you need to read each messages from multiple message in SOCKET RX buffer.
| sn | SOCKET number |
| submsg | sub-message pointer to find |
| subsize | the length of submsg |
Definition at line 689 of file socket.c.
References getSn_RX_RD, getSn_RX_RSR(), WIZCHIP_OFFSET_INC, WIZCHIP_READ(), and WIZCHIP_RXBUF_BLOCK.
1.8.17