-/*
- notify one connection of everything
- i have connected
-*/
-int notify_one(conn_list_t *new)
-{
- conn_list_t *p;
-cp
- for(p = conn_list; p != NULL; p = p->next)
- if(p != new && p->protocol_version > PROT_3)
- send_add_host(new, p);
-cp
- return 0;
-}
-
-/*
- The incoming request handlers
-*/
-
-int basic_info_h(conn_list_t *cl)
-{
- basic_info_t tmp;
-cp
- if(read(cl->meta_socket, &((char*)(&tmp))[1], sizeof(tmp)-1) <= 0)
- {
- syslog(LOG_ERR, "%d: Receive failed: %m", __LINE__);
- return -1;
- }
-
- cl->protocol_version = tmp.protocol;
- cl->port = tmp.portnr;
- cl->vpn_ip = tmp.vpn_ip;
- cl->vpn_mask = tmp.vpn_mask;
-
- if(cl->protocol_version < PROT_CURRENT)
- {
- syslog(LOG_ERR, "Peer uses protocol version %d which is too old.",
- cl->protocol_version);
- return -1;
- }
-
- if(debug_lvl > 2)
- syslog(LOG_DEBUG, "got BASIC_INFO(%hd," IP_ADDR_S "," IP_ADDR_S ")", cl->port,
- IP_ADDR_V(cl->vpn_ip), IP_ADDR_V(cl->vpn_mask));
- if(debug_lvl > 1)
- syslog(LOG_DEBUG, "Peer uses protocol version %d",
- cl->protocol_version);
-
- if(cl->status.outgoing)
- {
- if(setup_vpn_connection(cl) < 0)
- return -1;
- send_basic_info(cl);
- }
- else
- {
- if(setup_vpn_connection(cl) < 0)
- return -1;
- send_passphrase(cl);
- }
-
- cl->status.active = 0;
-cp
- return 0;
-}
-
-int passphrase_h(conn_list_t *cl)
-{
- char unused;
- unsigned short int len;
-cp
- if(read(cl->meta_socket, &unused, sizeof(unused)) <= 0)
- {
- syslog(LOG_ERR, "%d: Receive failed: %m", __LINE__);
- return -1;
- }
-
- if(read(cl->meta_socket, &len, sizeof(len)) <= 0)
- {
- syslog(LOG_ERR, "%d: Receive failed: %m", __LINE__);
- return -1;
- }
-
- cl->pp = xmalloc(len+4);
-
- cl->pp->len = len;
- if(read(cl->meta_socket, &(cl->pp->phrase), len) <= 0)
- {
- syslog(LOG_ERR, "%d: Receive failed: %m", __LINE__);
- return -1;
- }
-
- if(debug_lvl > 2)
- syslog(LOG_DEBUG, "got PASSPHRASE(%hd,...)", len);
-
- if(cl->status.outgoing)
- send_passphrase(cl);
- else
- send_public_key(cl);
-cp
- return 0;
-}
-
-int public_key_h(conn_list_t *cl)
-{
- char *g_n;
- unsigned short int len;
- char unused;
-cp
- if(read(cl->meta_socket, &unused, sizeof(unused)) <= 0)
- {
- syslog(LOG_ERR, "%d: Receive failed: %m", __LINE__);
- return -1;
- }
- if(read(cl->meta_socket, &len, sizeof(len)) <= 0)
- {
- syslog(LOG_ERR, "%d: Receive failed: %m", __LINE__);
- return -1;
- }
-
- g_n = xmalloc(len+2);
-
- if(read(cl->meta_socket, g_n, len+2) <= 0)
- {
- syslog(LOG_ERR, "%d: Receive failed: %m", __LINE__);
- return -1;
- }
-
- if(debug_lvl > 2)
- syslog(LOG_DEBUG, "got PUBLIC_KEY(%hd,%s)", len, g_n);
-
- if(verify_passphrase(cl, g_n))
- {
- /* intruder! */
- syslog(LOG_ERR, "Intruder: passphrase does not match.");
- return -1;
- }
-
- if(debug_lvl > 2)
- syslog(LOG_INFO, "Passphrase OK");
-
- if(cl->status.outgoing)
- send_public_key(cl);
- else
- send_ack(cl);
-
- cl->status.active = 1;
- notify_others(cl, NULL, send_add_host);
- notify_one(cl);
-cp
- return 0;
-}
-
-int ack_h(conn_list_t *cl)
-{
-cp
- if(debug_lvl > 2)
- syslog(LOG_DEBUG, "got ACK");
-
- cl->status.active = 1;
- syslog(LOG_NOTICE, "Connection with %s activated.", cl->hostname);
-cp
- return 0;
-}
-
-int termreq_h(conn_list_t *cl)
-{
-cp
- syslog(LOG_NOTICE, IP_ADDR_S " wants to quit", IP_ADDR_V(cl->vpn_ip));
- cl->status.termreq = 1;
- terminate_connection(cl);
-
- notify_others(cl, NULL, send_del_host);
-cp
- return 0;
-}
-
-int timeout_h(conn_list_t *cl)
-{
-cp
- syslog(LOG_NOTICE, IP_ADDR_S " says it's gotten a timeout from us", IP_ADDR_V(cl->vpn_ip));
- cl->status.termreq = 1;
- terminate_connection(cl);
-cp
- return 0;
-}
-
-int del_host_h(conn_list_t *cl)
-{
- del_host_t tmp;
- conn_list_t *fw;
-cp
- if(read(cl->meta_socket, &((char*)(&tmp))[1], sizeof(tmp)-1) <= 0)
- {
- syslog(LOG_ERR, "%d: Receive failed: %m", __LINE__);
- return -1;
- }
-
- if(debug_lvl > 2)
- syslog(LOG_DEBUG, "got DEL_HOST for " IP_ADDR_S,
- IP_ADDR_V(tmp.vpn_ip));
-
- if(!(fw = lookup_conn(tmp.vpn_ip)))
- {
- syslog(LOG_ERR, "Somebody wanted to delete " IP_ADDR_S " which does not exist?",
- IP_ADDR_V(tmp.vpn_ip));
- return 0;
- }
-
- notify_others(cl, fw, send_del_host);
-
- fw->status.termreq = 1;
- terminate_connection(fw);
-cp
- return 0;
-}
-
-int ping_h(conn_list_t *cl)
-{
-cp
- if(debug_lvl > 3)
- syslog(LOG_DEBUG, "responding to ping from " IP_ADDR_S, IP_ADDR_V(cl->vpn_ip));
- cl->status.pinged = 0;
- cl->status.got_pong = 1;
-
- send_pong(cl);
-cp
- return 0;
-}
-
-int pong_h(conn_list_t *cl)
-{
-cp
- if(debug_lvl > 3)
- syslog(LOG_DEBUG, "ok, got pong from " IP_ADDR_S, IP_ADDR_V(cl->vpn_ip));
- cl->status.got_pong = 1;
-cp
- return 0;
-}
-
-int add_host_h(conn_list_t *cl)
-{
- add_host_t tmp;
- conn_list_t *ncn, *fw;
-cp
- if(read(cl->meta_socket, &((char*)(&tmp))[1], sizeof(tmp)-1) <= 0)
- {
- syslog(LOG_ERR, "%d: Receive failed: %m", __LINE__);
- return -1;
- }
-
- if(debug_lvl > 2)
- syslog(LOG_DEBUG, "Add host request from " IP_ADDR_S, IP_ADDR_V(cl->vpn_ip));
- if(debug_lvl > 3)
- syslog(LOG_DEBUG, "got ADD_HOST(" IP_ADDR_S "," IP_ADDR_S ",%hd)",
- IP_ADDR_V(tmp.vpn_ip), IP_ADDR_V(tmp.vpn_mask), tmp.portnr);
-
- /*
- Suggestion of Hans Bayle
- */
- if((fw = lookup_conn(tmp.vpn_ip)))
- {
- notify_others(fw, cl, send_add_host);
- return 0;
- }
-
- ncn = new_conn_list();
- ncn->real_ip = tmp.real_ip;
- ncn->vpn_ip = tmp.vpn_ip;
- ncn->vpn_mask = tmp.vpn_mask;
- ncn->port = tmp.portnr;
- ncn->hostname = hostlookup(tmp.real_ip);
- ncn->nexthop = cl;
- ncn->next = conn_list;
- conn_list = ncn;
- ncn->status.active = 1;
- notify_others(ncn, cl, send_add_host);
-cp
- return 0;
-}