wmii/libixp2/socket.c

84 lines
1.7 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 <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/un.h>
#include <unistd.h>
#include "cext.h"
#include "ixp.h"
2005-12-05 01:45:59 +03:00
int ixp_connect_sock(char *sockfile)
2005-11-18 18:54:58 +03:00
{
2005-12-05 01:45:59 +03:00
int fd = 0;
struct sockaddr_un addr = { 0 };
socklen_t su_len;
2005-11-18 18:54:58 +03:00
/* init */
addr.sun_family = AF_UNIX;
strncpy(addr.sun_path, sockfile, sizeof(addr.sun_path));
su_len = sizeof(struct sockaddr) + strlen(addr.sun_path);
if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0)
return -1;
2005-12-05 01:45:59 +03:00
if (connect(fd, (struct sockaddr *) &addr, su_len)) {
2005-11-18 18:54:58 +03:00
close(fd);
return -1;
}
return fd;
}
2005-12-05 01:45:59 +03:00
int ixp_accept_sock(int fd)
2005-11-18 18:54:58 +03:00
{
2005-12-05 01:45:59 +03:00
socklen_t su_len;
struct sockaddr_un addr = { 0 };
2005-11-18 18:54:58 +03:00
su_len = sizeof(struct sockaddr);
2005-12-05 01:45:59 +03:00
return accept(fd, (struct sockaddr *) &addr, &su_len);
2005-11-18 18:54:58 +03:00
}
2005-12-05 01:45:59 +03:00
int ixp_create_sock(char *sockfile, char **errstr)
2005-11-18 18:54:58 +03:00
{
2005-12-05 01:45:59 +03:00
int fd;
int yes = 1;
struct sockaddr_un addr = { 0 };
socklen_t su_len;
2005-11-18 18:54:58 +03:00
signal(SIGPIPE, SIG_IGN);
if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
*errstr = "cannot open socket";
return -1;
}
if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR,
2005-12-05 01:45:59 +03:00
(char *) &yes, sizeof(yes)) < 0) {
2005-11-18 18:54:58 +03:00
*errstr = "cannot set socket options";
close(fd);
return -1;
}
addr.sun_family = AF_UNIX;
strncpy(addr.sun_path, sockfile, sizeof(addr.sun_path));
su_len = sizeof(struct sockaddr) + strlen(addr.sun_path);
2005-12-05 01:45:59 +03:00
if (bind(fd, (struct sockaddr *) &addr, su_len) < 0) {
2005-11-18 18:54:58 +03:00
*errstr = "cannot bind socket";
close(fd);
return -1;
}
chmod(sockfile, S_IRWXU);
if (listen(fd, IXP_MAX_CONN) < 0) {
*errstr = "cannot listen on socket";
close(fd);
return -1;
}
return fd;
}