?? ipc.c
字號:
/* * OLSR ad-hoc routing table management protocol GUI front-end * Copyright (C) 2003 Andreas T鴑nesen (andreto@ifi.uio.no) * * This file is part of olsr.org. * * uolsrGUI is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * uolsrGUI is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with olsr.org; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * */#include "common.h"#include "ipc.h"#include "packet.h"#include "routes.h"#ifdef WIN32#define close(x) closesocket(x)#undef errno#define errno WSAGetLastError()#undef strerror#define strerror(x) StrError(x)#define perror(x) WinSockPError(x)#endifint ipc_socket = 0;intipc_close(){ if(close(ipc_socket)) return 1; return 0;}intipc_connect(struct sockaddr_in *pin){#ifdef WIN32 int On = 1; unsigned long Len;#else int flags;#endif connected = 0; if(!ipc_socket) if((ipc_socket = socket(AF_INET, SOCK_STREAM, 0)) == -1) { perror("socket"); exit(1); } printf("Attempting connect..."); /* connect to PORT on HOST */ if (connect(ipc_socket,(struct sockaddr *) pin, sizeof(*pin)) < 0) { fprintf(stderr, "Error connecting %d - %s\n", errno, strerror(errno)); set_net_info_offline(); printf("connection refused\n"); } else { set_net_info("Connected!", FALSE); printf("Connected!!\n"); /* Setting socket non-blocking */ #ifdef WIN32 if (WSAIoctl(ipc_socket, FIONBIO, &On, sizeof (On), NULL, 0, &Len, NULL, NULL) < 0) { fprintf(stderr, "Error while making socket non-blocking!\n"); exit(1); }#else if ((flags = fcntl(ipc_socket, F_GETFL, 0)) < 0) { fprintf(stderr,"Error getting socket flags!\n"); exit(1); } if (fcntl(ipc_socket, F_SETFL, flags | O_NONBLOCK) < 0) { fprintf(stderr,"Error setting socket flags!\n"); exit(1); }#endif connected = 1; return 1; } return 0;}intipc_read(){ //int x, i; int bytes, tmp_len; char *tmp; union olsr_message *msg; union { char buf[BUFFSIZE+1]; union olsr_message olsr_msg; } inbuf; //char buf[BUFFSIZE+1]; //printf(".");fflush(stdout); memset(&inbuf, 0, sizeof(BUFFSIZE+1)); //buf[0] = '\0'; if(connected) { bytes = recv(ipc_socket, (char *)&inbuf, BUFFSIZE, 0); if (bytes == 0) { shutdown(ipc_socket, SHUT_RDWR); set_net_info("Disconnected from server...", TRUE); connected = 0; close(ipc_socket); } if(bytes > 0) { tmp = (char *) &inbuf.olsr_msg; /* x = 0; printf("\n\t"); for(i = 0; i < bytes;i++) { if(x == 4) { x = 0; printf("\n\t"); } x++; printf(" %03i", (u_char) tmp[i]); } printf("\n\nBytes read: %d - msg_size: %d\n", bytes, ntohs(inbuf.olsr.v4.olsr_packlen)); */ msg = &inbuf.olsr_msg; /* There can be(there probably are!) several packets in the buffer */ /* Should be the same for v4 and v6 */ if(ntohs(inbuf.olsr_msg.v4.olsr_msgsize) < bytes) { //printf("chomping...\n"); while(ntohs(msg->v4.olsr_msgsize) < bytes) { ipc_evaluate_message(msg); tmp_len = ntohs(msg->v4.olsr_msgsize); msg = (union olsr_message *) &tmp[tmp_len]; tmp = &tmp[tmp_len]; if(tmp_len == 0) break; bytes = bytes - tmp_len; tmp_len = ntohs(msg->v4.olsr_msgsize); //printf("%d/%d ", tmp_len, bytes); /* Copy to start of buffer */ if(tmp_len > bytes) { /* Copy the buffer */ //printf("READING END OF MESSAGE. %d bytes\n", tmp_len-bytes); //printf("\tCopying %d bytes\n", bytes); memcpy(&inbuf, tmp, bytes); //printf("\tRecieving %d bytes to buffer[%d]\n", tmp_len-bytes, bytes); bytes = recv(ipc_socket, (char *)&inbuf.buf[bytes], tmp_len-bytes, 0); //printf("\tBytes: %d Size: %d\n", bytes, ntohs(msgs->v4.olsr_packlen)); tmp = (char *)&inbuf.olsr_msg; msg = (union olsr_message *) tmp; } } //printf("\n"); } /* Only one (or the last) message */ ipc_evaluate_message(msg); } } return 1;}intipc_send(){ return 1;}intipc_evaluate_message(union olsr_message *olsr_in){ int ipc_pack = 0; olsr_u8_t type; int msgsize; char itoa_buf[10]; olsr_u8_t vtime; union olsr_ip_addr *originator; /* Find size, vtime, originator and type - same for IPv4 and 6 */ type = olsr_in->v4.olsr_msgtype; msgsize = ntohs(olsr_in->v4.olsr_msgsize); vtime = olsr_in->v4.olsr_vtime; originator = (union olsr_ip_addr *)&olsr_in->v4.originator; gui_itoa(msgsize, itoa_buf); switch(type) { case HELLO_MESSAGE: //printf("Recieved HELLO packet\n"); if(!freeze_packets) packet_list_add("HELLO", ip_to_string(originator), itoa_buf); if(ipversion == AF_INET) { process_hello(msgsize, vtime, originator, (union hello_message *)&olsr_in->v4.message.hello); } else { process_hello(msgsize, vtime, originator, (union hello_message *)&olsr_in->v6.message.hello); } break; case TC_MESSAGE: if(!freeze_packets) packet_list_add("TC", ip_to_string(originator), itoa_buf); if(ipversion == AF_INET) { process_tc(msgsize, vtime, originator, (union tc_message *)&olsr_in->v4.message.tc); //printf("Recieved TC packet from %s\n", ip_to_string(&m->olsr_tc->tc_origaddr)); } else { process_tc(msgsize, vtime, originator, (union tc_message *)&olsr_in->v6.message.tc); //printf("Recieved TC packet from %s\n", ip_to_string(&m->olsr_tc->tc_origaddr)); } break; case MID_MESSAGE: if(!freeze_packets) packet_list_add("MID", ip_to_string(originator), itoa_buf); if(ipversion == AF_INET) { process_mid(msgsize, vtime, originator, (union mid_message *)&olsr_in->v4.message.mid); //printf("Recieved MID packet from %s\n", ip_to_string(&m->olsr_mid->mid_origaddr)); } else { process_mid(msgsize, vtime, originator, (union mid_message *)&olsr_in->v6.message.mid); //printf("Recieved MID packet from %s\n", ip_to_string(&m->olsr_mid->mid_origaddr)); } break; case HNA_MESSAGE: if(!freeze_packets) packet_list_add("HNA", ip_to_string(originator), itoa_buf); if(ipversion == AF_INET) { process_hna(msgsize, vtime, originator, (union hna_message *)&olsr_in->v4.message.hna); //printf("Recieved HNA packet\n"); } else { process_hna(msgsize, vtime, originator, (union hna_message *)&olsr_in->v6.message.hna); //printf("Recieved HNA packet\n"); } break; case IPC_MESSAGE: //printf("Recieved IPC packet\n"); ipc_pack = 1; /* Don't add to buffer */ ipc_eval_route_packet((struct routemsg *) olsr_in); break; case IPC_NET: //printf("Recieved IPC packet\n"); ipc_pack = 1; /* Don't add to buffer */ ipc_eval_net_info((struct netmsg *) olsr_in); break; default: if(!freeze_packets) { char unk_label[8]; sprintf(unk_label, "%d", type); packet_list_add(unk_label, ip_to_string(originator), itoa_buf); } printf("Unknown packet type %d\n", type); } if(!freeze_packets && !ipc_pack) { add_packet_to_buffer(olsr_in, msgsize); } return 1;}intipc_eval_net_info(struct netmsg *msg){ char info[256]; printf("Evaluating NET info...\n"); /* printf("\tMain address: %s\n", ip_to_string(&msg->main_addr)); printf("\tMid adresses: %d\n", msg->mids); printf("\tHna adresses: %d\n", msg->hnas); printf("\tHELLO interval: %f\n", (float)(ntohs(msg->hello_int))); printf("\tHELLO LAN interval: %f\n", (float)(ntohs(msg->hello_lan_int)));
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -