Commit 4562e816f0ba7b8fb71645b03a8c8ea54eb1faf4
1 parent
7a8bdb3e
Add code to create a socket pair and send filedescriptors
Showing
7 changed files
with
114 additions
and
15 deletions
@@ -66,6 +66,7 @@ TR_CLASS(TR_Socket) { | @@ -66,6 +66,7 @@ TR_CLASS(TR_Socket) { | ||
66 | socklen_t addrlen; | 66 | socklen_t addrlen; |
67 | int handle; | 67 | int handle; |
68 | TR_SocketFin fin_state; | 68 | TR_SocketFin fin_state; |
69 | + int fd_getter; | ||
69 | }; | 70 | }; |
70 | TR_INSTANCE_INIT(TR_Socket); | 71 | TR_INSTANCE_INIT(TR_Socket); |
71 | TR_CLASSVARS_DECL(TR_Socket) {}; | 72 | TR_CLASSVARS_DECL(TR_Socket) {}; |
@@ -81,6 +82,7 @@ TR_CLASSVARS_DECL(TR_Socket) {}; | @@ -81,6 +82,7 @@ TR_CLASSVARS_DECL(TR_Socket) {}; | ||
81 | #define TR_socketAddrlen(socket) ((socket)->addrlen) | 82 | #define TR_socketAddrlen(socket) ((socket)->addrlen) |
82 | #define TR_socketHandle(socket) ((socket)->handle) | 83 | #define TR_socketHandle(socket) ((socket)->handle) |
83 | #define TR_socketFinState(socket) ((socket)->fin_state) | 84 | #define TR_socketFinState(socket) ((socket)->fin_state) |
85 | +#define TR_socketFdGetter(socket) ((socket)->fd_getter) | ||
84 | #define TR_socketFinRd(socket) \ | 86 | #define TR_socketFinRd(socket) \ |
85 | ((TR_socketFinState(socket) & TR_FIN_RD) == TR_FIN_RD) | 87 | ((TR_socketFinState(socket) & TR_FIN_RD) == TR_FIN_RD) |
86 | #define TR_socketFinWr(socket) \ | 88 | #define TR_socketFinWr(socket) \ |
@@ -118,7 +120,6 @@ TR_CLASSVARS_DECL(TR_Socket) {}; | @@ -118,7 +120,6 @@ TR_CLASSVARS_DECL(TR_Socket) {}; | ||
118 | 120 | ||
119 | TR_CLASS(TR_TcpSocket) { | 121 | TR_CLASS(TR_TcpSocket) { |
120 | TR_EXTENDS(TR_Socket); | 122 | TR_EXTENDS(TR_Socket); |
121 | - int listen; | ||
122 | int connected; | 123 | int connected; |
123 | }; | 124 | }; |
124 | TR_INSTANCE_INIT(TR_TcpSocket); | 125 | TR_INSTANCE_INIT(TR_TcpSocket); |
@@ -151,6 +152,8 @@ TR_TcpSocket TR_socketAccept(TR_TcpSocket); | @@ -151,6 +152,8 @@ TR_TcpSocket TR_socketAccept(TR_TcpSocket); | ||
151 | (TR_socketInit((socket), NULL)) | 152 | (TR_socketInit((socket), NULL)) |
152 | 153 | ||
153 | void TR_socketPair(TR_Socket[2], int); | 154 | void TR_socketPair(TR_Socket[2], int); |
155 | +void TR_socketSendFd(TR_Socket, int); | ||
156 | +int TR_socketGetFd(TR_Socket); | ||
154 | 157 | ||
155 | #endif // __TR_SOCKET_H__ | 158 | #endif // __TR_SOCKET_H__ |
156 | 159 |
@@ -13,6 +13,8 @@ TRIO = stream.c \ | @@ -13,6 +13,8 @@ TRIO = stream.c \ | ||
13 | socket_nonblock.c \ | 13 | socket_nonblock.c \ |
14 | socket_close.c \ | 14 | socket_close.c \ |
15 | socket_pair.c \ | 15 | socket_pair.c \ |
16 | + socket_send_fd.c \ | ||
17 | + socket_get_fd.c \ | ||
16 | socket_shutdown.c \ | 18 | socket_shutdown.c \ |
17 | socket_shutdown_read.c \ | 19 | socket_shutdown_read.c \ |
18 | socket_shutdown_write.c \ | 20 | socket_shutdown_write.c \ |
@@ -42,7 +42,6 @@ TR_socketAccept(TR_TcpSocket this) | @@ -42,7 +42,6 @@ TR_socketAccept(TR_TcpSocket this) | ||
42 | { | 42 | { |
43 | TR_Socket remote = | 43 | TR_Socket remote = |
44 | TR_new(TR_TcpSocket, TR_socketLog((TR_Socket)this), NULL, 0, 0); | 44 | TR_new(TR_TcpSocket, TR_socketLog((TR_Socket)this), NULL, 0, 0); |
45 | - //int flags; | ||
46 | 45 | ||
47 | remote->addrlen = ((TR_Socket)this)->addrlen; | 46 | remote->addrlen = ((TR_Socket)this)->addrlen; |
48 | remote->handle = accept( | 47 | remote->handle = accept( |
@@ -50,9 +49,6 @@ TR_socketAccept(TR_TcpSocket this) | @@ -50,9 +49,6 @@ TR_socketAccept(TR_TcpSocket this) | ||
50 | &(remote->addr.info), | 49 | &(remote->addr.info), |
51 | &(remote->addrlen)); | 50 | &(remote->addrlen)); |
52 | 51 | ||
53 | - //flags = fcntl(remote->handle, F_GETFL, 0); | ||
54 | - //fcntl(remote->handle, F_SETFL, flags | O_NONBLOCK); | ||
55 | - | ||
56 | if (-1 == remote->handle) { | 52 | if (-1 == remote->handle) { |
57 | TR_delete(remote); | 53 | TR_delete(remote); |
58 | } else { | 54 | } else { |
src/socket_get_fd.c
0 → 100644
1 | +/** | ||
2 | + * \file | ||
3 | + * | ||
4 | + * \author Georg Hopp | ||
5 | + * | ||
6 | + * \copyright | ||
7 | + * Copyright © 2014 Georg Hopp | ||
8 | + * | ||
9 | + * This program is free software: you can redistribute it and/or modify | ||
10 | + * it under the terms of the GNU General Public License as published by | ||
11 | + * the Free Software Foundation, either version 3 of the License, or | ||
12 | + * (at your option) any later version. | ||
13 | + * | ||
14 | + * This program is distributed in the hope that it will be useful, | ||
15 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
16 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
17 | + * GNU General Public License for more details. | ||
18 | + * | ||
19 | + * You should have received a copy of the GNU General Public License | ||
20 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
21 | + */ | ||
22 | + | ||
23 | +#include <errno.h> // for errno | ||
24 | +#include <unistd.h> | ||
25 | +#include <fcntl.h> | ||
26 | + | ||
27 | +#include "tr/socket.h" | ||
28 | +#include "tr/logger.h" | ||
29 | + | ||
30 | +int | ||
31 | +TR_socketGetFd(TR_Socket this) | ||
32 | +{ | ||
33 | + struct msghdr msg; | ||
34 | + struct cmsghdr * cmsg; | ||
35 | + char cmsgbuf[CMSG_SPACE(sizeof(int))]; | ||
36 | + int fd = 0; | ||
37 | + | ||
38 | + memset(&msg, 0, sizeof(msg)); | ||
39 | + msg.msg_control = cmsgbuf; | ||
40 | + msg.msg_controllen = sizeof(cmsgbuf); | ||
41 | + | ||
42 | + if (0 > recvmsg(TR_socketHandle(this), &msg, 0)) { | ||
43 | + return -1; | ||
44 | + } | ||
45 | + | ||
46 | + cmsg = CMSG_FIRSTHDR(&msg); | ||
47 | + if (cmsg == NULL || cmsg->cmsg_type != SCM_RIGHTS) { | ||
48 | + return -1; | ||
49 | + } | ||
50 | + | ||
51 | + memcpy(&fd, CMSG_DATA(cmsg), sizeof(int)); | ||
52 | + return fd; | ||
53 | +} | ||
54 | + | ||
55 | +// vim: set ts=4 sw=4: |
@@ -20,12 +20,6 @@ | @@ -20,12 +20,6 @@ | ||
20 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | 20 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
21 | */ | 21 | */ |
22 | 22 | ||
23 | -//#include <errno.h> | ||
24 | -//#include <stdlib.h> | ||
25 | -//#include <unistd.h> | ||
26 | -//#include <stdio.h> | ||
27 | -//#include <fcntl.h> | ||
28 | - | ||
29 | #include <sys/types.h> | 23 | #include <sys/types.h> |
30 | #include <sys/socket.h> | 24 | #include <sys/socket.h> |
31 | #include <netdb.h> | 25 | #include <netdb.h> |
src/socket_send_fd.c
0 → 100644
1 | +/** | ||
2 | + * \file | ||
3 | + * | ||
4 | + * \author Georg Hopp | ||
5 | + * | ||
6 | + * \copyright | ||
7 | + * Copyright © 2014 Georg Hopp | ||
8 | + * | ||
9 | + * This program is free software: you can redistribute it and/or modify | ||
10 | + * it under the terms of the GNU General Public License as published by | ||
11 | + * the Free Software Foundation, either version 3 of the License, or | ||
12 | + * (at your option) any later version. | ||
13 | + * | ||
14 | + * This program is distributed in the hope that it will be useful, | ||
15 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
16 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
17 | + * GNU General Public License for more details. | ||
18 | + * | ||
19 | + * You should have received a copy of the GNU General Public License | ||
20 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
21 | + */ | ||
22 | + | ||
23 | +#include <errno.h> // for errno | ||
24 | +#include <unistd.h> | ||
25 | +#include <fcntl.h> | ||
26 | + | ||
27 | +#include "tr/socket.h" | ||
28 | +#include "tr/logger.h" | ||
29 | + | ||
30 | +void | ||
31 | +TR_socketSendFd(TR_Socket this, int fd) | ||
32 | +{ | ||
33 | + struct msghdr msg; | ||
34 | + struct cmsghdr * cmsg; | ||
35 | + char cmsgbuf[CMSG_SPACE(sizeof(int))]; | ||
36 | + | ||
37 | + memset(&msg, 0, sizeof(msg)); | ||
38 | + msg.msg_control = cmsgbuf; | ||
39 | + msg.msg_controllen = sizeof(cmsgbuf); // necessary for CMSG_FIRSTHDR to | ||
40 | + // return the correct value | ||
41 | + cmsg = CMSG_FIRSTHDR(&msg); | ||
42 | + cmsg->cmsg_level = SOL_SOCKET; | ||
43 | + cmsg->cmsg_type = SCM_RIGHTS; | ||
44 | + cmsg->cmsg_len = CMSG_LEN(sizeof(int)); | ||
45 | + memcpy(CMSG_DATA(cmsg), &fd, sizeof(int)); | ||
46 | + msg.msg_controllen = cmsg->cmsg_len; // total size of all control blocks | ||
47 | + | ||
48 | + sendmsg(TR_socketHandle(this), &msg, 0); | ||
49 | + /* TODO error handling... */ | ||
50 | +} | ||
51 | + | ||
52 | +// vim: set ts=4 sw=4: |
@@ -41,10 +41,7 @@ tcpSocketCtor(void * _this, va_list * params) | @@ -41,10 +41,7 @@ tcpSocketCtor(void * _this, va_list * params) | ||
41 | TR_TcpSocket this = _this; | 41 | TR_TcpSocket this = _this; |
42 | 42 | ||
43 | TR_PARENTCALL(TR_TcpSocket, _this, TR_Class, ctor, params); | 43 | TR_PARENTCALL(TR_TcpSocket, _this, TR_Class, ctor, params); |
44 | - | ||
45 | TR_socketType((TR_Socket)this) = SOCK_STREAM; | 44 | TR_socketType((TR_Socket)this) = SOCK_STREAM; |
46 | - this->listen = FALSE; | ||
47 | - this->connected = FALSE; | ||
48 | 45 | ||
49 | return 0; | 46 | return 0; |
50 | } | 47 | } |
@@ -66,7 +63,7 @@ tcpSocketBind(void * _this) | @@ -66,7 +63,7 @@ tcpSocketBind(void * _this) | ||
66 | // error | 63 | // error |
67 | return -1; | 64 | return -1; |
68 | } | 65 | } |
69 | - this->listen = TRUE; | 66 | + ((TR_Socket)this)->fd_getter = TRUE; |
70 | 67 | ||
71 | return 0; | 68 | return 0; |
72 | } | 69 | } |
Please
register
or
login
to post a comment