l2tpns/control.c
David Parrish 7aa420ce9f * Update cli callbacks to work with libcli 1.6.
This supports privileged and unprivileged commands, as well as a configuration
  mode
* Add help for all cli commands
* Add "show version" command
* Fix uptime counter display
* Fix nasty bug where cluster basetime can be set to 0 when sending initial
  heartbeat
* Don't rmmod ip_conntrack, as this can take a lot of time
* Re-order logging in routeset such that the action is given before any error
* Use the correct gateway address when deleting routes
* Remove any routes when address changes
* Require authentication if telnet from remote ip
* Require enable password always
* Return error if show pool done on slave
* We MUST immediately exit if we're the wrong master!
2004-06-28 02:43:13 +00:00

74 lines
1.7 KiB
C

// L2TPNS: control
char const *cvs_id_control = "$Id: control.c,v 1.2 2004-06-28 02:43:13 fred_nerk Exp $";
#include <stdio.h>
#include <arpa/inet.h>
#include <errno.h>
#include <fcntl.h>
#include <malloc.h>
#include <netdb.h>
#include <string.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <signal.h>
#include <stdarg.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
#include "control.h"
int new_packet(short type, char *packet)
{
int id = (time(NULL) ^ (rand() * 1024*1024));
*(short *)(packet + 0) = ntohs(0x9012);
*(short *)(packet + 2) = ntohs(type);
*(int *)(packet + 6) = ntohl(id);
return 10;
}
int send_packet(int sockfd, int dest_ip, int dest_port, char *packet, int len)
{
struct sockaddr_in addr;
*(short *)(packet + 4) = ntohs(len);
memset(&addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
*(int*)&addr.sin_addr = htonl(dest_ip);
addr.sin_port = htons(dest_port);
if (sendto(sockfd, packet, len, 0, (void *) &addr, sizeof(addr)) < 0)
{
perror("sendto");
return 0;
}
return 1;
}
int read_packet(int sockfd, char *packet)
{
struct sockaddr_in addr;
int alen = sizeof(addr);
memset(&addr, 0, sizeof(addr));
return recvfrom(sockfd, packet, 1400, 0, (void *) &addr, &alen);
}
void dump_packet(char *packet, FILE *stream)
{
if (htons(*(short *)(packet + 0)) != 0x9012)
{
fprintf(stream, "Invalid packet identifier %x\n", htons(*(short *)(packet + 0)));
return;
}
fprintf(stream, "Control packet:\n");
fprintf(stream, " Type: %d\n", htons(*(short *)(packet + 2)));
fprintf(stream, " Length: %d\n", htons(*(short *)(packet + 4)));
fprintf(stream, " Identifier: %x\n", htonl(*(int *)(packet + 6)));
fprintf(stream, "\n");
}