Package list ibutils / upstream/1.5.7+0.2.gbd7e502 ibmgtsim / src / tcpcomm.cpp
upstream/1.5.7+0.2.gbd7e502

Tree @upstream/1.5.7+0.2.gbd7e502 (Download .tar.gz)

tcpcomm.cpp @upstream/1.5.7+0.2.gbd7e502raw · history · blame

  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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
/*
 * Copyright (c) 2004-2010 Mellanox Technologies LTD. All rights reserved.
 *
 * This software is available to you under a choice of one of two
 * licenses.  You may choose to be licensed under the terms of the GNU
 * General Public License (GPL) Version 2, available from the file
 * COPYING in the main directory of this source tree, or the
 * OpenIB.org BSD license below:
 *
 *     Redistribution and use in source and binary forms, with or
 *     without modification, are permitted provided that the following
 *     conditions are met:
 *
 *      - Redistributions of source code must retain the above
 *        copyright notice, this list of conditions and the following
 *        disclaimer.
 *
 *      - Redistributions in binary form must reproduce the above
 *        copyright notice, this list of conditions and the following
 *        disclaimer in the documentation and/or other materials
 *        provided with the distribution.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 *
 */

#include <algorithm>
#include <sys/socket.h>     /* for socket(), bind(), and connect() */
#include <arpa/inet.h>      /* for sockaddr_in and inet_ntoa() */
#include <string.h>         /* for memset() */
#include <netdb.h>          /* for get host by name */
#include <unistd.h>         /* for close() */
#include "tcpcomm.h"
#include "msgmgr.h"


/****************************************************/
/***************** class GenServer ******************/
/****************************************************/
/*
* The basic flow:
*
* GenServer(portNum, maxMsgSize)
*  createServerSocket(portNum)
*  Create a server thread : open up a server socket
*   serverThreadMain : simply wait for client connections
*    Create client thread for every connection
*     clientThreadMain  : wait for incoming messages
*      recv - wait for data on the socket
*      while message size > 0 and no send error
*       proccessClientMsg : handle incoming message and return the response.
*       send - send the response
*      close the socket cleaning up reg in active threads list
*/


/* maximal number of pending socket connections to handle -
 * the simulator server allowing for multiple clients to connect */
#define MAXPENDING 5


/* create the tcp server socket */
/* return the socket number or -1 if error */
int GenServer::createServerSocket(unsigned short port_num)
{
    int sock;                           /* socket to create */
    struct sockaddr_in servAddr;        /* Local address */

    MSGREG(errMsg1, 'E', "Fail to open socket", "server");
    MSGREG(errMsg2, 'V', "Fail to bind socket for port:$", "server");
    MSGREG(errMsg3, 'E', "Fail to listen to socket", "server");
    MSGREG(verbMsg1, 'V', "Server is listening on port:$ socket:$", "server");

    /* Create socket for incoming connections */
    if ((sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) {
        MSGSND(errMsg1);
        return -1;
    }

    /* Construct local address structure */
    memset(&servAddr, 0, sizeof(servAddr));         /* Zero out structure */
    servAddr.sin_family = AF_INET;                  /* Internet address family */
    servAddr.sin_addr.s_addr = htonl(INADDR_ANY);   /* Any incoming interface */
    servAddr.sin_port = htons(port_num);            /* Local port */

    /* Bind to the local address */
    if (bind(sock, (struct sockaddr *) &servAddr, sizeof(servAddr)) < 0) {
        MSGSND(errMsg2, port_num);
        return -1;
    }

    /* Mark the socket so it will listen for incoming connections */
    if (listen(sock, MAXPENDING) < 0) {
        MSGSND(errMsg3);
        return -1;
    }

    MSGSND(verbMsg1, port_num, sock );
    return sock;
}


/*
  handle client request -
  either create a new client conn or pass it there

  PARAMETERS:
  [in] request      : is a buffer allocated by the calling code
  [in] reqLen       : the length of the received request
  [out] resLen      : the length of the result
  [out] response    : internally allocated buffer for the result.

  return 1 on error 0 otherwise
*/
int
GenServer::proccessClientMsg(int clientSock,
        int reqLen, char request[],
        int &resLen, char *(pResponse[]))
{
    MSGREG(info1, 'I', "Received message:$", "server");
    MSGSND(info1, request);

    /* for now only echo */
    resLen = reqLen;
    *pResponse = new char[reqLen];
    strcpy(*pResponse, request);

    return 0;
}


/*
   the client thread worker function - basically loops to handle
   new client messages.
   Obtains a pointer to the server object and the new client socket in
   the given args.
*/
void *
GenServer::clientThreadMain(void *threadArgs)
{
    MSGREG(err2 ,'E' ,"Fail to send message (sent:$ should:$)", "server");
    MSGREG(msg1 ,'V' ,"Closed connection with client:$", "server");
    MSGREG(msg2 ,'V' ,"Waiting for messages from client:$", "server");

    /* Guarantees that thread resources are deallocated upon return */
    pthread_t threadID = pthread_self();
    pthread_detach(threadID);

    ClientThreadArgs *clientThreadArgs =
            static_cast<ClientThreadArgs*>(threadArgs);

    /* Extract socket file descriptor from argument */
    GenServer *pServer = clientThreadArgs->pServer;
    int clientSocket = clientThreadArgs-> clientSock;

    /* Deallocate memory for argument */
    delete clientThreadArgs;

    /* we store the received message in this buffer */
    char request[pServer->maxMsgBytes];
    char *response;

    int recvMsgSize;            /* Size of received message */
    int responseMsgSize;        /* size of the response */
    int sentMsgSize;            /* Size of sent message */
    int errOnSend = 0;          /* failing to send should quit */

    MSGSND(msg2 , clientSocket);

    /* Receive message from client */
    recvMsgSize = recv(clientSocket, request, pServer->maxMsgBytes, 0);
    while ((recvMsgSize != 0) && (!errOnSend)) {
        /* Handle the request */
        if (!pServer->proccessClientMsg(clientSocket,
                recvMsgSize, request, responseMsgSize, &response)) {
            sentMsgSize =
                    send(clientSocket, response, responseMsgSize, 0);
            delete [] response;

            /* if we fail to send - it is probably cause we lost the socket */
            if (sentMsgSize != responseMsgSize) {
                MSGSND(err2, sentMsgSize, responseMsgSize);
                errOnSend = 1;
            }
        }

        recvMsgSize = recv(clientSocket, request, pServer->maxMsgBytes, 0);
    }

    /* Close client socket */
    close(clientSocket);

    /* obtain the lock when cleaning up */
    pthread_mutex_lock(&pServer->lock);

    /* callback that can be used by server extensions for cleanup */
    pServer->closingClient(clientSocket);

    /* remove the client thread from the list of threads */
    std::list< pthread_t >::iterator lI =
            std::find(pServer->clientThreadsList.begin(),
                    pServer->clientThreadsList.end(),
                    threadID);
    if (lI != pServer->clientThreadsList.end())
        pServer->clientThreadsList.erase(lI);

    pthread_mutex_unlock(&pServer->lock);

    MSGSND(msg1 , clientSocket);
    return (NULL);
}


/* the server thread worker function */
/* obtains a pointer to the server object as its arg */
void *
GenServer::serverThreadMain(void *args)
{
    ServerThreadArgs *pArgs = static_cast<ServerThreadArgs *>(args);
    GenServer *pServer = pArgs->pServer;
    delete pArgs;

    MSGREG(errMsg1, 'E', "Fail to accept client", "server");
    MSGREG(verbMsg1, 'V', "Handling client $", "server");

    for (;;) {      /* run forever */
        int clntSock;                   /* Socket descriptor for client */
        struct sockaddr_in clntAddr;    /* Client address */
        unsigned int clntLen;           /* Length of client address data struct*/
        struct ClientThreadArgs *threadArgs;

        /* Set the size of the in-out parameter */
        clntLen = sizeof(clntAddr);

        /* Wait for a client to connect */
        clntSock = accept(pServer->serverSock,
                (struct sockaddr *)&clntAddr, &clntLen);
        if (clntSock < 0) {
            MSGSND(errMsg1);
            continue;
        }

        /* clntSock is connected to a client! */
        MSGSND(verbMsg1, inet_ntoa(clntAddr.sin_addr));

        /* Create separate memory for client argument */
        threadArgs = new ClientThreadArgs;
        if (!threadArgs) {
            MSGSND(errMsg1);
            exit(1);
        }

        threadArgs->pServer = pServer;
        threadArgs->clientSock = clntSock;

        /* Create client thread */
        pthread_t threadID;
        if (pthread_create(&threadID,
                NULL,
                GenServer::clientThreadMain,
                (void *)threadArgs) != 0) {
            MSGSND(errMsg1);
        }

        /* we probably want to register the client thread in the list */
        pthread_mutex_lock(&pServer->lock);
        pServer->clientThreadsList.push_back(threadID);
        pthread_mutex_unlock(&pServer->lock);
    }
}


/* construct the server */
/* if the server is not initialized correctly the serverSock is -1 */
GenServer::GenServer(unsigned short portNum, int maxMsgLen)
{
    MSGREG(errMsg1, 'F', "Fail to create server thread", "server");
    MSGREG(verbMsg1, 'V', "Started server thread", "server");

    serverPort = portNum;
    maxMsgBytes = maxMsgLen;

    /* initialize the lock object */
    pthread_mutex_init(&lock, NULL);

    /* setup the server listening  socket */
    serverSock = createServerSocket(portNum);

    /* we might have failed to gen the server - so avoid generating the thread */
    if (serverSock > 0) {
        /* we malloc the args as we want the server thread to deallocate them */
        ServerThreadArgs *pServerArgs = new ServerThreadArgs;
        pServerArgs->pServer = this;

        /* start the server thread providing it the server main loop function */
        if (pthread_create(&serverThreadId,
                NULL,
                GenServer::serverThreadMain,
                (void *)pServerArgs) != 0) {
            MSGSND(errMsg1);
            exit(1);
        }
    }
    MSGSND(verbMsg1);
}


/* server destructor */
GenServer::~GenServer()
{
    MSGREG(inf1, 'V', "Closing server on port:$", "server");
    MSGREG(inf2, 'V', "Cancelling server thread:$", "server");
    MSGREG(inf3, 'V', "Cancelling client thread:$", "server");

    MSGSND(inf1, serverPort);

    /* cleanup threads */
    pthread_mutex_lock(&lock);
    MSGSND(inf2, serverThreadId);

    /* we only have a thread if the socket was opened */
    if (isAlive()) {
        pthread_cancel(serverThreadId);

        for (std::list< pthread_t >::iterator tI = clientThreadsList.begin();
                tI != clientThreadsList.end();
                ++tI) {
            MSGSND(inf3, (*tI));
            pthread_cancel((*tI));
        }
    }
    pthread_mutex_unlock(&lock);
}


/****************************************************/
/***************** class GenClient ******************/
/****************************************************/
/* construct or die */
GenClient::GenClient(char *pHostName,
        unsigned short portNum,
        int maxRespLen)
{
    struct sockaddr_in servAddr;    /* server address */
    struct hostent *pHostEntry;     /* to be filled by gethostbyname */
    struct in_addr in;

    MSGREG(err1, 'F', "Fail to create socket", "client");
    MSGREG(err2, 'F', "Fail to gethostbyname:$", "client");
    MSGREG(err3, 'F', "No address list for host:$", "client");
    MSGREG(err4, 'F', "connect() failed for host:$ port:$", "client");
    MSGREG(inf1, 'I', "Connecting to host:$ ip:$ port:$", "client");

    hostName = new char[strlen(pHostName)+1];
    strcpy(hostName, pHostName);
    serverPort = portNum;
    maxResponseBytes = maxRespLen;

    /* Create a reliable, stream socket using TCP */
    if ((sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) {
        MSGSND(err1);
        exit(1);
    }

    pHostEntry = gethostbyname(hostName);
    if (!pHostEntry) {
        MSGSND(err2, hostName);
        exit(1);
    }

    if (!pHostEntry->h_addr_list) {
        MSGSND(err3, hostName);
        exit(1);
    }

    memcpy(&in.s_addr, pHostEntry->h_addr_list[0], sizeof(in.s_addr));
    MSGSND(inf1, hostName, inet_ntoa(in), portNum);

    /* Construct the server address structure */
    memset(&servAddr, 0, sizeof(servAddr));                 /* Zero out structure */
    servAddr.sin_family      = AF_INET;                     /* Internet address family */
    servAddr.sin_addr.s_addr = inet_addr(inet_ntoa(in));    /* Server IP address */
    servAddr.sin_port        = htons(portNum);              /* Server port */


    /* Establish the connection to the echo server */
    if (connect(sock, (struct sockaddr *) &servAddr, sizeof(servAddr)) < 0) {
        MSGSND(err4, hostName, portNum);
        exit(1);
    }
}


/* destructor - closing the socket */
GenClient::~GenClient()
{
    MSGREG(inf1, 'I', "Closing connection to server:$ port:$", "client");
    MSGSND(inf1, hostName, serverPort);
    close(sock);
}


/*
   send a message and wait for result
   The result buffer should be provided by the caller.
*/
int
GenClient::sendMsg(int reqLen, char request[],
        int &resLen, char response[])
{
    MSGREG(err1, 'E', "Fail to send.", "client");
    MSGREG(err2, 'E', "Fail to receive any response.", "client");

    /* Send the string to the server */
    if (send(sock, request, reqLen, 0) != reqLen) {
        MSGSND(err1);
        return 1;
    }

    /* Receive the same string back from the server */
    if ((resLen = recv(sock, response, maxResponseBytes, 0)) <= 0) {
        MSGSND(err2);
        return 1;
    }

    return 0;
}


/****************************************************/
/************** BUILD_TCP_COMM_SERVER ***************/
/****************************************************/
#ifdef BUILD_TCP_COMM_SERVER

#define MAX_MSG_SIZE 256

int main( int argc, char** argv)
{
    unsigned short servPort = 42561;    /* server port */
    char *hostName;                     /* Server Host Name */
    int bytesRcvd, totalBytesRcvd;      /* Bytes read in single recv() and total bytes read */
    if (argc > 2) { /* Test for correct number of arguments */
        fprintf(stderr, "Usage: %s [<Server Port>]\n", argv[0]);
        exit(1);
    }

    if (argc > 1) {
        servPort = atoi(argv[1]);
    }

    msgMgr(MsgShowAll, &std::cout);
    GenServer server(servPort, MAX_MSG_SIZE);

    while (1)
        sleep(100);

    exit(0);
}

#endif  /* BUILD_TCP_COMM_SERVER */


/****************************************************/
/************** BUILD_TCP_COMM_CLIENT ***************/
/****************************************************/
#ifdef BUILD_TCP_COMM_CLIENT

#define MAX_MSG_SIZE 256

int main( int argc, char** argv)
{
    unsigned short servPort;            /* server port */
    char *hostName;                     /* Server Host Name */
    int bytesRcvd, totalBytesRcvd;      /* Bytes read in single recv() and total bytes read */
    char *request   = new char[MAX_MSG_SIZE];
    char *response  = new char[MAX_MSG_SIZE];

    if ((argc < 3) || (argc > 4)) { /* Test for correct number of arguments */
        fprintf(stderr, "Usage: %s <Server IP> <msg> [<Server Port>]\n", argv[0]);
        exit(1);
    }

    hostName = argv[1];
    strcpy(request, argv[2]);

    if (argc == 4)
        servPort = atoi(argv[3]);     /* Use given port, if any */
    else
        servPort = 42561;

    msgMgr(MsgShowAll, &std::cout);

    /* initialize the client */
    MSGREG(msg1, 'I', "Initializing Client: Server:$ Port:$", "client");
    MSGSND(msg1, hostName, servPort);

    GenClient client( hostName, servPort, MAX_MSG_SIZE );

    /* send first message - client connect */
    MSGREG(msg2, 'I', "Sending (size:$) :$", "client");
    MSGSND(msg2, strlen(request)+1, request);
    int reqLen = strlen(request)+1;
    if (!client.sendMsg(reqLen, request, bytesRcvd, response)) {
        MSGREG(msg3, 'I', "Received (len:$) :$", "client");
        MSGSND(msg3, bytesRcvd, response);
    }
    exit(0);
}

#endif  /* BUILD_TCP_COMM_CLIENT */