udp.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. /*
  2. ** Command & Conquer Generals Zero Hour(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  4. **
  5. ** This program is free software: you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation, either version 3 of the License, or
  8. ** (at your option) any later version.
  9. **
  10. ** This program is distributed in the hope that it will be useful,
  11. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ** GNU General Public License for more details.
  14. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include "udp.h"
  19. #include "wlib/wdebug.h"
  20. UDP::UDP()
  21. {
  22. fd=0;
  23. }
  24. UDP::~UDP()
  25. {
  26. }
  27. sint32 UDP::Bind(char *Host,uint16 port)
  28. {
  29. char hostName[100];
  30. struct hostent *hostStruct;
  31. struct in_addr *hostNode;
  32. if (isdigit(Host[0]))
  33. return ( Bind( ntohl(inet_addr(Host)), port) );
  34. strcpy(hostName, Host);
  35. hostStruct = gethostbyname(Host);
  36. if (hostStruct == NULL)
  37. return (0);
  38. hostNode = (struct in_addr *) hostStruct->h_addr;
  39. return ( Bind(ntohl(hostNode->s_addr),port) );
  40. }
  41. // You must call bind, implicit binding is for sissies
  42. // Well... you can get implicit binding if you pass 0 for either arg
  43. sint32 UDP::Bind(uint32 IP,uint16 Port)
  44. {
  45. int retval;
  46. int status;
  47. IP=htonl(IP);
  48. Port=htons(Port);
  49. addr.sin_family=AF_INET;
  50. addr.sin_port=Port;
  51. addr.sin_addr.s_addr=IP;
  52. fd=socket(AF_INET,SOCK_DGRAM,DEFAULT_PROTOCOL);
  53. #ifdef _WINDOWS
  54. if (fd==SOCKET_ERROR)
  55. fd=-1;
  56. #endif
  57. if (fd==-1)
  58. return(UNKNOWN);
  59. retval=bind(fd,(struct sockaddr *)&addr,sizeof(addr));
  60. #ifdef _WINDOWS
  61. if (retval==SOCKET_ERROR)
  62. retval=-1;
  63. #endif
  64. if (retval==-1)
  65. {
  66. status=GetStatus();
  67. //CERR("Bind failure (" << status << ") IP " << IP << " PORT " << Port )
  68. return(status);
  69. }
  70. int namelen=sizeof(addr);
  71. getsockname(fd, (struct sockaddr *)&addr, &namelen);
  72. myIP=ntohl(addr.sin_addr.s_addr);
  73. myPort=ntohs(addr.sin_port);
  74. retval=SetBlocking(FALSE);
  75. if (retval==-1)
  76. fprintf(stderr,"Couldn't set nonblocking mode!\n");
  77. return(OK);
  78. }
  79. bit8 UDP::getLocalAddr(uint32 &ip, uint16 &port)
  80. {
  81. ip=myIP;
  82. port=myPort;
  83. return(OK);
  84. }
  85. // private function
  86. sint32 UDP::SetBlocking(bit8 block)
  87. {
  88. #ifdef _WINDOWS
  89. unsigned long flag=1;
  90. if (block)
  91. flag=0;
  92. int retval;
  93. retval=ioctlsocket(fd,FIONBIO,&flag);
  94. if (retval==SOCKET_ERROR)
  95. return(UNKNOWN);
  96. else
  97. return(OK);
  98. #else // UNIX
  99. int flags = fcntl(fd, F_GETFL, 0);
  100. if (block==FALSE) // set nonblocking
  101. flags |= O_NONBLOCK;
  102. else // set blocking
  103. flags &= ~(O_NONBLOCK);
  104. if (fcntl(fd, F_SETFL, flags) < 0)
  105. {
  106. return(UNKNOWN);
  107. }
  108. return(OK);
  109. #endif
  110. }
  111. sint32 UDP::Write(uint8 *msg,uint32 len,uint32 IP,uint16 port)
  112. {
  113. sint32 retval;
  114. struct sockaddr_in to;
  115. // This happens frequently
  116. if ((IP==0)||(port==0)) return(ADDRNOTAVAIL);
  117. errno=0;
  118. to.sin_port=htons(port);
  119. to.sin_addr.s_addr=htonl(IP);
  120. to.sin_family=AF_INET;
  121. ClearStatus();
  122. retval=sendto(fd,(char *)msg,len,0,(struct sockaddr *)&to,sizeof(to));
  123. #ifdef _WINDOWS
  124. if (retval==SOCKET_ERROR)
  125. retval=-1;
  126. #endif
  127. return(retval);
  128. }
  129. sint32 UDP::Read(uint8 *msg,uint32 len,sockaddr_in *from)
  130. {
  131. sint32 retval;
  132. int alen=sizeof(sockaddr_in);
  133. if (from!=NULL)
  134. {
  135. retval=recvfrom(fd,(char *)msg,len,0,(struct sockaddr *)from,&alen);
  136. #ifdef _WINDOWS
  137. if (retval==SOCKET_ERROR)
  138. retval=-1;
  139. #endif
  140. }
  141. else
  142. {
  143. retval=recvfrom(fd,(char *)msg,len,0,NULL,NULL);
  144. #ifdef _WINDOWS
  145. if (retval==SOCKET_ERROR)
  146. retval=-1;
  147. #endif
  148. }
  149. return(retval);
  150. }
  151. void UDP::ClearStatus(void)
  152. {
  153. #ifndef _WINDOWS
  154. errno=0;
  155. #endif
  156. }
  157. UDP::sockStat UDP::GetStatus(void)
  158. {
  159. #ifdef _WINDOWS
  160. int status=WSAGetLastError();
  161. if (status==0) return(OK);
  162. else if (status==WSAEINTR) return(INTR);
  163. else if (status==WSAEINPROGRESS) return(INPROGRESS);
  164. else if (status==WSAECONNREFUSED) return(CONNREFUSED);
  165. else if (status==WSAEINVAL) return(INVAL);
  166. else if (status==WSAEISCONN) return(ISCONN);
  167. else if (status==WSAENOTSOCK) return(NOTSOCK);
  168. else if (status==WSAETIMEDOUT) return(TIMEDOUT);
  169. else if (status==WSAEALREADY) return(ALREADY);
  170. else if (status==WSAEWOULDBLOCK) return(WOULDBLOCK);
  171. else if (status==WSAEBADF) return(BADF);
  172. else return(UNKNOWN);
  173. #else
  174. int status=errno;
  175. if (status==0) return(OK);
  176. else if (status==EINTR) return(INTR);
  177. else if (status==EINPROGRESS) return(INPROGRESS);
  178. else if (status==ECONNREFUSED) return(CONNREFUSED);
  179. else if (status==EINVAL) return(INVAL);
  180. else if (status==EISCONN) return(ISCONN);
  181. else if (status==ENOTSOCK) return(NOTSOCK);
  182. else if (status==ETIMEDOUT) return(TIMEDOUT);
  183. else if (status==EALREADY) return(ALREADY);
  184. else if (status==EAGAIN) return(AGAIN);
  185. else if (status==EWOULDBLOCK) return(WOULDBLOCK);
  186. else if (status==EBADF) return(BADF);
  187. else return(UNKNOWN);
  188. #endif
  189. }
  190. //
  191. // Wait for net activity on this socket
  192. //
  193. int UDP::Wait(sint32 sec,sint32 usec,fd_set &returnSet)
  194. {
  195. fd_set inputSet;
  196. FD_ZERO(&inputSet);
  197. FD_SET(fd,&inputSet);
  198. return(Wait(sec,usec,inputSet,returnSet));
  199. }
  200. //
  201. // Wait for net activity on a list of sockets
  202. //
  203. int UDP::Wait(sint32 sec,sint32 usec,fd_set &givenSet,fd_set &returnSet)
  204. {
  205. Wtime timeout,timenow,timethen;
  206. fd_set backupSet;
  207. int retval=0,done,givenMax;
  208. bit8 noTimeout=FALSE;
  209. timeval tv;
  210. returnSet=givenSet;
  211. backupSet=returnSet;
  212. if ((sec==-1)&&(usec==-1))
  213. noTimeout=TRUE;
  214. timeout.SetSec(sec);
  215. timeout.SetUsec(usec);
  216. timethen+=timeout;
  217. givenMax=fd;
  218. for (uint32 i=0; i<(sizeof(fd_set)*8); i++) // i=maxFD+1
  219. {
  220. if (FD_ISSET(i,&givenSet))
  221. givenMax=i;
  222. }
  223. ///DBGMSG("WAIT fd="<<fd<<" givenMax="<<givenMax);
  224. done=0;
  225. while( ! done)
  226. {
  227. if (noTimeout)
  228. retval=select(givenMax+1,&returnSet,0,0,NULL);
  229. else
  230. {
  231. timeout.GetTimevalMT(tv);
  232. retval=select(givenMax+1,&returnSet,0,0,&tv);
  233. }
  234. if (retval>=0)
  235. done=1;
  236. else if ((retval==-1)&&(errno==EINTR)) // in case of signal
  237. {
  238. if (noTimeout==FALSE)
  239. {
  240. timenow.Update();
  241. timeout=timethen-timenow;
  242. }
  243. if ((noTimeout==FALSE)&&(timenow.GetSec()==0)&&(timenow.GetUsec()==0))
  244. done=1;
  245. else
  246. returnSet=backupSet;
  247. }
  248. else // maybe out of memory?
  249. {
  250. done=1;
  251. }
  252. }
  253. ///DBGMSG("Wait retval: "<<retval);
  254. return(retval);
  255. }
  256. // Set the kernel buffer sizes for incoming, and outgoing packets
  257. //
  258. // Linux seems to have a buffer max of 32767 bytes for this,
  259. // (which is the default). If you try and set the size to
  260. // greater than the default it just sets it to 32767.
  261. bit8 UDP::SetInputBuffer(uint32 bytes)
  262. {
  263. #ifndef _WINDOWS
  264. int retval,arg=bytes;
  265. retval=setsockopt(fd,SOL_SOCKET,SO_RCVBUF,
  266. (char *)&arg,sizeof(int));
  267. if (retval==0)
  268. return(TRUE);
  269. else
  270. return(FALSE);
  271. #else
  272. return(FALSE);
  273. #endif
  274. }
  275. // Same note goes for the output buffer
  276. bit8 UDP::SetOutputBuffer(uint32 bytes)
  277. {
  278. #ifndef _WINDOWS
  279. int retval,arg=bytes;
  280. retval=setsockopt(fd,SOL_SOCKET,SO_SNDBUF,
  281. (char *)&arg,sizeof(int));
  282. if (retval==0)
  283. return(TRUE);
  284. else
  285. return(FALSE);
  286. #else
  287. return(FALSE);
  288. #endif
  289. }
  290. // Get the system buffer sizes
  291. int UDP::GetInputBuffer(void)
  292. {
  293. #ifndef _WINDOWS
  294. int retval,arg=0,len=sizeof(int);
  295. retval=getsockopt(fd,SOL_SOCKET,SO_RCVBUF,
  296. (char *)&arg,&len);
  297. return(arg);
  298. #else
  299. return(0);
  300. #endif
  301. }
  302. int UDP::GetOutputBuffer(void)
  303. {
  304. #ifndef _WINDOWS
  305. int retval,arg=0,len=sizeof(int);
  306. retval=getsockopt(fd,SOL_SOCKET,SO_SNDBUF,
  307. (char *)&arg,&len);
  308. return(arg);
  309. #else
  310. return(0);
  311. #endif
  312. }