📄 mn_ipay.c
字号:
/* $Id: mn_ipay.c,v 1.7 2001/01/31 22:06:13 jm Exp $ * Mobile Node - Ipay interface routines * * Dynamic hierarchial IP tunnel * Copyright (C) 1998-2001, Dynamics group * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License version 2 as * published by the Free Software Foundation. See README and COPYING for * more details. */#ifdef INCLUDE_IPAY#ifdef HAVE_CONFIG_H#include <config.h>#endif#include <stdlib.h>#include <sys/types.h>#include <sys/socket.h>#include <string.h>#include <errno.h>#include <ctype.h>#include <netinet/in.h>#include <arpa/inet.h>#include "mn.h"#include "mn_handler.h"#include "hashtable.h"#include "debug.h"#include "ipay-msg.h"#include "mn_ipay.h"extern struct mn_data mn;extern struct mn_config config;/* the last reported faIdentifier to Ipay buyer (with new FA message) */static IPAY_CHAR prev_faIdentifier;#define DEBUG_IPAY 'i'static void show_ipay_char(char *title, char *p){ int i; DEBUG(DEBUG_IPAY, "\t %s: ", title); for (i = 0; i < IPAY_CHAR_LEN; i++) { if (p[i] == '\0') break; if (isprint(p[i])) DEBUG(DEBUG_IPAY, "%c", p[i]); else DEBUG(DEBUG_IPAY, "<%u>", (unsigned int) p[i]); } DEBUG(DEBUG_IPAY, "\n");}static int ipay_check_len(int len, int expected){ if (len != expected) { DEBUG(DEBUG_IPAY, "\t - length mismatch (expected %i bytes)\n", expected); if (len < expected) { DEBUG(DEBUG_IPAY, "\t - too short - cannot parse\n"); return -1; } DEBUG(DEBUG_IPAY, "\t - ignoring extra bytes\n"); } return 0;}static int send_to_buyer(void *data, int len){ struct sockaddr_in addr; int res; if (!mn.ipay_in_use) return 0; memset(&addr, 0, sizeof(addr)); addr.sin_family = AF_INET; addr.sin_addr = config.ipay_buyer_addr; addr.sin_port = htons(config.ipay_buyer_port); DEBUG(DEBUG_IPAY, "\tsending %i bytes to %s:%i\n", len, inet_ntoa(addr.sin_addr), ntohs(addr.sin_port)); res = sendto(mn.ipay_sock, data, len, 0, (struct sockaddr *) &addr, sizeof(addr)); if (res < 0) { DEBUG(DEBUG_IPAY, "\tsendto - %s\n", strerror(errno)); return -1; } else if (res != len) { DEBUG(DEBUG_IPAY, "\tsend only %i/%i bytes\n", res, len); return -1; } return 0;}static int send_to_fa(void *data, int len){ struct sockaddr_in addr; int res; if (!mn.ipay_in_use) return 0; memset(&addr, 0, sizeof(addr)); addr.sin_family = AF_INET; addr.sin_addr = mn.fa_addr; addr.sin_port = htons(config.ipay_fa_port); DEBUG(DEBUG_IPAY, "\tsending %i bytes to %s:%i\n", len, inet_ntoa(addr.sin_addr), ntohs(addr.sin_port)); res = sendto(mn.ipay_sock_fa, data, len, 0, (struct sockaddr *) &addr, sizeof(addr)); if (res < 0) { DEBUG(DEBUG_IPAY, "\tsendto - %s\n", strerror(errno)); return -1; } else if (res != len) { DEBUG(DEBUG_IPAY, "\tsend only %i/%i bytes\n", res, len); return -1; } return 0;}static void handle_initialize(int s, char *msg, int len){ struct ipay_initialize *ini; int l; DEBUG(DEBUG_IPAY, "\tinitialize message\n"); if (ipay_check_len(len, sizeof(struct ipay_initialize)) < 0) return; ini = (struct ipay_initialize *) msg; show_ipay_char("mnIdentifier", ini->mnIdentifier); l = 0; while (l < IPAY_CHAR_LEN && ini->mnIdentifier[l] != '\0') l++; if (l == 0) { DEBUG(DEBUG_IPAY, "\tzero length NAI - ignoring\n"); return; } if (mn.nai.nai != NULL) free(mn.nai.nai); mn.nai.nai = malloc(l); if (mn.nai.nai == NULL) { DEBUG(DEBUG_IPAY, "\tmalloc failed\n"); mn.nai.len = 0; return; } mn.nai.len = l; memcpy(mn.nai.nai, ini->mnIdentifier, l); mn.ipay_in_use = 1; ipay_flush_priority_cache(0);}static int prio_avail = 0;static __u32 last_prio = 0;static void handle_policy_prio_reply(int s, char *msg, int len){ struct ipay_policy_prio_reply *prio; DEBUG(DEBUG_IPAY, "\tpolicy prio reply message\n"); if (ipay_check_len(len, sizeof(struct ipay_policy_prio_reply)) < 0) return; prio = (struct ipay_policy_prio_reply *) msg; DEBUG(DEBUG_IPAY, "\tpriority=%u\n", ntohl(prio->priority)); prio_avail = 1; last_prio = ntohl(prio->priority);}static void handle_purchase(int s, char *msg, int len){ struct ipay_purchase *pur; DEBUG(DEBUG_IPAY, "\tpurchase message\n"); if (ipay_check_len(len, sizeof(struct ipay_purchase)) < 0) return; pur = (struct ipay_purchase *) msg; DEBUG(DEBUG_IPAY, "\t timeStamp: %u\n", ntohl(pur->timeStamp)); show_ipay_char("mnIdentifier", pur->mnIdentifier); show_ipay_char("faIdentifier", pur->faIdentifier); DEBUG(DEBUG_IPAY, "\t purchaseSerialNumber: %u\n", ntohl(pur->purchaseSerialNumber)); DEBUG(DEBUG_IPAY, "\t microPaymentUnit: %u\n", ntohl(pur->microPaymentUnit)); DEBUG(DEBUG_IPAY, "\t maxNumberOfUnits: %u\n", ntohl(pur->maxNumberOfUnits)); show_ipay_char("firstPayment", pur->firstPayment); DEBUG(DEBUG_IPAY, "\t timePrice: %u\n", ntohl(pur->timePrice)); DEBUG(DEBUG_IPAY, "\t bytePrice: %u\n", ntohl(pur->bytePrice)); show_ipay_char("signature", pur->signature); send_to_fa(pur, len);}static void handle_allowance(int s, char *msg, int len){ struct ipay_allowance *allow; DEBUG(DEBUG_IPAY, "\tallowance message\n"); if (ipay_check_len(len, sizeof(struct ipay_allowance))) return; allow = (struct ipay_allowance *) msg; DEBUG(DEBUG_IPAY, "\t allowance: %u\n", ntohl(allow->allowance)); show_ipay_char("mnIdentifier", allow->mnIdentifier); DEBUG(DEBUG_IPAY, "\t timePrice: %u\n", ntohl(allow->timePrice)); DEBUG(DEBUG_IPAY, "\t bytePrice: %u\n", ntohl(allow->bytePrice)); DEBUG(DEBUG_IPAY, "\t apporovedNumberOfUnits: %u\n", ntohl(allow->approvedNumberOfUnits)); show_ipay_char("sessionKey", allow->sessionKey); show_ipay_char("signature", allow->signature); send_to_buyer(msg, len); if (!ntohl(allow->allowance)) { DEBUG(DEBUG_IPAY, "\tconnection to FA not allowed => marking " "FA unusable\n"); if (mn.current_adv != NULL) { mn.current_adv->priority = 0; mn.current_adv->reg_failed = 1; } find_agent(STATE_INIT); }}static void handle_please_pay_more(int s, char *msg, int len){ struct ipay_please_pay_more *pay; struct ipay_statistics stat; static time_t prev_stat = 0; time_t now; DEBUG(DEBUG_IPAY, "\tplease pay more message\n"); if (ipay_check_len(len, sizeof(struct ipay_please_pay_more))) return; pay = (struct ipay_please_pay_more *) msg; show_ipay_char("mnIdentifier", pay->mnIdentifier); DEBUG(DEBUG_IPAY, "\t usedTime: %u\n", ntohl(pay->usedTime)); DEBUG(DEBUG_IPAY, "\t usedByte: %u\n", ntohl(pay->usedByte)); show_ipay_char("sessionKey", pay->sessionKey); show_ipay_char("signature", pay->signature); memset(&stat, 0, sizeof(stat)); stat.type = htonl(IPAY_MSG_STATISTICS); if (mn.current_adv != NULL) { struct fa_nai_ext *nai; nai = (struct fa_nai_ext *) mn.current_adv->fa_nai; memcpy(stat.faIdentifier, MSG_NAI_DATA(nai), MIN(IPAY_CHAR_LEN, GET_NAI_LEN(nai))); } /* FIX statistics */ stat.bytes = htonl(0); time(&now); if (prev_stat == 0 || now < prev_stat) { stat.time = htonl(0); prev_stat = now; } else { stat.time = htonl(now - prev_stat); prev_stat = now; } DEBUG(DEBUG_IPAY, "\tsending statistics (bytes=%u, time=%u)\n", ntohl(stat.bytes), ntohl(stat.time)); send_to_buyer(&stat, sizeof(stat)); DEBUG(DEBUG_IPAY, "\tforwarding please pay more\n"); send_to_buyer(msg, len);}static void handle_micro_payment(int s, char *msg, int len){ struct ipay_micro_payment *pay; DEBUG(DEBUG_IPAY, "\tmicro payment message\n"); if (ipay_check_len(len, sizeof(struct ipay_micro_payment)) < 0) return; pay = (struct ipay_micro_payment *) msg; show_ipay_char("mnIdentifier", pay->mnIdentifier); show_ipay_char("nextPayment", pay->nextPayment); send_to_fa(msg, len);}void handle_ipay(int s){ char msg[MAXMSG]; struct sockaddr_in from; int fromlen, len; IPAY_TYPE *type; DEBUG(DEBUG_IPAY, "Ipay message from socket %i\n", s); fromlen = sizeof(from); len = recvfrom(s, msg, MAXMSG, 0, (struct sockaddr *) &from, &fromlen); if (len < 0) { DEBUG(DEBUG_IPAY, "\trecvfrom failed - %s\n", strerror(errno)); return; } DEBUG(DEBUG_IPAY, "\t%i bytes from %s:%i\n", len,
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -