Upgrade ofono to 1.2
[profile/ivi/ofono.git] / gatchat / gatppp.c
1 /*
2  *
3  *  PPP library with GLib integration
4  *
5  *  Copyright (C) 2009-2011  Intel Corporation. All rights reserved.
6  *
7  *  This program is free software; you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License version 2 as
9  *  published by the Free Software Foundation.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, write to the Free Software
18  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <stdio.h>
27 #include <fcntl.h>
28 #include <unistd.h>
29 #include <string.h>
30 #include <termios.h>
31 #include <sys/stat.h>
32 #include <sys/time.h>
33 #include <arpa/inet.h>
34
35 #include <glib.h>
36
37 #include "gatutil.h"
38 #include "gathdlc.h"
39 #include "gatppp.h"
40 #include "crc-ccitt.h"
41 #include "ppp.h"
42
43 #define DEFAULT_MRU     1500
44 #define DEFAULT_MTU     1500
45
46 #define PPP_ADDR_FIELD  0xff
47 #define PPP_CTRL        0x03
48
49 #define GUARD_TIMEOUTS 1500
50
51 enum ppp_phase {
52         PPP_PHASE_DEAD = 0,             /* Link dead */
53         PPP_PHASE_ESTABLISHMENT,        /* LCP started */
54         PPP_PHASE_AUTHENTICATION,       /* Auth started */
55         PPP_PHASE_NETWORK,              /* IPCP started */
56         PPP_PHASE_LINK_UP,              /* IPCP negotiation ok, link up */
57         PPP_PHASE_TERMINATION,          /* LCP Terminate phase */
58 };
59
60 struct _GAtPPP {
61         gint ref_count;
62         enum ppp_phase phase;
63         struct pppcp_data *lcp;
64         struct pppcp_data *ipcp;
65         struct ppp_net *net;
66         struct ppp_chap *chap;
67         GAtHDLC *hdlc;
68         gint mru;
69         gint mtu;
70         char username[256];
71         char password[256];
72         GAtPPPConnectFunc connect_cb;
73         gpointer connect_data;
74         GAtPPPDisconnectFunc disconnect_cb;
75         gpointer disconnect_data;
76         GAtPPPDisconnectReason disconnect_reason;
77         GAtDebugFunc debugf;
78         gpointer debug_data;
79         gboolean sta_pending;
80         guint ppp_dead_source;
81         GAtSuspendFunc suspend_func;
82         gpointer suspend_data;
83         int fd;
84         guint guard_timeout_source;
85         gboolean suspended;
86         gboolean xmit_acfc;
87         gboolean xmit_pfc;
88 };
89
90 void ppp_debug(GAtPPP *ppp, const char *str)
91 {
92         if (ppp == NULL || ppp->debugf == NULL)
93                 return;
94
95         ppp->debugf(str, ppp->debug_data);
96 }
97
98 static gboolean ppp_dead(gpointer userdata)
99 {
100         GAtPPP *ppp = userdata;
101
102         DBG(ppp, "");
103
104         ppp->ppp_dead_source = 0;
105
106         /* notify interested parties */
107         if (ppp->disconnect_cb)
108                 ppp->disconnect_cb(ppp->disconnect_reason,
109                                         ppp->disconnect_data);
110
111         return FALSE;
112 }
113
114 static void sta_sent(gpointer userdata)
115 {
116         GAtPPP *ppp = userdata;
117
118         DBG(ppp, "");
119
120         ppp->sta_pending = FALSE;
121
122         if (ppp->phase == PPP_PHASE_DEAD)
123                 ppp_dead(ppp);
124 }
125
126 struct ppp_header *ppp_packet_new(gsize infolen, guint16 protocol)
127 {
128         struct ppp_header *ppp_packet;
129
130         ppp_packet = g_try_malloc0(infolen + sizeof(*ppp_packet));
131         if (ppp_packet == NULL)
132                 return NULL;
133
134         ppp_packet->proto = htons(protocol);
135         ppp_packet->address = PPP_ADDR_FIELD;
136         ppp_packet->control = PPP_CTRL;
137
138         return ppp_packet;
139 }
140
141 /*
142  * Silently discard packets which are received when they shouldn't be
143  */
144 static inline gboolean ppp_drop_packet(GAtPPP *ppp, guint16 protocol)
145 {
146         switch (ppp->phase) {
147         case PPP_PHASE_ESTABLISHMENT:
148         case PPP_PHASE_TERMINATION:
149                 if (protocol != LCP_PROTOCOL)
150                         return TRUE;
151                 break;
152         case PPP_PHASE_AUTHENTICATION:
153                 if (protocol != LCP_PROTOCOL && protocol != CHAP_PROTOCOL)
154                         return TRUE;
155                 break;
156         case PPP_PHASE_DEAD:
157                 return TRUE;
158         case PPP_PHASE_NETWORK:
159                 if (protocol != LCP_PROTOCOL && protocol != CHAP_PROTOCOL &&
160                                         protocol != IPCP_PROTO)
161                         return TRUE;
162                 break;
163         case PPP_PHASE_LINK_UP:
164                 break;
165         }
166
167         return FALSE;
168 }
169
170 static void ppp_receive(const unsigned char *buf, gsize len, void *data)
171 {
172         GAtPPP *ppp = data;
173         unsigned int offset = 0;
174         guint16 protocol;
175         const guint8 *packet;
176
177         if (len == 0)
178                 return;
179
180         if (buf[0] == PPP_ADDR_FIELD && len >= 2 && buf[1] == PPP_CTRL)
181                 offset = 2;
182
183         if (len < offset + 1)
184                 return;
185
186         /* From RFC 1661:
187          * the Protocol field uses an extension mechanism consistent with the
188          * ISO 3309 extension mechanism for the Address field; the Least
189          * Significant Bit (LSB) of each octet is used to indicate extension
190          * of the Protocol field.  A binary "0" as the LSB indicates that the
191          * Protocol field continues with the following octet.  The presence
192          * of a binary "1" as the LSB marks the last octet of the Protocol
193          * field.
194          *
195          * To check for compression we simply check the LSB of the first
196          * protocol byte.
197          */
198
199         if (buf[offset] & 0x1) {
200                 protocol = buf[offset];
201                 offset += 1;
202         } else {
203                 if (len < offset + 2)
204                         return;
205
206                 protocol = get_host_short(buf + offset);
207                 offset += 2;
208         }
209
210         if (ppp_drop_packet(ppp, protocol))
211                 return;
212
213         packet = buf + offset;
214
215         switch (protocol) {
216         case PPP_IP_PROTO:
217                 ppp_net_process_packet(ppp->net, packet, len - offset);
218                 break;
219         case LCP_PROTOCOL:
220                 pppcp_process_packet(ppp->lcp, packet, len - offset);
221                 break;
222         case IPCP_PROTO:
223                 pppcp_process_packet(ppp->ipcp, packet, len - offset);
224                 break;
225         case CHAP_PROTOCOL:
226                 if (ppp->chap) {
227                         ppp_chap_process_packet(ppp->chap, packet,
228                                                         len - offset);
229                         break;
230                 }
231                 /* fall through */
232         default:
233                 pppcp_send_protocol_reject(ppp->lcp, buf, len);
234                 break;
235         };
236 }
237
238 static void ppp_send_lcp_frame(GAtPPP *ppp, guint8 *packet, guint infolen)
239 {
240         struct ppp_header *header = (struct ppp_header *) packet;
241         guint8 code;
242         guint32 xmit_accm = 0;
243         gboolean sta = FALSE;
244         gboolean lcp;
245
246         /*
247          * all LCP Link Configuration, Link Termination, and Code-Reject
248          * packets must be sent with the default sending ACCM
249          */
250         code = pppcp_get_code(packet);
251         lcp = code > 0 && code < 8;
252
253         /*
254          * If we're going down, we try to make sure to send the final
255          * ack before informing the upper layers via the ppp_disconnect
256          * function.  Once we enter PPP_DEAD phase, no further packets
257          * will be sent
258          */
259         if (code == PPPCP_CODE_TYPE_TERMINATE_ACK)
260                 sta = TRUE;
261
262         if (lcp) {
263                 xmit_accm = g_at_hdlc_get_xmit_accm(ppp->hdlc);
264                 g_at_hdlc_set_xmit_accm(ppp->hdlc, ~0U);
265         }
266
267         header->address = PPP_ADDR_FIELD;
268         header->control = PPP_CTRL;
269
270         if (g_at_hdlc_send(ppp->hdlc, packet,
271                         infolen + sizeof(*header)) == TRUE) {
272                 if (sta) {
273                         GAtIO *io = g_at_hdlc_get_io(ppp->hdlc);
274
275                         ppp->sta_pending = TRUE;
276                         g_at_io_set_write_done(io, sta_sent, ppp);
277                 }
278         } else
279                 DBG(ppp, "Failed to send a frame\n");
280
281         if (lcp)
282                 g_at_hdlc_set_xmit_accm(ppp->hdlc, xmit_accm);
283 }
284
285 static void ppp_send_acfc_frame(GAtPPP *ppp, guint8 *packet,
286                                         guint infolen)
287 {
288         struct ppp_header *header = (struct ppp_header *) packet;
289         guint offset = 0;
290
291         if (ppp->xmit_acfc)
292                 offset = 2;
293
294         /* We remove the only address and control field */
295         if (g_at_hdlc_send(ppp->hdlc, packet + offset,
296                                 infolen + sizeof(*header) - offset)
297                         == FALSE)
298                 DBG(ppp, "Failed to send a frame\n");
299 }
300
301 static void ppp_send_acfc_pfc_frame(GAtPPP *ppp, guint8 *packet,
302                                         guint infolen)
303 {
304         struct ppp_header *header = (struct ppp_header *) packet;
305         guint offset = 0;
306
307         if (ppp->xmit_acfc && ppp->xmit_pfc)
308                 offset = 3;
309         else if (ppp->xmit_acfc)
310                 offset = 2;
311         else if (ppp->xmit_pfc) {
312                 /* Shuffle AC bytes in place of the first protocol byte */
313                 packet[2] = packet[1];
314                 packet[1] = packet[0];
315                 offset = 1;
316         }
317
318         if (g_at_hdlc_send(ppp->hdlc, packet + offset,
319                                 infolen + sizeof(*header) - offset)
320                         == FALSE)
321                 DBG(ppp, "Failed to send a frame\n");
322 }
323
324 /*
325  * transmit out through the lower layer interface
326  *
327  * infolen - length of the information part of the packet
328  */
329 void ppp_transmit(GAtPPP *ppp, guint8 *packet, guint infolen)
330 {
331         guint16 proto = ppp_proto(packet);
332
333         if (proto == LCP_PROTOCOL) {
334                 ppp_send_lcp_frame(ppp, packet, infolen);
335                 return;
336         }
337
338         /*
339          * If the upper 8 bits of the protocol are 0, then send
340          * with PFC if enabled
341          */
342         if ((proto & 0xff00) == 0)
343                 ppp_send_acfc_pfc_frame(ppp, packet, infolen);
344         else
345                 ppp_send_acfc_frame(ppp, packet, infolen);
346 }
347
348 static inline void ppp_enter_phase(GAtPPP *ppp, enum ppp_phase phase)
349 {
350         DBG(ppp, "%d", phase);
351         ppp->phase = phase;
352
353         if (phase == PPP_PHASE_DEAD && ppp->sta_pending == FALSE)
354                 ppp->ppp_dead_source = g_idle_add(ppp_dead, ppp);
355 }
356
357 void ppp_set_auth(GAtPPP *ppp, const guint8* auth_data)
358 {
359         guint16 proto = get_host_short(auth_data);
360
361         switch (proto) {
362         case CHAP_PROTOCOL:
363                 if (ppp->chap)
364                         ppp_chap_free(ppp->chap);
365
366                 ppp->chap = ppp_chap_new(ppp, auth_data[2]);
367                 break;
368         default:
369                 DBG(ppp, "unknown authentication proto");
370                 break;
371         }
372 }
373
374 void ppp_auth_notify(GAtPPP *ppp, gboolean success)
375 {
376         if (success == FALSE) {
377                 ppp->disconnect_reason = G_AT_PPP_REASON_AUTH_FAIL;
378                 pppcp_signal_close(ppp->lcp);
379                 return;
380         }
381
382         ppp_enter_phase(ppp, PPP_PHASE_NETWORK);
383
384         /* Send UP & OPEN events to the IPCP layer */
385         pppcp_signal_open(ppp->ipcp);
386         pppcp_signal_up(ppp->ipcp);
387 }
388
389 void ppp_ipcp_up_notify(GAtPPP *ppp, const char *local, const char *peer,
390                                         const char *dns1, const char *dns2)
391 {
392         ppp->net = ppp_net_new(ppp, ppp->fd);
393
394         /*
395          * ppp_net_new took control over the fd, whatever happens is out of
396          * our hands now
397          */
398         ppp->fd = -1;
399
400         if (ppp->net == NULL) {
401                 ppp->disconnect_reason = G_AT_PPP_REASON_NET_FAIL;
402                 pppcp_signal_close(ppp->lcp);
403                 return;
404         }
405
406         if (ppp_net_set_mtu(ppp->net, ppp->mtu) == FALSE)
407                 DBG(ppp, "Unable to set MTU");
408
409         ppp_enter_phase(ppp, PPP_PHASE_LINK_UP);
410
411         if (ppp->connect_cb)
412                 ppp->connect_cb(ppp_net_get_interface(ppp->net),
413                                         local, peer, dns1, dns2,
414                                         ppp->connect_data);
415 }
416
417 void ppp_ipcp_down_notify(GAtPPP *ppp)
418 {
419         /* Most likely we failed to create the interface */
420         if (ppp->net == NULL)
421                 return;
422
423         ppp_net_free(ppp->net);
424         ppp->net = NULL;
425 }
426
427 void ppp_ipcp_finished_notify(GAtPPP *ppp)
428 {
429         if (ppp->phase != PPP_PHASE_NETWORK)
430                 return;
431
432         /* Our IPCP parameter negotiation failed */
433         ppp->disconnect_reason = G_AT_PPP_REASON_IPCP_FAIL;
434         pppcp_signal_close(ppp->ipcp);
435         pppcp_signal_close(ppp->lcp);
436 }
437
438 void ppp_lcp_up_notify(GAtPPP *ppp)
439 {
440         /* Wait for the peer to send us a challenge if we expect auth */
441         if (ppp->chap != NULL) {
442                 ppp_enter_phase(ppp, PPP_PHASE_AUTHENTICATION);
443                 return;
444         }
445
446         /* Otherwise proceed as if auth succeeded */
447         ppp_auth_notify(ppp, TRUE);
448 }
449
450 void ppp_lcp_down_notify(GAtPPP *ppp)
451 {
452         if (ppp->phase == PPP_PHASE_NETWORK || ppp->phase == PPP_PHASE_LINK_UP)
453                 pppcp_signal_down(ppp->ipcp);
454
455         if (ppp->disconnect_reason == G_AT_PPP_REASON_UNKNOWN)
456                 ppp->disconnect_reason = G_AT_PPP_REASON_PEER_CLOSED;
457
458         ppp_enter_phase(ppp, PPP_PHASE_TERMINATION);
459 }
460
461 void ppp_lcp_finished_notify(GAtPPP *ppp)
462 {
463         ppp_enter_phase(ppp, PPP_PHASE_DEAD);
464 }
465
466 void ppp_set_recv_accm(GAtPPP *ppp, guint32 accm)
467 {
468         g_at_hdlc_set_recv_accm(ppp->hdlc, accm);
469 }
470
471 void ppp_set_xmit_accm(GAtPPP *ppp, guint32 accm)
472 {
473         g_at_hdlc_set_xmit_accm(ppp->hdlc, accm);
474 }
475
476 /*
477  * The only time we use other than default MTU is when we are in
478  * the network phase.
479  */
480 void ppp_set_mtu(GAtPPP *ppp, const guint8 *data)
481 {
482         guint16 mtu = get_host_short(data);
483
484         ppp->mtu = mtu;
485 }
486
487 void ppp_set_xmit_acfc(GAtPPP *ppp, gboolean acfc)
488 {
489         ppp->xmit_acfc = acfc;
490 }
491
492 void ppp_set_xmit_pfc(GAtPPP *ppp, gboolean pfc)
493 {
494         ppp->xmit_pfc = pfc;
495 }
496
497 static void io_disconnect(gpointer user_data)
498 {
499         GAtPPP *ppp = user_data;
500
501         if (ppp->phase == PPP_PHASE_DEAD)
502                 return;
503
504         ppp->disconnect_reason = G_AT_PPP_REASON_LINK_DEAD;
505         pppcp_signal_down(ppp->lcp);
506         pppcp_signal_close(ppp->lcp);
507 }
508
509 static void ppp_proxy_suspend_net_interface(gpointer user_data)
510 {
511         GAtPPP *ppp = user_data;
512
513         ppp->suspended = TRUE;
514         ppp_net_suspend_interface(ppp->net);
515
516         if (ppp->suspend_func)
517                 ppp->suspend_func(ppp->suspend_data);
518 }
519
520 gboolean g_at_ppp_listen(GAtPPP *ppp, GAtIO *io)
521 {
522         ppp->hdlc = g_at_hdlc_new_from_io(io);
523         if (ppp->hdlc == NULL)
524                 return FALSE;
525
526         ppp->suspended = FALSE;
527         g_at_hdlc_set_receive(ppp->hdlc, ppp_receive, ppp);
528         g_at_hdlc_set_suspend_function(ppp->hdlc,
529                                         ppp_proxy_suspend_net_interface, ppp);
530         g_at_io_set_disconnect_function(io, io_disconnect, ppp);
531
532         ppp_enter_phase(ppp, PPP_PHASE_ESTABLISHMENT);
533
534         return TRUE;
535 }
536
537 /* Administrative Open */
538 gboolean g_at_ppp_open(GAtPPP *ppp, GAtIO *io)
539 {
540         ppp->hdlc = g_at_hdlc_new_from_io(io);
541         if (ppp->hdlc == NULL)
542                 return FALSE;
543
544         ppp->suspended = FALSE;
545         g_at_hdlc_set_receive(ppp->hdlc, ppp_receive, ppp);
546         g_at_hdlc_set_suspend_function(ppp->hdlc,
547                                         ppp_proxy_suspend_net_interface, ppp);
548         g_at_hdlc_set_no_carrier_detect(ppp->hdlc, TRUE);
549         g_at_io_set_disconnect_function(io, io_disconnect, ppp);
550
551         /* send an UP & OPEN events to the lcp layer */
552         pppcp_signal_up(ppp->lcp);
553         pppcp_signal_open(ppp->lcp);
554
555         ppp_enter_phase(ppp, PPP_PHASE_ESTABLISHMENT);
556
557         return TRUE;
558 }
559
560 gboolean g_at_ppp_set_credentials(GAtPPP *ppp, const char *username,
561                                         const char *password)
562 {
563         if (username && strlen(username) > 255)
564                 return FALSE;
565
566         if (password && strlen(password) > 255)
567                 return FALSE;
568
569         memset(ppp->username, 0, sizeof(ppp->username));
570         memset(ppp->password, 0, sizeof(ppp->password));
571
572         if (username)
573                 strcpy(ppp->username, username);
574
575         if (password)
576                 strcpy(ppp->password, password);
577
578         return TRUE;
579 }
580
581 const char *g_at_ppp_get_username(GAtPPP *ppp)
582 {
583         return ppp->username;
584 }
585
586 const char *g_at_ppp_get_password(GAtPPP *ppp)
587 {
588         return ppp->password;
589 }
590
591 void g_at_ppp_set_recording(GAtPPP *ppp, const char *filename)
592 {
593         if (ppp == NULL)
594                 return;
595
596         g_at_hdlc_set_recording(ppp->hdlc, filename);
597 }
598
599 void g_at_ppp_set_connect_function(GAtPPP *ppp, GAtPPPConnectFunc func,
600                                                         gpointer user_data)
601 {
602         if (func == NULL)
603                 return;
604
605         ppp->connect_cb = func;
606         ppp->connect_data = user_data;
607 }
608
609 void g_at_ppp_set_disconnect_function(GAtPPP *ppp, GAtPPPDisconnectFunc func,
610                                                         gpointer user_data)
611 {
612         if (func == NULL)
613                 return;
614
615         ppp->disconnect_cb = func;
616         ppp->disconnect_data = user_data;
617 }
618
619 void g_at_ppp_set_debug(GAtPPP *ppp, GAtDebugFunc func, gpointer user_data)
620 {
621         if (ppp == NULL)
622                 return;
623
624         ppp->debugf = func;
625         ppp->debug_data = user_data;
626 }
627
628 void g_at_ppp_set_suspend_function(GAtPPP *ppp, GAtSuspendFunc func,
629                                         gpointer user_data)
630 {
631         if (ppp == NULL)
632                 return;
633
634         ppp->suspend_func = func;
635         ppp->suspend_data = user_data;
636
637         if (ppp->hdlc != NULL)
638                 g_at_hdlc_set_suspend_function(ppp->hdlc,
639                                         ppp_proxy_suspend_net_interface, ppp);
640 }
641
642 void g_at_ppp_shutdown(GAtPPP *ppp)
643 {
644         if (ppp->phase == PPP_PHASE_DEAD || ppp->phase == PPP_PHASE_TERMINATION)
645                 return;
646
647         ppp->disconnect_reason = G_AT_PPP_REASON_LOCAL_CLOSE;
648         pppcp_signal_close(ppp->lcp);
649 }
650
651 static gboolean call_suspend_cb(gpointer user_data)
652 {
653         GAtPPP *ppp = user_data;
654
655         ppp->guard_timeout_source = 0;
656
657         if (ppp->suspend_func)
658                 ppp->suspend_func(ppp->suspend_data);
659
660         return FALSE;
661 }
662
663 static gboolean send_escape_sequence(gpointer user_data)
664 {
665         GAtPPP *ppp = user_data;
666         GAtIO *io = g_at_hdlc_get_io(ppp->hdlc);
667
668         g_at_io_write(io, "+++", 3);
669         ppp->guard_timeout_source  = g_timeout_add(GUARD_TIMEOUTS,
670                                                 call_suspend_cb, ppp);
671
672         return FALSE;
673 }
674
675 void g_at_ppp_suspend(GAtPPP *ppp)
676 {
677         if (ppp == NULL)
678                 return;
679
680         ppp->suspended = TRUE;
681         ppp_net_suspend_interface(ppp->net);
682         g_at_hdlc_suspend(ppp->hdlc);
683         ppp->guard_timeout_source = g_timeout_add(GUARD_TIMEOUTS,
684                                                 send_escape_sequence, ppp);
685 }
686
687 void g_at_ppp_resume(GAtPPP *ppp)
688 {
689         if (ppp == NULL)
690                 return;
691
692         if (g_at_hdlc_get_io(ppp->hdlc) == NULL) {
693                 io_disconnect(ppp);
694                 return;
695         }
696
697         ppp->suspended = FALSE;
698         g_at_io_set_disconnect_function(g_at_hdlc_get_io(ppp->hdlc),
699                                                         io_disconnect, ppp);
700         ppp_net_resume_interface(ppp->net);
701         g_at_hdlc_resume(ppp->hdlc);
702 }
703
704 void g_at_ppp_ref(GAtPPP *ppp)
705 {
706         g_atomic_int_inc(&ppp->ref_count);
707 }
708
709 void g_at_ppp_unref(GAtPPP *ppp)
710 {
711         gboolean is_zero;
712
713         if (ppp == NULL)
714                 return;
715
716         is_zero = g_atomic_int_dec_and_test(&ppp->ref_count);
717
718         if (is_zero == FALSE)
719                 return;
720
721         if (ppp->suspended == FALSE)
722                 g_at_io_set_disconnect_function(g_at_hdlc_get_io(ppp->hdlc),
723                                                         NULL, NULL);
724
725         if (ppp->net)
726                 ppp_net_free(ppp->net);
727         else if (ppp->fd >= 0)
728                 close(ppp->fd);
729
730         if (ppp->chap)
731                 ppp_chap_free(ppp->chap);
732
733         lcp_free(ppp->lcp);
734         ipcp_free(ppp->ipcp);
735
736         if (ppp->ppp_dead_source) {
737                 g_source_remove(ppp->ppp_dead_source);
738                 ppp->ppp_dead_source = 0;
739         }
740
741         if (ppp->guard_timeout_source) {
742                 g_source_remove(ppp->guard_timeout_source);
743                 ppp->guard_timeout_source = 0;
744         }
745
746         g_at_hdlc_unref(ppp->hdlc);
747
748         g_free(ppp);
749 }
750
751 void g_at_ppp_set_server_info(GAtPPP *ppp, const char *remote,
752                                 const char *dns1, const char *dns2)
753 {
754         guint32 r = 0;
755         guint32 d1 = 0;
756         guint32 d2 = 0;
757
758         inet_pton(AF_INET, remote, &r);
759         inet_pton(AF_INET, dns1, &d1);
760         inet_pton(AF_INET, dns2, &d2);
761
762         ipcp_set_server_info(ppp->ipcp, r, d1, d2);
763 }
764
765 void g_at_ppp_set_acfc_enabled(GAtPPP *ppp, gboolean enabled)
766 {
767         lcp_set_acfc_enabled(ppp->lcp, enabled);
768 }
769
770 void g_at_ppp_set_pfc_enabled(GAtPPP *ppp, gboolean enabled)
771 {
772         lcp_set_pfc_enabled(ppp->lcp, enabled);
773 }
774
775 static GAtPPP *ppp_init_common(gboolean is_server, guint32 ip)
776 {
777         GAtPPP *ppp;
778
779         ppp = g_try_malloc0(sizeof(GAtPPP));
780         if (ppp == NULL)
781                 return NULL;
782
783         ppp->ref_count = 1;
784         ppp->suspended = TRUE;
785         ppp->fd = -1;
786
787         /* set options to defaults */
788         ppp->mru = DEFAULT_MRU;
789         ppp->mtu = DEFAULT_MTU;
790
791         /* initialize the lcp state */
792         ppp->lcp = lcp_new(ppp, is_server);
793
794         /* initialize IPCP state */
795         ppp->ipcp = ipcp_new(ppp, is_server, ip);
796
797         return ppp;
798 }
799
800 GAtPPP *g_at_ppp_new(void)
801 {
802         return ppp_init_common(FALSE, 0);
803 }
804
805 GAtPPP *g_at_ppp_server_new_full(const char *local, int fd)
806 {
807         GAtPPP *ppp;
808         guint32 ip;
809
810         if (local == NULL)
811                 ip = 0;
812         else if (inet_pton(AF_INET, local, &ip) != 1)
813                 return NULL;
814
815         ppp = ppp_init_common(TRUE, ip);
816
817         if (ppp != NULL)
818                 ppp->fd = fd;
819
820         return ppp;
821 }
822
823 GAtPPP *g_at_ppp_server_new(const char *local)
824 {
825         return g_at_ppp_server_new_full(local, -1);
826 }