IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Nov 8, 2011, 2:54:20 PM (15 years ago)
Author:
eugene
Message:

add nice ability to pantasks/pcontrol/pclient; add ports to pantasks_server; minor updates to dvo skycoverage, etc

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/Ohana/src/opihi/lib.shell/SocketOps.c

    r23530 r32632  
    11# include "shell.h"
     2# include <unistd.h>
     3# define HOST_NAME_MAX 256
    24
    35# define MY_PORT 2000
     
    1113static int *VALID;
    1214
    13 int InitServerSocket (SockAddress *Address) {
    14 
     15int GetPortRange (int *start, int *stop, char *portinfo);
     16
     17int InitServerSocket (SockAddress *Address, char *hostname, char *portinfo) {
     18
     19  int start, stop;
    1520  int status, InitSocket, length;
    16 
    17 # if (0)
    18   struct hostent  *host;
    19   char tmpline[80], hostip[80];
    20 
    21   host = gethostbyname (hostname);
    22   bzero (hostip, 80);
    23   for (i = 0; i < host[0].h_length; i++) {
    24     sprintf (tmpline, "%u", (0xff & host[0].h_addr[i]));
    25     strcat (hostip, tmpline);
    26     if (i < host[0].h_length - 1) strcat (hostip, ".");
    27   }
    28 # endif
    29  
     21  char myHostname[HOST_NAME_MAX];
     22
     23  status = gethostname (myHostname, HOST_NAME_MAX);
     24
     25  fprintf (stderr, "target host: %s, real host: %s\n", hostname, myHostname);
     26
     27  GetPortRange (&start, &stop, portinfo);
     28
     29  fprintf (stderr, "using port range %d - %d\n", start, stop);
     30
    3031  Address[0].sin_family = AF_INET;
    31   Address[0].sin_port   = MY_PORT;
     32  Address[0].sin_port   = start;
    3233  Address[0].sin_addr.s_addr = INADDR_ANY; // use this line to bind any address / port?
    3334
    3435retry_server:
    35 
    36 # if (0) 
    37   status = inet_aton (hostip, &Address[0].sin_addr);
    38   if (!status) {
    39     gprint (GP_ERR, "invalid address\n");
    40     exit (4);
    41   }
    42 # endif
    4336
    4437  length = sizeof(Address[0]);
     
    7467    if (errno == EADDRINUSE) {
    7568        Address[0].sin_port ++;
    76         if (Address[0].sin_port > MY_PORT + 10) {
     69        if (Address[0].sin_port > stop) {
    7770          fprintf (stderr, "failed to find a usable port\n");
    7871          exit (6);
     
    154147}
    155148
    156 int GetClientSocket (char *hostname) {
    157 
    158   int i, status, InitSocket, length;
     149int GetPortRange (int *start, int *stop, char *portinfo) {
     150
     151  // portinfo is a port or port range of the form NN or NN:MM
     152  *start = MY_PORT;
     153  *stop = *start + 10;
     154  if (!portinfo) return TRUE;
     155  if (*portinfo == 0) return TRUE;
     156
     157  char *endptr;
     158  *start = strtol (portinfo, &endptr, 0);
     159  *stop = *start + 10; // default range of 10
     160  if (!endptr) {
     161    gprint (GP_ERR, "error in port range parsing : %s\n", portinfo);
     162    exit (20);
     163  }
     164  if (endptr == portinfo) {
     165    gprint (GP_ERR, "error in port range (%s), must be in form NN:MM or NN\n", portinfo);
     166    exit (20);
     167  }
     168  if (*endptr == 0) return TRUE;
     169
     170  if (*endptr != ':') {
     171    gprint (GP_ERR, "error in port range %s (wrong range separator, must be in form NN:MM)\n", portinfo);
     172    exit (20);
     173  }
     174  char *ptr = endptr + 1;
     175  *stop = strtol (ptr, &endptr, 0);
     176  if (endptr == ptr)  {
     177    gprint (GP_ERR, "error in port range (%s), must be in form NN:MM or NN\n", portinfo);
     178    exit (20);
     179  }
     180  if (*stop - *start > 20) {
     181    gprint (GP_ERR, "error in port range (%s), range must be 20 or less\n", portinfo);
     182    exit (20);
     183  }
     184  if (*stop < *start) {
     185    gprint (GP_ERR, "error in port range (%s), stop must >= start\n", portinfo);
     186    exit (20);
     187  }
     188  return TRUE;
     189}
     190
     191int GetClientSocket (char *hostname, char *portinfo) {
     192
     193  int i, status, InitSocket, length, start, stop;
    159194  SockAddress Address;
    160195  struct hostent  *host;
     
    169204  }
    170205
     206  GetPortRange (&start, &stop, portinfo);
     207
    171208  if (DEBUG) {
    172     gprint (GP_ERR, "trying %s (%s:%d)...", host[0].h_name, hostip, MY_PORT);
     209    gprint (GP_ERR, "trying %s (%s:%d)...", host[0].h_name, hostip, start);
    173210  }
    174211
    175212  Address.sin_family = AF_INET;
    176   Address.sin_port   = MY_PORT;
     213  Address.sin_port   = start;
    177214
    178215retry_client:
     
    195232    if (errno == ECONNREFUSED) {
    196233      Address.sin_port ++;
    197       if (Address.sin_port > MY_PORT + 10) exit (12);
     234      if (Address.sin_port > stop) exit (12);
    198235      goto retry_client;
    199236    }
Note: See TracChangeset for help on using the changeset viewer.