FACT++  1.0
int tcpip_open_server ( int  conn_id,
char *  task,
int *  port 
)

Definition at line 1182 of file tcpip.c.

References NET_CONNECTION::channel, closesock, dim_tcpip_init(), NET_CONNECTION::last_used, Listen_backlog, Net_conns, NET_CONNECTION::port, Read_buffer_size, NET_CONNECTION::reading, SEEK_PORT, START_PORT_RANGE, STOP_PORT_RANGE, time, NET_CONNECTION::timr_ent, ushort, Write_buffer_size, and NET_CONNECTION::write_timedout.

Referenced by dna_open_server(), and web_open_server().

1183 {
1184  /* Create connection: create and initialize socket stuff,
1185  * find a free port on this node.
1186  */
1187  struct sockaddr_in sockname;
1188  int path, val, ret_code, ret;
1189 
1190  dim_tcpip_init(0);
1191  if( (path = (int)socket(AF_INET, SOCK_STREAM, 0)) == -1 )
1192  {
1193  return(0);
1194  }
1195 
1196  val = 1;
1197  if ((ret_code = setsockopt(path, IPPROTO_TCP, TCP_NODELAY,
1198  (char*)&val, sizeof(val))) == -1 )
1199 
1200  {
1201 #ifdef DEBUG
1202  printf("Couln't set TCP_NODELAY\n");
1203 #endif
1204  closesock(path);
1205  return(0);
1206  }
1207 
1208  val = Write_buffer_size;
1209  if ((ret_code = setsockopt(path, SOL_SOCKET, SO_SNDBUF,
1210  (void *)&val, sizeof(val))) == -1 )
1211  {
1212 #ifdef DEBUG
1213  printf("Couln't set SO_SNDBUF\n");
1214 #endif
1215  closesock(path);
1216  return(0);
1217  }
1218 /*
1219  sval1 = sizeof(val1);
1220  if ((ret_code = getsockopt(path, SOL_SOCKET, SO_SNDBUF,
1221  (void *)&val1, &sval1)) == -1 )
1222  {
1223 #ifdef DEBUG
1224  printf("Couln't set SO_SNDBUF\n");
1225 #endif
1226  closesock(path);
1227  return(0);
1228  }
1229 printf("Set size to %d, got size %d\n", val, val1);
1230 */
1231  val = Read_buffer_size;
1232  if ((ret_code = setsockopt(path, SOL_SOCKET, SO_RCVBUF,
1233  (void *)&val, sizeof(val))) == -1 )
1234  {
1235 #ifdef DEBUG
1236  printf("Couln't set SO_RCVBUF\n");
1237 #endif
1238  closesock(path);
1239  return(0);
1240  }
1241 
1242  if( *port == SEEK_PORT )
1243  { /* Search a free one. */
1244  *port = START_PORT_RANGE - 1;
1245  do
1246  {
1247  (*port)++;
1248  sockname.sin_family = AF_INET;
1249  sockname.sin_addr.s_addr = INADDR_ANY;
1250  sockname.sin_port = htons((ushort) *port);
1251  if( *port > STOP_PORT_RANGE ) {
1252  errno = EADDRNOTAVAIL; /* fake an error code */
1253  closesock(path);
1254 #ifdef WIN32
1255  WSASetLastError(errno);
1256 #endif
1257  return(0);
1258  }
1259  ret = bind(path, (struct sockaddr*)&sockname, sizeof(sockname));
1260 /*
1261 printf("Trying port %d, ret = %d\n", *port, ret);
1262 */
1263  } while( ret == -1 );
1264 /*
1265  } while( bind(path, (struct sockaddr*)&sockname, sizeof(sockname)) == -1 );
1266 */
1267  } else {
1268 #ifndef WIN32
1269  val = 1;
1270  if( setsockopt(path, SOL_SOCKET, SO_REUSEADDR, (char*)&val,
1271  sizeof(val)) == -1 )
1272  {
1273 #ifdef DEBUG
1274  printf("Couln't set SO_REUSEADDR\n");
1275 #endif
1276  closesock(path);
1277  return(0);
1278  }
1279 #endif
1280  sockname.sin_family = AF_INET;
1281  sockname.sin_addr.s_addr = INADDR_ANY;
1282  sockname.sin_port = htons((ushort) *port);
1283  if( (ret = bind(path, (struct sockaddr*) &sockname, sizeof(sockname))) == -1 )
1284  {
1285  closesock(path);
1286  return(0);
1287  }
1288  }
1289 
1290  if( (ret = listen(path, Listen_backlog)) == -1 )
1291  {
1292  closesock(path);
1293  return(0);
1294  }
1295 
1296  strcpy( Net_conns[conn_id].node, "MYNODE" );
1297  strcpy( Net_conns[conn_id].task, task );
1298  Net_conns[conn_id].channel = path;
1299  Net_conns[conn_id].port = *port;
1300  Net_conns[conn_id].last_used = time(NULL);
1301  Net_conns[conn_id].reading = -1;
1302  Net_conns[conn_id].timr_ent = NULL;
1303  Net_conns[conn_id].write_timedout = 0;
1304  return(1);
1305 }
Definition: dns.c:26
TIMR_ENT * timr_ent
Definition: dim.h:413
DllExp DIM_NOSHARE NET_CONNECTION * Net_conns
Definition: conn_handler.c:32
#define ushort
Definition: tcpip.c:88
int dim_tcpip_init(int thr_flag)
Definition: tcpip.c:235
int channel
Definition: dim.h:398
int port
Definition: dim.h:409
time_t last_used
Definition: dim.h:414
Warning because the service this data corrsponds to might have been last updated longer ago than Local time
Definition: smartfact.txt:92
#define START_PORT_RANGE
Definition: dim.h:201
#define closesock(s)
Definition: tcpip.c:36
static int Write_buffer_size
Definition: tcpip.c:107
static int Read_buffer_size
Definition: tcpip.c:108
#define SEEK_PORT
Definition: dim.h:140
static int Listen_backlog
Definition: tcpip.c:103
int write_timedout
Definition: dim.h:412
#define STOP_PORT_RANGE
Definition: dim.h:202
int reading
Definition: dim.h:410

+ Here is the call graph for this function:

+ Here is the caller graph for this function: