File: socket.c

package info (click to toggle)
meanwhile 1.0.2-9
  • links: PTS
  • area: main
  • in suites: buster, stretch
  • size: 2,808 kB
  • ctags: 3,721
  • sloc: ansic: 13,873; sh: 8,360; makefile: 153
file content (327 lines) | stat: -rw-r--r-- 9,286 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327

/*
  Meanwhile - Unofficial Lotus Sametime Community Client Library
  Copyright (C) 2004  Christopher (siege) O'Brien
  
  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Library General Public
  License as published by the Free Software Foundation; either
  version 2 of the License, or (at your option) any later version.
  
  This library is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  Library General Public License for more details.
  
  You should have received a copy of the GNU Library General Public
  License along with this library; if not, write to the Free
  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/



/**
   @file socket.c
   
   This file is a simple demonstration of using unix socket code to
   connect a mwSession to a sametime server and get it fully logged
   in. It doesn't do anything after logging in.
   
   Here you'll find examples of:
    - opening a socket to the host
    - using the socket to feed data to the session
    - using a session handler to allow the session to write data to the
      socket
    - using a session handler to allow the session to close the socket
    - watching for error conditions on read/write
*/



#include <netdb.h>
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <unistd.h>

#include <glib.h>

#include <mw_common.h>
#include <mw_session.h>



/** help text if you don't give the right number of arguments */
#define HELP \
"Meanwhile sample socket client\n" \
"Usage: %s server userid password\n" \
"\n" \
"Connects to a sametime server and logs in with the supplied user ID\n" \
"and password. Doesn't actually do anything useful after that.\n\n"



/** how much to read from the socket in a single call */
#define BUF_LEN 2048



/* client data should be put into a structure and associated with the
   session. Then it will be available from the many call-backs
   handling events from the session */
struct sample_client {
  struct mwSession *session;  /* the actual meanwhile session */
  int sock;                   /* the socket connecting to the server */
  int sock_event;             /* glib event id polling the socket */
};



/* the io_close function from the session handler */
static void mw_session_io_close(struct mwSession *session) {
  struct sample_client *client;

  /* get the client data from the session */
  client = mwSession_getClientData(session);

  /* safety check */
  g_return_if_fail(client != NULL);

  /* if the client still has a socket to be closed, close it */
  if(client->sock) {    
    g_source_remove(client->sock_event);
    close(client->sock);
    client->sock = 0;
    client->sock_event = 0;
  }
}



/* the io_write function from the session handler */
static int mw_session_io_write(struct mwSession *session,
			       const guchar *buf, gsize len) {

  struct sample_client *client;
  int ret = 0;

  /* get the client data from the session */
  client = mwSession_getClientData(session);

  /* safety check */
  g_return_val_if_fail(client != NULL, -1);

  /* socket was already closed, so we can't possibly write to it */
  if(client->sock == 0) return -1;

  /* write out the data to the socket until it's all gone */
  while(len) {
    ret = write(client->sock, buf, len);
    if(ret <= 0) break;
    len -= ret;
  }

  /* if for some reason we couldn't finish writing all the data, there
     must have been a problem */
  if(len > 0) {
    /* stop watching the socket */
    g_source_remove(client->sock_event);

    /* close the socket */
    close(client->sock);

    /* remove traces of socket from client */
    client->sock = 0;
    client->sock_event = 0;

    /* return error code */
    return -1;
  }

  /* return success code */
  return 0;
}



/* the on_stateChange function from the session handler */
static void mw_session_stateChange(struct mwSession *session,
				   enum mwSessionState state, 
				   gpointer info) {

  if(state == mwSession_STARTED) {
    /* at this point the session is all ready to go. */
    printf("session fully started\n");
  }
}



/* the session handler structure is where you should indicate what
   functions will perform many of the functions necessary for the
   session to operate. Among these, only io_write and io_close are
   absolutely required. */
static struct mwSessionHandler mw_session_handler = {
  .io_write = mw_session_io_write,  /**< handle session to socket */
  .io_close = mw_session_io_close,  /**< handle session closing socket */
  .clear = NULL,                    /**< cleanup function */
  .on_stateChange = mw_session_stateChange,  /**< session status changed */
  .on_setPrivacyInfo = NULL,        /**< received privacy information */
  .on_setUserStatus = NULL,         /**< received status information */
  .on_admin = NULL,                 /**< received an admin message */
};



/** called from read_cb, attempts to read available data from sock and
    pass it to the session. Returns zero when the socket has been
    closed, less-than zero in the event of an error, and greater than
    zero for success */
static int read_recv(struct mwSession *session, int sock) {
  guchar buf[BUF_LEN];
  int len;

  len = read(sock, buf, BUF_LEN);
  if(len > 0) mwSession_recv(session, buf, len);

  return len;
}



/** callback registerd via g_io_add_watch in main, watches the socket
    for available data to be processed by the session */
static gboolean read_cb(GIOChannel *chan,
			GIOCondition cond,
			gpointer data) {

  struct sample_client *client = data;
  int ret = 0;

  if(cond & G_IO_IN) {
    ret = read_recv(client->session, client->sock);

    /* successful operation ends here, as the read_recv function
       should only return sero or lower in the event of a disconnect
       or error */
    if(ret > 0) return TRUE;
  }

  /* read problem occured if we're here, so we'll need to take care of
     it and clean up internal state */
  if(client->sock) {

    /* stop watching the socket */
    g_source_remove(client->sock_event);

    /* close it */
    close(client->sock);

    /* don't reference the socket or its event now that they're gone */
    client->sock = 0;
    client->sock_event = 0;
  }

  return FALSE;
}



/* address lookup used by init_sock */
static void init_sockaddr(struct sockaddr_in *addr,
			  const char *host, int port) {

  struct hostent *hostinfo;

  addr->sin_family = AF_INET;
  addr->sin_port = htons (port);
  hostinfo = gethostbyname(host);
  if(hostinfo == NULL) {
    fprintf(stderr, "Unknown host %s.\n", host);
    exit(1);
  }
  addr->sin_addr = *(struct in_addr *) hostinfo->h_addr;
}



/* open and return a network socket fd connected to host:port */
static int init_sock(const char *host, int port) {
  struct sockaddr_in srvrname;
  int sock;

  sock = socket(PF_INET, SOCK_STREAM, 0);
  if(sock < 0) {
    fprintf(stderr, "socket failure");
    exit(1);
  }
  
  init_sockaddr(&srvrname, host, port);
  connect(sock, (struct sockaddr *)&srvrname, sizeof(srvrname));

  return sock;
}



int main(int argc, char *argv[]) {

  /* the meanwhile session itself */
  struct mwSession *session;

  /* client program data */
  struct sample_client *client;

  /* something glib uses to watch the socket for available data */
  GIOChannel *io_chan;

  /* specify host, user, pass on the command line */
  if(argc != 4) {
    fprintf(stderr, HELP, *argv);
    return 1;
  }

  /* create the session and set the user and password */
  session = mwSession_new(&mw_session_handler);
  mwSession_setProperty(session, mwSession_AUTH_USER_ID, argv[2], NULL);
  mwSession_setProperty(session, mwSession_AUTH_PASSWORD, argv[3], NULL);


  /* create the client data. This is arbitrary data that a client will
     want to store along with the session for its own use */
  client = g_new0(struct sample_client, 1);
  client->session = session;

  /* associate the client data with the session, specifying an
     optional cleanup function which will be called upon the data when
     the session is free'd via mwSession_free */
  mwSession_setClientData(session, client, g_free);

  /* set up a connection to the host */
  client->sock = init_sock(argv[1], 1533);

  /* start the session. This will cause the session to send the
     handshake message (using the io_write function specified in the
     session handler). */
  mwSession_start(session);

  /* add a watch on the socket. Any data returning from the server
     will trigger read_cb, which will in turn read the data and pass
     it to the session for interpretation */
  io_chan = g_io_channel_unix_new(client->sock);
  client->sock_event = g_io_add_watch(io_chan, G_IO_IN | G_IO_ERR | G_IO_HUP,
				      read_cb, client);

  /* Create a new main loop and start it. This will cause the above
     watch to begin actually polling the socket. Use g_idle_add,
     g_timeout_add to insert events into the event loop */
  g_main_loop_run(g_main_loop_new(NULL, FALSE));

  /* this won't happen until after the main loop finally terminates */
  return 0;
}