1 /***********************************************************************
5 * Code for handling the UDP socket we send/receive on. All of our
6 * tunnels use a single UDP socket which stays open for the life of
9 * Copyright (C) 2002 by Roaring Penguin Software Inc.
11 * This software may be distributed under the terms of the GNU General
12 * Public License, Version 2, or (at your option) any later version.
16 ***********************************************************************/
18 static char const RCSID
[] =
19 "$Id: network.c,v 1.1.48.1 2005/08/08 12:05:25 honor Exp $";
23 #include <sys/socket.h>
24 #include <netinet/in.h>
34 static EventHandler
*NetworkReadHandler
= NULL
;
35 static void network_readable(EventSelector
*es
,
39 //char Hostname[MAX_HOSTNAME]; //2005-04-14 by kanki
42 sigint_handler(int sig
)
47 fprintf(stderr
, "In sigint handler: %d\n", count
);
54 /**********************************************************************
55 * %FUNCTION: network_init
57 * es -- an event selector
59 * >= 0 if all is OK, <0 if not
61 * Initializes network; opens socket on UDP port 1701; sets up
62 * event handler for incoming packets.
63 ***********************************************************************/
65 l2tp_network_init(EventSelector
*es
)
67 struct sockaddr_in me
;
70 //gethostname(Hostname, sizeof(Hostname)); //2005-04-14 by kanki
71 //Hostname[sizeof(Hostname)-1] = 0;
73 Event_HandleSignal(es
, SIGINT
, sigint_handler
);
75 if (NetworkReadHandler
) {
76 Event_DelHandler(es
, NetworkReadHandler
);
77 NetworkReadHandler
= NULL
;
82 Sock
= socket(PF_INET
, SOCK_DGRAM
, 0);
84 l2tp_set_errmsg("network_init: socket: %s", strerror(errno
));
88 me
.sin_family
= AF_INET
;
89 me
.sin_addr
= Settings
.listen_addr
;
90 me
.sin_port
= htons((uint16_t) Settings
.listen_port
);
91 if (bind(Sock
, (struct sockaddr
*) &me
, sizeof(me
)) < 0) {
92 l2tp_set_errmsg("network_init: bind: %s", strerror(errno
));
98 /* Set socket non-blocking */
99 flags
= fcntl(Sock
, F_GETFL
);
101 fcntl(Sock
, F_SETFL
, flags
);
103 /* Set up the network read handler */
104 Event_AddHandler(es
, Sock
, EVENT_FLAG_READABLE
,
105 network_readable
, NULL
);
109 /**********************************************************************
110 * %FUNCTION: network_readable
112 * es -- event selector
114 * flags -- event-handling flags telling what happened
119 * Called when a packet arrives on the UDP socket.
120 ***********************************************************************/
122 network_readable(EventSelector
*es
,
129 struct sockaddr_in from
;
130 dgram
= l2tp_dgram_take_from_wire(&from
);
133 /* It's a control packet if we get here */
134 l2tp_tunnel_handle_received_control_datagram(dgram
, es
, &from
);
135 l2tp_dgram_free(dgram
);