socket.h
Go to the documentation of this file.
1 /* vim: set ts=2 expandtab: */
2 /**
3  * @file socket.h
4  * @brief a simple object-oriented socket wrapper
5  * object model inspired by Zed Shaw
6  *
7  * @author Josh King (jheretic), jking@chambana.net
8  *
9  * @internal
10  * Created 03/07/2013
11  * Revision $Id: doxygen.commotion.templates,v 0.1 2013/01/01 09:00:00 jheretic Exp $
12  * Compiler gcc/g++
13  * Company The Open Technology Institute
14  * Copyright Copyright (c) 2013, Josh King
15  *
16  * This file is part of Commotion, Copyright (c) 2013, Josh King
17  *
18  * Commotion is free software: you can redistribute it and/or modify
19  * it under the terms of the GNU Affero General Public License as
20  * published by the Free Software Foundation, either version 3 of the
21  * License, or (at your option) any later version.
22  *
23  * Commotion is distributed in the hope that it will be useful,
24  * but WITHOUT ANY WARRANTY; without even the implied warranty of
25  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26  * GNU Affero General Public License for more details.
27  *
28  * You should have received a copy of the GNU Affero General Public License
29  * along with Commotion. If not, see <http://www.gnu.org/licenses/>.
30  *
31  * =====================================================================================
32  */
33 
34 #ifndef _SOCKET_H
35 #define _SOCKET_H
36 
37 #include <stdlib.h>
38 #include <stddef.h>
39 #include <stdbool.h>
40 #include "obj.h"
41 
42 #define MAX_IPPROTO 255
43 #define MAX_CONNECTIONS 32
44 
45 typedef struct co_fd_t co_fd_t;
46 typedef struct co_socket_t co_socket_t;
47 
48 struct co_fd_t {
49  co_obj_t _header;
50  uint8_t _exttype;
51  uint8_t _len;
52  co_socket_t *socket; // parent socket
53  int fd;
54 };
55 
56 /**
57  * @brief creates a file descriptor object
58  * @param fd file descriptor int
59  */
60 co_obj_t *co_fd_create(co_obj_t *parent, int fd);
61 
62 /**
63  * @struct co_socket_t contains file path and state information for socket
64  */
65 struct co_socket_t {
66  co_obj_t _header;
67  uint8_t _exttype;
68  uint8_t _len;
69  char *uri;
70  co_fd_t *fd; //socket file descriptor
71  co_obj_t *rfd_lst; //list of accept socket file descriptors
72  bool fd_registered;
73  struct sockaddr* local;
74  struct sockaddr* remote;
75  bool listen;
76  int (*init)(co_obj_t *self);
77  int (*destroy)(co_obj_t *self);
78  int (*hangup)(co_obj_t *self, co_obj_t *context);
79  int (*bind)(co_obj_t *self, const char *endpoint);
80  int (*connect)(co_obj_t *self, const char *endpoint);
81  int (*send)(co_obj_t *self, char *outgoing, size_t length);
82  int (*receive)(co_obj_t *self, co_obj_t *fd, char *incoming, size_t length);
83  int (*setopt)(co_obj_t *self, int level, int option, void *optval, socklen_t optvallen);
84  int (*getopt)(co_obj_t *self, int level, int option, void *optval, socklen_t optvallen);
85  int (*poll_cb)(co_obj_t *self, co_obj_t *context);
86  int (*register_cb)(co_obj_t *self, co_obj_t *context);
87  unsigned int events;
88 } __attribute__((packed));
89 
90 /**
91  * @brief creates a socket from specified values or initializes defaults
92  * @param size size of socket struct
93  * @param proto socket protocol
94  */
95 co_obj_t *co_socket_create(size_t size, co_socket_t proto);
96 
97 /**
98  * @brief creates a socket from specified values or initializes defaults
99  * @param size size of socket struct
100  * @param proto socket protocol
101  */
102 int co_socket_init(co_obj_t *self);
103 
104 /**
105  * @brief closes a socket and removes it from memory
106  * @param self socket name
107  */
108 int co_socket_destroy(co_obj_t *self);
109 
110 /**
111  * @brief closes a socket and changes its state information
112  * @param self socket name
113  * @param context co_obj_t context pointer (currently unused)
114  */
115 int co_socket_hangup(co_obj_t *self, co_obj_t *context);
116 
117 /**
118  * @brief sends a message on a specified socket
119  * @param self socket name
120  * @param outgoing message to be sent
121  * @param length length of message
122  */
123 int co_socket_send(co_obj_t *self, char *outgoing, size_t length);
124 
125 /**
126  * @brief receives a message on the listening socket
127  * @param self socket name
128  * @param incoming message received
129  * @param length length of message
130  */
131 int co_socket_receive(co_obj_t * self, co_obj_t *fd, char *incoming, size_t length);
132 
133 /**
134  * @brief sets custom socket options, if specified by user
135  * @param self socket name
136  * @param level the networking level to be customized
137  * @param option the option to be changed
138  * @param optval the value for the new option
139  * @param optvallen the length of the value for the new option
140  */
141 int co_socket_setopt(co_obj_t * self, int level, int option, void *optval, socklen_t optvallen);
142 
143 /**
144  * @brief gets custom socket options specified from the user
145  * @param self socket name
146  * @param level the networking level to be customized
147  * @param option the option to be changed
148  * @param optval the value for the new option
149  * @param optvallen the length of the value for the new option
150  */
151 int co_socket_getopt(co_obj_t * self, int level, int option, void *optval, socklen_t optvallen);
152 
153 typedef struct unix_socket_t unix_socket_t;
154 
155 /**
156  * @struct unix_socket_t struct for unix sockets. Contains protocol and file path to socket library
157  */
159  co_socket_t proto;
160  char *path;
161 } __attribute__((packed));
162 
163 /**
164  * @brief initializes a unix socket
165  * @param self socket name
166  */
167 int unix_socket_init(co_obj_t *self);
168 
169 /**
170  * @brief binds a unix socket to a specified endpoint
171  * @param self socket name
172  * @param endpoint specified endpoint for socket (file path)
173  */
174 int unix_socket_bind(co_obj_t *self, const char *endpoint);
175 
176 /**
177  * @brief connects a socket to specified endpoint
178  * @param self socket name
179  * @param endpoint specified endpoint for socket (file path)
180  */
181 int unix_socket_connect(co_obj_t *self, const char *endpoint);
182 
183 
184 #endif
Commotion object model.
int co_socket_setopt(co_obj_t *self, int level, int option, void *optval, socklen_t optvallen)
sets custom socket options, if specified by user
Definition: socket.c:210
int co_socket_receive(co_obj_t *self, co_obj_t *fd, char *incoming, size_t length)
receives a message on the listening socket
Definition: socket.c:173
int unix_socket_connect(co_obj_t *self, const char *endpoint)
connects a socket to specified endpoint
Definition: socket.c:303
int co_socket_send(co_obj_t *self, char *outgoing, size_t length)
sends a message on a specified socket
Definition: socket.c:151
Definition: obj.h:131
Definition: socket.h:65
int co_socket_init(co_obj_t *self)
creates a socket from specified values or initializes defaults
Definition: socket.c:94
int unix_socket_init(co_obj_t *self)
initializes a unix socket
Definition: socket.c:244
co_obj_t * co_socket_create(size_t size, co_socket_t proto)
creates a socket from specified values or initializes defaults
Definition: socket.c:66
co_obj_t * co_fd_create(co_obj_t *parent, int fd)
creates a file descriptor object
Definition: socket.c:47
Definition: socket.h:158
int co_socket_getopt(co_obj_t *self, int level, int option, void *optval, socklen_t optvallen)
gets custom socket options specified from the user
Definition: socket.c:227
Definition: socket.h:48
int co_socket_destroy(co_obj_t *self)
closes a socket and removes it from memory
Definition: socket.c:118
int unix_socket_bind(co_obj_t *self, const char *endpoint)
binds a unix socket to a specified endpoint
Definition: socket.c:264
int co_socket_hangup(co_obj_t *self, co_obj_t *context)
closes a socket and changes its state information
Definition: socket.c:128