~phablet-team/ofono/ofono-bug-updates

« back to all changes in this revision

Viewing changes to gatchat/ppp_net.c

  • Committer: Denis Kenzior
  • Author(s): Lucas De Marchi
  • Date: 2011-03-18 23:31:14 UTC
  • Revision ID: git-v1:888e07863b24026413bac8f449de377c879e1044
message: add cancelled state

Based on patch from Yang Gu <gyagp0@gmail.com>

Show diffs side-by-side

added added

removed removed

Lines of Context:
2
2
 *
3
3
 *  PPP library with GLib integration
4
4
 *
5
 
 *  Copyright (C) 2009-2011  Intel Corporation. All rights reserved.
 
5
 *  Copyright (C) 2009-2010  Intel Corporation. All rights reserved.
6
6
 *
7
7
 *  This program is free software; you can redistribute it and/or modify
8
8
 *  it under the terms of the GNU General Public License version 2 as
77
77
        return TRUE;
78
78
}
79
79
 
80
 
void ppp_net_process_packet(struct ppp_net *net, const guint8 *packet,
81
 
                                gsize plen)
 
80
void ppp_net_process_packet(struct ppp_net *net, const guint8 *packet)
82
81
{
83
82
        GIOStatus status;
84
83
        gsize bytes_written;
85
84
        guint16 len;
86
85
 
87
 
        if (plen < 4)
88
 
                return;
89
 
 
90
86
        /* find the length of the packet to transmit */
91
87
        len = get_host_short(&packet[2]);
92
88
        status = g_io_channel_write_chars(net->channel, (gchar *) packet,
93
 
                                                MIN(len, plen),
94
 
                                                &bytes_written, NULL);
95
 
 
96
 
        if (status != G_IO_STATUS_NORMAL)
97
 
                return;
 
89
                                                len, &bytes_written, NULL);
98
90
}
99
91
 
100
92
/*
131
123
        return net->if_name;
132
124
}
133
125
 
134
 
struct ppp_net *ppp_net_new(GAtPPP *ppp, int fd)
 
126
struct ppp_net *ppp_net_new(GAtPPP *ppp)
135
127
{
136
128
        struct ppp_net *net;
137
129
        GIOChannel *channel = NULL;
138
130
        struct ifreq ifr;
139
 
        int err;
 
131
        int fd, err;
140
132
 
141
133
        net = g_try_new0(struct ppp_net, 1);
142
134
        if (net == NULL)
143
 
                goto badalloc;
 
135
                return NULL;
144
136
 
145
137
        net->ppp_packet = ppp_packet_new(MAX_PACKET, PPP_IP_PROTO);
146
 
        if (net->ppp_packet == NULL)
 
138
        if (net->ppp_packet == NULL) {
 
139
                g_free(net);
 
140
                return NULL;
 
141
        }
 
142
 
 
143
        /* open a tun interface */
 
144
        fd = open("/dev/net/tun", O_RDWR);
 
145
        if (fd < 0)
147
146
                goto error;
148
147
 
149
 
        /*
150
 
         * If the fd value is still the default one,
151
 
         * open the tun interface and configure it.
152
 
         */
153
148
        memset(&ifr, 0, sizeof(ifr));
154
 
 
155
 
        if (fd < 0) {
156
 
                /* open a tun interface */
157
 
                fd = open("/dev/net/tun", O_RDWR);
158
 
                if (fd < 0) {
159
 
                        ppp_debug(ppp, "Couldn't open tun device. "
160
 
                                        "Do you run oFono as root and do you "
161
 
                                        "have the TUN module loaded?");
162
 
                        goto error;
163
 
                }
164
 
 
165
 
                ifr.ifr_flags = IFF_TUN | IFF_NO_PI;
166
 
                strcpy(ifr.ifr_name, "ppp%d");
167
 
 
168
 
                err = ioctl(fd, TUNSETIFF, (void *) &ifr);
169
 
                if (err < 0)
170
 
                        goto error;
171
 
        } else {
172
 
                err = ioctl(fd, TUNGETIFF, (void *) &ifr);
173
 
                if (err < 0)
174
 
                        goto error;
175
 
        }
 
149
        ifr.ifr_flags = IFF_TUN | IFF_NO_PI;
 
150
        strcpy(ifr.ifr_name, "ppp%d");
 
151
 
 
152
        err = ioctl(fd, TUNSETIFF, (void *) &ifr);
 
153
        if (err < 0)
 
154
                goto error;
176
155
 
177
156
        net->if_name = strdup(ifr.ifr_name);
178
157
 
199
178
        if (channel)
200
179
                g_io_channel_unref(channel);
201
180
 
202
 
        g_free(net->if_name);
203
 
        g_free(net->ppp_packet);
204
 
        g_free(net);
205
 
 
206
 
badalloc:
207
181
        if (fd >= 0)
208
182
                close(fd);
209
183
 
 
184
        g_free(net->if_name);
 
185
        g_free(net->ppp_packet);
 
186
        g_free(net);
210
187
        return NULL;
211
188
}
212
189
 
213
190
void ppp_net_free(struct ppp_net *net)
214
191
{
215
 
        if (net->watch) {
216
 
                g_source_remove(net->watch);
217
 
                net->watch = 0;
218
 
        }
219
 
 
 
192
        g_source_remove(net->watch);
220
193
        g_io_channel_unref(net->channel);
221
194
 
222
195
        g_free(net->ppp_packet);
223
196
        g_free(net->if_name);
224
197
        g_free(net);
225
198
}
226
 
 
227
 
void ppp_net_suspend_interface(struct ppp_net *net)
228
 
{
229
 
        if (net == NULL || net->channel == NULL)
230
 
                return;
231
 
 
232
 
        if (net->watch == 0)
233
 
                return;
234
 
 
235
 
        g_source_remove(net->watch);
236
 
        net->watch = 0;
237
 
}
238
 
 
239
 
void ppp_net_resume_interface(struct ppp_net *net)
240
 
{
241
 
        if (net == NULL || net->channel == NULL)
242
 
                return;
243
 
 
244
 
        net->watch = g_io_add_watch(net->channel,
245
 
                        G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL,
246
 
                        ppp_net_callback, net);
247
 
}