/* FUNCTION: tk_init_os() * * Initialize the NicheTask or Superloop OS envirnment * * PARAMS: none * * RETURN: none */ void tk_init_os(void) { iniche_os_ready = FALSE; tk_tasklist = tk_cur = tk_died = (TASK *)NULL; tk_wakeups = tk_allocfail = 0; /* calculate the number of tasks and the requested stack space */ TK_CALC_MEMORY(&in_modules[0], in_num_modules); #ifdef NPDEBUG dprintf("max tasks = %d, total stack = %d\n", tk_max_tasks, tk_max_stack); #endif /* allocate task structure */ tk_tasklist = (TASK *)npalloc(tk_max_tasks * sizeof(TASK)); if (tk_tasklist == (TASK *)NULL) panic("tasklist"); #ifndef SUPERLOOP /* initialize resource locks */ { int i; for (i = 0; i < NUM_RESID; i++) { if ((tk_resource[i] = MUTEX_ALLOC()) == (IN_MUTEX *)NULL) panic("mutex"); } } #endif /* SUPERLOOP */ }
char * ncpalloc(unsigned size) { char *ptr = npalloc(size); if(ptr) { ptr = (char *) alt_remap_uncached(ptr, size); } return ptr; }
int udp_client_add(void * pio, SOCKTYPE sock, ip_addr host) { UDPCLIENT tmpclient; in_udpechoq++; /* Allocate memory */ tmpclient = (UDPCLIENT) npalloc( sizeof(struct UdpClient) ) ; if ( tmpclient == NULL ) { ns_printf(pio,"Allocation error.\n"); in_udpechoq--; return UDPE_ALLOC_ERROR; } /* Add the new node to the queue */ if ( udpq == NULL ) { udpq = tmpclient ; tmpclient->next=NULL; } else { /* Insert it at the beginning */ tmpclient->next = udpq; udpq=tmpclient ; } /* Initialize data members */ tmpclient->sock = sock; /* UDP Echo Client's socket */ tmpclient->pio = pio; /* Client's I/O device */ tmpclient->rhost = host; /* UDP Echo Server's IP addr */ tmpclient->times = 1; /* Count of total EchoPkts to be sent */ tmpclient->send_cnt = 0; /* Num of Echo Pkts sent till now */ tmpclient->ticks = cticks; /* Timetick to track sending of echos */ tmpclient->state = UDP_IDLE ; /* Are we sending UDP Echos? No. */ tmpclient->replies = 0 ; /* Num of replies received so far */ tmpclient->len = 64 ; /* Default value of Echo Pkt */ tmpclient->tot_sent = 0; tmpclient->tot_rcvd = 0; in_udpechoq--; return SUCCESS ; }
/* FUNCTION: tk_mutex_create() * * Allocate and initialize a mutex. * * PARAMS: none * * RETURN: IN_MUTEX * ptr to mutex or NULL */ IN_MUTEX * tk_mutex_create(void) { IN_MUTEX *mutex = (IN_MUTEX *)npalloc(sizeof(IN_MUTEX)); if (mutex) { mutex->tk_owner = (TASK *)NULL; mutex->tk_waitq = (TASK *)NULL; mutex->tk_nesting = 0; #ifdef DEBUG_TASK mutex->tk_tag = MUTEX_TAG; #endif } return (mutex); }
/* FUNCTION: tk_sem_create() * * Allocate and initialize a [binary] semaphore. * If the maximum count is 1, this is a binary semaphore. * * PARAM1: int16_t maximum signal count * * RETURN: IN_SEM * ptr to semaphore or NULL */ IN_SEM * tk_sem_create(int16_t maxcnt) { IN_SEM *sem = (IN_SEM *)npalloc(sizeof(IN_SEM)); if (sem) { sem->tk_count = 0; sem->tk_maxcnt = maxcnt; sem->tk_waitq = (TASK *)NULL; #ifdef DEBUG_TASK sem->tk_tag = SEMA_TAG; #endif } return (sem); }
int tcp_client_add(void * pio) { TCPCLIENT tmpclient; int num_of_conn=0; tmpclient=tcpq; while (tmpclient) { num_of_conn++; tmpclient=tmpclient->next; } if (num_of_conn >= TCP_MAX_ECHO_CONN) { return TCPE_ALL_SOCKS_USED; } /* Allocate memory */ tmpclient = (TCPCLIENT)npalloc(sizeof(struct TcpClient)) ; if (tmpclient == NULL) { ns_printf(pio,"Allocation error.\n"); return TCPE_ALLOC_ERROR; } /* Add the new node to the head of the queue */ tmpclient->next = tcpq; tcpq=tmpclient; /* Initialize data members */ tmpclient->sock = INVALID_SOCKET; tmpclient->pio = pio; tmpclient->rhost = 0; tmpclient->times = 1; /* Count of total EchoPkts to be sent */ tmpclient->send_cnt = 0; /* Num of Echo Pkts sent till now */ tmpclient->ticks = cticks; /* Timetick to track sending of echos */ tmpclient->state = TCP_IDLE ; /* Are we sending TCP Echos? No. */ tmpclient->replies = 0; /* Num of replies received so far */ tmpclient->len = 64; /* Default value of Echo Pkt */ tmpclient->tot_sent = 0; tmpclient->tot_rcvd = 0; return SUCCESS; }
int tcp_secho_init(void * pio) { int e; /* error holder */ struct sockaddr_in me; /* my IP info, for bind() */ int opt; if (tcpecho_server) { ns_printf(pio,"tcp echo srv - starting.\n"); /* open TCP socket */ elisten_sock = socket(AF_INET, SOCK_STREAM, 0); if (elisten_sock == INVALID_SOCKET) { ns_printf(pio,"TCP echo: bad socket: %d\n", elisten_sock); return TCPE_BAD_SOCKET ; } opt = 1; e = t_setsockopt(elisten_sock, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)); if (e != 0) { e = t_errno(elisten_sock); dtrap(); dprintf("error %d setting SO_REUSEADDR on port %d\n", e, ECHO_PORT); return SYS_SOCKETNULL; } me.sin_family = AF_INET; me.sin_addr.s_addr = INADDR_ANY; me.sin_port = htons(ECHO_PORT); e = bind(elisten_sock, (struct sockaddr*)&me, sizeof(me)); if (e != 0) { e = t_errno(elisten_sock); ns_printf(pio,"tcp_echo: bad socket bind: %d, %s\n", e, so_perror(e)); socketclose(elisten_sock); elisten_sock = INVALID_SOCKET; return TCPE_BIND_FAILED ; } e = listen(elisten_sock, 3); if (e != 0) { e = t_errno(elisten_sock); ns_printf(pio,"tcp_echo: bad socket listen: %d %s\n", e, so_perror(e)); socketclose(elisten_sock); elisten_sock = INVALID_SOCKET; return TCPE_LISTEN_FAILED; } /* for listen socket into Non-blocking mode so we can poll accept */ sock_noblock(elisten_sock, TRUE); } else ns_printf(pio,"tcp echo server not enabled\n"); srv_inbuf = (char *)npalloc(ECHOBUFSIZE); if (srv_inbuf == NULL) { ns_printf(pio, "tcp server: alloc failed\n"); socketclose(elisten_sock); elisten_sock = INVALID_SOCKET; return TCPE_LISTEN_FAILED; } return SUCCESS; }
int TK_NEWTASK(struct inet_taskinfo * nettask) { INT8U error; OS_STK * stack; stack = (OS_STK*)npalloc(nettask->stacksize); if(!stack) panic("stack alloc"); #if OS_TASK_CREATE_EXT_EN > 0 error = OSTaskCreateExt( nettask->entry, NULL, stack + (nettask->stacksize/sizeof(OS_STK)) - 1, nettask->priority, nettask->priority, stack, (INT32U)nettask->stacksize / sizeof(OS_STK), NULL, OS_TASK_OPT_STK_CHK | OS_TASK_OPT_STK_CLR); #else #ifndef TCPWAKE_RTOS #error !TCPWAKE_RTOS requires OS_TASK_CREATE_EXT_EN #endif error = OSTaskCreate( (void (*)(void *)) nettask->entry, NULL, stack + (nettask->stacksize/sizeof(OS_STK)) - 1, nettask->priority); #endif /* If we go here, then there's another task using our priority */ /* Tell the user and exit with an error */ if (error == OS_PRIO_EXIST) { char curr_task[OS_TASK_NAME_SIZE]; INT8U err; OSTaskNameGet(nettask->priority, curr_task, &err); curr_task[OS_TASK_NAME_SIZE-1]=0; printf("Priority requested for task \"%s\" (Prio:%d) conflicts with "\ "already running task \"%s\" (Prio: %d)\n", nettask->name, nettask->priority, curr_task, nettask->priority); printf("You may wish to check your task priority settings in "\ "\"<bsp path>\\iniche\\src\\h\\nios2\\ipport.h\" against "\ "the priority settings in your application and recompile.\n\n"); } else if (error == OS_PRIO_INVALID) { printf("Priority requested for task \"%s\" (Prio:%d) exceeds "\ "available priority levels in the system (OS_LOWEST_PRIO = %d)\n\n", nettask->name, nettask->priority, OS_LOWEST_PRIO); printf("Please modify the tasks priority level, or modify the "\ "\"Lowest assignable priority\" setting in the MicroC/OS-II "\ "component\n"); } else if (error != OS_NO_ERR) { /* All other errors are fatal */ printf("Task create error /(MicroC/OS-II error code:%d/) on %s\n", error, nettask->name); return (-1); } /* Include the task name, so that uc/osII (os aware) debuggers can * display it. */ OSTaskNameSet(nettask->priority, &nettask->name[0], &error); nettask->stackbase = (char*)stack; *nettask->tk_ptr = (INT8U)nettask->priority; printf("Created \"%s\" task (Prio: %d)\n", (char *)nettask->name, nettask->priority); return (0); }
int ping6_send(struct ping6 *ping) { PACKET pkt; struct icmp6req *pinghdr; char addrbuf[40]; /* for printf()ing */ int plen = sizeof(struct icmp6req) + ping->length; int err = 0; int sendflags; int bytesleft; int offset; /* try for a pkt chain */ LOCK_NET_RESOURCE(FREEQ_RESID); PK_ALLOC(pkt, plen + MaxLnh + sizeof(struct ipv6)); UNLOCK_NET_RESOURCE(FREEQ_RESID); if (pkt == NULL) { ping->count = 0; /* mark session for deletion */ return (ENP_NOBUFFER); } pkt->flags = 0; /* prepare for cb_read */ pkt->nb_prot = pkt->nb_buff + MaxLnh + sizeof(struct ipv6); /* got chain? */ if (pkt->pk_next == NULL) pkt->nb_plen = plen; /* no */ else pkt->nb_plen = pkt->nb_blen - MaxLnh - sizeof(struct ipv6); /* yes */ /* Advance to point where we write the data */ offset = sizeof(struct icmp6req); pkt->nb_tlen = offset; bytesleft = ping->length; while (bytesleft > PINGSTRSIZE) { err = cb_read(pkt, offset, (uint8_t *)pingdata6, PINGSTRSIZE); if (err < 0) break; offset += err; bytesleft -= err; } if (bytesleft && (err >= 0)) err = cb_read(pkt, offset, (uint8_t *)pingdata6, bytesleft); /* read in data - user or standard? */ /* got err? */ if (err <= 0) { LOCK_NET_RESOURCE(FREEQ_RESID); PK_FREE(pkt); UNLOCK_NET_RESOURCE(FREEQ_RESID); ping->count = 0; /* mark session for deletion */ return (ENP_NOBUFFER); } #ifdef IP6_ROUTING /* Put scopeID in pkt */ pkt->soxopts = npalloc(sizeof(struct ip_socopts)); if (pkt->soxopts == NULL) { LOCK_NET_RESOURCE(FREEQ_RESID); pk_free(pkt); UNLOCK_NET_RESOURCE(FREEQ_RESID); ping->count = 0; /* mark session for deletion */ return (ENP_NOBUFFER); } pkt->soxopts->ip_scopeid = ping->scopeID; #endif pinghdr = (struct icmp6req *)pkt->nb_prot; pinghdr->code = 0; pinghdr->type = ICMP6_ECHOREQ; pinghdr->id = ping->sess_id; pinghdr->sequence = (htons((unshort)ping->sent)); ping->sent++; pkt->net = ping->net; pkt->type = htons(0x86dd); /* multicast ping? */ if (ping->fhost.addr[0] == 0xFF) { pkt->flags |= PKF_MCAST; /* send mac multicast */ sendflags = 0; /* no routing */ } else { pkt->flags &= ~PKF_MCAST; /* send mac unicast */ /* see if we can skip the routing step */ if(pkt->net && (!IP6EQ(&ping->nexthop, &ip6unspecified))) { pkt->nexthop = &ping->nexthop; /* set next hop */ sendflags = IP6F_NOROUTE; } else sendflags = IP6F_ALL; } /* is the scope global? */ if ( (ping->fhost.addr[0] == 0xFF) && ((ping->fhost.addr[1] & 0xF) == 0xE) ) { /* yup - it's a global ping */ pkt->flags |= PKF_IPV6_GBL_PING; /* global ping */ } /* loopback? */ if (IN6_IS_ADDR_LOOPBACK((struct in6_addr *)&(ping->fhost.addr))) IP6CPY((struct in6_addr *)&(ping->lhost.addr), (struct in6_addr *)&(ping->fhost.addr)); /* both are loopback */ /* put prot at IPv6 hdr */ pkt->nb_prot -= sizeof(struct ipv6); pkt->nb_plen += sizeof(struct ipv6); pkt->nb_tlen += sizeof(struct ipv6); /* set time for next ping */ ping->nextping = TIME_ADD(CTICKS, ping->ping6_interval); err = icmp6_send(pkt, &ping->lhost, &ping->fhost, sendflags); pkt->net->icmp6_ifmib.OutEchos++; if (err < 0) { /* Don't record gio error, since we're going to kill this * ping session anyway. */ gio_printf(ping->gio, "error %d sending ping; sess %d, seq:%d\n", err, ntohs(ping->sess_id), ping->sent); ping->count = 0; /* mark session for deletion by timer */ } else if ((err == 1) || (err == 0)) { err = gio_printf(ping->gio, "Sent ping; sess: %d, Seq: %d to %s\n", ntohs(ping->sess_id), ping->sent, print_ip6(&ping->fhost, addrbuf)); } return (0); }