~ubuntu-branches/ubuntu/wily/modemmanager/wily

« back to all changes in this revision

Viewing changes to test/mm-test-pppd-plugin.c

  • Committer: Package Import Robot
  • Author(s): Mathieu Trudel-Lapierre
  • Date: 2013-12-20 15:35:26 UTC
  • mfrom: (1.2.2)
  • Revision ID: package-import@ubuntu.com-20131220153526-j3xu9gp51ltzlizc
Tags: 1.0.0-1
* New upstream release. (Closes: #728214, #728215)
* debian/control: add a versioned dependency for libqmi -- we need at least
  version 1.4.
* debian/control: build-depends on libmbim-glib-dev 1.4
* debian/rules: run the local autogen.sh rather than gnome-autogen.sh.
* debian/rules: drop --with-tests. 
* debian/rules: clean up after gtk-doc files left around.
* debian/patches/glib_fixes.patch: link against glib explicitly for the
  huawei modem helper tests.
* debian/libmm-glib0.symbols: updated symbols.
* debian/watch: update watch file for new location of upstream source.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
2
 
/* This program is free software; you can redistribute it and/or modify
3
 
 * it under the terms of the GNU General Public License as published by
4
 
 * the Free Software Foundation; either version 2 of the License, or
5
 
 * (at your option) any later version.
6
 
 *
7
 
 * This program is distributed in the hope that it will be useful,
8
 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10
 
 * GNU General Public License for more details.
11
 
 *
12
 
 * You should have received a copy of the GNU General Public License along
13
 
 * with this program; if not, write to the Free Software Foundation, Inc.,
14
 
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
15
 
 *
16
 
 * Copyright (C) 2008 Novell, Inc.
17
 
 * Copyright (C) 2008 - 2009 Red Hat, Inc.
18
 
 */
19
 
 
20
 
#include <string.h>
21
 
#include <pppd/pppd.h>
22
 
#include <pppd/fsm.h>
23
 
#include <pppd/ipcp.h>
24
 
#include <sys/socket.h>
25
 
#include <netinet/in.h>
26
 
#include <arpa/inet.h>
27
 
#include <glib.h>
28
 
 
29
 
int plugin_init (void);
30
 
 
31
 
char pppd_version[] = VERSION;
32
 
char *my_user = NULL;
33
 
char *my_pass = NULL;
34
 
char *my_file = NULL;
35
 
 
36
 
static void
37
 
mm_phasechange (void *data, int arg)
38
 
{
39
 
        const char *ppp_phase = NULL;
40
 
 
41
 
        switch (arg) {
42
 
        case PHASE_DEAD:
43
 
                ppp_phase = "dead";
44
 
                break;
45
 
        case PHASE_INITIALIZE:
46
 
                ppp_phase = "initialize";
47
 
                break;
48
 
        case PHASE_SERIALCONN:
49
 
                ppp_phase = "serial connection";
50
 
                break;
51
 
        case PHASE_DORMANT:
52
 
                ppp_phase = "dormant";
53
 
                break;
54
 
        case PHASE_ESTABLISH:
55
 
                ppp_phase = "establish";
56
 
                break;
57
 
        case PHASE_AUTHENTICATE:
58
 
                ppp_phase = "authenticate";
59
 
                break;
60
 
        case PHASE_CALLBACK:
61
 
                ppp_phase = "callback";
62
 
                break;
63
 
        case PHASE_NETWORK:
64
 
                ppp_phase = "network";
65
 
                break;
66
 
        case PHASE_RUNNING:
67
 
                ppp_phase = "running";
68
 
                break;
69
 
        case PHASE_TERMINATE:
70
 
                ppp_phase = "terminate";
71
 
                break;
72
 
        case PHASE_DISCONNECT:
73
 
                ppp_phase = "disconnect";
74
 
                break;
75
 
        case PHASE_HOLDOFF:
76
 
                ppp_phase = "holdoff";
77
 
                break;
78
 
        case PHASE_MASTER:
79
 
                ppp_phase = "master";
80
 
                break;
81
 
        default:
82
 
                ppp_phase = "unknown";
83
 
                break;
84
 
        }
85
 
 
86
 
        g_message ("mm-test-ppp-plugin: (%s): phase now '%s'", __func__, ppp_phase);
87
 
}
88
 
 
89
 
static void
90
 
append_ip4_addr (GString *str, const char *tag, guint32 addr)
91
 
{
92
 
        char buf[INET_ADDRSTRLEN + 2];
93
 
        struct in_addr tmp_addr = { .s_addr = addr };
94
 
 
95
 
        memset (buf, 0, sizeof (buf));
96
 
 
97
 
        if (inet_ntop (AF_INET, &tmp_addr, buf, sizeof (buf) - 1))
98
 
                g_string_append_printf (str, "%s %s\n", tag, buf);
99
 
}
100
 
 
101
 
static void
102
 
mm_ip_up (void *data, int arg)
103
 
{
104
 
        ipcp_options opts = ipcp_gotoptions[0];
105
 
        ipcp_options peer_opts = ipcp_hisoptions[0];
106
 
        guint32 pppd_made_up_address = htonl (0x0a404040 + ifunit);
107
 
        GString *contents;
108
 
        GError *err = NULL;
109
 
        gboolean success;
110
 
 
111
 
        g_message ("mm-test-ppp-plugin: (%s): ip-up event", __func__);
112
 
 
113
 
        if (!opts.ouraddr) {
114
 
                g_warning ("mm-test-ppp-plugin: (%s): didn't receive an internal IP from pppd!", __func__);
115
 
                mm_phasechange (NULL, PHASE_DEAD);
116
 
                return;
117
 
        }
118
 
 
119
 
        contents = g_string_sized_new (100);
120
 
 
121
 
        g_string_append_printf (contents, "iface %s\n", ifname);
122
 
 
123
 
        append_ip4_addr (contents, "addr", opts.ouraddr);
124
 
 
125
 
        /* Prefer the peer options remote address first, _unless_ pppd made the
126
 
         * address up, at which point prefer the local options remote address,
127
 
         * and if that's not right, use the made-up address as a last resort.
128
 
         */
129
 
        if (peer_opts.hisaddr && (peer_opts.hisaddr != pppd_made_up_address))
130
 
                append_ip4_addr (contents, "gateway", peer_opts.hisaddr);
131
 
        else if (opts.hisaddr)
132
 
                append_ip4_addr (contents, "gateway", opts.hisaddr);
133
 
        else if (peer_opts.hisaddr == pppd_made_up_address) {
134
 
                /* As a last resort, use the made-up address */
135
 
                append_ip4_addr (contents, "gateway", peer_opts.hisaddr);
136
 
        }
137
 
 
138
 
        if (opts.dnsaddr[0] || opts.dnsaddr[1]) {
139
 
                if (opts.dnsaddr[0])
140
 
                        append_ip4_addr (contents, "dns1", opts.dnsaddr[0]);
141
 
                if (opts.dnsaddr[1])
142
 
                        append_ip4_addr (contents, "dns2", opts.dnsaddr[1]);
143
 
        }
144
 
 
145
 
        if (opts.winsaddr[0] || opts.winsaddr[1]) {
146
 
                if (opts.winsaddr[0])
147
 
                        append_ip4_addr (contents, "wins1", opts.winsaddr[0]);
148
 
                if (opts.winsaddr[1])
149
 
                        append_ip4_addr (contents, "wins2", opts.winsaddr[1]);
150
 
        }
151
 
 
152
 
        g_string_append (contents, "DONE\n");
153
 
 
154
 
        success = g_file_set_contents (my_file, contents->str, -1, &err);
155
 
        if (success)
156
 
                g_message ("nm-ppp-plugin: (%s): saved IP4 config to %s", __func__, my_file);
157
 
        else {
158
 
                g_message ("nm-ppp-plugin: (%s): error saving IP4 config to %s: (%d) %s",
159
 
                           __func__, my_file, err->code, err->message);
160
 
                g_clear_error (&err);
161
 
        }
162
 
 
163
 
        g_string_free (contents, TRUE);
164
 
}
165
 
 
166
 
static int
167
 
get_chap_check()
168
 
{
169
 
        return 1;
170
 
}
171
 
 
172
 
static int
173
 
get_pap_check()
174
 
{
175
 
        return 1;
176
 
}
177
 
 
178
 
static int
179
 
get_credentials (char *username, char *password)
180
 
{
181
 
        size_t len;
182
 
 
183
 
        if (username && !password) {
184
 
                /* pppd is checking pap support; return 1 for supported */
185
 
                return 1;
186
 
        }
187
 
 
188
 
        g_message ("nm-ppp-plugin: (%s): sending credentials (%s / %s)",
189
 
                   __func__,
190
 
                   my_user ? my_user : "",
191
 
                   my_pass ? my_pass : "");
192
 
 
193
 
        if (my_user) {
194
 
                len = strlen (my_user) + 1;
195
 
                len = len < MAXNAMELEN ? len : MAXNAMELEN;
196
 
 
197
 
                strncpy (username, my_user, len);
198
 
                username[len - 1] = '\0';
199
 
        }
200
 
 
201
 
        if (my_pass) {
202
 
                len = strlen (my_pass) + 1;
203
 
                len = len < MAXSECRETLEN ? len : MAXSECRETLEN;
204
 
 
205
 
                strncpy (password, my_pass, len);
206
 
                password[len - 1] = '\0';
207
 
        }
208
 
 
209
 
        return 1;
210
 
}
211
 
 
212
 
static void
213
 
mm_exit_notify (void *data, int arg)
214
 
{
215
 
        g_message ("mm-test-ppp-plugin: (%s): cleaning up", __func__);
216
 
 
217
 
        g_free (my_user);
218
 
        my_user = NULL;
219
 
        g_free (my_pass);
220
 
        my_pass = NULL;
221
 
        g_free (my_file);
222
 
        my_file = NULL;
223
 
}
224
 
 
225
 
int
226
 
plugin_init (void)
227
 
{
228
 
        char **args;
229
 
 
230
 
        g_message ("mm-test-ppp-plugin: (%s): initializing", __func__);
231
 
 
232
 
        /* mm-test passes the file + username + password in as the 'ipparam' arg
233
 
         * to pppd.
234
 
         */
235
 
        args = g_strsplit (ipparam, "+", 0);
236
 
        if (!args || g_strv_length (args) != 3) {
237
 
                g_message ("mm-test-ppp-plugin: (%s): ipparam arguments error ('%s')",
238
 
                           __func__, ipparam);
239
 
                return -1;
240
 
        }
241
 
 
242
 
        my_user = (args[0] && strlen (args[0])) ? g_strdup (args[0]) : NULL;
243
 
        my_pass = (args[1] && strlen (args[1])) ? g_strdup (args[1]) : NULL;
244
 
        my_file = (args[2] && strlen (args[2])) ? g_strdup (args[2]) : NULL;
245
 
 
246
 
        g_strfreev (args);
247
 
 
248
 
        if (!my_file) {
249
 
                g_message ("mm-test-ppp-plugin: (%s): missing IP config file",
250
 
                           __func__);
251
 
                return -1;
252
 
        }
253
 
 
254
 
        chap_passwd_hook = get_credentials;
255
 
        chap_check_hook = get_chap_check;
256
 
        pap_passwd_hook = get_credentials;
257
 
        pap_check_hook = get_pap_check;
258
 
 
259
 
        add_notifier (&phasechange, mm_phasechange, NULL);
260
 
        add_notifier (&ip_up_notifier, mm_ip_up, NULL);
261
 
        add_notifier (&exitnotify, mm_exit_notify, NULL);
262
 
 
263
 
        return 0;
264
 
}