00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040 #include "cxutils/networking/udpserver.h"
00041 #include <string.h>
00042 #include <limits.h>
00043 #include <iostream>
00044
00045 #ifdef WIN32
00046 #else
00047 #include <errno.h>
00048 #endif
00049
00050 using namespace std;
00051 using namespace CxUtils;
00052
00058 UdpServer::UdpServer()
00059 {
00060 mPort = 0;
00061 mComputer = 0;
00062 mHP = NULL;
00063 memset((void *)&mService, 0, sizeof(struct sockaddr_in));
00064 memset((void *)&mSendAddr, 0, sizeof(struct sockaddr_in));
00065 memset((void *)&mRecvAddr, 0, sizeof(struct sockaddr_in));
00066 mSocketType = UDPServer;
00067 }
00068
00074 UdpServer::~UdpServer()
00075 {
00076 Shutdown();
00077 }
00078
00084 void UdpServer::Shutdown()
00085 {
00086 if(mSocket)
00087 {
00088 #ifdef WIN32
00089 closesocket(mSocket);
00090 #else
00091 close(mSocket);
00092 #endif
00093 mSocket = 0;
00094 }
00095 Socket::Shutdown();
00096 mPort = 0;
00097 mComputer = 0;
00098 this->mSocketType = UDPServer;
00099 memset((void *)&mService, 0, sizeof(struct sockaddr_in));
00100 memset((void *)&mSendAddr, 0, sizeof(struct sockaddr_in));
00101 memset((void *)&mRecvAddr, 0, sizeof(struct sockaddr_in));
00102 }
00103
00104
00114 int UdpServer::InitializeSocket(const unsigned short port)
00115 {
00116
00117 Shutdown();
00118
00119 memset((void *)&mService, 0, sizeof(struct sockaddr_in));
00120 memset((void *)&mSendAddr, 0, sizeof(struct sockaddr_in));
00121 memset((void *)&mRecvAddr, 0, sizeof(struct sockaddr_in));
00122
00123
00124 mSocket = (int)socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
00125 mPort = port;
00126
00127 if(mSocket > 0)
00128 {
00129 mRecvAddr.sin_family = AF_INET;
00130 mRecvAddr.sin_port = htons(mPort);
00131 mRecvAddr.sin_addr.s_addr = htonl(INADDR_ANY);
00132
00133 int bufferSize = 262144;
00134 int on = 1;
00135
00136 if( bind(mSocket, (struct sockaddr *) &mRecvAddr, sizeof(mRecvAddr)) >= 0)
00137 {
00138 int code = 0;
00139
00140 if(mNetworkInterface >= 0)
00141 {
00142 IP4Address::List myHostnames;
00143 GetHostAddresses(myHostnames);
00144 if(mNetworkInterface < (int)(myHostnames.size()))
00145 {
00146 mRecvAddr.sin_addr.s_addr = *((long *)gethostbyname(myHostnames[mNetworkInterface].mString.c_str())->h_addr);
00147 mRecvAddress = IP4Address(myHostnames[mNetworkInterface]);
00148 setsockopt(mSocket, IPPROTO_IP, IP_MULTICAST_IF, (const char*) &mRecvAddr, sizeof(mRecvAddr));
00149 }
00150 }
00151
00152 int options = 0;
00153 options |= setsockopt(mSocket, SOL_SOCKET, SO_RCVBUF, (char *)&bufferSize, (int)sizeof(bufferSize));
00154 options |= setsockopt(mSocket, SOL_SOCKET, SO_SNDBUF, (char *)&bufferSize, (int)sizeof(bufferSize));
00155
00156 if(options != 0)
00157 {
00158 cout << "UDP ERROR: Failed to set UDP socket options.\n";
00159 Shutdown();
00160 return 0;
00161 }
00162
00163 mServiceLength = sizeof(mService);
00164
00165 mGoodSocket = true;
00166 memcpy(&mService, &mRecvAddr, sizeof(mRecvAddr));
00167 memcpy(&this->mSendAddr, (struct sockaddr *)(&mService), sizeof(mSendAddr));
00168 }
00169 else
00170 {
00171
00172 Shutdown();
00173 }
00174 }
00175
00176 return mGoodSocket;
00177
00178 }
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235
00252 int UdpServer::InitializeMulticastSocket(const unsigned short port,
00253 const IP4Address& multicastGroup,
00254 const bool allowReuse)
00255 {
00256
00257 Shutdown();
00258
00259 memset((void *)&mService, 0, sizeof(struct sockaddr_in));
00260 memset((void *)&mSendAddr, 0, sizeof(struct sockaddr_in));
00261 memset((void *)&mRecvAddr, 0, sizeof(struct sockaddr_in));
00262
00263
00264 mSocket = (int)socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
00265 mPort = port;
00266
00267 if(mSocket > 0)
00268 {
00269 mRecvAddr.sin_family = AF_INET;
00270 mRecvAddr.sin_port = htons(mPort);
00271 mRecvAddr.sin_addr.s_addr = htonl(INADDR_ANY);
00272
00273 int bufferSize = 262144;
00274 int on = 1;
00275
00276
00277 if(allowReuse == true && setsockopt(mSocket, SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on)) < 0)
00278 {
00279 cout << "UDP ERROR: setsockopt failed SO_REUSEADDR. ERRNO: " << errno << "-" << strerror(errno) << "\n";
00280 Shutdown();
00281 return 0;
00282 }
00283
00284 if(bind(mSocket, (struct sockaddr *) &mRecvAddr, sizeof(mRecvAddr)) >= 0)
00285 {
00286 struct ip_mreq mreq;
00287 int code = 0;
00288
00289 if(mNetworkInterface >= 0)
00290 {
00291 IP4Address::List myHostnames;
00292 GetHostAddresses(myHostnames);
00293 if(mNetworkInterface < (int)(myHostnames.size()))
00294 {
00295
00296
00297 mRecvAddr.sin_addr.s_addr = *((long *)gethostbyname(myHostnames[mNetworkInterface].mString.c_str())->h_addr);
00298 mRecvAddress = IP4Address(myHostnames[mNetworkInterface]);
00299 mreq.imr_interface.s_addr = inet_addr(mRecvAddress.mString.c_str());
00300 }
00301 else
00302 {
00303 mreq.imr_interface.s_addr = htonl(INADDR_ANY);
00304 }
00305 }
00306 else
00307 {
00308 mreq.imr_interface.s_addr = htonl(INADDR_ANY);
00309 }
00310
00311 mreq.imr_multiaddr.s_addr = inet_addr(multicastGroup.mString.c_str());
00312
00313 int options = 0;
00314 options |= setsockopt(mSocket, SOL_SOCKET, SO_RCVBUF, (char *)&bufferSize, (int)sizeof(bufferSize));
00315 options |= setsockopt(mSocket, SOL_SOCKET, SO_SNDBUF, (char *)&bufferSize, (int)sizeof(bufferSize));
00316 options |= setsockopt(mSocket, IPPROTO_IP, IP_ADD_MEMBERSHIP, (char *)&mreq, sizeof(mreq));
00317
00318 if(options != 0)
00319 {
00320 #ifdef WIN32
00321 int errorCode = WSAGetLastError();
00322 cout << "UDP ERROR: " << errorCode << "\n";
00323 #else
00324 cout << "UDP ERROR: Failed to set UDP socket options.\n";
00325 #endif
00326 Shutdown();
00327 return 0;
00328 }
00329
00330 mServiceLength = sizeof(mService);
00331
00332 mGoodSocket = true;
00333 memcpy(&mService, &mRecvAddr, sizeof(mRecvAddr));
00334 memcpy(&this->mSendAddr, (struct sockaddr *)(&mService), sizeof(mSendAddr));
00335 }
00336 else
00337 {
00338
00339 Shutdown();
00340 }
00341 }
00342
00343 return mGoodSocket;
00344
00345 }
00346
00347
00365 int UdpServer::RecvToBuffer(char *buffer,
00366 const unsigned int length,
00367 const long int timeOutMilliseconds,
00368 IPAddress* ipAddress,
00369 unsigned short* port) const
00370 {
00371 if(!IsValid())
00372 {
00373 return 0;
00374 }
00375
00376 if(!Socket::IsIncommingData(this, timeOutMilliseconds))
00377 {
00378 return 0;
00379 }
00380
00381 int rcvd = 0;
00382
00383 sockaddr *r, *s;
00384
00385 r = (sockaddr *)(&mRecvAddr);
00386 s = (sockaddr *)(&mSendAddr);
00387 memset(r, 0, sizeof(struct sockaddr));
00388 int recvlen = sizeof(struct sockaddr);
00389
00390 #ifdef WIN32
00391 rcvd = recvfrom(mSocket,
00392 buffer,
00393 length,
00394 0,
00395 (struct sockaddr *)r,
00396 &recvlen);
00397 #else
00398 rcvd = recvfrom(mSocket,
00399 buffer,
00400 length,
00401 0,
00402 (struct sockaddr *)r,
00403 (socklen_t *)&recvlen);
00404 #endif
00405
00406
00407
00408 if(rcvd > 0)
00409 {
00410 memcpy(s, r, sizeof(struct sockaddr));
00411 if(port || ipAddress)
00412 {
00413 struct sockaddr_in in;
00414 memcpy(&in, &mRecvAddr, sizeof(struct sockaddr_in));
00415 if(ipAddress && dynamic_cast<IP4Address*>(ipAddress))
00416 {
00417 *((IP4Address*)(ipAddress)) = std::string(inet_ntoa(in.sin_addr));
00418 }
00419 if(port)
00420 {
00421 *port = (unsigned short)ntohs(in.sin_port);
00422 }
00423 }
00424 }
00425 else
00426 {
00427 rcvd = 0;
00428 }
00429
00430 return rcvd;
00431 }
00432
00433
00434