wmii/libixp/transport.c

78 lines
1.5 KiB
C
Raw Normal View History

2005-11-18 18:54:58 +03:00
/*
2006-01-20 17:20:24 +03:00
* (C)opyright MMIV-MMVI Anselm R. Garbe <garbeam at gmail dot com>
2005-11-18 18:54:58 +03:00
* 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"
2005-12-21 18:18:11 +03:00
unsigned int
ixp_send_message(int fd, void *msg, unsigned int msize, char **errstr)
2005-11-18 18:54:58 +03:00
{
2006-03-23 14:53:41 +03:00
unsigned int num = 0;
int r;
2005-11-18 18:54:58 +03:00
2006-03-23 14:53:41 +03:00
/* send message */
while(num < msize) {
r = write(fd, msg + num, msize - num);
if(r == -1 && errno == EINTR)
continue;
if(r < 1) {
*errstr = "broken pipe";
return 0;
}
num += r;
}
return num;
2005-11-18 18:54:58 +03:00
}
2005-12-21 18:18:11 +03:00
static unsigned int
ixp_recv_data(int fd, void *msg, unsigned int msize, char **errstr)
2005-11-18 18:54:58 +03:00
{
2006-03-23 14:53:41 +03:00
unsigned int num = 0;
int r = 0;
2005-11-18 18:54:58 +03:00
2006-03-23 14:53:41 +03:00
/* receive data */
while(num < msize) {
r = read(fd, msg + num, msize - num);
if(r == -1 && errno == EINTR)
continue;
if(r < 1) {
*errstr = "broken pipe";
return 0;
}
num += r;
}
return num;
2005-11-18 18:54:58 +03:00
}
2005-12-21 18:18:11 +03:00
unsigned int
ixp_recv_message(int fd, void *msg, unsigned int msglen, char **errstr)
2005-11-18 18:54:58 +03:00
{
2006-03-23 14:53:41 +03:00
unsigned int msize;
2005-11-18 18:54:58 +03:00
2006-03-23 14:53:41 +03:00
/* receive header */
if(ixp_recv_data(fd, msg, sizeof(unsigned int), errstr) !=
sizeof(unsigned int))
return 0;
ixp_unpack_u32((void *)&msg, nil, &msize);
2006-03-23 14:53:41 +03:00
if(msize > msglen) {
*errstr = "invalid message header";
return 0;
}
/* receive message */
2006-06-12 15:34:59 +04:00
if(ixp_recv_data(fd, msg, msize - sizeof(unsigned int), errstr)
!= msize - sizeof(unsigned int))
2006-03-23 14:53:41 +03:00
return 0;
return msize;
2005-11-18 18:54:58 +03:00
}