diff options
author | Guillaume Zajac <guillaume.zajac@linux.intel.com> | 2011-05-19 11:58:28 +0200 |
---|---|---|
committer | Denis Kenzior <denkenz@gmail.com> | 2011-05-24 12:10:22 -0500 |
commit | 7e5ccc18d77bb0919519747647f7f6559d313830 (patch) | |
tree | 74052085491308946692ebcb920edeab717c1267 /gatchat/ppp_net.c | |
parent | db5e8287871633f40d88f20774d425d73a886773 (diff) | |
download | ofono-7e5ccc18d77bb0919519747647f7f6559d313830.tar.bz2 |
gatppp: Add new contructor to use external fd
Diffstat (limited to 'gatchat/ppp_net.c')
-rw-r--r-- | gatchat/ppp_net.c | 47 |
1 files changed, 32 insertions, 15 deletions
diff --git a/gatchat/ppp_net.c b/gatchat/ppp_net.c index 25bcfa47..52b35542 100644 --- a/gatchat/ppp_net.c +++ b/gatchat/ppp_net.c @@ -123,35 +123,52 @@ const char *ppp_net_get_interface(struct ppp_net *net) return net->if_name; } -struct ppp_net *ppp_net_new(GAtPPP *ppp) +struct ppp_net *ppp_net_new(GAtPPP *ppp, int fd) { struct ppp_net *net; GIOChannel *channel = NULL; struct ifreq ifr; - int fd, err; + int err; net = g_try_new0(struct ppp_net, 1); - if (net == NULL) + if (net == NULL) { + if (fd >= 0) + close(fd); + return NULL; + } net->ppp_packet = ppp_packet_new(MAX_PACKET, PPP_IP_PROTO); if (net->ppp_packet == NULL) { + if (fd >= 0) + close(fd); + g_free(net); return NULL; } - /* open a tun interface */ - fd = open("/dev/net/tun", O_RDWR); - if (fd < 0) - goto error; - - memset(&ifr, 0, sizeof(ifr)); - ifr.ifr_flags = IFF_TUN | IFF_NO_PI; - strcpy(ifr.ifr_name, "ppp%d"); - - err = ioctl(fd, TUNSETIFF, (void *) &ifr); - if (err < 0) - goto error; + /* + * If the fd value is still the default one, + * open the tun interface and configure it. + */ + if (fd < 0) { + /* open a tun interface */ + fd = open("/dev/net/tun", O_RDWR); + if (fd < 0) + goto error; + + memset(&ifr, 0, sizeof(ifr)); + ifr.ifr_flags = IFF_TUN | IFF_NO_PI; + strcpy(ifr.ifr_name, "ppp%d"); + + err = ioctl(fd, TUNSETIFF, (void *) &ifr); + if (err < 0) + goto error; + } else { + err = ioctl(fd, TUNGETIFF, (void *) &ifr); + if (err < 0) + goto error; + } net->if_name = strdup(ifr.ifr_name); |