-/*
- setup an outgoing meta (tcp) socket
-*/
-int setup_outgoing_meta_socket(connection_t *cl)
-{
- int flags;
- struct sockaddr_in a;
- config_t const *cfg;
- int option;
-cp
- if(debug_lvl >= DEBUG_CONNECTIONS)
- syslog(LOG_INFO, _("Trying to connect to %s"), cl->hostname);
-
- if((cfg = get_config_val(cl->config, config_port)) == NULL)
- cl->port = 655;
- else
- cl->port = cfg->data.val;
-
- cl->meta_socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
- if(cl->meta_socket == -1)
- {
- syslog(LOG_ERR, _("Creating socket for %s port %d failed: %m"),
- cl->hostname, cl->port);
- return -1;
- }
-
- /* Bind first to get a fix on our source port */
-
- a.sin_family = AF_INET;
- a.sin_port = htons(0);
- a.sin_addr.s_addr = htonl(INADDR_ANY);
-
- if(bind(cl->meta_socket, (struct sockaddr *)&a, sizeof(struct sockaddr)))
- {
- close(cl->meta_socket);
- syslog(LOG_ERR, _("System call `%s' failed: %m"), "bind");
- return -1;
- }
-
- /* Optimize TCP settings */
-
- option = 1;
- setsockopt(cl->meta_socket, SOL_SOCKET, SO_KEEPALIVE, &option, sizeof(option));
- setsockopt(cl->meta_socket, SOL_TCP, TCP_NODELAY, &option, sizeof(option));
-
- option = IPTOS_LOWDELAY;
- setsockopt(cl->meta_socket, SOL_IP, IP_TOS, &option, sizeof(option));
-
- /* Connect */
-
- a.sin_family = AF_INET;
- a.sin_port = htons(cl->port);
- a.sin_addr.s_addr = htonl(cl->address);
-
- if(connect(cl->meta_socket, (struct sockaddr *)&a, sizeof(a)) == -1)
- {
- close(cl->meta_socket);
- syslog(LOG_ERR, _("%s port %hd: %m"), cl->hostname, cl->port);
- return -1;
- }
-
- flags = fcntl(cl->meta_socket, F_GETFL);
- if(fcntl(cl->meta_socket, F_SETFL, flags | O_NONBLOCK) < 0)
- {
- close(cl->meta_socket);
- syslog(LOG_ERR, _("fcntl for %s port %d: %m"),
- cl->hostname, cl->port);
- return -1;
- }
-
- if(debug_lvl >= DEBUG_CONNECTIONS)
- syslog(LOG_INFO, _("Connected to %s port %hd"),
- cl->hostname, cl->port);
-
- cl->status.meta = 1;
-cp
- return 0;
-}