libsocket
inetclientdgram.cpp
Go to the documentation of this file.
1 #include <cstring>
2 #include <iostream>
3 #include <string>
4 
5 #include <stdio.h>
6 #include <sys/socket.h>
7 #include <sys/types.h>
8 #include <unistd.h>
9 
10 // oo wrapper around libinetsocket
11 /*
12  The committers of the libsocket project, all rights reserved
13  (c) 2012, dermesser <lbo@spheniscida.de>
14 
15  Redistribution and use in source and binary forms, with or without
16  modification, are permitted provided that the following conditions are met:
17 
18  1. Redistributions of source code must retain the above copyright notice,
19  this list of conditions and the following disclaimer.
20  2. Redistributions in binary form must reproduce the above copyright notice,
21  this list of conditions and the following disclaimer in the documentation
22  and/or other materials provided with the distribution.
23 
24  THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS “AS IS” AND ANY
25  EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
26  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
27  DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
28  DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
29  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
31  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 
35 */
36 
49 #include <libinetsocket.h>
50 #include <exception.hpp>
51 #include <inetclientdgram.hpp>
52 
53 #include <fcntl.h>
54 #ifndef SOCK_NONBLOCK
55 #define SOCK_NONBLOCK O_NONBLOCK
56 #endif
57 
58 namespace libsocket {
59 using std::string;
60 
61 // Constructors
62 
69 inet_dgram_client::inet_dgram_client(int proto_osi3, int flags) {
70  setup(proto_osi3, flags);
71 }
72 
82 inet_dgram_client::inet_dgram_client(const char* dsthost, const char* dstport,
83  int proto_osi3, int flags) {
84  setup(dsthost, dstport, proto_osi3, flags);
85 }
86 
97  const string& dstport, int proto_osi3,
98  int flags) {
99  setup(dsthost, dstport, proto_osi3, flags);
100 }
101 
108 void inet_dgram_client::setup(int proto_osi3, int flags) {
109  if (-1 == (sfd = create_inet_dgram_socket(proto_osi3, flags)))
110  throw socket_exception(__FILE__, __LINE__,
111  "inet_dgram_client::inet_dgram_client() - Could "
112  "not create inet dgram socket!");
113  proto = proto_osi3;
114 
115  is_nonblocking = flags & SOCK_NONBLOCK;
116 }
117 
127 void inet_dgram_client::setup(const char* dsthost, const char* dstport,
128  int proto_osi3, int flags) {
129  // Retrieve address family
130  if (proto_osi3 == LIBSOCKET_BOTH) proto_osi3 = get_address_family(dsthost);
131 
132  if (-1 == (sfd = create_inet_dgram_socket(proto_osi3, flags)))
133  throw socket_exception(__FILE__, __LINE__,
134  "inet_dgram_client::inet_dgram_client() - Could "
135  "not create inet dgram socket!");
136 
137  inet_dgram_client::connect(dsthost, dstport);
138 
139  proto = proto_osi3;
140  is_nonblocking = flags & SOCK_NONBLOCK;
141 }
142 
152 void inet_dgram_client::setup(const string& dsthost, const string& dstport,
153  int proto_osi3, int flags) {
154  setup(dsthost.c_str(), dstport.c_str(), proto_osi3, flags);
155 }
156 
166 void inet_dgram_client::connect(const char* dsthost, const char* dstport) {
167  if (sfd == -1)
168  throw socket_exception(
169  __FILE__, __LINE__,
170  "inet_dgram_client::connect() - Socket has already been closed!",
171  false);
172  if (-1 == (connect_inet_dgram_socket(sfd, dsthost, dstport)))
173  throw socket_exception(
174  __FILE__, __LINE__,
175  "inet_dgram_client::connect() - Could not connect dgram socket! "
176  "(Maybe this socket has a wrong address family?)");
177 
178  host = dsthost;
179  port = dstport;
180  connected = true;
181 }
182 
192 void inet_dgram_client::connect(const string& dsthost, const string& dstport) {
193  if (sfd == -1)
194  throw socket_exception(
195  __FILE__, __LINE__,
196  "inet_dgram_client::connect() - Socket has already been closed!",
197  false);
198  if (-1 ==
199  (connect_inet_dgram_socket(sfd, dsthost.c_str(), dstport.c_str())))
200  throw socket_exception(
201  __FILE__, __LINE__,
202  "inet_dgram_client::connect() - Could not connect dgram socket! "
203  "(Maybe this socket has a wrong address family?)");
204 
205  host = dsthost;
206  port = dstport;
207  connected = true;
208 }
209 
210 /*
211  * @brief Break association to host. Does not close the socket.
212  *
213  * *Should actually be called 'disconnect'*
214  *
215  */
216 void inet_dgram_client::deconnect(void) {
217  if (-1 == (connect_inet_dgram_socket(sfd, NULL, NULL)))
218  throw socket_exception(
219  __FILE__, __LINE__,
220  "inet_dgram_client::deconnect() - Could not disconnect!");
221 
222  connected = false;
223  host.clear();
224  port.clear();
225 }
226 } // namespace libsocket
Contains libsocket elements.
Definition: dgramclient.hpp:41
int get_address_family(const char *hostname)
Look up which address families a host supports.
This class is instantiated and thrown when an error occurs. If there's an error somewhere in libsocke...
Definition: exception.hpp:52
void setup(int proto_osi3, int flags=0)
Set up normal datagram socket (connectable). [NOT FOR EXTERNAL USE].
string port
The port we're bound or connected to.
Definition: inetbase.hpp:57
int sfd
sfd is the sockets API file descriptor
Definition: socket.hpp:74
int proto
Which internet protocol version we're using.
Definition: inetbase.hpp:59
int connect_inet_dgram_socket(int sfd, const char *host, const char *service)
Connect a UDP socket.
void connect(const char *dsthost, const char *dstport)
Connect datagram socket.
inet_dgram_client(int proto_osi3, int flags=0)
Create normal datagram socket (connectable).
string host
The address we're bound or connected to.
Definition: inetbase.hpp:55
int create_inet_dgram_socket(char proto_osi3, int flags)
Creates a new UDP/IP socket.
Contains all functions available in the C libsocket.