summaryrefslogtreecommitdiff
path: root/src/udpsocket.cpp
blob: b577302f8dc396ba7827931af523d60c728550d9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
/*
 * Copyright (C) 2007-2011 Xagasoft, All rights reserved.
 *
 * This file is part of the libbu++ library and is released under the
 * terms of the license contained in the file LICENSE.
 */

#ifndef WIN32 //not on windows

#include "bu/udpsocket.h"

#include "bu/sio.h"
using namespace Bu;
#include <fcntl.h>

#include <errno.h>
#include <arpa/inet.h> 
#include <sys/socket.h>
#include <netinet/in.h>
#include <sys/utsname.h>

namespace Bu { subExceptionDef( UdpSocketException ) }

#define saTarget ( *((struct sockaddr_in *)paTarget) )

Bu::UdpSocket::UdpSocket( int iUdpSocket ) :
	iUdpSocket( iUdpSocket ),
	paTarget( NULL ),
	bBound( false )
{
}

Bu::UdpSocket::UdpSocket( const Bu::String &sAddr, int iPort, int iFlags ) :
	iUdpSocket( 0 ),
	paTarget( NULL ),
	bBound( false )
{
	iUdpSocket = socket( PF_INET, SOCK_DGRAM, 0 );
	if( iUdpSocket < 0 )
	{
		throw UdpSocketException("Couldn't open udp socket: %s",
			strerror( errno )
			);
	}

	if( (iFlags&Broadcast) )
	{
		int broadcast = 1;
		if( (setsockopt( iUdpSocket, SOL_SOCKET, SO_BROADCAST,
			&broadcast, sizeof(broadcast) )) == -1)
		{
			throw UdpSocketException("Couldn't set udp socket to broadcast: %s",
				strerror( errno )
				);
		}
	}
	
	paTarget = new struct sockaddr_in;
	saTarget.sin_family = AF_INET;
	saTarget.sin_port = htons( iPort );
	saTarget.sin_addr.s_addr = inet_addr( sAddr.getStr() ); // INADDR_ANY;
	memset( saTarget.sin_zero, '\0', sizeof(saTarget.sin_zero) );
	
	if( (iFlags&Read) )
	{
		if( bind( iUdpSocket, (struct sockaddr*)paTarget, sizeof(struct sockaddr_in) )
				== -1 )
		{
			throw UdpSocketException("Couldn't bind port to udp socket: %s",
				strerror( errno )
				);
		}
		bBound = true;
	}
}

Bu::UdpSocket::~UdpSocket()
{
	close();
	delete (struct sockaddr_in *)paTarget;
	paTarget = NULL;
}

Bu::String Bu::UdpSocket::addrToStr( const addr &a )
{
	Bu::String sOut;
	sOut.format("%d.%d.%d.%d",
		(a&0xff),
		(a&0xff00)>>8,
		(a&0xff0000)>>16,
		(a&0xff000000)>>24
		);

	return sOut;
}

void Bu::UdpSocket::close()
{
	::close( iUdpSocket );
}

Bu::size Bu::UdpSocket::read( void *pBuf, Bu::size nBytes )
{
	return recv( iUdpSocket, pBuf, nBytes, 0 );
}

Bu::size Bu::UdpSocket::read( void *pBuf, Bu::size nBytes,
		Bu::UdpSocket::addr &aHost, int &iPort )
{
	sockaddr_in name;
	size_t size = sizeof(name);
	Bu::size ret = recvfrom( iUdpSocket, pBuf, nBytes, 0,
			(struct sockaddr *)&name, &size );
	aHost = name.sin_addr.s_addr;
	iPort = ntohs(name.sin_port);
	return ret;
}

Bu::size Bu::UdpSocket::write( const void *pBuf, Bu::size nBytes )
{
	if( bBound )
	{
		return sendto( iUdpSocket, pBuf, nBytes, 0, NULL, 0 );
	}
	else
	{
		return sendto( iUdpSocket, pBuf, nBytes, 0,
			(struct sockaddr*)paTarget, sizeof(struct sockaddr_in) );
	}
}

Bu::size Bu::UdpSocket::tell()
{
	throw Bu::UnsupportedException();
}

void Bu::UdpSocket::seek( Bu::size )
{
	throw Bu::UnsupportedException();
}

void Bu::UdpSocket::setPos( Bu::size )
{
	throw Bu::UnsupportedException();
}

void Bu::UdpSocket::setPosEnd( Bu::size )
{
	throw Bu::UnsupportedException();
}

bool Bu::UdpSocket::isEos()
{
	return false;
}

bool Bu::UdpSocket::isOpen()
{
	return true;
}

void Bu::UdpSocket::flush()
{
}

bool Bu::UdpSocket::canRead()
{
	return bBound;
}

bool Bu::UdpSocket::canWrite()
{
	return true;
}

bool Bu::UdpSocket::isReadable()
{
	return bBound;
}

bool Bu::UdpSocket::isWritable()
{
	return true;
}

bool Bu::UdpSocket::isSeekable()
{
	return false;
}

bool Bu::UdpSocket::isBlocking()
{
	return true;
}

void Bu::UdpSocket::setBlocking( bool bBlocking )
{
#ifndef WIN32
	if( bBlocking )
	{
		fcntl( iUdpSocket, F_SETFL, fcntl( iUdpSocket, F_GETFL, 0 ) & (~O_NONBLOCK) );
	}
	else
	{
		fcntl( iUdpSocket, F_SETFL, fcntl( iUdpSocket, F_GETFL, 0 ) | O_NONBLOCK );
	}
#else
	u_long iMode;
	if( bBlocking )
		iMode = 0;
	else
		iMode = 1;
	//-------------------------
	// Set the socket I/O mode: In this case FIONBIO
	// enables or disables the blocking mode for the 
	// socket based on the numerical value of iMode.
	// If iMode = 0, blocking is enabled; 
	// If iMode != 0, non-blocking mode is enabled.
	bu_ioctlsocket(iUdpSocket, FIONBIO, &iMode);
#endif	
}

void Bu::UdpSocket::setSize( Bu::size )
{
	throw Bu::UnsupportedException();
}

Bu::size Bu::UdpSocket::getSize() const
{
	throw Bu::UnsupportedException();
}

Bu::size Bu::UdpSocket::getBlockSize() const
{
	return 1500;
}

Bu::String Bu::UdpSocket::getLocation() const
{
	throw Bu::UnsupportedException();
}

#endif