| 1 |
// UDSocket class - implements BaseSocket for UNIX domain sockets |
| 2 |
|
| 3 |
// For all support, instructions and copyright go to: |
| 4 |
// http://e2guardian.org/ |
| 5 |
// Released under the GPL v2, with the OpenSSL exception described in the README file. |
| 6 |
|
| 7 |
// INCLUDES |
| 8 |
|
| 9 |
#ifdef HAVE_CONFIG_H |
| 10 |
#include "e2config.h" |
| 11 |
#endif |
| 12 |
#include "UDSocket.hpp" |
| 13 |
#include "Logger.hpp" |
| 14 |
|
| 15 |
#include <csignal> |
| 16 |
#include <fcntl.h> |
| 17 |
#include <sys/time.h> |
| 18 |
#include <pwd.h> |
| 19 |
#include <cerrno> |
| 20 |
#include <cstring> |
| 21 |
#include <unistd.h> |
| 22 |
#include <stdexcept> |
| 23 |
#include <stddef.h> |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
// necessary for calculating size of sockaddr_un in a portable manner |
| 28 |
|
| 29 |
#ifndef offsetof |
| 30 |
#define offsetof(TYPE, MEMBER) ((size_t) & ((TYPE *)0)->MEMBER) |
| 31 |
#endif |
| 32 |
|
| 33 |
// IMPLEMENTATION |
| 34 |
|
| 35 |
// constructor - creates default UNIX domain socket & clears address structs |
| 36 |
UDSocket::UDSocket() |
| 37 |
{ |
| 38 |
sck = socket(PF_UNIX, SOCK_STREAM, 0); |
| 39 |
infds[0].fd = sck; |
| 40 |
outfds[0].fd = sck; |
| 41 |
memset(&my_adr, 0, sizeof my_adr); |
| 42 |
memset(&peer_adr, 0, sizeof peer_adr); |
| 43 |
my_adr.sun_family = AF_UNIX; |
| 44 |
strcpy(my_adr.sun_path, ""); |
| 45 |
peer_adr.sun_family = AF_UNIX; |
| 46 |
strcpy(peer_adr.sun_path, ""); |
| 47 |
my_adr_length = 0; |
| 48 |
} |
| 49 |
|
| 50 |
// create socket from pre-existing FD (address structs will be invalid!) |
| 51 |
UDSocket::UDSocket(int fd) |
| 52 |
: BaseSocket(fd) |
| 53 |
{ |
| 54 |
memset(&my_adr, 0, sizeof my_adr); |
| 55 |
memset(&peer_adr, 0, sizeof peer_adr); |
| 56 |
my_adr.sun_family = AF_UNIX; |
| 57 |
strcpy(my_adr.sun_path, ""); |
| 58 |
peer_adr.sun_family = AF_UNIX; |
| 59 |
strcpy(peer_adr.sun_path, ""); |
| 60 |
my_adr_length = 0; |
| 61 |
} |
| 62 |
|
| 63 |
// create socket from given FD & local address (checkme: is it local or remote that gets passed in here?) |
| 64 |
UDSocket::UDSocket(int newfd, struct sockaddr_un myadr) |
| 65 |
: BaseSocket(newfd) |
| 66 |
{ |
| 67 |
my_adr = myadr; |
| 68 |
my_adr_length = sizeof(my_adr.sun_family) + strlen(my_adr.sun_path); |
| 69 |
} |
| 70 |
|
| 71 |
// close socket & clear address structs |
| 72 |
void UDSocket::reset() |
| 73 |
{ |
| 74 |
this->baseReset(); |
| 75 |
sck = socket(PF_UNIX, SOCK_STREAM, 0); |
| 76 |
infds[0].fd = sck; |
| 77 |
outfds[0].fd = sck; |
| 78 |
memset(&my_adr, 0, sizeof my_adr); |
| 79 |
memset(&peer_adr, 0, sizeof peer_adr); |
| 80 |
my_adr.sun_family = AF_UNIX; |
| 81 |
strcpy(my_adr.sun_path, ""); |
| 82 |
peer_adr.sun_family = AF_UNIX; |
| 83 |
strcpy(peer_adr.sun_path, ""); |
| 84 |
my_adr_length = 0; |
| 85 |
} |
| 86 |
|
| 87 |
// accept incoming connection & return new UDSocket |
| 88 |
UDSocket *UDSocket::accept() |
| 89 |
{ |
| 90 |
my_adr_length = sizeof(my_adr.sun_family) + strlen(my_adr.sun_path); |
| 91 |
int newfd = this->baseAccept((struct sockaddr *)&my_adr, &my_adr_length); |
| 92 |
UDSocket *s = new UDSocket(newfd, my_adr); |
| 93 |
return s; |
| 94 |
} |
| 95 |
|
| 96 |
// connect to given server (following default constructor) |
| 97 |
int UDSocket::connect(const char *path) |
| 98 |
{ |
| 99 |
if (strlen(path) > 108) |
| 100 |
return -1; |
| 101 |
|
| 102 |
DEBUG_network("uds connect:", path); |
| 103 |
strcpy(my_adr.sun_path, path); |
| 104 |
|
| 105 |
my_adr_length = offsetof(struct sockaddr_un, sun_path) + strlen(path); |
| 106 |
|
| 107 |
return ::connect(sck, (struct sockaddr *)&my_adr, my_adr_length); |
| 108 |
} |
| 109 |
|
| 110 |
// bind socket to given path |
| 111 |
int UDSocket::bind(const char *path) |
| 112 |
{ // to bind a unix domain socket to a path |
| 113 |
if (strlen(path) > 108) |
| 114 |
return -1; |
| 115 |
|
| 116 |
unlink(path); |
| 117 |
|
| 118 |
strcpy(my_adr.sun_path, path); |
| 119 |
|
| 120 |
my_adr_length = offsetof(struct sockaddr_un, sun_path) + strlen(path); |
| 121 |
|
| 122 |
return ::bind(sck, (struct sockaddr *)&my_adr, my_adr_length); |
| 123 |
} |