wmii/libixp2/transport.c

76 lines
1.4 KiB
C
Raw Normal View History

2005-11-18 18:54:58 +03:00
/*
* (C)opyright MMIV-MMV Anselm R. Garbe <garbeam at gmail dot com>
* See LICENSE file for license details.
*/
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <unistd.h>
#include "ixp.h"
#include <cext.h>
2005-12-05 01:45:59 +03:00
u32 ixp_send_message(int fd, void *msg, u32 msize, char **errstr)
2005-11-18 18:54:58 +03:00
{
2005-12-05 01:45:59 +03:00
u32 num = 0;
int r;
2005-11-18 18:54:58 +03:00
/* send message */
while (num < msize) {
r = write(fd, msg + num, msize - num);
if (r == -1 && errno == EINTR)
continue;
if (r < 1) {
*errstr = "cannot send message";
return 0;
}
num += r;
}
return num;
}
2005-12-05 01:45:59 +03:00
static u32 ixp_recv_data(int fd, void *msg, u32 msize, char **errstr)
2005-11-18 18:54:58 +03:00
{
2005-12-05 01:45:59 +03:00
u32 num = 0;
int r = 0;
2005-11-18 18:54:58 +03:00
/* receive data */
while (num < msize) {
r = read(fd, msg + num, msize - num);
if (r == -1 && errno == EINTR)
continue;
if (r < 1) {
*errstr = "cannot receive data";
return 0;
}
num += r;
}
return num;
}
2005-12-05 01:45:59 +03:00
u32 ixp_recv_message(int fd, void *msg, u32 msglen, char **errstr)
2005-11-18 18:54:58 +03:00
{
2005-12-05 01:45:59 +03:00
u32 msize;
2005-11-18 18:54:58 +03:00
/* receive header */
if (ixp_recv_data(fd, msg, sizeof(u32), errstr) != sizeof(u32))
return 0;
ixp_dec_u32(msg, &msize);
if (msize > msglen) {
*errstr = "message size exceeds buffer size";
return 0;
}
/* receive message */
if (ixp_recv_data(fd, msg + sizeof(u32), msize - sizeof(u32), errstr)
2005-12-05 01:45:59 +03:00
!= msize - sizeof(u32))
2005-11-18 18:54:58 +03:00
return 0;
return msize;
}