|
Functions | |
| __PSOCKET | __socketCreate (u8 type, u16 port) |
| Creates a socket. | |
| i32 | __socketDestroy (__PSOCKET socket) |
| Destroys a socket. | |
| __PSOCKET | __socketFind (u8 proto, u16 local_port, u16 remote_port, u32 remote_addr) |
| Finds a socket. | |
| i32 | __socketClose (__PSOCKET socket) |
| Closes a socket. | |
| i32 | __socketSendTo (__PSOCKET socket, u32 addr, u16 port, __PVOID buf, u32 qty) |
| Sends data to a specific remote end. Only for UDP sockets. | |
| i32 | __socketRecvFrom (__PSOCKET socket, u32 *addr, u16 *port, __PVOID buf, u32 qty) |
| Reads data from a socket retrieving the address and port of the sender. | |
| i32 | __socketListen (__PSOCKET socket, u16 port) |
| Waits for incoming connections. | |
| i32 | __socketConnect (__PSOCKET socket, u32 addr, u16 port) |
| Tries to establish a connection with a remote host. | |
| i32 | __socketBind (__PSOCKET socket, u32 addr) |
| Bind a socket to a port. | |
| i32 | __socketRecv (__PSOCKET socket, __PVOID buf, u32 qty) |
| Reads data from a TCP socket. | |
| i32 | __socketSend (__PSOCKET socket, __PVOID buf, u32 qty) |
| Sends data through a TCP socket. | |
| __PSOCKET | __socketAccept (__PSOCKET socket, u32 *addr) |
| Accepts an incoming connection. | |
| i32 | __socketSetSockOpt (__PSOCKET socket, i16 optname, __PVOID optval, u32 optlen) |
| Sets socket options. | |
| __PSOCKET __socketCreate | ( | u8 | type, |
| u16 | port | ||
| ) |
Creates a socket.
Call this function to create a socket. The returned pointer can be used with the rest of socket or device functions.
| type | Type of socket. One of the following values.
|
| port | Port of the socket. Can be zero, in this case the function will choose an appropriate port. |
Definition at line 413 of file socket.c.
{
__PSOCKET sock;
__PSOCKET_PDB pdb;
__EVENT *rxev;
__EVENT *txev;
u8* buffer;
if (type != __SOCK_TYPE_TCP && type != __SOCK_TYPE_UDP) return __NULL;
/* Check if the port is already opened by another socket */
pdb = __sock_list;
while (pdb)
{
if (pdb->local_port == port) return __NULL;
pdb = pdb->next;
}
/* Dynamically allocate a buffer for our socket, TX-RX event, and PDB */
buffer = __heapAllocZero(sizeof(__SOCKET) +
sizeof(__EVENT) + sizeof(__EVENT) +
sizeof(__SOCKET_PDB));
if (!buffer) return __NULL;
/* Pointers */
sock = (__PSOCKET) buffer;
pdb = (__PSOCKET_PDB) ((u8*) sock + sizeof(__SOCKET));
rxev = (__EVENT*) ((u8*) pdb + sizeof(__SOCKET_PDB));
txev = (__EVENT*) ((u8*) rxev + sizeof(__EVENT));
/* Assign events */
sock->dv_rxev = rxev;
sock->dv_txev = txev;
/* Assign device functions */
sock->dv_init = __sockInit;
sock->dv_deinit = __sockDeinit;
sock->dv_flush = __sockFlush;
sock->dv_open = __sockOpen;
sock->dv_read = __sockRead;
sock->dv_write = __sockWrite;
sock->dv_ioctl = __sockIOCtl;
sock->dv_size = __sockSize;
sock->dv_close = __sockClose;
/* Configure PDB */
pdb->type = type;
pdb->sock = sock;
pdb->rxsize = 10; /* TODO Configurable RX buffer size */
if (port == 0)
{
port = __sock_last_port;
__sock_last_port++;
}
pdb->local_port = __htons(port);
if (type == __SOCK_TYPE_UDP) pdb->rxtmo = 0;
sock->dv_pdb = pdb;
__sock_count++;
/* Name */
__strFmt(sock->dv_name, "sock%lu", __sock_count);
/* Call initialize */
if (__deviceInit(sock, __NULL, 0) != __DEV_OK)
{
__heapFree(buffer);
return __NULL;
}
if (pdb->type == __SOCK_TYPE_UDP)
{
__deviceOpen(sock, 0);
}
__systemDisableScheduler();
pdb->next = __sock_list;
__sock_list = pdb;
__systemEnableScheduler();
/* Add our socket to the available devices list */
__deviceAdd(sock, 1);
return sock;
}

| i32 __socketDestroy | ( | __PSOCKET | socket | ) |
Destroys a socket.
The unsent/unread that will be deallocated and lost.
| socket | Pointer to a socket. |
Definition at line 512 of file socket.c.
{
__PSOCKET_PDB list;
__PSOCKET_PDB pdb = socket->dv_pdb;
/* Remove the socket from the devices list */
__deviceRemove(socket);
/* Call de-initialize */
__deviceDeinit(socket);
/* Remove the socket from the linked list */
__systemDisableScheduler();
list = __sock_list;
if (list == pdb)
{
__sock_list = pdb->next;
} else {
while (list)
{
if (list->next == pdb)
{
list->next = pdb->next;
break;
}
list = list->next;
}
}
__systemEnableScheduler();
if (__eventGetWaitingThreads(socket->dv_rxev))
{
__eventAbort(socket->dv_rxev);
}
if (__eventGetWaitingThreads(socket->dv_txev))
{
__eventAbort(socket->dv_txev);
}
/* Freeing the socket will also free the PDB and the events */
__heapFree(socket);
return 0;
}

| __PSOCKET __socketFind | ( | u8 | proto, |
| u16 | local_port, | ||
| u16 | remote_port, | ||
| u32 | remote_addr | ||
| ) |
Finds a socket.
| proto | Type of protocol.
|
| local_port | Local socket port. |
| remote_port | Remote socket port. |
| remote_addr | Remote address. |
Definition at line 573 of file socket.c.
{
__PSOCKET_PDB pdb = __sock_list;
if (proto == __PROTO_UDP)
{
while (pdb)
{
if (pdb->local_port == local_port) return pdb->sock;
pdb = pdb->next;
}
} else if (proto == __PROTO_TCP)
{
/* TODO find TCP socket */
}
return __NULL;
}
| i32 __socketClose | ( | __PSOCKET | socket | ) |
Closes a socket.
| socket | Pointer to a socket. |
Definition at line 599 of file socket.c.
{
return __deviceClose(socket);
}

| i32 __socketSendTo | ( | __PSOCKET | socket, |
| u32 | addr, | ||
| u16 | port, | ||
| __PVOID | buf, | ||
| u32 | qty | ||
| ) |
Sends data to a specific remote end. Only for UDP sockets.
| socket | Pointer to a socket created with __socketCreate(). |
| addr | Remote IP address (in network byte order). |
| port | Remote port (in host byte order). |
| buf | Pointer to a buffer containing the data to be sent. |
| qty | Quantity of data to be sent, in bytes. |
Definition at line 615 of file socket.c.
{
__PSOCKET_PDB pd = socket->dv_pdb;
pd->tx_dst_addr.addr_type = __NET_ADDR_IP;
pd->tx_dst_addr.type.ip = addr;
pd->tx_dst_port = __htons(port);
return __deviceWrite(socket, buf, qty);
}

| i32 __socketRecvFrom | ( | __PSOCKET | socket, |
| u32 * | addr, | ||
| u16 * | port, | ||
| __PVOID | buf, | ||
| u32 | qty | ||
| ) |
Reads data from a socket retrieving the address and port of the sender.
| socket | Pointer to a socket created with __socketCreate(). |
| addr | Pointer to a 32-bit value to receive the remote address. |
| port | Pointer to a 16-bit value to receive the remote port. |
| buf | Pointer to a buffer containing the data to be sent. |
| qty | Quantity of data to be read. |
Definition at line 637 of file socket.c.
{
i32 ret;
__PSOCKET_PDB pd = socket->dv_pdb;
ret = __deviceRead(socket, buf, qty);
if (ret > 0)
{
if (addr) *addr = __ntohl(pd->rx_src_addr.type.ip);
if (port) *port = __ntohs(pd->rx_src_port);
}
return ret;
}

| i32 __socketListen | ( | __PSOCKET | socket, |
| u16 | port | ||
| ) |
| i32 __socketConnect | ( | __PSOCKET | socket, |
| u32 | addr, | ||
| u16 | port | ||
| ) |
| i32 __socketBind | ( | __PSOCKET | socket, |
| u32 | addr | ||
| ) |
| i32 __socketRecv | ( | __PSOCKET | socket, |
| __PVOID | buf, | ||
| u32 | qty | ||
| ) |
| i32 __socketSend | ( | __PSOCKET | socket, |
| __PVOID | buf, | ||
| u32 | qty | ||
| ) |
| __PSOCKET __socketAccept | ( | __PSOCKET | socket, |
| u32 * | addr | ||
| ) |
| i32 __socketSetSockOpt | ( | __PSOCKET | socket, |
| i16 | optname, | ||
| __PVOID | optval, | ||
| u32 | optlen | ||
| ) |
Sets socket options.
| socket | Pointer to a socket created with __socketCreate(). |
| optname | Option, one of the following values:
|
| optval | Pointer to the option values. Depends on the optname parameter value. |
| optlen | Length of the optval parameter value. |
Definition at line 731 of file socket.c.
{
if (!socket) return -1;
switch (optname)
{
case __SOCK_SO_RCVBUF:
if (!optval || optlen != 4) return -1;
return __deviceIOCtl(socket, __IOCTL_SET_RXBUF_SIZE, (u32) *((u32*) optval), __NULL, 0);
case __SOCK_SO_SNDTIMEO:
if (!optval || optlen != 4) return -1;
return __deviceIOCtl(socket, __IOCTL_SETTXTIMEOUT, (u16) *((u32*) optval), __NULL, 0);
case __SOCK_SO_RCVTIMEO:
if (!optval || optlen != 4) return -1;
return __deviceIOCtl(socket, __IOCTL_SETRXTIMEOUT, (u16) *((u32*) optval), __NULL, 0);
}
return -1;
}
