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

unix.superglobalmegacorp.com

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