1  | 
     | 
     | 
    /*	$OpenBSD: pptp_subr.c,v 1.4 2012/05/08 13:15:12 yasuoka Exp $ */  | 
    
    
    2  | 
     | 
     | 
     | 
    
    
    3  | 
     | 
     | 
    /*-  | 
    
    
    4  | 
     | 
     | 
     * Copyright (c) 2009 Internet Initiative Japan Inc.  | 
    
    
    5  | 
     | 
     | 
     * All rights reserved.  | 
    
    
    6  | 
     | 
     | 
     *  | 
    
    
    7  | 
     | 
     | 
     * Redistribution and use in source and binary forms, with or without  | 
    
    
    8  | 
     | 
     | 
     * modification, are permitted provided that the following conditions  | 
    
    
    9  | 
     | 
     | 
     * are met:  | 
    
    
    10  | 
     | 
     | 
     * 1. Redistributions of source code must retain the above copyright  | 
    
    
    11  | 
     | 
     | 
     *    notice, this list of conditions and the following disclaimer.  | 
    
    
    12  | 
     | 
     | 
     * 2. Redistributions in binary form must reproduce the above copyright  | 
    
    
    13  | 
     | 
     | 
     *    notice, this list of conditions and the following disclaimer in the  | 
    
    
    14  | 
     | 
     | 
     *    documentation and/or other materials provided with the distribution.  | 
    
    
    15  | 
     | 
     | 
     *  | 
    
    
    16  | 
     | 
     | 
     * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND  | 
    
    
    17  | 
     | 
     | 
     * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE  | 
    
    
    18  | 
     | 
     | 
     * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE  | 
    
    
    19  | 
     | 
     | 
     * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE  | 
    
    
    20  | 
     | 
     | 
     * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL  | 
    
    
    21  | 
     | 
     | 
     * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS  | 
    
    
    22  | 
     | 
     | 
     * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)  | 
    
    
    23  | 
     | 
     | 
     * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT  | 
    
    
    24  | 
     | 
     | 
     * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY  | 
    
    
    25  | 
     | 
     | 
     * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF  | 
    
    
    26  | 
     | 
     | 
     * SUCH DAMAGE.  | 
    
    
    27  | 
     | 
     | 
     */  | 
    
    
    28  | 
     | 
     | 
     | 
    
    
    29  | 
     | 
     | 
    /* utility functions for the pptp.c */  | 
    
    
    30  | 
     | 
     | 
     | 
    
    
    31  | 
     | 
     | 
    #include <sys/types.h>  | 
    
    
    32  | 
     | 
     | 
    #include <sys/socket.h>  | 
    
    
    33  | 
     | 
     | 
    #include <sys/time.h>  | 
    
    
    34  | 
     | 
     | 
    #include <netinet/in.h>  | 
    
    
    35  | 
     | 
     | 
    #include <stdio.h>  | 
    
    
    36  | 
     | 
     | 
    #include <string.h>  | 
    
    
    37  | 
     | 
     | 
    #include <event.h>  | 
    
    
    38  | 
     | 
     | 
     | 
    
    
    39  | 
     | 
     | 
    #include "bytebuf.h"  | 
    
    
    40  | 
     | 
     | 
    #include "hash.h"  | 
    
    
    41  | 
     | 
     | 
    #include "slist.h"  | 
    
    
    42  | 
     | 
     | 
     | 
    
    
    43  | 
     | 
     | 
    #include "pptp.h"  | 
    
    
    44  | 
     | 
     | 
    #include "pptp_local.h"  | 
    
    
    45  | 
     | 
     | 
    #include "pptp_subr.h"  | 
    
    
    46  | 
     | 
     | 
     | 
    
    
    47  | 
     | 
     | 
    /* convert the Faming Capability bit as CSV strings */  | 
    
    
    48  | 
     | 
     | 
    const char *  | 
    
    
    49  | 
     | 
     | 
    pptp_framing_string(uint32_t bits)  | 
    
    
    50  | 
     | 
     | 
    { | 
    
    
    51  | 
     | 
     | 
    	static char framing_cap_buf[40];  | 
    
    
    52  | 
     | 
     | 
     | 
    
    
    53  | 
     | 
     | 
    	snprintf(framing_cap_buf, sizeof(framing_cap_buf), "%s%s",  | 
    
    
    54  | 
     | 
     | 
    	    (bits & PPTP_CTRL_FRAMING_ASYNC)? ",async" : "",  | 
    
    
    55  | 
     | 
     | 
    	    (bits & PPTP_CTRL_FRAMING_SYNC)? ",sync" : "");  | 
    
    
    56  | 
     | 
     | 
     | 
    
    
    57  | 
     | 
     | 
    	if (strlen(framing_cap_buf) > 0)  | 
    
    
    58  | 
     | 
     | 
    		return &framing_cap_buf[1];  | 
    
    
    59  | 
     | 
     | 
     | 
    
    
    60  | 
     | 
     | 
    	return "";  | 
    
    
    61  | 
     | 
     | 
    }  | 
    
    
    62  | 
     | 
     | 
     | 
    
    
    63  | 
     | 
     | 
     | 
    
    
    64  | 
     | 
     | 
    /* convert the Bearer Capability bit as CSV strings */  | 
    
    
    65  | 
     | 
     | 
    const char *  | 
    
    
    66  | 
     | 
     | 
    pptp_bearer_string(uint32_t bits)  | 
    
    
    67  | 
     | 
     | 
    { | 
    
    
    68  | 
     | 
     | 
    	static char bearer_cap_buf[40];  | 
    
    
    69  | 
     | 
     | 
     | 
    
    
    70  | 
     | 
     | 
    	snprintf(bearer_cap_buf, sizeof(bearer_cap_buf), "%s%s",  | 
    
    
    71  | 
     | 
     | 
    	    (bits &PPTP_CTRL_BEARER_ANALOG)? ",analog" : "",  | 
    
    
    72  | 
     | 
     | 
    	    (bits &PPTP_CTRL_BEARER_DIGITAL)? ",digital" : "");  | 
    
    
    73  | 
     | 
     | 
     | 
    
    
    74  | 
     | 
     | 
    	if (strlen(bearer_cap_buf) > 0)  | 
    
    
    75  | 
     | 
     | 
    		return &bearer_cap_buf[1];  | 
    
    
    76  | 
     | 
     | 
     | 
    
    
    77  | 
     | 
     | 
    	return "";  | 
    
    
    78  | 
     | 
     | 
    }  | 
    
    
    79  | 
     | 
     | 
     | 
    
    
    80  | 
     | 
     | 
    /* build common header part of a control packet */  | 
    
    
    81  | 
     | 
     | 
    void  | 
    
    
    82  | 
     | 
     | 
    pptp_init_header(struct pptp_ctrl_header *header, int length, int ctrl_mes_type)  | 
    
    
    83  | 
     | 
     | 
    { | 
    
    
    84  | 
     | 
     | 
    	header->length = htons(length);  | 
    
    
    85  | 
     | 
     | 
    	header->pptp_message_type = htons(PPTP_MES_TYPE_CTRL);  | 
    
    
    86  | 
     | 
     | 
    	header->magic_cookie = htonl(PPTP_MAGIC_COOKIE);  | 
    
    
    87  | 
     | 
     | 
    	header->control_message_type = htons(ctrl_mes_type);  | 
    
    
    88  | 
     | 
     | 
    	header->reserved0 = 0;  | 
    
    
    89  | 
     | 
     | 
    }  | 
    
    
    90  | 
     | 
     | 
     | 
    
    
    91  | 
     | 
     | 
    #define	NAME_VAL(x)	{ x, #x } | 
    
    
    92  | 
     | 
     | 
    static struct _label_name { | 
    
    
    93  | 
     | 
     | 
    	int		label;  | 
    
    
    94  | 
     | 
     | 
    	const char	*name;  | 
    
    
    95  | 
     | 
     | 
    } pptp_StopCCRQ_reason_labels[] = { | 
    
    
    96  | 
     | 
     | 
    	NAME_VAL(PPTP_StopCCRQ_REASON_NONE),  | 
    
    
    97  | 
     | 
     | 
    	NAME_VAL(PPTP_StopCCRQ_REASON_STOP_PROTOCOL),  | 
    
    
    98  | 
     | 
     | 
    	NAME_VAL(PPTP_StopCCRQ_REASON_STOP_LOCAL_SHUTDOWN),  | 
    
    
    99  | 
     | 
     | 
    }, pptp_StopCCRP_result_labels[] = { | 
    
    
    100  | 
     | 
     | 
    	NAME_VAL(PPTP_StopCCRP_RESULT_OK),  | 
    
    
    101  | 
     | 
     | 
    	NAME_VAL(PPTP_StopCCRP_RESULT_GENERIC_ERROR),  | 
    
    
    102  | 
     | 
     | 
    }, pptp_general_error_labels[] = { | 
    
    
    103  | 
     | 
     | 
    	NAME_VAL(PPTP_ERROR_NONE),  | 
    
    
    104  | 
     | 
     | 
    	NAME_VAL(PPTP_ERROR_NOT_CONNECTED),  | 
    
    
    105  | 
     | 
     | 
    	NAME_VAL(PPTP_ERROR_BAD_FORMAT),  | 
    
    
    106  | 
     | 
     | 
    	NAME_VAL(PPTP_ERROR_NO_RESOURCE),  | 
    
    
    107  | 
     | 
     | 
    	NAME_VAL(PPTP_ERROR_BAD_CALL),  | 
    
    
    108  | 
     | 
     | 
    }, pptp_ctrl_mes_type_labels[] = { | 
    
    
    109  | 
     | 
     | 
    	NAME_VAL(PPTP_CTRL_MES_CODE_SCCRQ),  | 
    
    
    110  | 
     | 
     | 
    	NAME_VAL(PPTP_CTRL_MES_CODE_SCCRP),  | 
    
    
    111  | 
     | 
     | 
    	NAME_VAL(PPTP_CTRL_MES_CODE_StopCCRQ),  | 
    
    
    112  | 
     | 
     | 
    	NAME_VAL(PPTP_CTRL_MES_CODE_StopCCRP),  | 
    
    
    113  | 
     | 
     | 
    	NAME_VAL(PPTP_CTRL_MES_CODE_ECHO_RQ),  | 
    
    
    114  | 
     | 
     | 
    	NAME_VAL(PPTP_CTRL_MES_CODE_ECHO_RP),  | 
    
    
    115  | 
     | 
     | 
    	NAME_VAL(PPTP_CTRL_MES_CODE_OCRQ),  | 
    
    
    116  | 
     | 
     | 
    	NAME_VAL(PPTP_CTRL_MES_CODE_OCRP),  | 
    
    
    117  | 
     | 
     | 
    	NAME_VAL(PPTP_CTRL_MES_CODE_ICRQ),  | 
    
    
    118  | 
     | 
     | 
    	NAME_VAL(PPTP_CTRL_MES_CODE_ICRP),  | 
    
    
    119  | 
     | 
     | 
    	NAME_VAL(PPTP_CTRL_MES_CODE_ICCN),  | 
    
    
    120  | 
     | 
     | 
    	NAME_VAL(PPTP_CTRL_MES_CODE_CCR),  | 
    
    
    121  | 
     | 
     | 
    	NAME_VAL(PPTP_CTRL_MES_CODE_CDN),  | 
    
    
    122  | 
     | 
     | 
    	NAME_VAL(PPTP_CTRL_MES_CODE_SLI),  | 
    
    
    123  | 
     | 
     | 
    }, pptp_CDN_result_labels[] = { | 
    
    
    124  | 
     | 
     | 
    	NAME_VAL(PPTP_CDN_RESULT_LOST_CARRIER),  | 
    
    
    125  | 
     | 
     | 
    	NAME_VAL(PPTP_CDN_RESULT_GENRIC_ERROR),  | 
    
    
    126  | 
     | 
     | 
    	NAME_VAL(PPTP_CDN_RESULT_ADMIN_SHUTDOWN),  | 
    
    
    127  | 
     | 
     | 
    	NAME_VAL(PPTP_CDN_RESULT_REQUEST),  | 
    
    
    128  | 
     | 
     | 
    };  | 
    
    
    129  | 
     | 
     | 
     | 
    
    
    130  | 
     | 
     | 
     | 
    
    
    131  | 
     | 
     | 
    /* value to strings convert macros */  | 
    
    
    132  | 
     | 
     | 
    #define LABEL_TO_STRING(func_name, label_names, prefix_len)		\  | 
    
    
    133  | 
     | 
     | 
    	const char *							\  | 
    
    
    134  | 
     | 
     | 
    	func_name(int code)						\  | 
    
    
    135  | 
     | 
     | 
    	{								\ | 
    
    
    136  | 
     | 
     | 
    		int i;							\  | 
    
    
    137  | 
     | 
     | 
    									\  | 
    
    
    138  | 
     | 
     | 
    		for (i = 0; i < countof(label_names); i++) {		\ | 
    
    
    139  | 
     | 
     | 
    			if (label_names[i].label == code)		\  | 
    
    
    140  | 
     | 
     | 
    				return label_names[i].name + prefix_len;\  | 
    
    
    141  | 
     | 
     | 
    		}							\  | 
    
    
    142  | 
     | 
     | 
    									\  | 
    
    
    143  | 
     | 
     | 
    		return "UNKNOWN";					\  | 
    
    
    144  | 
     | 
     | 
    	}  | 
    
    
    145  | 
     | 
     | 
    LABEL_TO_STRING(pptp_StopCCRQ_reason_string, pptp_StopCCRQ_reason_labels, 21)  | 
    
    
    146  | 
     | 
     | 
    LABEL_TO_STRING(pptp_StopCCRP_result_string, pptp_StopCCRP_result_labels, 21)  | 
    
    
    147  | 
     | 
     | 
    LABEL_TO_STRING(pptp_general_error_string, pptp_general_error_labels, 11)  | 
    
    
    148  | 
     | 
     | 
    LABEL_TO_STRING(pptp_ctrl_mes_type_string, pptp_ctrl_mes_type_labels, 19)  | 
    
    
    149  | 
     | 
     | 
    LABEL_TO_STRING(pptp_CDN_result_string, pptp_CDN_result_labels, 16)  |