-/*
- 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, unsigned char *d, int len)
-{
- basic_info_t *tmp = (basic_info_t*)d;
-cp
- 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, unsigned char *d, int len)
-{
- passphrase_t *tmp = (passphrase_t*)d;
-cp
- cl->pp = xmalloc(tmp->len+3);
- memcpy(cl->pp, tmp, tmp->len+3);
-
- if(debug_lvl > 2)
- syslog(LOG_DEBUG, "got PASSPHRASE(%hd,...)", cl->pp->len);
-
- if(cl->status.outgoing)
- send_passphrase(cl);
- else
- send_public_key(cl);
-cp
- return 0;
-}
-
-int public_key_h(conn_list_t *cl, unsigned char *d, int len)
-{
- char *g_n;
- public_key_t *tmp = (public_key_t*)d;
-cp
- if(debug_lvl > 2)
- syslog(LOG_DEBUG, "got PUBLIC_KEY(%hd,%s)", tmp->len, &tmp->key);
-
- g_n = xmalloc(tmp->len+1);
- strcpy(g_n, &tmp->key);
-
- 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, unsigned char *d, int len)
-{
-cp
- if(debug_lvl > 2)
- syslog(LOG_DEBUG, "got ACK");
-
- cl->status.active = 1;
- syslog(LOG_NOTICE, "Connection with %s activated.", cl->hostname);
-
- /*
- === FIXME ===
- Now I'm going to cheat. The meta protocol is actually
- a stream of requests, that may come in in the same TCP
- packet. This is the only place that it will happen,
- though.
- I may change it in the future, if it appears that this
- is not retainable.
- */
- if(len > 1) /* An ADD_HOST follows */
- {
- if(request_handlers[d[1]] == NULL)
- syslog(LOG_ERR, "Unknown request %d.", d[1]);
- if(request_handlers[d[1]](cl, d + 1, len - 1) < 0)
- return -1;
- }
-cp
- return 0;
-}
-
-int termreq_h(conn_list_t *cl, unsigned char *d, int len)
-{
-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, unsigned char *d, int len)
-{
-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, unsigned char *d, int len)
-{
- del_host_t *tmp = (del_host_t*)d;
- conn_list_t *fw;
-cp
- 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, unsigned char *d, int len)
-{
-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, unsigned char *d, int len)
-{
-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, unsigned char *d, int len)
-{
- add_host_t *tmp = (add_host_t*)d;
- conn_list_t *ncn, *fw;
-cp
- 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);
-
- /*
- again, i'm cheating here. see the comment in ack_h.
- Naughty zarq! Now you see what cheating will get you... [GS]
- */
- if(len > sizeof(add_host_t)) /* Another ADD_HOST follows */
- {
- if(request_handlers[d[sizeof(add_host_t)]] == NULL)
- syslog(LOG_ERR, "Unknown request %d.", d[sizeof(add_host_t)]);
- if(request_handlers[d[sizeof(add_host_t)]](cl, d + sizeof(add_host_t), len - sizeof(add_host_t)) < 0)
- return -1;
- }
-cp
- return 0;
-}