Skip to content

Commit e16f061

Browse files
committed
Unix socket peer connection
1 parent a8233d9 commit e16f061

File tree

5 files changed

+74
-17
lines changed

5 files changed

+74
-17
lines changed

network.c

+56-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
#include <sys/types.h>
44
#include <sys/socket.h>
5+
#include <sys/un.h>
56
#include <netdb.h>
67
#include <errno.h>
78
#include <string.h>
@@ -10,8 +11,8 @@
1011
#include <unistd.h>
1112

1213
/*
13-
* Create a file descriptor connected to a socket peer.
14-
* Client sockets will be connected, listening sockets will be bound.
14+
* Create a file descriptor connected to a network socket peer.
15+
* Client sockets will be connected, listening sockets will be bound/listened.
1516
* Returns -1 in case of failure, a valid fd otherwise.
1617
*/
1718
int network_socket(char* host, char* port, int socktype, int listener){
@@ -97,6 +98,59 @@ int network_socket(char* host, char* port, int socktype, int listener){
9798
return fd;
9899
}
99100

101+
/*
102+
* Create a file descriptor connected to a unix socket peer.
103+
* Client sockets will be connected, listening sockets will be bound.
104+
* Returns -1 in case of failure, a valid fd otherwise.
105+
*/
106+
int network_socket_unix(char* path, int socktype, int listener){
107+
struct sockaddr_un addr = {
108+
.sun_family = AF_UNIX,
109+
};
110+
int fd = socket(AF_UNIX, socktype, 0), flags;
111+
112+
if(fd < 0){
113+
fprintf(stderr, "Failed to create socket: %s\n", strerror(errno));
114+
return -1;
115+
}
116+
117+
//set nonblocking
118+
flags = fcntl(fd, F_GETFL, 0);
119+
if(fcntl(fd, F_SETFL, flags | O_NONBLOCK) < 0){
120+
fprintf(stderr, "Failed to set socket nonblocking: %s\n", strerror(errno));
121+
close(fd);
122+
return -1;
123+
}
124+
125+
strncpy(addr.sun_path, path, (strlen(path) < (sizeof(addr.sun_path) - 1)) ? strlen(path) : (sizeof(addr.sun_path) - 1));
126+
127+
if(listener){
128+
unlink(path);
129+
if(bind(fd, (struct sockaddr*) &addr, sizeof(addr))){
130+
fprintf(stderr, "Failed to bind: %s\n", strerror(errno));
131+
close(fd);
132+
return -1;
133+
}
134+
135+
if(listen(fd, SOMAXCONN)){
136+
fprintf(stderr, "Failed to listen: %s\n", strerror(errno));
137+
close(fd);
138+
return -1;
139+
}
140+
141+
return fd;
142+
}
143+
144+
//connect clients
145+
if(connect(fd, (struct sockaddr*) &addr, sizeof(addr))){
146+
fprintf(stderr, "Failed to connect: %s\n", strerror(errno));
147+
close(fd);
148+
return -1;
149+
}
150+
151+
return fd;
152+
}
153+
100154
/*
101155
* Send arbitrary data over multiple writes if necessary.
102156
* Returns 0 on success

network.h

+1
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@
33

44
/* Socket interface convenience functions */
55
int network_socket(char* host, char* port, int socktype, int listener);
6+
int network_socket_unix(char* path, int socktype, int listener);
67
int network_send(int fd, uint8_t* data, size_t length);
78
int network_send_str(int fd, char* data);

plugin.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ static char** framing_function_name = NULL;
1010

1111
int plugin_framing_load(char* path){
1212
//TODO load plugins
13-
return 1;
13+
return 0;
1414
}
1515

1616
int plugin_backend_load(char* backend_requested, ws_backend* backend){
1717
//TODO load backend
18-
return 1;
18+
return 0;
1919
}
2020

2121
int plugin_register_framing(char* name, ws_framing func){

websocksy.c

+13-10
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,11 @@ static peer_transport client_detect_transport(char* host){
113113
}
114114
else if(!strncmp(host, "unix://", 8)){
115115
memmove(host, host + 8, strlen(host) - 7);
116-
return peer_unix;
116+
return peer_unix_stream;
117+
}
118+
else if(!strncmp(host, "unix-dgram://", 13)){
119+
memmove(host, host + 13, strlen(host) - 12);
120+
return peer_unix_dgram;
117121
}
118122

119123
fprintf(stderr, "Peer address %s does not include any known protocol identifier, guessing tcp_client\n", host);
@@ -123,7 +127,8 @@ static peer_transport client_detect_transport(char* host){
123127
/* Establish peer connection for negotiated websocket */
124128
int client_connect(websocket* ws){
125129
ws->peer = config.backend.query(ws->request_path, ws->protocols, ws->protocol, ws->headers, ws->header, ws);
126-
if(!ws->peer.host || !ws->peer.port){
130+
if(!ws->peer.host){
131+
//TODO check port if network socket
127132
//no peer provided
128133
return 1;
129134
}
@@ -146,19 +151,17 @@ int client_connect(websocket* ws){
146151
case peer_udp_client:
147152
ws->peer_fd = network_socket(ws->peer.host, ws->peer.port, SOCK_DGRAM, 0);
148153
break;
149-
case peer_tcp_server:
150-
//TODO implement tcp server mode
151-
fprintf(stderr, "TCP Server mode not yet implemented\n");
152-
return 1;
153-
case peer_udp_server:
154-
ws->peer_fd = network_socket(ws->peer.host, ws->peer.port, SOCK_DGRAM, 1);
155-
break;
156154
case peer_fifo_tx:
157155
case peer_fifo_rx:
158-
case peer_unix:
159156
//TODO implement other peer modes
160157
fprintf(stderr, "Peer connection mode not yet implemented\n");
161158
return 1;
159+
case peer_unix_stream:
160+
ws->peer_fd = network_socket_unix(ws->peer.host, SOCK_STREAM, 0);
161+
break;
162+
case peer_unix_dgram:
163+
ws->peer_fd = network_socket_unix(ws->peer.host, SOCK_DGRAM, 0);
164+
break;
162165
default:
163166
fprintf(stderr, "Invalid peer transport selected\n");
164167
return 1;

websocksy.h

+2-3
Original file line numberDiff line numberDiff line change
@@ -92,11 +92,10 @@ typedef enum {
9292
peer_transport_detect,
9393
peer_tcp_client,
9494
peer_udp_client,
95-
peer_tcp_server,
96-
peer_udp_server,
9795
peer_fifo_tx,
9896
peer_fifo_rx,
99-
peer_unix
97+
peer_unix_stream,
98+
peer_unix_dgram
10099
} peer_transport;
101100

102101
/* Peer address model */

0 commit comments

Comments
 (0)