int llopen(unsigned int port, unsigned int flag) { if (init_link(port)) return -1; linklayer.flag = flag; int fd = open_port_file(port); if (flag == RECEIVER) { if (rec_set(fd)) return -1; if (send_ua(fd, flag)) return -1; } else if (flag == SENDER) { if (send_set(fd)) return -1; if (rec_ua(fd, flag)) return -1; } else { printf("ENTER A VALID STATUS!\n"); close_port_file(fd); return -1; } initialized = -1; return fd; }
int receive_set(int fd, int count) { int res; char buf[255]; //RECEIVE SET strcpy(buf,""); while (STOP_REC == FALSE) { res = read(fd,buf,1); printf("%x\n", buf[res-1]); switch(count) { case 0: if(buf[res-1] == FLAG) count++; break; case 1: if(buf[res-1] == A_SEND) count++; else if(buf[res-1] == FLAG) break; else count=0; break; case 2: if(buf[res-1] == C_SET) count++; else if(buf[res-1] == FLAG) count=1; else count=0; break; case 3: if(buf[res-1] == (A_SEND^C_SET)) count++; else if (buf[res-1] == FLAG) count = 1; else count = 0; break; case 4: if (buf[res-1] == FLAG) { printf("Received SET!\n"); return send_ua(fd); } else count = 0; break; } } return -1; }
int advt_addr6(struct in6_addr* addr6, int prefix_len, char* prov_ifname) { /* First, we need to find a proper device to assign the address */ char* if_name = get_if(addr6, &prefix_len, prov_ifname); int i; if (NULL == if_name) { cl_log(LOG_ERR, "no valid mechanisms"); return OCF_ERR_GENERIC; } /* Send unsolicited advertisement packet to neighbor */ for (i = 0; i < UA_REPEAT_COUNT; i++) { send_ua(addr6, if_name); sleep(1); } return OCF_SUCCESS; }
int llclose(int fd, unsigned int flag) { if (!initialized) { printf("THIS IS ALREADY CLOSE\n"); return -1; } if (flag == SENDER) { if (send_disc(fd, flag)) return -1; if (rec_disc(fd, flag)) return -1; if (send_ua(fd, flag)) return -1; } if (close_port_file(fd)) return -1; return 0; }
int start_addr6(struct in6_addr* addr6, int prefix_len, char* prov_ifname) { int i; char* if_name; if(OCF_SUCCESS == status_addr6(addr6,prefix_len,prov_ifname)) { return OCF_SUCCESS; } /* we need to find a proper device to assign the address */ if_name = find_if(addr6, &prefix_len, prov_ifname); if (NULL == if_name) { cl_log(LOG_ERR, "no valid mechanisms"); return OCF_ERR_GENERIC; } /* Assign the address */ if (0 != assign_addr6(addr6, prefix_len, if_name)) { cl_log(LOG_ERR, "failed to assign the address to %s", if_name); return OCF_ERR_GENERIC; } /* Check whether the address available */ for (i = 0; i < QUERY_COUNT; i++) { if (0 == is_addr6_available(addr6)) { break; } sleep(1); } if (i == QUERY_COUNT) { cl_log(LOG_ERR, "failed to ping the address"); return OCF_ERR_GENERIC; } /* Send unsolicited advertisement packet to neighbor */ for (i = 0; i < UA_REPEAT_COUNT; i++) { send_ua(addr6, if_name); sleep(1); } return OCF_SUCCESS; }
int llread(int control) { int state = 0; int equalize = 0; int res = 0; int count_buf = 0; char buf[MAX_SIZE]; char ant[1]; //RECEIVE INF strcpy(buf,""); while(TRUE) { res = read(appLayer.fd,buf,1); switch(state) { case 0: if(buf[res-1] == FLAG) state++; count_buf = 0; ll.sequenceNumber = 0; break; case 1: if(buf[res-1] == A_SEND) state++; else if(buf[res-1] == FLAG) break; else state=0; strcpy(ll.frame,""); count_buf = 0; ll.sequenceNumber = 0; ant[0] = '\0'; break; case 2: if(buf[res-1] == C_SET) state++; else if(buf[res-1] == C_SI && setNum == 1) state = 5; else if(buf[res-1] == C_SF && control == 1 && setNum == 1) state=5; else if (buf[res-1] == C_DISC) state=8; else if(buf[res-1] == FLAG) state=1; else state=0; break; case 3: if(buf[res-1] == (A_SEND^C_SET)) state++; else if (buf[res-1] == FLAG) state = 1; else state = 0; break; case 4: if (buf[res-1] == FLAG) { printf("Received SET!\n"); state = 0; setNum = 1; return send_ua(); } else state = 0; break; case 5: if(buf[res-1] == (C_SI^A_SEND)) { state = 6; equalize = 0; } else if(buf[res-1] == (C_SF^A_SEND)) { state = 6; equalize = 1; } else if (buf[res-1] == FLAG) state = 1; else state = 0; ant[0] = buf[res-1]; break; case 6: if(buf[res-1] == FLAG) state = 1; else if(buf[res-1] == AFT_FLAG) { if(ant[0] == ESC) { ll.frame[count_buf] = FLAG; count_buf++; ant[0] = '\0'; } else { ll.frame[count_buf] = AFT_FLAG; count_buf++; ant[0] = AFT_FLAG; } } else if(buf[res-1] == AFT_ESC){ if(ant[0] == ESC){ ll.frame[count_buf] = ESC; count_buf++; ant[0] = '\0'; } else { ll.frame[count_buf] = AFT_ESC; count_buf++; ant[0] = AFT_ESC; } } else if(buf[res-1] == ESC) { ant[0] = ESC; break; } else if(buf[res-1] == (C_SI^A_SEND) || buf[res-1] == (C_SF^A_SEND)){ state = 7; ant[0] = buf[res-1]; } else { ll.frame[count_buf] = buf[res-1]; count_buf++; ant[0] = buf[res-1]; } break; case 7: if(buf[res-1] == FLAG){ printf("Successfully destuffed bytes and saved message! %d bytes\n", count_buf); ant[0]='\0'; state = 0; ll.sequenceNumber = count_buf; return equalize+1; } else{ ll.frame[count_buf] = ant[0]; count_buf++; ll.frame[count_buf] = buf[res-1]; ant[0]=buf[res-1]; count_buf++; state=6; } break; case 8: if(buf[res-1] == (A_SEND^C_DISC)) state=9; else if (buf[res-1] == FLAG) state = 1; else state = 0; break; case 9: if(buf[res-1] == FLAG){ printf("Sending DISC and shutting down!\n"); return 3; //send_disc_nc(); } else state = 0; break; default: printf("Error llread: state is not between 0 and 9!\n"); break; } } return -1; }