root/vtcross/trunk/src/lib/socketcomm/socketcomm.cpp @ 232

Revision 232, 7.7 KB (checked in by bhilburn, 15 years ago)

Changed more \t over to spaces. Please check your vimrcs!!!

Line 
1/* Virginia Tech Cognitive Radio Open Source Systems
2 * Virginia Tech, 2009
3 *
4 * TODO LICENSE INFORMATION GOES HERE
5 */
6
7/* TODO DESCRIPTION OF FILE.
8 */
9
10
11#include <arpa/inet.h>
12#include <cstdlib>
13#include <cstring>
14#include <iostream>
15#include <fcntl.h>
16#include <sys/ioctl.h>
17#include <netdb.h>
18#include <netinet/in.h>
19#include <stdint.h>
20#include <string>
21#include <sys/types.h>
22#include <sys/socket.h>
23
24#include "vtcross/common.h"
25#include "vtcross/containers.h"
26#include "vtcross/debug.h"
27#include "vtcross/error.h"
28#include "vtcross/socketcomm.h"
29
30
31// TODO can someone write a description of how this function is operating? I'm
32// not sure I understand why it is making two separate blocking calls to recv
33//
34// TODO also, it appears that this function can, at maximum, receive 256 bytes
35// without causing a buffer overflow. can someone confirm/deny?
36void
37ReadMessage(int32_t socketFD, char* msgBuffer)
38{
39
40    // Peek at the buffer to get real message length.
41    // Messages are termination with a "\0" to denote EOM.
42    ssize_t msgLength = recv(socketFD, msgBuffer, 256, MSG_PEEK);
43    if(msgLength < 0)
44        ERROR(1, "Error reading from socket.\n");
45    if(msgLength == 0)
46        ERROR(1, "Remote component closed connection. 1\n");
47
48    size_t i;
49    for(i = 0; i < 256; i++) {
50        if(strcmp(&msgBuffer[i], "\0") == 0)
51            break;
52    }
53
54    // Read the message into msgBuffer
55    msgLength = recv(socketFD, msgBuffer, i + 1, 0);
56    if(msgLength < 0)
57        ERROR(1, "Error reading from socket.\n");
58    if(msgLength == 0)
59        ERROR(1, "Remote component closed connection. 2\n");
60}
61
62
63int32_t
64ClientSocket(const char* serverName, const char* serverPort)
65{
66    int32_t socketFD;
67    int32_t portNumber;
68
69    struct sockaddr_in serv_addr;
70    struct hostent *server;
71   
72    server = gethostbyname(serverName);
73    if(server == NULL)
74        ERROR(1, "No server found by that hostname.\n");
75
76    portNumber = atoi(serverPort);
77
78    socketFD = socket(AF_INET, SOCK_STREAM, 0);
79    if(socketFD < 0)
80        ERROR(1, "Error opening socket\n");
81
82    memset((void *) &serv_addr, 0, sizeof(serv_addr));
83    serv_addr.sin_family = AF_INET;
84    serv_addr.sin_port = htons(portNumber);
85    memcpy((char *) &serv_addr.sin_addr.s_addr, (char *) server->h_addr, \
86            server->h_length);
87
88    if(connect(socketFD, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0)
89        ERROR(1, "Error connecting to remote socket.\n");
90
91    return socketFD;
92}
93
94   
95/* TODO I'm fairly certain this function is unnecessary, see function below for more details...
96int32_t
97SendMessage(int32_t socketFD, char* message)
98{
99     
100    // TODO explain this. What, exactly, does the below line do and how does it
101    // affect the rest of the function?
102    strcat(message, "\0000");
103
104    ssize_t numSentBytes = send(socketFD, message, (strlen(message) + 1), 0);
105    if(numSentBytes < 0) {
106        ERROR(1, "Error sending to server.");
107    }
108    else if(numSentBytes == 0) {
109        LOG("socket_comm::SendMessage - Server closed the socket.\n");
110    }
111   
112    return numSentBytes;
113}
114*/
115
116// TODO this function is here to handle calls to send const char* messages. Note
117// that the std::string.c_str() function auto-appends a null character at the
118// end of the cstring, so the strcat function call in the previous function
119// isn't necessary here... I think... although I still don't really understand
120// what exactly that call is for.
121int32_t
122SendMessage(int32_t socketFD, const char* message)
123{
124    ssize_t numSentBytes = send(socketFD, message, (strlen(message) + 1), 0);
125    if(numSentBytes < 0) {
126        ERROR(1, "Error sending to server %i (%i): %s\n",socketFD, numSentBytes, message);
127    }
128    else if(numSentBytes == 0) {
129        LOG("socket_comm::SendMessage - Server closed the socket.\n");
130    }
131   
132    return numSentBytes;
133}
134
135
136// TODO This function is currently returning 1... always... is this necessary?
137// If we want a fail/success return type, then why aren't we ever returning a
138// failure?
139int32_t
140GetParameter(int32_t socketFD, struct Parameter pList[], \
141        struct Radio_Info *radio_info)
142{
143    char buffer[256];
144    memset(buffer, 0, 256);
145
146    ReadMessage(socketFD, buffer);
147    radio_info->numParameters = atoi(buffer);
148    LOG("socket_comm::GetParameter - Number of parameters: %d\n", \
149            radio_info->numParameters);
150   
151    for(size_t i = 0; i < radio_info->numParameters; i++) {
152        memset(buffer, 0, 256);
153        ReadMessage(socketFD, buffer);
154        LOG("socket_comm::GetParameter - Name: %s\n", buffer);
155        pList[i].name = std::string(buffer);
156   
157        memset(buffer, 0, 256);
158        ReadMessage(socketFD, buffer);
159        LOG("socket_comm::GetParameter - Units: %s\n", buffer);
160        pList[i].units = std::string(buffer);
161
162        memset(buffer, 0, 256);
163        ReadMessage(socketFD, buffer);
164        LOG("socket_comm::GetParameter - Min: %s\n", buffer);
165        pList[i].min = atof(buffer);
166   
167        memset(buffer, 0, 256);
168        ReadMessage(socketFD, buffer);
169        LOG("socket_comm::GetParameter - Max: %s\n", buffer);
170        pList[i].max = atof(buffer);
171   
172        memset(buffer, 0, 256);
173        ReadMessage(socketFD, buffer);
174        LOG("socket_comm::GetParameter - Step: %s\n", buffer);
175        pList[i].step = atof(buffer);
176   
177        memset(buffer, 0, 256);
178        ReadMessage(socketFD, buffer);
179        LOG("socket_comm::GetParameter - Value: %s\n", buffer);
180        pList[i].value = atof(buffer);
181    }
182
183    return 1;
184}
185
186
187// TODO if we are just returing fail/success here, then why not return a bool
188// instead of an entire 32 bit integer?  Seems wasteful.
189int32_t
190GetRequest(int32_t socketFD, struct Parameter pList[], struct Radio_Info *radio_info)
191{
192    char buffer[256];
193    memset(buffer, 0, 256);
194   
195    ReadMessage(socketFD, buffer);
196
197    if(strcmp(buffer, "val") != 0) {
198        LOG("socket_comm::GetRequest - Unexpected control data received.\n\n");
199        return 0;
200    }
201
202    LOG("socket_comm::GetRequest - Getting parameters.\n\n");
203    GetParameter(socketFD, pList, radio_info);
204
205    return 1;
206}
207
208
209int32_t
210AcceptTCPConnection(int32_t serverSock)
211{
212    struct sockaddr_in echoClientAddr;
213
214    uint32_t clientLength = sizeof(echoClientAddr);
215
216    int32_t clientSocket = accept(serverSock, NULL, NULL);
217    if(clientSocket < 0) {
218        //WARNING("ALERT: Could not establish connection with client socket.\n");
219        return -1;
220    }
221    //LOG("Handling client %s\n", inet_ntoa(echoClientAddr.sin_addr));
222
223    return clientSocket;
224}
225
226
227int32_t
228CreateTCPServerSocket(uint16_t port)
229{
230    struct sockaddr_in echoServerAddr;
231
232    int32_t localSocket = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
233    if (localSocket < 0)
234        ERROR(1, "socket() failed\n");
235     
236    /* Construct the local address structure */
237    memset(&echoServerAddr, 0, sizeof(echoServerAddr));
238    echoServerAddr.sin_family = AF_INET;
239    echoServerAddr.sin_addr.s_addr = htonl(INADDR_ANY);
240    echoServerAddr.sin_port = htons(port);
241
242    /* Bind to the local address */
243    if(bind(localSocket, (struct sockaddr *) &echoServerAddr, \
244            sizeof(echoServerAddr)) < 0)
245        ERROR(1, "bind() failed\n");
246
247    /* Mark the socket so it will listen for incoming connections */
248    if(listen(localSocket, 5) < 0)
249        ERROR(1, "listen() failed\n");
250
251    return localSocket;
252}
253
254int32_t
255InitializeTCPServerPort(int32_t servSock)
256{
257    int32_t rc, on = 1;
258
259    rc = setsockopt(servSock, SOL_SOCKET, SO_REUSEADDR, (char*)&on, sizeof(on));
260    if(rc < 0) {
261        shutdown(servSock, 2);
262        close(servSock);
263        ERROR(1,"setsockopt() failed\n");
264    }
265
266    rc = ioctl(servSock, FIONBIO, (char*)&on);
267    if(rc < 0) {
268        shutdown(servSock, 2);
269        close(servSock);
270        ERROR(1,"ioctl() failed\n");
271    }
272
273    return 1;
274}
Note: See TracBrowser for help on using the browser.