libsocket
inetdgram.cpp
Go to the documentation of this file.
1 #include <string.h>
2 #include <iostream>
3 #include <memory>
4 #include <string>
5 
6 #include <stdio.h>
7 #include <sys/socket.h>
8 #include <sys/types.h>
9 #include <unistd.h>
10 
11 // oo wrapper around libinetsocket
12 /*
13  The committers of the libsocket project, all rights reserved
14  (c) 2012, dermesser <lbo@spheniscida.de>
15 
16  Redistribution and use in source and binary forms, with or without
17  modification, are permitted provided that the following conditions are met:
18 
19  1. Redistributions of source code must retain the above copyright notice,
20  this list of conditions and the following disclaimer.
21  2. Redistributions in binary form must reproduce the above copyright notice,
22  this list of conditions and the following disclaimer in the documentation
23  and/or other materials provided with the distribution.
24 
25  THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS “AS IS” AND ANY
26  EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
27  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
28  DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
29  DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
30  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
31  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
32  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
34  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 
36 */
37 
47 #include <libinetsocket.h>
48 #include <exception.hpp>
49 #include <inetdgram.hpp>
50 
51 namespace libsocket {
52 using std::string;
53 
54 // I/O
55 
56 // I
57 
79 ssize_t inet_dgram::rcvfrom(void* buf, size_t len, char* hostbuf,
80  size_t hostbuflen, char* portbuf, size_t portbuflen,
81  int rcvfrom_flags, bool numeric) {
82  ssize_t bytes;
83  int num = ((numeric == true) ? LIBSOCKET_NUMERIC : 0);
84 
85  if (-1 == sfd)
86  throw socket_exception(__FILE__, __LINE__,
87  "inet_dgram::rcvfrom() - Socket is closed!",
88  false);
89 
90  if (-1 == (bytes = recvfrom_inet_dgram_socket(
91  sfd, buf, len, hostbuf, hostbuflen, portbuf, portbuflen,
92  rcvfrom_flags, num))) {
93  if (is_nonblocking && errno == EWOULDBLOCK)
94  return -1;
95  else
96  throw socket_exception(__FILE__, __LINE__,
97  "inet_dgram::rcvfrom() - recvfrom() failed "
98  "-- could not receive data from peer!");
99  }
100 
101  return bytes;
102 }
103 
122 ssize_t inet_dgram::rcvfrom(void* buf, size_t len, string& srchost,
123  string& srcport, int rcvfrom_flags, bool numeric) {
124  ssize_t bytes;
125 
126  using std::unique_ptr;
127  unique_ptr<char[]> from_host(new char[1024]);
128  unique_ptr<char[]> from_port(new char[32]);
129 
130  memset(from_host.get(), 0, 1024);
131  memset(from_port.get(), 0, 32);
132  memset(buf, 0, len);
133 
134  // Error checking already done in rcvfrom() method.
135  bytes = rcvfrom(buf, len, from_host.get(), 1023, from_port.get(), 31,
136  rcvfrom_flags, numeric);
137 
138  srchost.resize(strlen(from_host.get()));
139  srcport.resize(strlen(from_port.get()));
140 
141  srchost.assign(from_host.get());
142  srcport.assign(from_port.get());
143 
144  return bytes;
145 }
146 
167 ssize_t inet_dgram::rcvfrom(string& buf, string& srchost, string& srcport,
168  int rcvfrom_flags, bool numeric) {
169  ssize_t bytes;
170 
171  using std::unique_ptr;
172  unique_ptr<char[]> cbuf(new char[buf.size()]);
173 
174  memset(cbuf.get(), 0, buf.size());
175 
176  bytes = rcvfrom(cbuf.get(), static_cast<size_t>(buf.size()), srchost,
177  srcport, rcvfrom_flags,
178  numeric); // calling inet_dgram::rcvfrom(void*, size_t,
179  // string&, string&, int, bool)
180 
181  buf.resize(bytes);
182 
183  buf.assign(cbuf.get(), bytes);
184 
185  return bytes;
186 }
187 
188 // O
189 
207 ssize_t inet_dgram::sndto(const void* buf, size_t len, const char* dsthost,
208  const char* dstport, int sndto_flags) {
209  ssize_t bytes;
210 
211  if (-1 == sfd)
212  throw socket_exception(__FILE__, __LINE__,
213  "inet_dgram::sendto() - Socket already closed!",
214  false);
215 
216  if (-1 == (bytes = sendto_inet_dgram_socket(sfd, buf, len, dsthost, dstport,
217  sndto_flags))) {
218  if (is_nonblocking && errno == EWOULDBLOCK)
219  return -1;
220  else
221  throw socket_exception(__FILE__, __LINE__,
222  "inet_dgram::sndto() - Error at sendto");
223  }
224 
225  return bytes;
226 }
227 
243 ssize_t inet_dgram::sndto(const void* buf, size_t len, const string& dsthost,
244  const string& dstport, int sndto_flags) {
245  ssize_t bytes;
246 
247  bytes = sndto(buf, len, dsthost.c_str(), dstport.c_str(), sndto_flags);
248 
249  return bytes;
250 }
251 
266 ssize_t inet_dgram::sndto(const string& buf, const string& dsthost,
267  const string& dstport, int sndto_flags) {
268  ssize_t bytes;
269 
270  bytes = sndto(buf.c_str(), buf.size(), dsthost.c_str(), dstport.c_str(),
271  sndto_flags);
272 
273  return bytes;
274 }
275 } // namespace libsocket
Contains libsocket elements.
Definition: dgramclient.hpp:41
This class is instantiated and thrown when an error occurs. If there's an error somewhere in libsocke...
Definition: exception.hpp:52
ssize_t sendto_inet_dgram_socket(int sfd, const void *buf, size_t size, const char *host, const char *service, int sendto_flags)
This function is the equivalent to sendto(2)
ssize_t rcvfrom(void *buf, size_t len, char *srchost, size_t hostlen, char *srcport, size_t portlen, int rcvfrom_flags=0, bool numeric=false)
Receives data from peer.
Definition: inetdgram.cpp:79
int sfd
sfd is the sockets API file descriptor
Definition: socket.hpp:74
ssize_t recvfrom_inet_dgram_socket(int sfd, void *buffer, size_t size, char *src_host, size_t src_host_len, char *src_service, size_t src_service_len, int recvfrom_flags, int numeric)
Receive data from a UDP/IP socket.
ssize_t sndto(const void *buf, size_t len, const char *dsthost, const char *dstport, int sndto_flags=0)
Send data to UDP peer.
Definition: inetdgram.cpp:207
Contains all functions available in the C libsocket.