HelenOS sources
This source file includes following definitions.
- udp_callback_create
- udp_create
- udp_destroy
- udp_assoc_create
- udp_assoc_destroy
- udp_assoc_set_nolocal
- udp_assoc_send_msg
- udp_assoc_userptr
- udp_rmsg_size
- udp_rmsg_read
- udp_rmsg_remote_ep
- udp_rerr_type
- udp_rerr_code
- udp_rmsg_info
- udp_rmsg_discard
- udp_assoc_get
- udp_ev_data
- udp_cb_conn
#include <errno.h>
#include <inet/endpoint.h>
#include <inet/udp.h>
#include <ipc/services.h>
#include <ipc/udp.h>
#include <loc.h>
#include <stdlib.h>
static void udp_cb_conn(ipc_call_t *, void *);
static errno_t udp_callback_create(udp_t *udp)
{
async_exch_t *exch = async_exchange_begin(udp->sess);
aid_t req = async_send_0(exch, UDP_CALLBACK_CREATE, NULL);
port_id_t port;
errno_t rc = async_create_callback_port(exch, INTERFACE_UDP_CB, 0, 0,
udp_cb_conn, udp, &port);
async_exchange_end(exch);
if (rc != EOK)
return rc;
errno_t retval;
async_wait_for(req, &retval);
return retval;
}
errno_t udp_create(udp_t **rudp)
{
udp_t *udp;
service_id_t udp_svcid;
errno_t rc;
udp = calloc(1, sizeof(udp_t));
if (udp == NULL) {
rc = ENOMEM;
goto error;
}
list_initialize(&udp->assoc);
fibril_mutex_initialize(&udp->lock);
fibril_condvar_initialize(&udp->cv);
rc = loc_service_get_id(SERVICE_NAME_UDP, &udp_svcid,
IPC_FLAG_BLOCKING);
if (rc != EOK) {
rc = EIO;
goto error;
}
udp->sess = loc_service_connect(udp_svcid, INTERFACE_UDP,
IPC_FLAG_BLOCKING);
if (udp->sess == NULL) {
rc = EIO;
goto error;
}
rc = udp_callback_create(udp);
if (rc != EOK) {
rc = EIO;
goto error;
}
*rudp = udp;
return EOK;
error:
free(udp);
return rc;
}
void udp_destroy(udp_t *udp)
{
if (udp == NULL)
return;
async_hangup(udp->sess);
fibril_mutex_lock(&udp->lock);
while (!udp->cb_done)
fibril_condvar_wait(&udp->cv, &udp->lock);
fibril_mutex_unlock(&udp->lock);
free(udp);
}
errno_t udp_assoc_create(udp_t *udp, inet_ep2_t *epp, udp_cb_t *cb, void *arg,
udp_assoc_t **rassoc)
{
async_exch_t *exch;
udp_assoc_t *assoc;
ipc_call_t answer;
assoc = calloc(1, sizeof(udp_assoc_t));
if (assoc == NULL)
return ENOMEM;
exch = async_exchange_begin(udp->sess);
aid_t req = async_send_0(exch, UDP_ASSOC_CREATE, &answer);
errno_t rc = async_data_write_start(exch, (void *)epp,
sizeof(inet_ep2_t));
async_exchange_end(exch);
if (rc != EOK) {
errno_t rc_orig;
async_wait_for(req, &rc_orig);
if (rc_orig != EOK)
rc = rc_orig;
goto error;
}
async_wait_for(req, &rc);
if (rc != EOK)
goto error;
assoc->udp = udp;
assoc->id = ipc_get_arg1(&answer);
assoc->cb = cb;
assoc->cb_arg = arg;
list_append(&assoc->ludp, &udp->assoc);
*rassoc = assoc;
return EOK;
error:
free(assoc);
return (errno_t) rc;
}
void udp_assoc_destroy(udp_assoc_t *assoc)
{
async_exch_t *exch;
if (assoc == NULL)
return;
list_remove(&assoc->ludp);
exch = async_exchange_begin(assoc->udp->sess);
errno_t rc = async_req_1_0(exch, UDP_ASSOC_DESTROY, assoc->id);
async_exchange_end(exch);
free(assoc);
(void) rc;
}
errno_t udp_assoc_set_nolocal(udp_assoc_t *assoc)
{
async_exch_t *exch;
exch = async_exchange_begin(assoc->udp->sess);
errno_t rc = async_req_1_0(exch, UDP_ASSOC_SET_NOLOCAL, assoc->id);
async_exchange_end(exch);
return rc;
}
errno_t udp_assoc_send_msg(udp_assoc_t *assoc, inet_ep_t *dest, void *data,
size_t bytes)
{
async_exch_t *exch;
inet_ep_t ddest;
exch = async_exchange_begin(assoc->udp->sess);
aid_t req = async_send_1(exch, UDP_ASSOC_SEND_MSG, assoc->id, NULL);
if (dest == NULL) {
inet_ep_init(&ddest);
dest = &ddest;
}
errno_t rc = async_data_write_start(exch, (void *)dest,
sizeof(inet_ep_t));
if (rc != EOK) {
async_exchange_end(exch);
async_forget(req);
return rc;
}
rc = async_data_write_start(exch, data, bytes);
if (rc != EOK) {
async_forget(req);
return rc;
}
async_exchange_end(exch);
if (rc != EOK) {
async_forget(req);
return rc;
}
async_wait_for(req, &rc);
return rc;
}
void *udp_assoc_userptr(udp_assoc_t *assoc)
{
return assoc->cb_arg;
}
size_t udp_rmsg_size(udp_rmsg_t *rmsg)
{
return rmsg->size;
}
errno_t udp_rmsg_read(udp_rmsg_t *rmsg, size_t off, void *buf, size_t bsize)
{
async_exch_t *exch;
ipc_call_t answer;
exch = async_exchange_begin(rmsg->udp->sess);
aid_t req = async_send_1(exch, UDP_RMSG_READ, off, &answer);
errno_t rc = async_data_read_start(exch, buf, bsize);
async_exchange_end(exch);
if (rc != EOK) {
async_forget(req);
return rc;
}
errno_t retval;
async_wait_for(req, &retval);
if (retval != EOK) {
return retval;
}
return EOK;
}
void udp_rmsg_remote_ep(udp_rmsg_t *rmsg, inet_ep_t *ep)
{
*ep = rmsg->remote_ep;
}
uint8_t udp_rerr_type(udp_rerr_t *rerr)
{
return 0;
}
uint8_t udp_rerr_code(udp_rerr_t *rerr)
{
return 0;
}
static errno_t udp_rmsg_info(udp_t *udp, udp_rmsg_t *rmsg)
{
async_exch_t *exch;
inet_ep_t ep;
ipc_call_t answer;
exch = async_exchange_begin(udp->sess);
aid_t req = async_send_0(exch, UDP_RMSG_INFO, &answer);
errno_t rc = async_data_read_start(exch, &ep, sizeof(inet_ep_t));
async_exchange_end(exch);
if (rc != EOK) {
async_forget(req);
return rc;
}
errno_t retval;
async_wait_for(req, &retval);
if (retval != EOK)
return retval;
rmsg->udp = udp;
rmsg->assoc_id = ipc_get_arg1(&answer);
rmsg->size = ipc_get_arg2(&answer);
rmsg->remote_ep = ep;
return EOK;
}
static errno_t udp_rmsg_discard(udp_t *udp)
{
async_exch_t *exch;
exch = async_exchange_begin(udp->sess);
errno_t rc = async_req_0_0(exch, UDP_RMSG_DISCARD);
async_exchange_end(exch);
return rc;
}
static errno_t udp_assoc_get(udp_t *udp, sysarg_t id, udp_assoc_t **rassoc)
{
list_foreach(udp->assoc, ludp, udp_assoc_t, assoc) {
if (assoc->id == id) {
*rassoc = assoc;
return EOK;
}
}
return EINVAL;
}
static void udp_ev_data(udp_t *udp, ipc_call_t *icall)
{
udp_rmsg_t rmsg;
udp_assoc_t *assoc;
errno_t rc;
while (true) {
rc = udp_rmsg_info(udp, &rmsg);
if (rc != EOK) {
break;
}
rc = udp_assoc_get(udp, rmsg.assoc_id, &assoc);
if (rc != EOK) {
continue;
}
if (assoc->cb != NULL && assoc->cb->recv_msg != NULL)
assoc->cb->recv_msg(assoc, &rmsg);
rc = udp_rmsg_discard(udp);
if (rc != EOK) {
break;
}
}
async_answer_0(icall, EOK);
}
static void udp_cb_conn(ipc_call_t *icall, void *arg)
{
udp_t *udp = (udp_t *)arg;
while (true) {
ipc_call_t call;
async_get_call(&call);
if (!ipc_get_imethod(&call)) {
async_answer_0(&call, EOK);
goto out;
}
switch (ipc_get_imethod(&call)) {
case UDP_EV_DATA:
udp_ev_data(udp, &call);
break;
default:
async_answer_0(&call, ENOTSUP);
break;
}
}
out:
fibril_mutex_lock(&udp->lock);
udp->cb_done = true;
fibril_mutex_unlock(&udp->lock);
fibril_condvar_broadcast(&udp->cv);
}
HelenOS homepage, sources at GitHub