- Timestamp:
- Mar 5, 2012, 5:19:48 PM (14 years ago)
- Location:
- branches/meh_branches/ppstack_test
- Files:
-
- 2 edited
-
. (modified) (1 prop)
-
Ohana/src/opihi/lib.shell/SocketOps.c (modified) (6 diffs)
Legend:
- Unmodified
- Added
- Removed
-
branches/meh_branches/ppstack_test
- Property svn:mergeinfo changed
-
branches/meh_branches/ppstack_test/Ohana/src/opihi/lib.shell/SocketOps.c
r23530 r33415 1 1 # include "shell.h" 2 # include <unistd.h> 3 # define HOST_NAME_MAX 256 2 4 3 5 # define MY_PORT 2000 … … 11 13 static int *VALID; 12 14 13 int InitServerSocket (SockAddress *Address) { 14 15 int GetPortRange (int *start, int *stop, char *portinfo); 16 17 int InitServerSocket (SockAddress *Address, char *hostname, char *portinfo) { 18 19 int start, stop; 15 20 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 30 31 Address[0].sin_family = AF_INET; 31 Address[0].sin_port = MY_PORT;32 Address[0].sin_port = start; 32 33 Address[0].sin_addr.s_addr = INADDR_ANY; // use this line to bind any address / port? 33 34 34 35 retry_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 # endif43 36 44 37 length = sizeof(Address[0]); … … 74 67 if (errno == EADDRINUSE) { 75 68 Address[0].sin_port ++; 76 if (Address[0].sin_port > MY_PORT + 10) {69 if (Address[0].sin_port > stop) { 77 70 fprintf (stderr, "failed to find a usable port\n"); 78 71 exit (6); … … 154 147 } 155 148 156 int GetClientSocket (char *hostname) { 157 158 int i, status, InitSocket, length; 149 int 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 191 int GetClientSocket (char *hostname, char *portinfo) { 192 193 int i, status, InitSocket, length, start, stop; 159 194 SockAddress Address; 160 195 struct hostent *host; … … 169 204 } 170 205 206 GetPortRange (&start, &stop, portinfo); 207 171 208 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); 173 210 } 174 211 175 212 Address.sin_family = AF_INET; 176 Address.sin_port = MY_PORT;213 Address.sin_port = start; 177 214 178 215 retry_client: … … 195 232 if (errno == ECONNREFUSED) { 196 233 Address.sin_port ++; 197 if (Address.sin_port > MY_PORT + 10) exit (12);234 if (Address.sin_port > stop) exit (12); 198 235 goto retry_client; 199 236 }
Note:
See TracChangeset
for help on using the changeset viewer.
