//这个函数打开TCP端口SON_PORT, 等待来自本地SIP进程的进入连接. //在本地SIP进程连接之后, 这个函数持续接收来自SIP进程的sendpkt_arg_t结构, 并将报文发送到重叠网络中的下一跳. //如果下一跳的节点ID为BROADCAST_NODEID, 报文应发送到所有邻居节点. void waitSIP() { int listenfd, connfd; int option = 1; struct sockaddr_in caddr, saddr; if ((listenfd = socket(AF_INET, SOCK_STREAM, 0)) < 0){ printf("Listen is Wrong !!!!!!!\n"); exit(2); } if (setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &option, sizeof(option)) < 0){ printf("Error!!!!!\n"); exit(2); } memset(&saddr, 0, sizeof(saddr)); saddr.sin_family = AF_INET; saddr.sin_addr.s_addr = htonl(INADDR_ANY); saddr.sin_port = htons(SON_PORT); bind(listenfd, (struct sockaddr*)&saddr, sizeof(saddr)); listen(listenfd, 2); socklen_t len = sizeof(caddr); connfd = accept(listenfd, (struct sockaddr*)&caddr, &len); close(listenfd); sip_conn = connfd; sip_pkt_t* pkt = (sip_pkt_t *)malloc(sizeof(sip_pkt_t)); int *nextNode = (int *)malloc(sizeof(int)); while(1){ if(getpktToSend(pkt, nextNode,sip_conn) > 0){ if(*nextNode == BROADCAST_NODEID){ int j = 0; while(j < neighNum){ sendpkt(pkt, nt[j].conn); j ++; } } else{ int j = 0; while(j < neighNum){ if(*nextNode == nt[j].nodeID){ sendpkt(pkt, nt[j].conn); j ++; } } } } } }
//这个函数打开TCP端口SON_PORT, 等待来自本地SIP进程的进入连接. //在本地SIP进程连接之后, 这个函数持续接收来自SIP进程的sendpkt_arg_t结构, 并将报文发送到重叠网络中的下一跳. //如果下一跳的节点ID为BROADCAST_NODEID, 报文应发送到所有邻居节点. void waitSIP() { struct sockaddr_in servaddr, cliaddr; socklen_t cliaddr_len; int listenfd; listenfd = socket(AF_INET, SOCK_STREAM, 0); bzero(&servaddr, sizeof(servaddr)); servaddr.sin_family = AF_INET; servaddr.sin_addr.s_addr = htonl(INADDR_ANY); servaddr.sin_port = htons(SON_PORT); bind(listenfd, (struct sockaddr *)&servaddr, sizeof(servaddr)); listen(listenfd, 20); sip_conn = accept(listenfd, (struct sockaddr *)&cliaddr, &cliaddr_len); //recv pkt from sip and send sip_pkt_t pkt; int nextNode, conn; while(1){ if(getpktToSend(&pkt,&nextNode,sip_conn) == 1){ if (pkt.header.type == SIP){ if((conn = getConnByID(nextNode)) >= 0){ if(sendpkt(&pkt,conn) < 0) printf("Error in send pkt to Nbr\n"); } else { printf("Wrong Node id nextNode = %d\n",nextNode); } } else if (pkt.header.type == ROUTE_UPDATE){ int nbr_num = topology_getNbrNum(); int i; for (i = 0; i < nbr_num; i++){ if (nt[i].conn >= 0 && sendpkt(&pkt, nt[i].conn) < 0){ printf("Error in send route update pkt to Nbr\n"); } } } } } printf("Error in getpktToSend\n"); }
//这个函数打开TCP端口SON_PORT, 等待来自本地SIP进程的进入连接. //在本地SIP进程连接之后, 这个函数持续接收来自SIP进程的sendpkt_arg_t结构, 并将报文发送到重叠网络中的下一跳. //如果下一跳的节点ID为BROADCAST_NODEID, 报文应发送到所有邻居节点. void waitSIP() { int sockfd; struct sockaddr_in cli_addr, serv_addr; socklen_t clilen; if((sockfd = socket(AF_INET, SOCK_STREAM, 0))<0) { perror("Error create socket\n"); exit(-1); } const int one = 1; //port可以立即重新使用 setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)); serv_addr.sin_family=AF_INET; serv_addr.sin_addr.s_addr = htonl(INADDR_ANY); serv_addr.sin_port = htons(SON_PORT); if(bind(sockfd, (struct sockaddr*)&serv_addr, sizeof(serv_addr))<0) { perror("Error bind\n"); exit(-1); } listen(sockfd, MAX_NODE_NUM); clilen = sizeof(cli_addr); while(1) { sip_conn = accept(sockfd, (struct sockaddr*)&cli_addr, &clilen); sip_pkt_t* sip_pkt; int next; while(1) { sip_pkt = (sip_pkt_t*)malloc(sizeof(sip_pkt_t)); memset(sip_pkt, 0, sizeof(sip_pkt_t)); if(getpktToSend(sip_pkt, &next, sip_conn)>0) { if(next == BROADCAST_NODEID) { int nbrnum = topology_getNbrNum(); int i = 0; for(i=0; i<nbrnum; i++) { //if(nt[i].conn > 0) { sendpkt(sip_pkt, nt[i].conn); //} //else { //printf("sendpkt conn<0, id: %d\n", nt[i].nodeID); //} } } else { if(sip_pkt->header.type == SIP) { printf("Receive stcp packet!\n"); } int nbrnum = topology_getNbrNum(); int i = 0; for(i=0; i<nbrnum; i++) { if(next == nt[i].nodeID) { printf("send message to node %d\n", next); sendpkt(sip_pkt, nt[i].conn); break; } } } } else { free(sip_pkt); close(sip_conn); break; } free(sip_pkt); } } return; }
//这个函数打开TCP端口SON_PORT, 等待来自本地SIP进程的进入连接. //在本地SIP进程连接之后, 这个函数持续接收来自SIP进程的sendpkt_arg_t结构, 并将报文发送到重叠网络中的下一跳. //如果下一跳的节点ID为BROADCAST_NODEID, 报文应发送到所有邻居节点. void waitSIP() { //你需要编写这里的代码. int nbrNum = topology_getNbrNum(); int listenfd=0; struct sockaddr_in servaddr,cliaddr; socklen_t clilen; if ((listenfd=socket(AF_INET, SOCK_STREAM,0))<0) { puts("create socket error!"); } servaddr.sin_family=AF_INET; servaddr.sin_addr.s_addr=htonl(INADDR_ANY); servaddr.sin_port=htons(SON_PORT); const int on = 1; setsockopt(listenfd,SOL_SOCKET,SO_REUSEADDR,&on,sizeof(on)); bind(listenfd,(struct sockaddr *)&servaddr, sizeof(servaddr)); puts("SON Wait For SIP Server Init Success!"); listen(listenfd,1); while(1){ sip_conn=accept(listenfd,(struct sockaddr *)&cliaddr,&clilen); puts("Accept SIP Process!"); int nextNode=0; sip_pkt_t* recv_seg=malloc(sizeof(sip_pkt_t)); memset(recv_seg,0,sizeof(sip_pkt_t)); while (1){ if (getpktToSend(recv_seg,&nextNode,sip_conn)!=1) //continue; break; if (nextNode==BROADCAST_NODEID){ //char *flags=malloc(nbrNum); //memset(flags,0,nbrNum); for (int i=0;i<nbrNum;i++) { if (sendpkt(recv_seg,nt[i].conn)==-1) { //flags[i]=1; } }/* for (int i=0;i<nbrNum;i++) { if (flags[i]==1) { forwardpktToSIP(build_failpkt(nt[i].nodeID),sip_conn); } } free(flags);*/ } else{ for (int i=0;i<nbrNum;i++) { if (nt[i].nodeID==nextNode) { if (sendpkt(recv_seg,nt[i].conn)==-1) { //forwardpktToSIP(build_failpkt(nt[i].nodeID),sip_conn); } break; } } } } printf("####SIP stop!! listening for next SIP\n"); close(sip_conn); } }
//这个函数打开TCP端口SON_PORT, 等待来自本地SIP进程的进入连接. //在本地SIP进程连接之后, 这个函数持续接收来自SIP进程的sendpkt_arg_t结构, 并将报文发送到重叠网络中的下一跳. //如果下一跳的节点ID为BROADCAST_NODEID, 报文应发送到所有邻居节点. void waitSIP() { int listenfd; socklen_t clilen; clilen = sizeof(struct sockaddr_in); struct sockaddr_in cliaddr, servaddr; memset(&cliaddr, 0, sizeof(cliaddr)); memset(&servaddr, 0, sizeof(servaddr)); listenfd = socket(AF_INET, SOCK_STREAM, 0); servaddr.sin_family = AF_INET; // servaddr.sin_addr.s_addr = INADDR_ANY; inet_aton("127.0.0.1", &servaddr.sin_addr); servaddr.sin_port = htons(SON_PORT); const int on = 1; setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(const int)); bind(listenfd, (struct sockaddr *)&servaddr, sizeof(servaddr)); listen(listenfd, MAX_NODE_NUM); while (1) { // wait for sip connection memset(&cliaddr, 0, sizeof(struct sockaddr)); sip_conn = accept(listenfd, (struct sockaddr *)&cliaddr, &clilen); if (sip_conn == -1) { print_pos(); printf("ERROR: accept sip connection failed\n"); sleep(1); continue; } /* recv from sip and son forward start here */ // print_pos(); // printf("MSG: sip access ok, sip_conn = %d, begin to convey sip->son\n", sip_conn); sip_pkt_t tmp; int nNode, result; while (1){ memset(&tmp, 0, sizeof(sip_pkt_t)); result = getpktToSend(&tmp, &nNode, sip_conn); if(result == -1) { print_pos(); printf("ERROR: get pkt from sip failed\nMSG: Break loop wait another sip connect\n"); sleep(1); break; } // print_pos(); // printf("MSG: get sip_pkt_t to send ok, send to nbr...\n"); // if broad cast, send to every neighbor if (nNode == BROADCAST_NODEID){ int ct = topology_getNbrNum(); int i; for (i = 0; i < ct; i++){ // print_pos(); // printf("MSG: broadcast send to socket %d\n", nt[i].conn); result = sendpkt(&tmp, nt[i].conn); if (result == -1){ print_pos(); printf("ERROR: send pkt to broadcast failed\n"); continue; } } // printf("MSG: send sip_pkt_t to broadcast ok\n"); } else{ // others, send to next hop int destConn = getConnByNode(nNode); if (destConn == -1) { print_pos(); printf("ERROR: conn not set up\n"); } result = sendpkt(&tmp, destConn); if (result == -1){ printf("ERROR: send pkt to next node failed\n"); continue; } // printf("MSG: send sip_pkt_t to nbr ok, socket=%d\n", destConn); } } } }
//这个函数打开TCP端口SON_PORT, 等待来自本地SIP进程的进入连接. //在本地SIP进程连接之后, 这个函数持续接收来自SIP进程的sendpkt_arg_t结构, 并将报文发送到重叠网络中的下一跳. //如果下一跳的节点ID为BROADCAST_NODEID, 报文应发送到所有邻居节点. void waitSIP() { socklen_t clilen; int listenfd; //char buf[MAXLINE]; struct sockaddr_in cliaddr,servaddr; listenfd = socket(AF_INET,SOCK_STREAM,0); memset(&servaddr, 0, sizeof(struct sockaddr_in)); servaddr.sin_family=AF_INET; servaddr.sin_addr.s_addr=htonl(INADDR_ANY); servaddr.sin_port=htons(SON_PORT); bind(listenfd,(struct sockaddr *)&servaddr,sizeof(servaddr)); listen(listenfd,MAX_NODE_NUM);//开始监听 clilen = sizeof(cliaddr); int connfd = accept(listenfd,(struct sockaddr *)&cliaddr,&clilen); sip_conn = connfd; printf("IP linked\n"); while(1) { sip_pkt_t* pkt = (sip_pkt_t*)malloc(sizeof(sip_pkt_t)); memset(pkt,0,sizeof(sip_pkt_t)); int nextNode = 0; // 如果成功接收sendpkt_arg_t结构, 返回1, 否则返回-1. if(getpktToSend(pkt,&nextNode,sip_conn)==1) { printf("in son ready to send:%d\n",pkt->header.src_nodeID); if(nextNode == BROADCAST_NODEID) { int i = 0; for(i = 0 ;i <topology_getNbrNum();i++) { printf("send to everyone!\n"); if(nt[i].conn == -1) { printf("%d sleep no more send to him\n",nt[i].nodeID); continue; } if(sendpkt(pkt, nt[i].conn) == 1) { printf("send to nextnode sucess! %d\n",nt[i].conn); } else { printf("send fail %d",i); } } } else { int i = 0; for(i = 0 ;i <topology_getNbrNum();i++) { if(nextNode == nt[i].nodeID) { break; } } if(i == topology_getNbrNum()) { printf("cant find next node!\n"); exit(0); } if(nt[i].conn == -1) { printf("%d sleep no more send to him\n",nt[i].nodeID); continue; } // 如果报文发送成功, 返回1, 否则返回-1. if(sendpkt(pkt, nt[i].conn) == 1) { printf("send to nextnode sucess!\n"); } else { printf("send to nextnode fail\n"); } } } else { printf("SIP stop!!!!!!!!!!!!!!!\n"); break; } } }