]> git.vomp.tv Git - vompserver.git/blob - dsock.c
New logging system, bug fixed where client could gazump recordings or
[vompserver.git] / dsock.c
1 /*
2     Copyright 2004-2005 Chris Tallon
3
4     This file is part of VOMP.
5
6     VOMP is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     VOMP is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with VOMP; if not, write to the Free Software
18     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19 */
20
21 #include "dsock.h"
22
23 DatagramSocket::DatagramSocket(short port)
24 {
25   myPort = port;
26   addrlen = sizeof(struct sockaddr);
27   log = Log::getInstance();
28   initted = 0;
29
30   if ((socketnum = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1)
31   {
32     log->log("UDP", Log::CRIT, "Socket error");
33     return;
34   }
35
36   myAddr.sin_family = AF_INET;         // host byte order
37   myAddr.sin_port = htons(myPort);     // short, network byte order
38   myAddr.sin_addr.s_addr = INADDR_ANY; // auto-fill with my IP
39   memset(&(myAddr.sin_zero), 0, 8);    // zero the rest of the struct
40   if (bind(socketnum, (struct sockaddr *)&myAddr, addrlen) == -1)
41   {
42     log->log("UDP", Log::CRIT, "Bind error");
43     close(socketnum);
44     return;
45   }
46   initted = 1;
47
48   FD_ZERO(&readfds);
49   FD_SET(socketnum, &readfds);
50   tv.tv_sec = 0;
51   tv.tv_usec = 0;
52 }
53
54 DatagramSocket::~DatagramSocket()
55 {
56   if (initted) close(socketnum);
57   initted = 0;
58 }
59
60 unsigned char DatagramSocket::waitforMessage(unsigned char how)
61 {
62   if (!initted) return 0;
63
64   /* how = 0 - block
65      how = 1 - start new wait
66      how = 2 - continue wait
67   */
68
69   struct timeval* passToSelect = NULL;
70
71
72   if (how == 0)
73   {
74     passToSelect = NULL;
75   }
76   else if (how == 1)
77   {
78     tv.tv_sec = 1;
79     tv.tv_usec = 100000;
80     passToSelect = &tv;
81   }
82   else if (how == 2)
83   {
84     if ((tv.tv_sec == 0) && (tv.tv_usec == 0))  // protection in case timer = 0
85     {
86       tv.tv_sec = 1;
87       tv.tv_usec = 100000;
88     }
89     passToSelect = &tv;
90   }
91   FD_ZERO(&readfds);
92   FD_SET(socketnum, &readfds);
93
94   if (select(socketnum + 1, &readfds, NULL, NULL, passToSelect) <= 0) return 1;
95
96   if ((mlength = recvfrom(socketnum, buf, MAXBUFLEN, 0, (struct sockaddr *)&theirAddr, &addrlen)) == -1)
97   {
98     log->log("UDP", Log::DEBUG, "recvfrom error");
99     return 0;
100   }
101   else
102   {
103     memset(&buf[mlength], 0, MAXBUFLEN - mlength);
104     strcpy(fromIPA, inet_ntoa(theirAddr.sin_addr));
105     fromPort = ntohs(theirAddr.sin_port);
106     log->log("UDP", Log::DEBUG, "%s:%i received length %i", fromIPA, fromPort, mlength);
107     return 2;
108   }
109
110   /* Return 0, failure
111      Return 1, nothing happened, timer expired
112      Return 2, packet arrived (timer not expired)
113   */
114 }
115
116 int DatagramSocket::getDataLength(void) const
117 {
118   return mlength;
119 }
120
121 char *DatagramSocket::getData(void)             {  return buf;  }
122 char *DatagramSocket::getFromIPA(void)          {  return fromIPA;  }
123 short DatagramSocket::getFromPort(void) const   {  return fromPort; }
124
125 void DatagramSocket::send(char *ipa, short port, char *message, int length)
126 {
127   int sentLength = 0;
128
129   theirAddr.sin_family = AF_INET;      // host byte order
130   theirAddr.sin_port = htons(port);    // short, network byte order
131   struct in_addr tad;                  // temp struct tad needed to pass to theirAddr.sin_addr
132   tad.s_addr = inet_addr(ipa);
133   theirAddr.sin_addr = tad;            // address
134   memset(&(theirAddr.sin_zero), 0, 8); // zero the rest of the struct
135
136   sentLength = sendto(socketnum, message, length, 0, (struct sockaddr *)&theirAddr, addrlen);
137   if (sentLength == length)
138   {
139     log->log("UDP", Log::DEBUG, "%s:%i sent length %i", ipa, port, length);
140     return;
141   }
142
143   log->log("UDP", Log::DEBUG, "%s:%i send failed %i", ipa, port, length);
144
145   sentLength = sendto(socketnum, message, length, 0, (struct sockaddr *)&theirAddr, addrlen);
146   if (sentLength == length)
147   {
148     log->log("UDP", Log::DEBUG, "%s:%i sent length %i 2nd try", ipa, port, length);
149     return;
150   }
151
152   log->log("UDP", Log::DEBUG, "%s:%i send failed %i 2nd try", ipa, port, length);
153 }
154