Annotation of previous/src/enet_slirp.c, revision 1.1

1.1     ! root        1: #include "m68000.h"
        !             2: #include "ethernet.h"
        !             3: #include "enet_slirp.h"
        !             4: #include "queue.h"
        !             5: 
        !             6: #include <SDL.h>
        !             7: #ifndef _WIN32
        !             8: #include <signal.h>
        !             9: #endif
        !            10: 
        !            11: 
        !            12: /****************/
        !            13: /* -- SLIRP -- */
        !            14: 
        !            15: #include <arpa/inet.h>
        !            16: 
        !            17: /* slirp prototypes */
        !            18: extern  int slirp_init(void);
        !            19: int slirp_redir(int is_udp, int host_port, struct in_addr guest_addr, int guest_port);
        !            20: extern  void slirp_input(const uint8_t *pkt, int pkt_len);
        !            21: extern  int slirp_select_fill(int *pnfds,
        !            22:                               fd_set *readfds, fd_set *writefds, fd_set *xfds);
        !            23: extern  void slirp_select_poll(fd_set *readfds, fd_set *writefds, fd_set *xfds);
        !            24: extern  void slirp_exit(int);
        !            25: void slirp_debug_init(char*,int);
        !            26: extern  void slirp_output(const unsigned char *pkt, int pkt_len);
        !            27: extern  int slirp_can_output(void);
        !            28: 
        !            29: /* queue prototypes */
        !            30: queueADT       slirpq;
        !            31: 
        !            32: int slirp_inited;
        !            33: static SDL_mutex *slirp_mutex = NULL;
        !            34: SDL_Thread *tick_func_handle;
        !            35: 
        !            36: //Is slirp initalized?
        !            37: //Is set to true from the init, and false on ethernet disconnect
        !            38: int slirp_can_output(void)
        !            39: {
        !            40:     return slirp_inited;
        !            41: }
        !            42: 
        !            43: //This is a callback function for SLiRP that sends a packet
        !            44: //to the calling library.  In this case I stuff
        !            45: //it in q queue
        !            46: void slirp_output (const unsigned char *pkt, int pkt_len)
        !            47: {
        !            48:     struct queuepacket *p;
        !            49:     p=(struct queuepacket *)malloc(sizeof(struct queuepacket));
        !            50:     SDL_LockMutex(slirp_mutex);
        !            51:     p->len=pkt_len;
        !            52:     memcpy(p->data,pkt,pkt_len);
        !            53:     QueueEnter(slirpq,p);
        !            54:     SDL_UnlockMutex(slirp_mutex);
        !            55:     Log_Printf(LOG_WARN, "[SLIRP] Output packet with %i bytes to queue",pkt_len);
        !            56: }
        !            57: 
        !            58: //This function is to be periodically called
        !            59: //to keep the internal packet state flowing.
        !            60: void slirp_tick(void)
        !            61: {
        !            62:     int ret2,nfds;
        !            63:     struct timeval tv;
        !            64:     fd_set rfds, wfds, xfds;
        !            65:     int timeout;
        !            66:     nfds=-1;
        !            67:     
        !            68:     if (slirp_inited)
        !            69:     {
        !            70:         FD_ZERO(&rfds);
        !            71:         FD_ZERO(&wfds);
        !            72:         FD_ZERO(&xfds);
        !            73:         timeout=slirp_select_fill(&nfds,&rfds,&wfds,&xfds); //this can crash
        !            74:         
        !            75:         if(timeout<0)
        !            76:             timeout=500;
        !            77:         tv.tv_sec=0;
        !            78:         tv.tv_usec = timeout;    //basilisk default 10000
        !            79:         
        !            80:         ret2 = select(nfds + 1, &rfds, &wfds, &xfds, &tv);
        !            81:         if(ret2>=0){
        !            82:             SDL_LockMutex(slirp_mutex);
        !            83:             slirp_select_poll(&rfds, &wfds, &xfds);
        !            84:             SDL_UnlockMutex(slirp_mutex);
        !            85:         }
        !            86:     }
        !            87: }
        !            88: 
        !            89: static int tick_func(void *arg)
        !            90: {
        !            91:     while(slirp_inited)
        !            92:     {
        !            93:         SDL_Delay(10);
        !            94:         slirp_tick();
        !            95:     }
        !            96:     return 0;
        !            97: }
        !            98: 
        !            99: 
        !           100: void enet_slirp_queue_poll(void)
        !           101: {
        !           102:     SDL_LockMutex(slirp_mutex);
        !           103:     if (QueuePeek(slirpq)>0)
        !           104:     {
        !           105:         struct queuepacket *qp;
        !           106:         qp=QueueDelete(slirpq);
        !           107:         Log_Printf(LOG_WARN, "[SLIRP] Getting packet from queue");
        !           108:         enet_receive(qp->data,qp->len);
        !           109:         free(qp);
        !           110:     }
        !           111:     SDL_UnlockMutex(slirp_mutex);
        !           112: }
        !           113: 
        !           114: void enet_slirp_input(Uint8 *pkt, int pkt_len) {
        !           115:     if (slirp_inited) {
        !           116:         Log_Printf(LOG_WARN, "[SLIRP] Input packet with %i bytes",enet_tx_buffer.size);
        !           117:         SDL_LockMutex(slirp_mutex);
        !           118:         slirp_input(pkt,pkt_len);
        !           119:         SDL_UnlockMutex(slirp_mutex);
        !           120:     }
        !           121: }
        !           122: 
        !           123: void enet_slirp_stop(void) {
        !           124:     int ret;
        !           125:     
        !           126:     if(slirp_inited) {
        !           127:         Log_Printf(LOG_WARN, "Stopping SLIRP");
        !           128:         slirp_inited=0;
        !           129:         //slirp_exit(0);
        !           130:         QueueDestroy(slirpq);
        !           131:         SDL_DestroyMutex(slirp_mutex);
        !           132:         SDL_WaitThread(tick_func_handle, &ret);
        !           133:     }
        !           134: }
        !           135: 
        !           136: void enet_slirp_start(void) {
        !           137:     struct in_addr guest_addr;
        !           138:     
        !           139:     if (!slirp_inited) {
        !           140:         Log_Printf(LOG_WARN, "Starting SLIRP");
        !           141: #ifndef _WIN32
        !           142:         signal(SIGPIPE, SIG_IGN);
        !           143: #endif
        !           144:         slirp_init();
        !           145:         slirpq = QueueCreate();
        !           146:         slirp_inited=1;
        !           147:         //SDL_Delay(500);
        !           148:         slirp_mutex=SDL_CreateMutex();
        !           149:         tick_func_handle=SDL_CreateThread(tick_func,"SLiRPTickThread", (void *)NULL);
        !           150:         inet_aton("10.0.2.15", &guest_addr);
        !           151:         slirp_redir(0, 42323, guest_addr, 23);
        !           152:     }
        !           153: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.