more fix on Ec/Ev.
[gss-tcad.git] / src / solver / mix1 / mixsocket.cc
blob3fab84fdf7f7a09ed36adf9ccafbaa6942dabbdf
1 /*****************************************************************************/
2 /* 8888888 88888888 88888888 */
3 /* 8 8 8 */
4 /* 8 8 8 */
5 /* 8 88888888 88888888 */
6 /* 8 8888 8 8 */
7 /* 8 8 8 8 */
8 /* 888888 888888888 888888888 */
9 /* */
10 /* A Two-Dimensional General Purpose Semiconductor Simulator. */
11 /* */
12 /* GSS 0.4x */
13 /* Last update: March 29, 2007 */
14 /* */
15 /* Gong Ding */
16 /* gdiso@ustc.edu */
17 /* NINT, No.69 P.O.Box, Xi'an City, China */
18 /* */
19 /*****************************************************************************/
20 #include "mix1.h"
21 #include "log.h"
22 #include "string.h"
25 sigjmp_buf net_buf;
27 /* This function gets called whenever the pipe broken.
28 See the signal(2) manpage for more information. */
29 inline void signal_handler(int signum)
31 switch (signum)
33 case SIGPIPE:
34 printf("Received signal SIGPIPE. It seems ngspice terminated.\n");
35 siglongjmp(net_buf,1);
36 default:
37 break;
41 /* ----------------------------------------------------------------------------
42 * NDEVServer: This function set TCP/IP socket and connect to NGSPICE.
44 int NDEVServer(int port,int &listener, int &client)
46 char dotted_ip[15]; /* Buffer for converting the resolved address to a readable format. */
47 struct sockaddr_in sa; /* Connection address. */
48 socklen_t sa_len;
49 char sendbuf[128];
50 signal(SIGPIPE, signal_handler);
51 listener = socket(PF_INET, SOCK_STREAM, IPPROTO_IP);
52 if (listener < 0)
54 gss_log.string_buf()<<"Unable to create a listener socket: "<<strerror(errno)<<"\n";
55 gss_log.record();
56 return 1;
58 /* Now bind the listener to a local address. This uses
59 the same sockaddr_in structure as connect. */
60 sa_len = sizeof(sa);
61 //bzero(&sa, sa_len);
62 memset(&sa,0,sa_len);
63 sa.sin_family = AF_INET;
64 sa.sin_port = htons(port);
65 sa.sin_addr.s_addr = htonl(INADDR_ANY); /* Listen on all interfaces. */
66 if (bind(listener, (sockaddr *)&sa, sa_len) < 0)
68 gss_log.string_buf()<<"Unable to bind to port "<<port<<": "<<strerror(errno)<<"\n";
69 gss_log.record();
70 return 1;
72 /* Let the networking system know we're accepting
73 connections on this socket. Ask for a connection
74 queue of five clients. (If more than five clients
75 try to connect before we call accept, some will
76 be denied.) */
77 if (listen(listener, 1) < 0)
79 gss_log.string_buf()<<"Unable to listen: "<<strerror(errno)<<"\n";
80 gss_log.record();
81 return 1;
83 gss_log.string_buf()<<"Waiting for ngspice...";
84 gss_log.record();
85 client = accept(listener, (sockaddr *)&sa, &sa_len);
86 /* We now have a live client. Print information
87 about it and then send something over the wire. */
88 inet_ntop(AF_INET, &sa.sin_addr, dotted_ip, 15);
90 /* Waiting for ngspice query data */
91 recv(client,sendbuf,128,MSG_FLAG);
92 if(strcmp(sendbuf,NG_QUERY)) /* Query error */
94 gss_log.string_buf()<<"Query information error from ngspice.\n";
95 gss_log.record();
96 return 1;
98 gss_log.string_buf()<<"Received ngspice connection from "<<dotted_ip<<"\n";
99 gss_log.record();
100 /* ACK to ngspice */
101 sprintf(sendbuf,NDEV_REPLY);
102 send(client,sendbuf,128,0);
104 return 0;