Commit a4928fe6 authored by Alessandro Rubini's avatar Alessandro Rubini Committed by Aurelio Colosimo

posix-socket.c: remove #ifdef and support '-e' command line option

This is still not working, but the patch makes no functional change,
only turns #ifdef into if() based on '-e' command line argument.
parent 71295b1a
......@@ -20,10 +20,6 @@
#include <pptp/diag.h>
#include "posix.h"
/* Uncomment one (and only one!) of the following */
/* #define PP_NET_UDP */
#define PP_NET_IEEE_802_3
/* posix_recv_msg uses recvmsg for timestamp query */
int posix_recv_msg(int fd, void *pkt, int len, TimeInternal *t)
{
......@@ -109,9 +105,12 @@ int posix_recv_msg(int fd, void *pkt, int len, TimeInternal *t)
int posix_recv_packet(struct pp_instance *ppi, void *pkt, int len,
TimeInternal *t)
{
#ifdef PP_NET_UDP
struct pp_channel *ch1 = NULL, *ch2 = NULL;
if (OPTS(ppi)->ethernet_mode)
return posix_recv_msg(NP(ppi)->ch[PP_NP_GEN].fd, pkt, len, t);
/* else: UDP */
if (POSIX_ARCH(ppi)->rcv_switch) {
ch1 = &(NP(ppi)->ch[PP_NP_GEN]);
ch2 = &(NP(ppi)->ch[PP_NP_EVT]);
......@@ -129,23 +128,20 @@ int posix_recv_packet(struct pp_instance *ppi, void *pkt, int len,
if (ch2->pkt_present)
return posix_recv_msg(ch2->fd, pkt, len, t);
return -1;
#endif /* PP_NET_UDP */
#ifdef PP_NET_IEEE_802_3
/* raw sockets implementation always use gen socket */
return posix_recv_msg(NP(ppi)->ch[PP_NP_GEN].fd, pkt, len, t);
#endif /* PP_NET_IEEE_802_3 */
return -1;
}
int posix_send_packet(struct pp_instance *ppi, void *pkt, int len, int chtype,
int use_pdelay_addr)
{
#ifdef PP_NET_UDP
struct sockaddr_in addr;
if (OPTS(ppi)->ethernet_mode) {
/* raw sockets implementation always use gen socket */
return send(NP(ppi)->ch[PP_NP_GEN].fd, pkt, len, 0);
}
/* else: UDP */
addr.sin_family = AF_INET;
addr.sin_port = htons(chtype == PP_NP_GEN ? PP_GEN_PORT : PP_EVT_PORT);
......@@ -156,12 +152,6 @@ int posix_send_packet(struct pp_instance *ppi, void *pkt, int len, int chtype,
return sendto(NP(ppi)->ch[chtype].fd, pkt, len, 0,
(struct sockaddr *)&addr, sizeof(struct sockaddr_in));
#endif /* PP_NET_UDP */
#ifdef PP_NET_IEEE_802_3
/* raw sockets implementation always use gen socket */
return send(NP(ppi)->ch[PP_NP_GEN].fd, pkt, len, 0);
#endif /* PP_NET_IEEE_802_3 */
return -1;
}
......@@ -176,15 +166,60 @@ int pp_send_packet(struct pp_instance *ppi, void *pkt, int len, int chtype,
int posix_open_ch(struct pp_instance *ppi, char *ifname, int chtype)
{
#ifdef PP_NET_UDP
int sock = -1;
int temp, iindex;
struct in_addr iface_addr, net_addr;
struct ifreq ifr;
struct sockaddr_in addr;
struct sockaddr_ll addr_ll;
struct ip_mreq imr;
char addr_str[INET_ADDRSTRLEN];
if (OPTS(ppi)->ethernet_mode) {
/* open socket */
sock = socket(PF_PACKET, SOCK_RAW, ETH_P_1588);
if (sock < 0) {
pp_diag_error_str2(ppi, "socket()", strerror(errno));
return -1;
}
/* hw interface information */
memset(&ifr, 0, sizeof(ifr));
strcpy(ifr.ifr_name, ifname);
if (ioctl(sock, SIOCGIFINDEX, &ifr) < 0) {
pp_diag_error_str2(ppi, "SIOCGIFINDEX",
strerror(errno));
close(sock);
return -1;
}
iindex = ifr.ifr_ifindex;
if (ioctl(sock, SIOCGIFHWADDR, &ifr) < 0) {
pp_diag_error_str2(ppi, "SIOCGIFHWADDR",
strerror(errno));
close(sock);
return -1;
}
memcpy(NP(ppi)->ch[chtype].addr, ifr.ifr_hwaddr.sa_data, 6);
/* bind and setsockopt */
memset(&addr_ll, 0, sizeof(addr));
addr_ll.sll_family = AF_PACKET;
addr_ll.sll_protocol = htons(ETH_P_1588);
addr_ll.sll_ifindex = iindex;
if (bind(sock, (struct sockaddr *)&addr_ll,
sizeof(addr_ll)) < 0) {
pp_diag_error_str2(ppi, "bind", strerror(errno));
close(sock);
return -1;
}
NP(ppi)->ch[chtype].fd = sock;
return 0;
}
/* else: UDP */
sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
if (sock < 0) {
pp_diag_error_str2(ppi, "socket()", strerror(errno));
......@@ -315,56 +350,6 @@ int posix_open_ch(struct pp_instance *ppi, char *ifname, int chtype)
NP(ppi)->ch[chtype].fd = sock;
return 0;
#endif /* PP_NET_UDP */
#ifdef PP_NET_IEEE_802_3
int sock, iindex;
struct ifreq ifr;
struct sockaddr_ll addr;
/* open socket */
sock = socket(PF_PACKET, SOCK_RAW, PP_ETHERTYPE);
if (sock < 0) {
pp_diag_error_str2(ppi, "socket()", strerror(errno));
return -1;
}
/* hw interface information */
memset(&ifr, 0, sizeof(ifr));
strcpy(ifr.ifr_name, ifname);
if (ioctl(sock, SIOCGIFINDEX, &ifr) < 0) {
pp_diag_error_str2(ppi, "SIOCGIFINDEX", strerror(errno));
close(sock);
return -1;
}
iindex = ifr.ifr_ifindex;
if (ioctl(sock, SIOCGIFHWADDR, &ifr) < 0) {
pp_diag_error_str2(ppi, "SIOCGIFHWADDR", strerror(errno));
close(sock);
return -1;
}
memcpy(NP(ppi)->ch[chtype].addr, ifr.ifr_hwaddr.sa_data, 6);
/* bind and setsockopt */
memset(&addr, 0, sizeof(addr));
addr.sll_family = AF_PACKET;
addr.sll_protocol = htons(PP_ETHERTYPE);
addr.sll_ifindex = iindex;
if (bind(sock, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
pp_diag_error_str2(ppi, "bind", strerror(errno));
close(sock);
return -1;
}
NP(ppi)->ch[chtype].fd = sock;
return 0;
#endif /* PP_NET_IEEE_802_3 */
return -1;
}
/*
......@@ -372,27 +357,22 @@ int posix_open_ch(struct pp_instance *ppi, char *ifname, int chtype)
*/
int posix_net_init(struct pp_instance *ppi)
{
#ifdef PP_NET_UDP
int i;
if (OPTS(ppi)->ethernet_mode) {
PP_PRINTF("posix_net_init IEEE 802.3\n");
/* raw sockets implementation always use gen socket */
return posix_open_ch(ppi, OPTS(ppi)->iface_name, PP_NP_GEN);
}
/* else: UDP */
PP_PRINTF("posix_net_init UDP\n");
for (i = PP_NP_GEN; i <= PP_NP_EVT; i++) {
if (posix_open_ch(ppi, OPTS(ppi)->iface_name, i))
return -1;
}
return 0;
#endif /* PP_NET_UDP */
#ifdef PP_NET_IEEE_802_3
PP_PRINTF("posix_net_init IEEE 802.3\n");
/* raw sockets implementation always use gen socket */
if (posix_open_ch(ppi, OPTS(ppi)->iface_name, PP_NP_GEN))
return -1;
return 0;
#endif /* PP_NET_IEEE_802_3 */
PP_PRINTF("Error: pptp Compiled with unsupported network protocol!!\n");
return -1;
}
int pp_net_init(struct pp_instance *ppi)
......@@ -404,11 +384,16 @@ int pp_net_init(struct pp_instance *ppi)
*/
int posix_net_shutdown(struct pp_instance *ppi)
{
#ifdef PP_NET_UDP
struct ip_mreq imr;
int fd;
int i;
if (OPTS(ppi)->ethernet_mode) {
close(NP(ppi)->ch[PP_NP_GEN].fd);
return 0;
}
/* else: UDP */
for (i = PP_NP_GEN; i <= PP_NP_EVT; i++) {
fd = NP(ppi)->ch[i].fd;
if (fd < 0)
......@@ -437,14 +422,6 @@ int posix_net_shutdown(struct pp_instance *ppi)
NP(ppi)->peer_mcast_addr = 0;
return 0;
#endif /* PP_NET_UDP */
#ifdef PP_NET_IEEE_802_3
close(NP(ppi)->ch[PP_NP_GEN].fd);
#endif /* PP_NET_IEEE_802_3 */
return -1;
}
int posix_net_check_pkt(struct pp_instance *ppi, int delay_ms)
......@@ -466,7 +443,27 @@ int posix_net_check_pkt(struct pp_instance *ppi, int delay_ms)
NP(ppi)->ch[PP_NP_GEN].pkt_present = 0;
NP(ppi)->ch[PP_NP_EVT].pkt_present = 0;
#ifdef PP_NET_UDP
if (OPTS(ppi)->ethernet_mode) {
maxfd = NP(ppi)->ch[PP_NP_GEN].fd;
FD_ZERO(&set);
FD_SET(NP(ppi)->ch[PP_NP_GEN].fd, &set);
i = select(maxfd + 1, &set, NULL, NULL, &arch_data->tv);
if (i < 0 && errno != EINTR)
exit(__LINE__);
if (i < 0)
return -1;
if (i == 0)
return 0;
NP(ppi)->ch[PP_NP_GEN].pkt_present = 1;
return 1;
}
/* else: UDP */
maxfd = (NP(ppi)->ch[PP_NP_GEN].fd > NP(ppi)->ch[PP_NP_EVT].fd) ?
NP(ppi)->ch[PP_NP_GEN].fd : NP(ppi)->ch[PP_NP_EVT].fd;
FD_ZERO(&set);
......@@ -477,13 +474,11 @@ int posix_net_check_pkt(struct pp_instance *ppi, int delay_ms)
if (i < 0 && errno != EINTR)
exit(__LINE__);
if (i < 0) {
ret = i;
goto _end;
}
if (i < 0)
return -1;
if (i == 0)
goto _end;
return 0;
if (FD_ISSET(NP(ppi)->ch[PP_NP_GEN].fd, &set)) {
ret++;
......@@ -495,39 +490,7 @@ int posix_net_check_pkt(struct pp_instance *ppi, int delay_ms)
NP(ppi)->ch[PP_NP_EVT].pkt_present = 1;
}
_end:
return ret;
#endif /* PP_NET_UDP */
#ifdef PP_NET_IEEE_802_3
maxfd = NP(ppi)->ch[PP_NP_GEN].fd;
FD_ZERO(&set);
FD_SET(NP(ppi)->ch[PP_NP_GEN].fd, &set);
i = select(maxfd + 1, &set, NULL, NULL, &arch_data->tv);
if (i < 0 && errno != EINTR)
exit(__LINE__);
if (i < 0) {
ret = i;
goto _end;
}
if (i == 0)
goto _end;
if (FD_ISSET(NP(ppi)->ch[PP_NP_GEN].fd, &set)) {
ret++;
NP(ppi)->ch[PP_NP_GEN].pkt_present = 1;
}
_end:
return ret;
#endif /* PP_NET_IEEE_802_3 */
return -1;
}
int pp_net_shutdown(struct pp_instance *ppi)
......
......@@ -12,6 +12,10 @@
#include <pptp/ieee1588_types.h>
#include <pptp/constants.h>
#ifndef ETH_P_1588
#define ETH_P_1588 0x88F7
#endif
/* Macros for diagnostic prints. Set pp_diag_verbosity as 0 or 1 (PP_V macros
* disabled/enabled) */
extern int pp_diag_verbosity;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment